Skip to content

Commit 2af163d

Browse files
Skylion007henryiii
andauthored
Fix: 3.11 beta support (#3923)
* Placeholder commit for 3.11 testing * Does this fix it? * Try suggestion * Placeholder commit for 3.11 testing * Does this fix it? * Try suggestion * fix: try using modern init for embedded interp Signed-off-by: Henry Schreiner <[email protected]> * fix: error message changed in 3.11 * fix: apply logic in Python manually Signed-off-by: Henry Schreiner <[email protected]> * fix autodetect dynamic attrs in 3.11 * fix: include error message if possible in error Signed-off-by: Henry Schreiner <[email protected]> * ci: enable standard Python 3.11 testing Signed-off-by: Henry Schreiner <[email protected]> * Make dynamic attrs condtiion exclusive to ver. Co-authored-by: Henry Schreiner <[email protected]>
1 parent c42e3ab commit 2af163d

File tree

8 files changed

+92
-48
lines changed

8 files changed

+92
-48
lines changed

.github/workflows/ci.yml

+3-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ jobs:
3030
- '3.6'
3131
- '3.9'
3232
- '3.10'
33+
- '3.11-dev'
3334
- 'pypy-3.7'
3435
- 'pypy-3.8'
3536
- 'pypy-3.9'
@@ -185,8 +186,8 @@ jobs:
185186
- python-version: "3.9"
186187
python-debug: true
187188
valgrind: true
188-
# - python-version: "3.11-dev"
189-
# python-debug: false
189+
- python-version: "3.11-dev"
190+
python-debug: false
190191

191192
name: "🐍 ${{ matrix.python-version }}${{ matrix.python-debug && '-dbg' || '' }} (deadsnakes)${{ matrix.valgrind && ' • Valgrind' || '' }} • x64"
192193
runs-on: ubuntu-latest

.github/workflows/upstream.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ env:
1414

1515
jobs:
1616
standard:
17-
name: "🐍 3.11 dev • ubuntu-latest • x64"
17+
name: "🐍 3.11 latest internals • ubuntu-latest • x64"
1818
runs-on: ubuntu-latest
1919
if: "contains(github.event.pull_request.labels.*.name, 'python dev')"
2020

include/pybind11/attr.h

+5-3
Original file line numberDiff line numberDiff line change
@@ -345,9 +345,11 @@ struct type_record {
345345

346346
bases.append((PyObject *) base_info->type);
347347

348-
if (base_info->type->tp_dictoffset != 0) {
349-
dynamic_attr = true;
350-
}
348+
#if PY_VERSION_HEX < 0x030B0000
349+
dynamic_attr |= base_info->type->tp_dictoffset != 0;
350+
#else
351+
dynamic_attr |= (base_info->type->tp_flags & Py_TPFLAGS_MANAGED_DICT) != 0;
352+
#endif
351353

352354
if (caster) {
353355
base_info->implicit_casts.emplace_back(type, caster);

include/pybind11/detail/class.h

+4
Original file line numberDiff line numberDiff line change
@@ -545,8 +545,12 @@ extern "C" inline int pybind11_clear(PyObject *self) {
545545
inline void enable_dynamic_attributes(PyHeapTypeObject *heap_type) {
546546
auto *type = &heap_type->ht_type;
547547
type->tp_flags |= Py_TPFLAGS_HAVE_GC;
548+
#if PY_VERSION_HEX < 0x030B0000
548549
type->tp_dictoffset = type->tp_basicsize; // place dict at the end
549550
type->tp_basicsize += (ssize_t) sizeof(PyObject *); // and allocate enough space for it
551+
#else
552+
type->tp_flags |= Py_TPFLAGS_MANAGED_DICT;
553+
#endif
550554
type->tp_traverse = pybind11_traverse;
551555
type->tp_clear = pybind11_clear;
552556

include/pybind11/embed.h

+56-32
Original file line numberDiff line numberDiff line change
@@ -86,37 +86,6 @@ inline wchar_t *widen_chars(const char *safe_arg) {
8686
return widened_arg;
8787
}
8888

89-
/// Python 2.x/3.x-compatible version of `PySys_SetArgv`
90-
inline void set_interpreter_argv(int argc, const char *const *argv, bool add_program_dir_to_path) {
91-
// Before it was special-cased in python 3.8, passing an empty or null argv
92-
// caused a segfault, so we have to reimplement the special case ourselves.
93-
bool special_case = (argv == nullptr || argc <= 0);
94-
95-
const char *const empty_argv[]{"\0"};
96-
const char *const *safe_argv = special_case ? empty_argv : argv;
97-
if (special_case) {
98-
argc = 1;
99-
}
100-
101-
auto argv_size = static_cast<size_t>(argc);
102-
// SetArgv* on python 3 takes wchar_t, so we have to convert.
103-
std::unique_ptr<wchar_t *[]> widened_argv(new wchar_t *[argv_size]);
104-
std::vector<std::unique_ptr<wchar_t[], wide_char_arg_deleter>> widened_argv_entries;
105-
widened_argv_entries.reserve(argv_size);
106-
for (size_t ii = 0; ii < argv_size; ++ii) {
107-
widened_argv_entries.emplace_back(widen_chars(safe_argv[ii]));
108-
if (!widened_argv_entries.back()) {
109-
// A null here indicates a character-encoding failure or the python
110-
// interpreter out of memory. Give up.
111-
return;
112-
}
113-
widened_argv[ii] = widened_argv_entries.back().get();
114-
}
115-
116-
auto *pysys_argv = widened_argv.get();
117-
PySys_SetArgvEx(argc, pysys_argv, static_cast<int>(add_program_dir_to_path));
118-
}
119-
12089
PYBIND11_NAMESPACE_END(detail)
12190

12291
/** \rst
@@ -146,9 +115,64 @@ inline void initialize_interpreter(bool init_signal_handlers = true,
146115
pybind11_fail("The interpreter is already running");
147116
}
148117

118+
#if PY_VERSION_HEX < 0x030B0000
119+
149120
Py_InitializeEx(init_signal_handlers ? 1 : 0);
150121

151-
detail::set_interpreter_argv(argc, argv, add_program_dir_to_path);
122+
// Before it was special-cased in python 3.8, passing an empty or null argv
123+
// caused a segfault, so we have to reimplement the special case ourselves.
124+
bool special_case = (argv == nullptr || argc <= 0);
125+
126+
const char *const empty_argv[]{"\0"};
127+
const char *const *safe_argv = special_case ? empty_argv : argv;
128+
if (special_case) {
129+
argc = 1;
130+
}
131+
132+
auto argv_size = static_cast<size_t>(argc);
133+
// SetArgv* on python 3 takes wchar_t, so we have to convert.
134+
std::unique_ptr<wchar_t *[]> widened_argv(new wchar_t *[argv_size]);
135+
std::vector<std::unique_ptr<wchar_t[], detail::wide_char_arg_deleter>> widened_argv_entries;
136+
widened_argv_entries.reserve(argv_size);
137+
for (size_t ii = 0; ii < argv_size; ++ii) {
138+
widened_argv_entries.emplace_back(detail::widen_chars(safe_argv[ii]));
139+
if (!widened_argv_entries.back()) {
140+
// A null here indicates a character-encoding failure or the python
141+
// interpreter out of memory. Give up.
142+
return;
143+
}
144+
widened_argv[ii] = widened_argv_entries.back().get();
145+
}
146+
147+
auto *pysys_argv = widened_argv.get();
148+
149+
PySys_SetArgvEx(argc, pysys_argv, static_cast<int>(add_program_dir_to_path));
150+
#else
151+
PyConfig config;
152+
PyConfig_InitIsolatedConfig(&config);
153+
config.install_signal_handlers = init_signal_handlers ? 1 : 0;
154+
155+
PyStatus status = PyConfig_SetBytesArgv(&config, argc, const_cast<char *const *>(argv));
156+
if (PyStatus_Exception(status)) {
157+
// A failure here indicates a character-encoding failure or the python
158+
// interpreter out of memory. Give up.
159+
PyConfig_Clear(&config);
160+
throw std::runtime_error(PyStatus_IsError(status) ? status.err_msg
161+
: "Failed to prepare CPython");
162+
}
163+
status = Py_InitializeFromConfig(&config);
164+
PyConfig_Clear(&config);
165+
if (PyStatus_Exception(status)) {
166+
throw std::runtime_error(PyStatus_IsError(status) ? status.err_msg
167+
: "Failed to init CPython");
168+
}
169+
if (add_program_dir_to_path) {
170+
PyRun_SimpleString("import sys, os.path; "
171+
"sys.path.insert(0, "
172+
"os.path.abspath(os.path.dirname(sys.argv[0])) "
173+
"if sys.argv and os.path.exists(sys.argv[0]) else '')");
174+
}
175+
#endif
152176
}
153177

154178
/** \rst

setup.cfg

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ classifiers =
1919
Programming Language :: Python :: 3.8
2020
Programming Language :: Python :: 3.9
2121
Programming Language :: Python :: 3.10
22+
Programming Language :: Python :: 3.11
2223
License :: OSI Approved :: BSD License
2324
Programming Language :: Python :: Implementation :: PyPy
2425
Programming Language :: Python :: Implementation :: CPython

tests/requirements.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
build==0.7.0
1+
build==0.8.0
22
numpy==1.21.5; platform_python_implementation=="PyPy" and sys_platform=="linux" and python_version=="3.7"
33
numpy==1.19.3; platform_python_implementation!="PyPy" and python_version=="3.6"
44
numpy==1.21.5; platform_python_implementation!="PyPy" and python_version>="3.7" and python_version<"3.10"

tests/test_methods_and_attributes.py

+21-9
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
1+
import sys
2+
13
import pytest
24

35
import env # noqa: F401
46
from pybind11_tests import ConstructorStats
57
from pybind11_tests import methods_and_attributes as m
68

9+
NO_GETTER_MSG = (
10+
"unreadable attribute" if sys.version_info < (3, 11) else "object has no getter"
11+
)
12+
NO_SETTER_MSG = (
13+
"can't set attribute" if sys.version_info < (3, 11) else "object has no setter"
14+
)
15+
NO_DELETER_MSG = (
16+
"can't delete attribute" if sys.version_info < (3, 11) else "object has no deleter"
17+
)
18+
719

820
def test_methods_and_attributes():
921
instance1 = m.ExampleMandA()
@@ -102,47 +114,47 @@ def test_properties():
102114

103115
with pytest.raises(AttributeError) as excinfo:
104116
dummy = instance.def_property_writeonly # unused var
105-
assert "unreadable attribute" in str(excinfo.value)
117+
assert NO_GETTER_MSG in str(excinfo.value)
106118

107119
instance.def_property_writeonly = 4
108120
assert instance.def_property_readonly == 4
109121

110122
with pytest.raises(AttributeError) as excinfo:
111123
dummy = instance.def_property_impossible # noqa: F841 unused var
112-
assert "unreadable attribute" in str(excinfo.value)
124+
assert NO_GETTER_MSG in str(excinfo.value)
113125

114126
with pytest.raises(AttributeError) as excinfo:
115127
instance.def_property_impossible = 5
116-
assert "can't set attribute" in str(excinfo.value)
128+
assert NO_SETTER_MSG in str(excinfo.value)
117129

118130

119131
def test_static_properties():
120132
assert m.TestProperties.def_readonly_static == 1
121133
with pytest.raises(AttributeError) as excinfo:
122134
m.TestProperties.def_readonly_static = 2
123-
assert "can't set attribute" in str(excinfo.value)
135+
assert NO_SETTER_MSG in str(excinfo.value)
124136

125137
m.TestProperties.def_readwrite_static = 2
126138
assert m.TestProperties.def_readwrite_static == 2
127139

128140
with pytest.raises(AttributeError) as excinfo:
129141
dummy = m.TestProperties.def_writeonly_static # unused var
130-
assert "unreadable attribute" in str(excinfo.value)
142+
assert NO_GETTER_MSG in str(excinfo.value)
131143

132144
m.TestProperties.def_writeonly_static = 3
133145
assert m.TestProperties.def_readonly_static == 3
134146

135147
assert m.TestProperties.def_property_readonly_static == 3
136148
with pytest.raises(AttributeError) as excinfo:
137149
m.TestProperties.def_property_readonly_static = 99
138-
assert "can't set attribute" in str(excinfo.value)
150+
assert NO_SETTER_MSG in str(excinfo.value)
139151

140152
m.TestProperties.def_property_static = 4
141153
assert m.TestProperties.def_property_static == 4
142154

143155
with pytest.raises(AttributeError) as excinfo:
144156
dummy = m.TestProperties.def_property_writeonly_static
145-
assert "unreadable attribute" in str(excinfo.value)
157+
assert NO_GETTER_MSG in str(excinfo.value)
146158

147159
m.TestProperties.def_property_writeonly_static = 5
148160
assert m.TestProperties.def_property_static == 5
@@ -160,7 +172,7 @@ def test_static_properties():
160172

161173
with pytest.raises(AttributeError) as excinfo:
162174
dummy = instance.def_property_writeonly_static # noqa: F841 unused var
163-
assert "unreadable attribute" in str(excinfo.value)
175+
assert NO_GETTER_MSG in str(excinfo.value)
164176

165177
instance.def_property_writeonly_static = 4
166178
assert instance.def_property_static == 4
@@ -180,7 +192,7 @@ def test_static_properties():
180192
properties_override = m.TestPropertiesOverride()
181193
with pytest.raises(AttributeError) as excinfo:
182194
del properties_override.def_readonly
183-
assert "can't delete attribute" in str(excinfo.value)
195+
assert NO_DELETER_MSG in str(excinfo.value)
184196

185197

186198
def test_static_cls():

0 commit comments

Comments
 (0)