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 6 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
7 changes: 7 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 @@ -96,6 +97,9 @@ jobs:
-DCMAKE_CXX_FLAGS="/DPYBIND11_RUN_TESTING_WITH_SMART_HOLDER_AS_DEFAULT_BUT_NEVER_USE_IN_PRODUCTION_PLEASE /GR /EHsc"
- runs-on: 'ubuntu-latest'
python: 'graalpy-24.1'
# Only working one for 3.14.0b1 on Windows
- runs-on: windows-2022
python: '3.14t'

exclude:
# The setup-python action currently doesn't have graalpy for windows
Expand All @@ -108,6 +112,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
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
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,12 +15,12 @@

sys.path.append(".")

if sys.version_info >= (3, 14):
if sys.version_info >= (3, 15):
import interpreters
elif sys.version_info >= (3, 13):
elif sys.version_info >= (3, 14):
import _interpreters as interpreters
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like they didn't do this rename in 3.14, so it should be _interpreters for 3.13 and 3.14. Maybe just get rid of the 3.15 and leave it as :

    if sys.version_info >= (3, 13):
        import _interpreters as interpreters

Or future proof it as:

    if sys.version_info >= (3, 13):
        try: 
            import interpreters
        except ImportError:
            import _interpreters as interpreters

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 think there will be a pypi package for 3.14 eventually (though I think it was supposed to come to 3.13 originally, so who knows...), but let's just leave it as <3.15 for now.

elif sys.version_info >= (3, 12):
import _xxsubinterpreters as interpreters

Check failure on line 23 in tests/test_multiple_interpreters.py

View workflow job for this annotation

GitHub Actions / 🐍 3.13 • ubuntu-24.04 • x64

test_independent_subinterpreters ModuleNotFoundError: No module named '_xxsubinterpreters'
else:
pytest.skip("Test requires a the interpreters stdlib module")

Expand Down Expand Up @@ -87,7 +87,7 @@
sys.path.append(".")

if sys.version_info >= (3, 14):
import interpreters

Check failure on line 90 in tests/test_multiple_interpreters.py

View workflow job for this annotation

GitHub Actions / 🐍 3.14 • ubuntu-24.04 • x64

test_dependent_subinterpreters ModuleNotFoundError: No module named 'interpreters'
elif sys.version_info >= (3, 13):
import _interpreters as interpreters
elif sys.version_info >= (3, 12):
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
Loading