| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 26e7bbf commit 7be667d
6 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -76,6 +76,21 @@ static inline void _Py_SetImmortal(PyObject *op) | |||
| 76 | 76 | } | |
| 77 | 77 | #define _Py_SetImmortal(op) _Py_SetImmortal(_PyObject_CAST(op)) | |
| 78 | 78 | ||
| 79 | + /* _Py_ClearImmortal() should only be used during runtime finalization. */ | ||
| 80 | + static inline void _Py_ClearImmortal(PyObject *op) | ||
| 81 | + { | ||
| 82 | + if (op) { | ||
| 83 | + assert(op->ob_refcnt == _Py_IMMORTAL_REFCNT); | ||
| 84 | + op->ob_refcnt = 1; | ||
| 85 | + Py_DECREF(op); | ||
| 86 | + } | ||
| 87 | + } | ||
| 88 | + #define _Py_ClearImmortal(op) \ | ||
| 89 | + do { \ | ||
| 90 | + _Py_ClearImmortal(_PyObject_CAST(op)); \ | ||
| 91 | + op = NULL; \ | ||
| 92 | + } while (0) | ||
| 93 | + | ||
| 79 | 94 | static inline void | |
| 80 | 95 | _Py_DECREF_SPECIALIZED(PyObject *op, const destructor destruct) | |
| 81 | 96 | { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -46,11 +46,9 @@ typedef struct { | |||
| 46 | 46 | PyTypeObject *type; | |
| 47 | 47 | int readying; | |
| 48 | 48 | int ready; | |
| 49 | - // XXX tp_dict, tp_bases, and tp_mro can probably be statically | ||
| 50 | - // allocated, instead of dynamically and stored on the interpreter. | ||
| 49 | + // XXX tp_dict can probably be statically allocated, | ||
| 50 | + // instead of dynamically and stored on the interpreter. | ||
| 51 | 51 | PyObject *tp_dict; | |
| 52 | - PyObject *tp_bases; | ||
| 53 | - PyObject *tp_mro; | ||
| 54 | 52 | PyObject *tp_subclasses; | |
| 55 | 53 | /* We never clean up weakrefs for static builtin types since | |
| 56 | 54 | they will effectively never get triggered. However, there | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1593,6 +1593,39 @@ def test_module_state_shared_in_global(self): | |||
| 1593 | 1593 | self.assertEqual(main_attr_id, subinterp_attr_id) | |
| 1594 | 1594 | ||
| 1595 | 1595 | ||
| 1596 | + class BuiltinStaticTypesTests(unittest.TestCase): | ||
| 1597 | + | ||
| 1598 | + TYPES = [ | ||
| 1599 | + object, | ||
| 1600 | + type, | ||
| 1601 | + int, | ||
| 1602 | + str, | ||
| 1603 | + dict, | ||
| 1604 | + type(None), | ||
| 1605 | + bool, | ||
| 1606 | + BaseException, | ||
| 1607 | + Exception, | ||
| 1608 | + Warning, | ||
| 1609 | + DeprecationWarning, # Warning subclass | ||
| 1610 | + ] | ||
| 1611 | + | ||
| 1612 | + def test_tp_bases_is_set(self): | ||
| 1613 | + # PyTypeObject.tp_bases is documented as public API. | ||
| 1614 | + # See https://github.com/python/cpython/issues/105020. | ||
| 1615 | + for typeobj in self.TYPES: | ||
| 1616 | + with self.subTest(typeobj): | ||
| 1617 | + bases = _testcapi.type_get_tp_bases(typeobj) | ||
| 1618 | + self.assertIsNot(bases, None) | ||
| 1619 | + | ||
| 1620 | + def test_tp_mro_is_set(self): | ||
| 1621 | + # PyTypeObject.tp_bases is documented as public API. | ||
| 1622 | + # See https://github.com/python/cpython/issues/105020. | ||
| 1623 | + for typeobj in self.TYPES: | ||
| 1624 | + with self.subTest(typeobj): | ||
| 1625 | + mro = _testcapi.type_get_tp_mro(typeobj) | ||
| 1626 | + self.assertIsNot(mro, None) | ||
| 1627 | + | ||
| 1628 | + | ||
| 1596 | 1629 | class TestThreadState(unittest.TestCase): | |
| 1597 | 1630 | ||
| 1598 | 1631 | @threading_helper.reap_threads | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,3 @@ | |||
| 1 | + ``PyTypeObject.tp_bases`` (and ``tp_mro``) for builtin static types are now | ||
| 2 | + shared by all interpreters, whereas in 3.12-beta1 they were stored on | ||
| 3 | + ``PyInterpreterState``. Also note that now the tuples are immortal objects. | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2606,6 +2606,27 @@ type_assign_version(PyObject *self, PyObject *type) | |||
| 2606 | 2606 | } | |
| 2607 | 2607 | ||
| 2608 | 2608 | ||
| 2609 | + static PyObject * | ||
| 2610 | + type_get_tp_bases(PyObject *self, PyObject *type) | ||
| 2611 | + { | ||
| 2612 | + PyObject *bases = ((PyTypeObject *)type)->tp_bases; | ||
| 2613 | + if (bases == NULL) { | ||
| 2614 | + Py_RETURN_NONE; | ||
| 2615 | + } | ||
| 2616 | + return Py_NewRef(bases); | ||
| 2617 | + } | ||
| 2618 | + | ||
| 2619 | + static PyObject * | ||
| 2620 | + type_get_tp_mro(PyObject *self, PyObject *type) | ||
| 2621 | + { | ||
| 2622 | + PyObject *mro = ((PyTypeObject *)type)->tp_mro; | ||
| 2623 | + if (mro == NULL) { | ||
| 2624 | + Py_RETURN_NONE; | ||
| 2625 | + } | ||
| 2626 | + return Py_NewRef(mro); | ||
| 2627 | + } | ||
| 2628 | + | ||
| 2629 | + | ||
| 2609 | 2630 | // Test PyThreadState C API | |
| 2610 | 2631 | static PyObject * | |
| 2611 | 2632 | test_tstate_capi(PyObject *self, PyObject *Py_UNUSED(args)) | |
@@ -3361,6 +3382,8 @@ static PyMethodDef TestMethods[] = { | |||
| 3361 | 3382 | {"test_py_is_funcs", test_py_is_funcs, METH_NOARGS}, | |
| 3362 | 3383 | {"type_get_version", type_get_version, METH_O, PyDoc_STR("type->tp_version_tag")}, | |
| 3363 | 3384 | {"type_assign_version", type_assign_version, METH_O, PyDoc_STR("PyUnstable_Type_AssignVersionTag")}, | |
| 3385 | + {"type_get_tp_bases", type_get_tp_bases, METH_O}, | ||
| 3386 | + {"type_get_tp_mro", type_get_tp_mro, METH_O}, | ||
| 3364 | 3387 | {"test_tstate_capi", test_tstate_capi, METH_NOARGS, NULL}, | |
| 3365 | 3388 | {"frame_getlocals", frame_getlocals, METH_O, NULL}, | |
| 3366 | 3389 | {"frame_getglobals", frame_getglobals, METH_O, NULL}, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -268,12 +268,6 @@ clear_tp_dict(PyTypeObject *self) | |||
| 268 | 268 | static inline PyObject * | |
| 269 | 269 | lookup_tp_bases(PyTypeObject *self) | |
| 270 | 270 | { | |
| 271 | - if (self->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN) { | ||
| 272 | - PyInterpreterState *interp = _PyInterpreterState_GET(); | ||
| 273 | - static_builtin_state *state = _PyStaticType_GetState(interp, self); | ||
| 274 | - assert(state != NULL); | ||
| 275 | - return state->tp_bases; | ||
| 276 | - } | ||
| 277 | 271 | return self->tp_bases; | |
| 278 | 272 | } | |
| 279 | 273 | ||
@@ -287,12 +281,22 @@ _PyType_GetBases(PyTypeObject *self) | |||
| 287 | 281 | static inline void | |
| 288 | 282 | set_tp_bases(PyTypeObject *self, PyObject *bases) | |
| 289 | 283 | { | |
| 284 | + assert(PyTuple_CheckExact(bases)); | ||
| 290 | 285 | if (self->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN) { | |
| 291 | - PyInterpreterState *interp = _PyInterpreterState_GET(); | ||
| 292 | - static_builtin_state *state = _PyStaticType_GetState(interp, self); | ||
| 293 | - assert(state != NULL); | ||
| 294 | - state->tp_bases = bases; | ||
| 295 | - return; | ||
| 286 | + // XXX tp_bases can probably be statically allocated for each | ||
| 287 | + // static builtin type. | ||
| 288 | + assert(_Py_IsMainInterpreter(_PyInterpreterState_GET())); | ||
| 289 | + assert(self->tp_bases == NULL); | ||
| 290 | + if (PyTuple_GET_SIZE(bases) == 0) { | ||
| 291 | + assert(self->tp_base == NULL); | ||
| 292 | + } | ||
| 293 | + else { | ||
| 294 | + assert(PyTuple_GET_SIZE(bases) == 1); | ||
| 295 | + assert(PyTuple_GET_ITEM(bases, 0) == (PyObject *)self->tp_base); | ||
| 296 | + assert(self->tp_base->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN); | ||
| 297 | + assert(_Py_IsImmortal(self->tp_base)); | ||
| 298 | + } | ||
| 299 | + _Py_SetImmortal(bases); | ||
| 296 | 300 | } | |
| 297 | 301 | self->tp_bases = bases; | |
| 298 | 302 | } | |
@@ -301,10 +305,14 @@ static inline void | |||
| 301 | 305 | clear_tp_bases(PyTypeObject *self) | |
| 302 | 306 | { | |
| 303 | 307 | if (self->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN) { | |
| 304 | - PyInterpreterState *interp = _PyInterpreterState_GET(); | ||
| 305 | - static_builtin_state *state = _PyStaticType_GetState(interp, self); | ||
| 306 | - assert(state != NULL); | ||
| 307 | - Py_CLEAR(state->tp_bases); | ||
| 308 | + if (_Py_IsMainInterpreter(_PyInterpreterState_GET())) { | ||
| 309 | + if (self->tp_bases != NULL | ||
| 310 | + && PyTuple_GET_SIZE(self->tp_bases) > 0) | ||
| 311 | + { | ||
| 312 | + assert(_Py_IsImmortal(self->tp_bases)); | ||
| 313 | + _Py_ClearImmortal(self->tp_bases); | ||
| 314 | + } | ||
| 315 | + } | ||
| 308 | 316 | return; | |
| 309 | 317 | } | |
| 310 | 318 | Py_CLEAR(self->tp_bases); | |
@@ -314,12 +322,6 @@ clear_tp_bases(PyTypeObject *self) | |||
| 314 | 322 | static inline PyObject * | |
| 315 | 323 | lookup_tp_mro(PyTypeObject *self) | |
| 316 | 324 | { | |
| 317 | - if (self->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN) { | ||
| 318 | - PyInterpreterState *interp = _PyInterpreterState_GET(); | ||
| 319 | - static_builtin_state *state = _PyStaticType_GetState(interp, self); | ||
| 320 | - assert(state != NULL); | ||
| 321 | - return state->tp_mro; | ||
| 322 | - } | ||
| 323 | 325 | return self->tp_mro; | |
| 324 | 326 | } | |
| 325 | 327 | ||
@@ -333,12 +335,14 @@ _PyType_GetMRO(PyTypeObject *self) | |||
| 333 | 335 | static inline void | |
| 334 | 336 | set_tp_mro(PyTypeObject *self, PyObject *mro) | |
| 335 | 337 | { | |
| 338 | + assert(PyTuple_CheckExact(mro)); | ||
| 336 | 339 | if (self->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN) { | |
| 337 | - PyInterpreterState *interp = _PyInterpreterState_GET(); | ||
| 338 | - static_builtin_state *state = _PyStaticType_GetState(interp, self); | ||
| 339 | - assert(state != NULL); | ||
| 340 | - state->tp_mro = mro; | ||
| 341 | - return; | ||
| 340 | + // XXX tp_mro can probably be statically allocated for each | ||
| 341 | + // static builtin type. | ||
| 342 | + assert(_Py_IsMainInterpreter(_PyInterpreterState_GET())); | ||
| 343 | + assert(self->tp_mro == NULL); | ||
| 344 | + /* Other checks are done via set_tp_bases. */ | ||
| 345 | + _Py_SetImmortal(mro); | ||
| 342 | 346 | } | |
| 343 | 347 | self->tp_mro = mro; | |
| 344 | 348 | } | |
@@ -347,10 +351,14 @@ static inline void | |||
| 347 | 351 | clear_tp_mro(PyTypeObject *self) | |
| 348 | 352 | { | |
| 349 | 353 | if (self->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN) { | |
| 350 | - PyInterpreterState *interp = _PyInterpreterState_GET(); | ||
| 351 | - static_builtin_state *state = _PyStaticType_GetState(interp, self); | ||
| 352 | - assert(state != NULL); | ||
| 353 | - Py_CLEAR(state->tp_mro); | ||
| 354 | + if (_Py_IsMainInterpreter(_PyInterpreterState_GET())) { | ||
| 355 | + if (self->tp_mro != NULL | ||
| 356 | + && PyTuple_GET_SIZE(self->tp_mro) > 0) | ||
| 357 | + { | ||
| 358 | + assert(_Py_IsImmortal(self->tp_mro)); | ||
| 359 | + _Py_ClearImmortal(self->tp_mro); | ||
| 360 | + } | ||
| 361 | + } | ||
| 354 | 362 | return; | |
| 355 | 363 | } | |
| 356 | 364 | Py_CLEAR(self->tp_mro); | |
@@ -7153,6 +7161,14 @@ type_ready_preheader(PyTypeObject *type) | |||
| 7153 | 7161 | static int | |
| 7154 | 7162 | type_ready_mro(PyTypeObject *type) | |
| 7155 | 7163 | { | |
| 7164 | + if (type->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN) { | ||
| 7165 | + if (!_Py_IsMainInterpreter(_PyInterpreterState_GET())) { | ||
| 7166 | + assert(lookup_tp_mro(type) != NULL); | ||
| 7167 | + return 0; | ||
| 7168 | + } | ||
| 7169 | + assert(lookup_tp_mro(type) == NULL); | ||
| 7170 | + } | ||
| 7171 | + | ||
| 7156 | 7172 | /* Calculate method resolution order */ | |
| 7157 | 7173 | if (mro_internal(type, NULL) < 0) { | |
| 7158 | 7174 | return -1; | |
| Back | FazBrowse Home | New Git URL |
0 commit comments