-
Notifications
You must be signed in to change notification settings - Fork 2.2k
fix: allow subinterpreters to be manually disabled #5708
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -256,9 +256,13 @@ | |
|
||
// Slightly faster code paths are available when PYBIND11_HAS_SUBINTERPRETER_SUPPORT is *not* | ||
// defined, so avoid defining it for implementations that do not support subinterpreters. However, | ||
// defining it unnecessarily is not expected to break anything. | ||
#if PY_VERSION_HEX >= 0x030C0000 && !defined(PYPY_VERSION) && !defined(GRAALVM_PYTHON) | ||
# define PYBIND11_HAS_SUBINTERPRETER_SUPPORT | ||
// defining it unnecessarily is not expected to break anything (other than old iOS targets). | ||
#ifndef PYBIND11_HAS_SUBINTERPRETER_SUPPORT | ||
# if PY_VERSION_HEX >= 0x030C0000 && !defined(PYPY_VERSION) && !defined(GRAALVM_PYTHON) | ||
# define PYBIND11_HAS_SUBINTERPRETER_SUPPORT 1 | ||
# else | ||
# define PYBIND11_HAS_SUBINTERPRETER_SUPPORT 0 | ||
# endif | ||
Comment on lines
+261
to
+265
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changing defined/undefined to 1/0 is a backward-incompatible change, which breaks preprocessor
See also: /* Define to 1 when compiling for experimental free-threaded builds */
#ifdef Py_GIL_DISABLED
/* We undefine if it was set to zero because all later checks are #ifdef.
* Note that non-Windows builds do not do this, and so every effort should
* be made to avoid defining the variable at all when not desired. However,
* sysconfig.get_config_var always returns a 1 or a 0, and so it seems likely
* that a build backend will define it with the value.
*/
# if Py_GIL_DISABLED == 0
# undef Py_GIL_DISABLED
# endif
#endif There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I was aware of that when I reviewed this PR. My judgement: That's OK/best in this case, because that define was added only very recently, and was included only in release candidates. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Release candidates exist exactly so that we can make last minute backward in compatible changes to new features. :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Though, honestly, I hadn't thought of that way of doing it, and if you think it's more consistent, I wouldn't be opposed to it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does it make sense to undefine the macro if it equals 0? Like CPython does for no-gil on Windows: /* Define to 1 when compiling for experimental free-threaded builds */
#ifdef Py_GIL_DISABLED
/* We undefine if it was set to zero because all later checks are #ifdef.
* Note that non-Windows builds do not do this, and so every effort should
* be made to avoid defining the variable at all when not desired. However,
* sysconfig.get_config_var always returns a 1 or a 0, and so it seems likely
* that a build backend will define it with the value.
*/
# if Py_GIL_DISABLED == 0
# undef Py_GIL_DISABLED
# endif
#endif There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Ah ... I remembered that wrong and didn't double-check.
My vote:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
We may need to undefine the macro if it is defined as 0 before the first usage (see #5708 (comment)). The macro might be defined via the C flags, like what this PR did:
Since the I workaround this with a macro (https://github.com/metaopt/optree/pull/227/files#diff-b552e0451de79baea976f15c4ee5458fa7b6faa75bebb8b3780a90ec3d40a427R56): // NOLINTNEXTLINE[bugprone-macro-parentheses]
#define NONZERO_OR_EMPTY(MACRO) ((MACRO + 0 != 0) || (0 - MACRO - 1 >= 0))
#if defined(HAS_XXX) && NONZERO_OR_EMPTY(HAS_XXX)
// do something
#else
// do something else
#endif
#undef NONZERO_OR_EMPTY There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Applying the "keep it simple" and "be consistent" principles here: I think this is all we need: #ifndef PYBIND11_HAS_SUBINTERPRETER_SUPPORT
# if PY_VERSION_HEX >= 0x030C0000 && !defined(PYPY_VERSION) && !defined(GRAALVM_PYTHON)
# define PYBIND11_HAS_SUBINTERPRETER_SUPPORT 1
# endif
#endif
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, I know what's needed, just teaching in an hour, so haven't had time to apply it. It's something like this: #ifndef PYBIND11_HAS_SUBINTERPRETER_SUPPORT
# if PY_VERSION_HEX >= 0x030C0000 && !defined(PYPY_VERSION) && !defined(GRAALVM_PYTHON)
# define PYBIND11_HAS_SUBINTERPRETER_SUPPORT 1
# endif
#else
# if PYBIND11_HAS_SUBINTERPRETER_SUPPORT == 0
# undefine PYBIND11_HAS_SUBINTERPRETER_SUPPORT
# endif
#endif Then after that, simple ifdefs can be used throughout the code instead of checking this one and only this one for 1/0. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds good! Looking around some more after seeing @henryiii's suggestion: I didn't realize before that Therefore: It's not inconsistent to add the (There is at least one other non- |
||
#endif | ||
|
||
// 3.12 Compatibility | ||
|
Uh oh!
There was an error while loading. Please reload this page.