Skip to content
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

fix: support Python 3.13.0b1 (PEP 667 fix) #5127

Merged
merged 5 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 6 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@ jobs:
python:
- '3.6'
- '3.9'
- '3.10'
- '3.11'
- '3.12'
- '3.13'
- 'pypy-3.8'
- 'pypy-3.9'
- 'pypy-3.10'
Expand Down Expand Up @@ -64,6 +63,10 @@ jobs:
# Inject a couple Windows 2019 runs
- runs-on: windows-2019
python: '3.9'
# Extra ubuntu latest job
- runs-on: ubuntu-latest
python: '3.11'


name: "🐍 ${{ matrix.python }} • ${{ matrix.runs-on }} • x64 ${{ matrix.args }}"
runs-on: ${{ matrix.runs-on }}
Expand All @@ -75,6 +78,7 @@ 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
12 changes: 12 additions & 0 deletions include/pybind11/pybind11.h
Original file line number Diff line number Diff line change
Expand Up @@ -1344,8 +1344,14 @@ using module = module_;
/// Return a dictionary representing the global variables in the current execution frame,
/// or ``__main__.__dict__`` if there is no frame (usually when the interpreter is embedded).
inline dict globals() {
#if PY_VERSION_HEX >= 0x030d0000
PyObject *p = PyEval_GetFrameGlobals();
return p ? reinterpret_steal<dict>(p)
: reinterpret_borrow<dict>(module_::import("__main__").attr("__dict__").ptr());
#else
PyObject *p = PyEval_GetGlobals();
return reinterpret_borrow<dict>(p ? p : module_::import("__main__").attr("__dict__").ptr());
#endif
}

template <typename... Args, typename = detail::enable_if_t<args_are_all_keyword_or_ds<Args...>()>>
Expand Down Expand Up @@ -2770,7 +2776,12 @@ get_type_override(const void *this_ptr, const type_info *this_type, const char *
PyCodeObject *f_code = PyFrame_GetCode(frame);
// f_code is guaranteed to not be NULL
if ((std::string) str(f_code->co_name) == name && f_code->co_argcount > 0) {
# if PY_VERSION_HEX >= 0x030d0000
PyObject *locals = PyEval_GetFrameLocals();
# else
PyObject *locals = PyEval_GetLocals();
Py_INCREF(locals);
Copy link
Collaborator

@rwgk rwgk May 27, 2024

Choose a reason for hiding this comment

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

@henryiii This Py_INCREF(locals) appears before the locals != nullptr test, therefore I think it could segfault in object.h: op->ob_refcnt++;. Did you consider that already? Did you mean to use Py_XINCREF(locals) here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes I did, thanks! I think it’s very unlikely that this will be null, but best to be correct!

# endif
if (locals != nullptr) {
# if PY_VERSION_HEX >= 0x030b0000
PyObject *co_varnames = PyCode_GetVarnames(f_code);
Expand All @@ -2780,6 +2791,7 @@ get_type_override(const void *this_ptr, const type_info *this_type, const char *
PyObject *self_arg = PyTuple_GET_ITEM(co_varnames, 0);
Py_DECREF(co_varnames);
PyObject *self_caller = dict_getitem(locals, self_arg);
Py_DECREF(locals);
if (self_caller == self.ptr()) {
Py_DECREF(f_code);
Py_DECREF(frame);
Expand Down
Loading