Skip to content

Commit

Permalink
fix: Use PyObject_VisitManagedDict() of Python 3.13 (#4973)
Browse files Browse the repository at this point in the history
* fix: Use PyObject_VisitManagedDict() of Python 3.13

Use PyObject_VisitManagedDict() and PyObject_ClearManagedDict() in
pybind11_traverse() and pybind11_clear() on Python 3.13 and newer.

* Add Python 3.13 CI

* tests: don't get numpy/scipy on 3.13 yet

* ci: move 3.13 to upstream

Signed-off-by: Henry Schreiner <[email protected]>

---------

Signed-off-by: Henry Schreiner <[email protected]>
Co-authored-by: Henry Schreiner <[email protected]>
  • Loading branch information
vstinner and henryiii authored Dec 15, 2023
1 parent daea113 commit dc477fa
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
1 change: 0 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ jobs:
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python }}
allow-prereleases: true

- name: Setup Boost (Linux)
# Can't use boost + define _
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/upstream.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,24 @@ concurrency:

env:
PIP_BREAK_SYSTEM_PACKAGES: 1
PIP_ONLY_BINARY: ":all:"
# For cmake:
VERBOSE: 1

jobs:
standard:
name: "🐍 3.12 latest • ubuntu-latest • x64"
name: "🐍 3.13 latest • ubuntu-latest • x64"
runs-on: ubuntu-latest
# Only runs when the 'python dev' label is selected
if: "contains(github.event.pull_request.labels.*.name, 'python dev')"

steps:
- uses: actions/checkout@v4

- name: Setup Python 3.12
- name: Setup Python 3.13
uses: actions/setup-python@v5
with:
python-version: "3.12-dev"
python-version: "3.13"
allow-prereleases: true

- name: Setup Boost
run: sudo apt-get install libboost-dev
Expand Down
8 changes: 8 additions & 0 deletions include/pybind11/detail/class.h
Original file line number Diff line number Diff line change
Expand Up @@ -519,8 +519,12 @@ inline PyObject *make_object_base_type(PyTypeObject *metaclass) {

/// dynamic_attr: Allow the garbage collector to traverse the internal instance `__dict__`.
extern "C" inline int pybind11_traverse(PyObject *self, visitproc visit, void *arg) {
#if PY_VERSION_HEX >= 0x030D0000
PyObject_VisitManagedDict(self, visit, arg);
#else
PyObject *&dict = *_PyObject_GetDictPtr(self);
Py_VISIT(dict);
#endif
// https://docs.python.org/3/c-api/typeobj.html#c.PyTypeObject.tp_traverse
#if PY_VERSION_HEX >= 0x03090000
Py_VISIT(Py_TYPE(self));
Expand All @@ -530,8 +534,12 @@ extern "C" inline int pybind11_traverse(PyObject *self, visitproc visit, void *a

/// dynamic_attr: Allow the GC to clear the dictionary.
extern "C" inline int pybind11_clear(PyObject *self) {
#if PY_VERSION_HEX >= 0x030D0000
PyObject_ClearManagedDict(self);
#else
PyObject *&dict = *_PyObject_GetDictPtr(self);
Py_CLEAR(dict);
#endif
return 0;
}

Expand Down
4 changes: 2 additions & 2 deletions tests/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ numpy~=1.25.0; python_version=="3.9" and platform_python_implementation=='PyPy'
numpy~=1.19.3; platform_python_implementation!="PyPy" and python_version=="3.6"
numpy~=1.21.5; platform_python_implementation!="PyPy" and python_version>="3.7" and python_version<"3.10"
numpy~=1.22.2; platform_python_implementation!="PyPy" and python_version=="3.10"
numpy~=1.26.0; platform_python_implementation!="PyPy" and python_version>="3.11"
numpy~=1.26.0; platform_python_implementation!="PyPy" and python_version>="3.11" and python_version<"3.13"
pytest~=7.0
pytest-timeout
scipy~=1.5.4; platform_python_implementation!="PyPy" and python_version<"3.10"
scipy~=1.8.0; platform_python_implementation!="PyPy" and python_version=="3.10"
scipy~=1.11.1; platform_python_implementation!="PyPy" and python_version>="3.11"
scipy~=1.11.1; platform_python_implementation!="PyPy" and python_version>="3.11" and python_version<"3.13"

0 comments on commit dc477fa

Please sign in to comment.