Skip to content

Commit af32b3e

Browse files
authored
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.
1 parent f0a6481 commit af32b3e

File tree

7 files changed

+24
-10
lines changed

7 files changed

+24
-10
lines changed

Include/cpython/objimpl.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ PyAPI_FUNC(int) PyObject_IS_GC(PyObject *obj);
9191
#endif
9292

9393

94-
/* Test if a type supports weak references */
95-
#define PyType_SUPPORTS_WEAKREFS(t) ((t)->tp_weaklistoffset > 0)
94+
// Test if a type supports weak references
95+
PyAPI_FUNC(int) PyType_SUPPORTS_WEAKREFS(PyTypeObject *type);
9696

9797
PyAPI_FUNC(PyObject **) PyObject_GET_WEAKREFS_LISTPTR(PyObject *op);

Include/internal/pycore_object.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,11 @@ extern int _Py_CheckSlotResult(
200200
// See also the Py_TPFLAGS_READY flag.
201201
#define _PyType_IsReady(type) ((type)->tp_dict != NULL)
202202

203+
// Test if a type supports weak references
204+
static inline int _PyType_SUPPORTS_WEAKREFS(PyTypeObject *type) {
205+
return (type->tp_weaklistoffset > 0);
206+
}
207+
203208
extern PyObject* _PyType_AllocNoTrack(PyTypeObject *type, Py_ssize_t nitems);
204209

205210
extern int _PyObject_InitializeDict(PyObject *obj);

Modules/_weakref.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ _weakref_getweakrefcount_impl(PyObject *module, PyObject *object)
2828
{
2929
PyWeakReference **list;
3030

31-
if (!PyType_SUPPORTS_WEAKREFS(Py_TYPE(object)))
31+
if (!_PyType_SUPPORTS_WEAKREFS(Py_TYPE(object)))
3232
return 0;
3333

3434
list = GET_WEAKREFS_LISTPTR(object);
@@ -85,7 +85,7 @@ weakref_getweakrefs(PyObject *self, PyObject *object)
8585
{
8686
PyObject *result = NULL;
8787

88-
if (PyType_SUPPORTS_WEAKREFS(Py_TYPE(object))) {
88+
if (_PyType_SUPPORTS_WEAKREFS(Py_TYPE(object))) {
8989
PyWeakReference **list = GET_WEAKREFS_LISTPTR(object);
9090
Py_ssize_t count = _PyWeakref_GetWeakrefCount(*list);
9191

Modules/gcmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -791,7 +791,7 @@ handle_weakrefs(PyGC_Head *unreachable, PyGC_Head *old)
791791
_PyWeakref_ClearRef((PyWeakReference *)op);
792792
}
793793

794-
if (! PyType_SUPPORTS_WEAKREFS(Py_TYPE(op)))
794+
if (! _PyType_SUPPORTS_WEAKREFS(Py_TYPE(op)))
795795
continue;
796796

797797
/* It supports weakrefs. Does it have any? */

Objects/exceptions.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3729,7 +3729,7 @@ _PyErr_TrySetFromCause(const char *format, ...)
37293729
base_exc_size = _PyExc_BaseException.tp_basicsize;
37303730
same_basic_size = (
37313731
caught_type_size == base_exc_size ||
3732-
(PyType_SUPPORTS_WEAKREFS(caught_type) &&
3732+
(_PyType_SUPPORTS_WEAKREFS(caught_type) &&
37333733
(caught_type_size == base_exc_size + (Py_ssize_t)sizeof(PyObject *))
37343734
)
37353735
);

Objects/typeobject.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2473,12 +2473,21 @@ type_init(PyObject *cls, PyObject *args, PyObject *kwds)
24732473
return 0;
24742474
}
24752475

2476+
24762477
unsigned long
24772478
PyType_GetFlags(PyTypeObject *type)
24782479
{
24792480
return type->tp_flags;
24802481
}
24812482

2483+
2484+
int
2485+
PyType_SUPPORTS_WEAKREFS(PyTypeObject *type)
2486+
{
2487+
return _PyType_SUPPORTS_WEAKREFS(type);
2488+
}
2489+
2490+
24822491
/* Determine the most derived metatype. */
24832492
PyTypeObject *
24842493
_PyType_CalculateMetaclass(PyTypeObject *metatype, PyObject *bases)

Objects/weakrefobject.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ weakref___new__(PyTypeObject *type, PyObject *args, PyObject *kwargs)
299299
PyWeakReference *ref, *proxy;
300300
PyWeakReference **list;
301301

302-
if (!PyType_SUPPORTS_WEAKREFS(Py_TYPE(ob))) {
302+
if (!_PyType_SUPPORTS_WEAKREFS(Py_TYPE(ob))) {
303303
PyErr_Format(PyExc_TypeError,
304304
"cannot create weak reference to '%s' object",
305305
Py_TYPE(ob)->tp_name);
@@ -794,7 +794,7 @@ PyWeakref_NewRef(PyObject *ob, PyObject *callback)
794794
PyWeakReference **list;
795795
PyWeakReference *ref, *proxy;
796796

797-
if (!PyType_SUPPORTS_WEAKREFS(Py_TYPE(ob))) {
797+
if (!_PyType_SUPPORTS_WEAKREFS(Py_TYPE(ob))) {
798798
PyErr_Format(PyExc_TypeError,
799799
"cannot create weak reference to '%s' object",
800800
Py_TYPE(ob)->tp_name);
@@ -853,7 +853,7 @@ PyWeakref_NewProxy(PyObject *ob, PyObject *callback)
853853
PyWeakReference **list;
854854
PyWeakReference *ref, *proxy;
855855

856-
if (!PyType_SUPPORTS_WEAKREFS(Py_TYPE(ob))) {
856+
if (!_PyType_SUPPORTS_WEAKREFS(Py_TYPE(ob))) {
857857
PyErr_Format(PyExc_TypeError,
858858
"cannot create weak reference to '%s' object",
859859
Py_TYPE(ob)->tp_name);
@@ -949,7 +949,7 @@ PyObject_ClearWeakRefs(PyObject *object)
949949
PyWeakReference **list;
950950

951951
if (object == NULL
952-
|| !PyType_SUPPORTS_WEAKREFS(Py_TYPE(object))
952+
|| !_PyType_SUPPORTS_WEAKREFS(Py_TYPE(object))
953953
|| Py_REFCNT(object) != 0)
954954
{
955955
PyErr_BadInternalCall();

0 commit comments

Comments
 (0)