Skip to content

Commit 2b7e2df

Browse files
authored
Port mypyc to Python 3.11: use Py_SET_SIZE() (#11652)
"Py_TYPE(t) = metaclass;" fails with a compiler error on Python 3.11: * https://docs.python.org/dev/whatsnew/3.11.html#c-api-changes * https://docs.python.org/dev/c-api/structures.html#c.Py_TYPE * Replace "Py_TYPE(t) = metaclass;" with "Py_SET_TYPE(t, metaclass);" * Replace "Py_SIZE(self) = newsize;" with "Py_SET_SIZE(self, newsize);" * Add a copy of pythoncapi_compat.h header to add Py_SET_TYPE() and Py_SET_SIZE() functions to Python 3.8 and older: https://github.com/pythoncapi/pythoncapi_compat
1 parent 7a2aff8 commit 2b7e2df

File tree

3 files changed

+399
-4
lines changed

3 files changed

+399
-4
lines changed

mypyc/lib-rt/misc_ops.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// These are registered in mypyc.primitives.misc_ops.
44

55
#include <Python.h>
6+
#include "pythoncapi_compat.h"
67
#include "CPy.h"
78

89
PyObject *CPy_GetCoro(PyObject *obj)
@@ -148,7 +149,7 @@ PyObject *CPyType_FromTemplate(PyObject *template,
148149
// to being type. (This allows us to avoid needing to initialize
149150
// it explicitly on windows.)
150151
if (!Py_TYPE(template_)) {
151-
Py_TYPE(template_) = &PyType_Type;
152+
Py_SET_TYPE(template_, &PyType_Type);
152153
}
153154
PyTypeObject *metaclass = Py_TYPE(template_);
154155

@@ -249,7 +250,7 @@ PyObject *CPyType_FromTemplate(PyObject *template,
249250
// the mro. It was needed for mypy.stats. I need to investigate
250251
// what is actually going on here.
251252
Py_INCREF(metaclass);
252-
Py_TYPE(t) = metaclass;
253+
Py_SET_TYPE(t, metaclass);
253254

254255
if (dummy_class) {
255256
if (PyDict_Merge(t->ht_type.tp_dict, dummy_class->tp_dict, 0) != 0)

0 commit comments

Comments
 (0)