From af32b3ef1fbad3c2242627a14398320960a0cb45 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 27 Jan 2022 03:00:55 +0100 Subject: bpo-40170: PyType_SUPPORTS_WEAKREFS() becomes a regular function (GH-30938) Convert the PyType_SUPPORTS_WEAKREFS() macro to a regular function. It no longer access the PyTypeObject.tp_weaklistoffset member directly. Add _PyType_SUPPORTS_WEAKREFS() static inline functions, used internally by Python for best performance. --- Include/cpython/objimpl.h | 4 ++-- Include/internal/pycore_object.h | 5 +++++ Modules/_weakref.c | 4 ++-- Modules/gcmodule.c | 2 +- Objects/exceptions.c | 2 +- Objects/typeobject.c | 9 +++++++++ Objects/weakrefobject.c | 8 ++++---- 7 files changed, 24 insertions(+), 10 deletions(-) diff --git a/Include/cpython/objimpl.h b/Include/cpython/objimpl.h index 4a905c2..7fff96e 100644 --- a/Include/cpython/objimpl.h +++ b/Include/cpython/objimpl.h @@ -91,7 +91,7 @@ PyAPI_FUNC(int) PyObject_IS_GC(PyObject *obj); #endif -/* Test if a type supports weak references */ -#define PyType_SUPPORTS_WEAKREFS(t) ((t)->tp_weaklistoffset > 0) +// Test if a type supports weak references +PyAPI_FUNC(int) PyType_SUPPORTS_WEAKREFS(PyTypeObject *type); PyAPI_FUNC(PyObject **) PyObject_GET_WEAKREFS_LISTPTR(PyObject *op); diff --git a/Include/internal/pycore_object.h b/Include/internal/pycore_object.h index be308cd..5fe4ddb 100644 --- a/Include/internal/pycore_object.h +++ b/Include/internal/pycore_object.h @@ -200,6 +200,11 @@ extern int _Py_CheckSlotResult( // See also the Py_TPFLAGS_READY flag. #define _PyType_IsReady(type) ((type)->tp_dict != NULL) +// Test if a type supports weak references +static inline int _PyType_SUPPORTS_WEAKREFS(PyTypeObject *type) { + return (type->tp_weaklistoffset > 0); +} + extern PyObject* _PyType_AllocNoTrack(PyTypeObject *type, Py_ssize_t nitems); extern int _PyObject_InitializeDict(PyObject *obj); diff --git a/Modules/_weakref.c b/Modules/_weakref.c index e33cba2..edc09b9 100644 --- a/Modules/_weakref.c +++ b/Modules/_weakref.c @@ -28,7 +28,7 @@ _weakref_getweakrefcount_impl(PyObject *module, PyObject *object) { PyWeakReference **list; - if (!PyType_SUPPORTS_WEAKREFS(Py_TYPE(object))) + if (!_PyType_SUPPORTS_WEAKREFS(Py_TYPE(object))) return 0; list = GET_WEAKREFS_LISTPTR(object); @@ -85,7 +85,7 @@ weakref_getweakrefs(PyObject *self, PyObject *object) { PyObject *result = NULL; - if (PyType_SUPPORTS_WEAKREFS(Py_TYPE(object))) { + if (_PyType_SUPPORTS_WEAKREFS(Py_TYPE(object))) { PyWeakReference **list = GET_WEAKREFS_LISTPTR(object); Py_ssize_t count = _PyWeakref_GetWeakrefCount(*list); diff --git a/Modules/gcmodule.c b/Modules/gcmodule.c index 16f8c2b..802c3ea 100644 --- a/Modules/gcmodule.c +++ b/Modules/gcmodule.c @@ -791,7 +791,7 @@ handle_weakrefs(PyGC_Head *unreachable, PyGC_Head *old) _PyWeakref_ClearRef((PyWeakReference *)op); } - if (! PyType_SUPPORTS_WEAKREFS(Py_TYPE(op))) + if (! _PyType_SUPPORTS_WEAKREFS(Py_TYPE(op))) continue; /* It supports weakrefs. Does it have any? */ diff --git a/Objects/exceptions.c b/Objects/exceptions.c index d8bfb31..ea8a310 100644 --- a/Objects/exceptions.c +++ b/Objects/exceptions.c @@ -3729,7 +3729,7 @@ _PyErr_TrySetFromCause(const char *format, ...) base_exc_size = _PyExc_BaseException.tp_basicsize; same_basic_size = ( caught_type_size == base_exc_size || - (PyType_SUPPORTS_WEAKREFS(caught_type) && + (_PyType_SUPPORTS_WEAKREFS(caught_type) && (caught_type_size == base_exc_size + (Py_ssize_t)sizeof(PyObject *)) ) ); diff --git a/Objects/typeobject.c b/Objects/typeobject.c index 4527593..39e8b46 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -2473,12 +2473,21 @@ type_init(PyObject *cls, PyObject *args, PyObject *kwds) return 0; } + unsigned long PyType_GetFlags(PyTypeObject *type) { return type->tp_flags; } + +int +PyType_SUPPORTS_WEAKREFS(PyTypeObject *type) +{ + return _PyType_SUPPORTS_WEAKREFS(type); +} + + /* Determine the most derived metatype. */ PyTypeObject * _PyType_CalculateMetaclass(PyTypeObject *metatype, PyObject *bases) diff --git a/Objects/weakrefobject.c b/Objects/weakrefobject.c index b992040..76121f9 100644 --- a/Objects/weakrefobject.c +++ b/Objects/weakrefobject.c @@ -299,7 +299,7 @@ weakref___new__(PyTypeObject *type, PyObject *args, PyObject *kwargs) PyWeakReference *ref, *proxy; PyWeakReference **list; - if (!PyType_SUPPORTS_WEAKREFS(Py_TYPE(ob))) { + if (!_PyType_SUPPORTS_WEAKREFS(Py_TYPE(ob))) { PyErr_Format(PyExc_TypeError, "cannot create weak reference to '%s' object", Py_TYPE(ob)->tp_name); @@ -794,7 +794,7 @@ PyWeakref_NewRef(PyObject *ob, PyObject *callback) PyWeakReference **list; PyWeakReference *ref, *proxy; - if (!PyType_SUPPORTS_WEAKREFS(Py_TYPE(ob))) { + if (!_PyType_SUPPORTS_WEAKREFS(Py_TYPE(ob))) { PyErr_Format(PyExc_TypeError, "cannot create weak reference to '%s' object", Py_TYPE(ob)->tp_name); @@ -853,7 +853,7 @@ PyWeakref_NewProxy(PyObject *ob, PyObject *callback) PyWeakReference **list; PyWeakReference *ref, *proxy; - if (!PyType_SUPPORTS_WEAKREFS(Py_TYPE(ob))) { + if (!_PyType_SUPPORTS_WEAKREFS(Py_TYPE(ob))) { PyErr_Format(PyExc_TypeError, "cannot create weak reference to '%s' object", Py_TYPE(ob)->tp_name); @@ -949,7 +949,7 @@ PyObject_ClearWeakRefs(PyObject *object) PyWeakReference **list; if (object == NULL - || !PyType_SUPPORTS_WEAKREFS(Py_TYPE(object)) + || !_PyType_SUPPORTS_WEAKREFS(Py_TYPE(object)) || Py_REFCNT(object) != 0) { PyErr_BadInternalCall(); -- cgit v0.12