Skip to content

Commit 500194e

Browse files
committed
Fix Python 3.10 (PEP-620) incompatibility.
1 parent cd953cf commit 500194e

File tree

6 files changed

+13
-14
lines changed

6 files changed

+13
-14
lines changed

include/boost/python/detail/wrap_python.hpp

+4
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,11 @@ typedef int pid_t;
227227

228228
# define PyVarObject_HEAD_INIT(type, size) \
229229
PyObject_HEAD_INIT(type) size,
230+
#endif
230231

232+
#if PY_VERSION_HEX < 0x030900A4
233+
# define Py_SET_TYPE(obj, type) ((Py_TYPE(obj) = (type)), (void)0)
234+
# define Py_SET_SIZE(obj, size) ((Py_SIZE(obj) = (size)), (void)0)
231235
#endif
232236

233237

include/boost/python/object/make_instance.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ struct make_instance_impl
4747

4848
// Note the position of the internally-stored Holder,
4949
// for the sake of destruction
50-
Py_SIZE(instance) = offsetof(instance_t, storage);
50+
Py_SET_SIZE(instance, offsetof(instance_t, storage));
5151

5252
// Release ownership of the python object
5353
protect.cancel();

src/object/class.cpp

+5-10
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ namespace objects
208208
{
209209
if (static_data_object.tp_dict == 0)
210210
{
211-
Py_TYPE(&static_data_object) = &PyType_Type;
211+
Py_SET_TYPE(&static_data_object, &PyType_Type);
212212
static_data_object.tp_base = &PyProperty_Type;
213213
if (PyType_Ready(&static_data_object))
214214
return 0;
@@ -316,7 +316,7 @@ namespace objects
316316
{
317317
if (class_metatype_object.tp_dict == 0)
318318
{
319-
Py_TYPE(&class_metatype_object) = &PyType_Type;
319+
Py_SET_TYPE(&class_metatype_object, &PyType_Type);
320320
class_metatype_object.tp_base = &PyType_Type;
321321
if (PyType_Ready(&class_metatype_object))
322322
return type_handle();
@@ -374,12 +374,7 @@ namespace objects
374374
// like, so we'll store the total size of the object
375375
// there. A negative number indicates that the extra
376376
// instance memory is not yet allocated to any holders.
377-
#if PY_VERSION_HEX >= 0x02060000
378-
Py_SIZE(result) =
379-
#else
380-
result->ob_size =
381-
#endif
382-
-(static_cast<int>(offsetof(instance<>,storage) + instance_size));
377+
Py_SET_SIZE(result,-static_cast<int>(offsetof(instance<>,storage) + instance_size));
383378
}
384379
return (PyObject*)result;
385380
}
@@ -470,7 +465,7 @@ namespace objects
470465
{
471466
if (class_type_object.tp_dict == 0)
472467
{
473-
Py_TYPE(&class_type_object) = incref(class_metatype().get());
468+
Py_SET_TYPE(&class_type_object, incref(class_metatype().get()));
474469
class_type_object.tp_base = &PyBaseObject_Type;
475470
if (PyType_Ready(&class_type_object))
476471
return type_handle();
@@ -739,7 +734,7 @@ void* instance_holder::allocate(PyObject* self_, std::size_t holder_offset, std:
739734
assert(holder_offset >= offsetof(objects::instance<>,storage));
740735

741736
// Record the fact that the storage is occupied, noting where it starts
742-
Py_SIZE(self) = holder_offset;
737+
Py_SET_SIZE(self, holder_offset);
743738
return (char*)self + holder_offset;
744739
}
745740
else

src/object/enum.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ namespace
153153
{
154154
if (enum_type_object.tp_dict == 0)
155155
{
156-
Py_TYPE(&enum_type_object) = incref(&PyType_Type);
156+
Py_SET_TYPE(&enum_type_object, incref(&PyType_Type));
157157
#if PY_VERSION_HEX >= 0x03000000
158158
enum_type_object.tp_base = &PyLong_Type;
159159
#else

src/object/function.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ function::function(
107107
PyObject* p = this;
108108
if (Py_TYPE(&function_type) == 0)
109109
{
110-
Py_TYPE(&function_type) = &PyType_Type;
110+
Py_SET_TYPE(&function_type, &PyType_Type);
111111
::PyType_Ready(&function_type);
112112
}
113113

src/object/life_support.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ PyObject* make_nurse_and_patient(PyObject* nurse, PyObject* patient)
9393

9494
if (Py_TYPE(&life_support_type) == 0)
9595
{
96-
Py_TYPE(&life_support_type) = &PyType_Type;
96+
Py_SET_TYPE(&life_support_type, &PyType_Type);
9797
PyType_Ready(&life_support_type);
9898
}
9999

0 commit comments

Comments
 (0)