Skip to content

fix: support Python 3.14 #5646

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
May 17, 2025
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ jobs:
python:
- '3.8'
- '3.13'
- '3.14'
- 'pypy-3.10'
- 'pypy-3.11'
- 'graalpy-24.2'
Expand Down Expand Up @@ -108,6 +109,9 @@ jobs:
# No NumPy for PyPy 3.10 ARM
- runs-on: macos-14
python: 'pypy-3.10'
# Beta 1 broken for compiling on GHA (thinks it's free-threaded)
- runs-on: windows-2022
python: '3.14'


name: "🐍 ${{ matrix.python }} • ${{ matrix.runs-on }} • x64 ${{ matrix.args }}"
Expand Down
7 changes: 6 additions & 1 deletion include/pybind11/eval.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,12 @@ object eval_file(str fname, object global = globals(), object local = object())

int closeFile = 1;
std::string fname_str = (std::string) fname;
FILE *f = _Py_fopen_obj(fname.ptr(), "r");
FILE *f =
# if PY_VERSION_HEX >= 0x030E0000
Py_fopen(fname.ptr(), "r");
# else
_Py_fopen_obj(fname.ptr(), "r");
# endif
if (!f) {
PyErr_Clear();
pybind11_fail("File \"" + fname_str + "\" could not be opened!");
Expand Down
3 changes: 2 additions & 1 deletion include/pybind11/pytypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -2583,7 +2583,8 @@ str_attr_accessor object_api<D>::doc() const {

template <typename D>
object object_api<D>::annotations() const {
#if PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION <= 9
#if PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION <= 9 \
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the suppressed due to low confidence comment, but instead we should just use the HEX version like everywhere else.

|| PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 14
// https://docs.python.org/3/howto/annotations.html#accessing-the-annotations-dict-of-an-object-in-python-3-9-and-older
if (!hasattr(derived(), "__annotations__")) {
setattr(derived(), "__annotations__", dict());
Expand Down
6 changes: 5 additions & 1 deletion pybind11/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from __future__ import annotations

import argparse
import functools
import re
import sys
import sysconfig
Expand Down Expand Up @@ -49,7 +50,10 @@ def print_includes() -> None:


def main() -> None:
parser = argparse.ArgumentParser()
make_parser = functools.partial(argparse.ArgumentParser, allow_abbrev=False)
if sys.version_info >= (3, 14):
make_parser = functools.partial(make_parser, color=True, suggest_on_error=True)
parser = make_parser()
parser.add_argument(
"--version",
action="version",
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ classifiers = [
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",
"Programming Language :: Python :: 3.14",
"Programming Language :: Python :: Implementation :: PyPy",
"Programming Language :: Python :: Implementation :: CPython",
"Programming Language :: C++",
Expand Down
1 change: 0 additions & 1 deletion tests/pybind11_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ PYBIND11_MODULE(pybind11_tests, m, py::mod_gil_not_used()) {
m.attr("cpp_std") = cpp_std();
m.attr("PYBIND11_INTERNALS_ID") = PYBIND11_INTERNALS_ID;
// Free threaded Python uses UINT32_MAX for immortal objects.
m.attr("PYBIND11_REFCNT_IMMORTAL") = UINT32_MAX;
m.attr("PYBIND11_SIMPLE_GIL_MANAGEMENT") =
#if defined(PYBIND11_SIMPLE_GIL_MANAGEMENT)
true;
Expand Down
20 changes: 14 additions & 6 deletions tests/test_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,17 @@
import pytest

import env
from pybind11_tests import PYBIND11_REFCNT_IMMORTAL, ConstructorStats, UserType
from pybind11_tests import ConstructorStats, UserType
from pybind11_tests import class_ as m

UINT32MAX = 2**32 - 1


def refcount_immortal(ob: object) -> int:
if _is_immortal := getattr(sys, "_is_immortal", None):
return UINT32MAX if _is_immortal(ob) else sys.getrefcount(ob)
return sys.getrefcount(ob)


def test_obj_class_name():
expected_name = "UserType" if env.PYPY else "pybind11_tests.UserType"
Expand Down Expand Up @@ -382,23 +390,23 @@ def test_brace_initialization():
@pytest.mark.xfail("env.PYPY or env.GRAALPY")
def test_class_refcount():
"""Instances must correctly increase/decrease the reference count of their types (#1029)"""
from sys import getrefcount

class PyDog(m.Dog):
pass

for cls in m.Dog, PyDog:
refcount_1 = getrefcount(cls)
refcount_1 = refcount_immortal(cls)
molly = [cls("Molly") for _ in range(10)]
refcount_2 = getrefcount(cls)
refcount_2 = refcount_immortal(cls)

del molly
pytest.gc_collect()
refcount_3 = getrefcount(cls)
refcount_3 = refcount_immortal(cls)

# Python may report a large value here (above 30 bits), that's also fine
assert refcount_1 == refcount_3
assert (refcount_2 > refcount_1) or (
refcount_2 == refcount_1 == PYBIND11_REFCNT_IMMORTAL
refcount_2 == refcount_1 and refcount_1 >= 2**29
)


Expand Down
5 changes: 5 additions & 0 deletions tests/test_methods_and_attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,11 @@ def test_property_rvalue_policy():

# https://foss.heptapod.net/pypy/pypy/-/issues/2447
@pytest.mark.xfail("env.PYPY")
@pytest.mark.xfail(
sys.version_info == (3, 14, 0, "beta", 1),
reason="3.14.0b1 bug: https://github.com/python/cpython/issues/133912",
strict=True,
)
def test_dynamic_attributes():
instance = m.DynamicClass()
assert not hasattr(instance, "foo")
Expand Down
4 changes: 2 additions & 2 deletions tests/test_multiple_interpreters.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def test_independent_subinterpreters():

sys.path.append(".")

if sys.version_info >= (3, 14):
if sys.version_info >= (3, 15):
import interpreters
elif sys.version_info >= (3, 13):
import _interpreters as interpreters
Expand Down Expand Up @@ -86,7 +86,7 @@ def test_dependent_subinterpreters():

sys.path.append(".")

if sys.version_info >= (3, 14):
if sys.version_info >= (3, 15):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this final for 3.14, or could this still change before the 3.14.0 release?

If the latter: maybe add a comment?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will continue to work. I think intepreters is supposed to be released on PyPI during the 3.14 lifecycle.

import interpreters
elif sys.version_info >= (3, 13):
import _interpreters as interpreters
Expand Down
2 changes: 1 addition & 1 deletion tests/test_operator_overloading.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,4 @@ def test_overriding_eq_reset_hash():
def test_return_set_of_unhashable():
with pytest.raises(TypeError) as excinfo:
m.get_unhashable_HashMe_set()
assert str(excinfo.value.__cause__).startswith("unhashable type:")
assert "unhashable type" in str(excinfo.value.__cause__)
16 changes: 15 additions & 1 deletion tests/test_pickling.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import pickle
import re
import sys

import pytest

Expand Down Expand Up @@ -62,7 +63,20 @@ def test_roundtrip(cls_name):


@pytest.mark.xfail("env.PYPY")
@pytest.mark.parametrize("cls_name", ["PickleableWithDict", "PickleableWithDictNew"])
@pytest.mark.parametrize(
"cls_name",
[
pytest.param(
"PickleableWithDict",
marks=pytest.mark.xfail(
sys.version_info == (3, 14, 0, "beta", 1),
reason="3.14.0b1 bug: https://github.com/python/cpython/issues/133912",
strict=True,
),
),
"PickleableWithDictNew",
],
)
def test_roundtrip_with_dict(cls_name):
cls = getattr(m, cls_name)
p = cls("test_value")
Expand Down
4 changes: 4 additions & 0 deletions tests/test_pytypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1143,6 +1143,10 @@ def test_dict_ranges(tested_dict, expected):

# https://docs.python.org/3/howto/annotations.html#accessing-the-annotations-dict-of-an-object-in-python-3-9-and-older
def get_annotations_helper(o):
if sys.version_info >= (3, 14):
import annotationlib

return annotationlib.get_annotations(o) or None
if isinstance(o, type):
return o.__dict__.get("__annotations__", None)
return getattr(o, "__annotations__", None)
Expand Down
Loading