]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-40170: PyObject_GET_WEAKREFS_LISTPTR() becomes a function (GH-19377)
authorVictor Stinner <vstinner@python.org>
Mon, 6 Apr 2020 12:07:02 +0000 (14:07 +0200)
committerGitHub <noreply@github.com>
Mon, 6 Apr 2020 12:07:02 +0000 (14:07 +0200)
Convert the PyObject_GET_WEAKREFS_LISTPTR() macro to a function to
hide implementation details: the macro accessed directly to the
PyTypeObject.tp_weaklistoffset member.

Add _PyObject_GET_WEAKREFS_LISTPTR() static inline function to the
internal C API.

Include/cpython/objimpl.h
Include/internal/pycore_object.h
Misc/NEWS.d/next/C API/2020-04-05-00-10-45.bpo-40170.6nFYbY.rst [new file with mode: 0644]
Modules/_weakref.c
Modules/gcmodule.c
Objects/object.c
Objects/typeobject.c
Objects/weakrefobject.c

index 8e3c964cf44e76c0d3c591a943d0f74c011a2cfa..2f802e92a6cdf82b1018aac9caede6dd5bf24f35 100644 (file)
@@ -138,8 +138,7 @@ PyAPI_FUNC(PyObject *) _PyObject_GC_Calloc(size_t size);
 /* Test if a type supports weak references */
 #define PyType_SUPPORTS_WEAKREFS(t) ((t)->tp_weaklistoffset > 0)
 
-#define PyObject_GET_WEAKREFS_LISTPTR(o) \
-    ((PyObject **) (((char *) (o)) + Py_TYPE(o)->tp_weaklistoffset))
+PyAPI_FUNC(PyObject **) PyObject_GET_WEAKREFS_LISTPTR(PyObject *op);
 
 #ifdef __cplusplus
 }
index 10a5746997ec72592e32e358c9f8dce254df8166..002b70007b639c99c4a44bc9d1d94c0242620fb4 100644 (file)
@@ -87,6 +87,13 @@ extern void _Py_PrintReferences(FILE *);
 extern void _Py_PrintReferenceAddresses(FILE *);
 #endif
 
+static inline PyObject **
+_PyObject_GET_WEAKREFS_LISTPTR(PyObject *op)
+{
+    Py_ssize_t offset = Py_TYPE(op)->tp_weaklistoffset;
+    return (PyObject **)((char *)op + offset);
+}
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/Misc/NEWS.d/next/C API/2020-04-05-00-10-45.bpo-40170.6nFYbY.rst b/Misc/NEWS.d/next/C API/2020-04-05-00-10-45.bpo-40170.6nFYbY.rst
new file mode 100644 (file)
index 0000000..3c4e33b
--- /dev/null
@@ -0,0 +1,3 @@
+Convert the :c:func:`PyObject_GET_WEAKREFS_LISTPTR` macro to a function to hide
+implementation details: the macro accessed directly to the
+:c:member:`PyTypeObject.tp_weaklistoffset` member.
index cd7c4c159ac1b6a9c6e7bc10bacd66e1cd3f203d..e33cba2a3dd81f2cd0de04d1ca7d4e5afafdafb9 100644 (file)
@@ -1,8 +1,9 @@
 #include "Python.h"
+#include "pycore_object.h"   // _PyObject_GET_WEAKREFS_LISTPTR
 
 
 #define GET_WEAKREFS_LISTPTR(o) \
-        ((PyWeakReference **) PyObject_GET_WEAKREFS_LISTPTR(o))
+        ((PyWeakReference **) _PyObject_GET_WEAKREFS_LISTPTR(o))
 
 /*[clinic input]
 module _weakref
index cf164c17d7bf17502970dbdfebfde48de9c9d638..1bc41fb83d8a6097f746f7ecf2493d96b3b28363 100644 (file)
@@ -788,7 +788,7 @@ handle_weakrefs(PyGC_Head *unreachable, PyGC_Head *old)
 
         /* It supports weakrefs.  Does it have any? */
         wrlist = (PyWeakReference **)
-                                PyObject_GET_WEAKREFS_LISTPTR(op);
+                                _PyObject_GET_WEAKREFS_LISTPTR(op);
 
         /* `op` may have some weakrefs.  March over the list, clear
          * all the weakrefs, and move the weakrefs with callbacks
index e6d0da1c775c261f4bd9a10ac1e9bfe49ab5a1e7..05241e8a740d1406afffe5785ff441e2b8c42128 100644 (file)
@@ -2206,6 +2206,14 @@ _Py_Dealloc(PyObject *op)
     (*dealloc)(op);
 }
 
+
+PyObject **
+PyObject_GET_WEAKREFS_LISTPTR(PyObject *op)
+{
+    return _PyObject_GET_WEAKREFS_LISTPTR(op);
+}
+
+
 #ifdef __cplusplus
 }
 #endif
index bed50d1e7f2d3c6c721edc77ed08330fb218a9cc..bdd16af929079fac637dd25f4c50134707bcd9e5 100644 (file)
@@ -1271,7 +1271,7 @@ subtype_dealloc(PyObject *self)
         if (type->tp_weaklistoffset && !base->tp_weaklistoffset) {
             /* Modeled after GET_WEAKREFS_LISTPTR() */
             PyWeakReference **list = (PyWeakReference **) \
-                PyObject_GET_WEAKREFS_LISTPTR(self);
+                _PyObject_GET_WEAKREFS_LISTPTR(self);
             while (*list)
                 _PyWeakref_ClearRef(*list);
         }
index 7a5d9fb88af14d891ff182825ad2e373fb8a6cd1..1e6697b729c9c0856b8b9e5f133417e17f023312 100644 (file)
@@ -1,9 +1,10 @@
 #include "Python.h"
+#include "pycore_object.h"   // _PyObject_GET_WEAKREFS_LISTPTR
 #include "structmember.h"
 
 
 #define GET_WEAKREFS_LISTPTR(o) \
-        ((PyWeakReference **) PyObject_GET_WEAKREFS_LISTPTR(o))
+        ((PyWeakReference **) _PyObject_GET_WEAKREFS_LISTPTR(o))
 
 
 Py_ssize_t