[Python-checkins] gh-101430: Update tracemalloc to handle presize properly. (gh-101745)

corona10 webhook-mailer at python.org
Thu Feb 9 18:30:20 EST 2023


https://github.com/python/cpython/commit/5b946d371979a772120e6ee7d37f9b735769d433
commit: 5b946d371979a772120e6ee7d37f9b735769d433
branch: main
author: Dong-hee Na <donghee.na at python.org>
committer: corona10 <donghee.na92 at gmail.com>
date: 2023-02-10T08:30:03+09:00
summary:

gh-101430: Update tracemalloc to handle presize properly. (gh-101745)

files:
A Misc/NEWS.d/next/Core and Builtins/2023-02-10-01-15-57.gh-issue-101430.T3Gegb.rst
M Modules/_tracemalloc.c
M Objects/object.c

diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-02-10-01-15-57.gh-issue-101430.T3Gegb.rst b/Misc/NEWS.d/next/Core and Builtins/2023-02-10-01-15-57.gh-issue-101430.T3Gegb.rst
new file mode 100644
index 000000000000..e617d8524214
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2023-02-10-01-15-57.gh-issue-101430.T3Gegb.rst	
@@ -0,0 +1,2 @@
+Update :mod:`tracemalloc` to handle presize of object properly. Patch by
+Dong-hee Na.
diff --git a/Modules/_tracemalloc.c b/Modules/_tracemalloc.c
index 9826ad2935be..d69c5636486d 100644
--- a/Modules/_tracemalloc.c
+++ b/Modules/_tracemalloc.c
@@ -2,6 +2,7 @@
 #include "pycore_fileutils.h"     // _Py_write_noraise()
 #include "pycore_gc.h"            // PyGC_Head
 #include "pycore_hashtable.h"     // _Py_hashtable_t
+#include "pycore_object.h"        // _PyType_PreHeaderSize
 #include "pycore_pymem.h"         // _Py_tracemalloc_config
 #include "pycore_runtime.h"       // _Py_ID()
 #include "pycore_traceback.h"
@@ -1400,20 +1401,16 @@ _tracemalloc__get_object_traceback(PyObject *module, PyObject *obj)
 /*[clinic end generated code: output=41ee0553a658b0aa input=29495f1b21c53212]*/
 {
     PyTypeObject *type;
-    void *ptr;
     traceback_t *traceback;
 
     type = Py_TYPE(obj);
-    if (PyType_IS_GC(type)) {
-        ptr = (void *)((char *)obj - sizeof(PyGC_Head));
-    }
-    else {
-        ptr = (void *)obj;
-    }
+    const size_t presize = _PyType_PreHeaderSize(type);
+    uintptr_t ptr = (uintptr_t)((char *)obj - presize);
 
-    traceback = tracemalloc_get_traceback(DEFAULT_DOMAIN, (uintptr_t)ptr);
-    if (traceback == NULL)
+    traceback = tracemalloc_get_traceback(DEFAULT_DOMAIN, ptr);
+    if (traceback == NULL) {
         Py_RETURN_NONE;
+    }
 
     return traceback_to_pyobject(traceback, NULL);
 }
@@ -1723,14 +1720,9 @@ _PyTraceMalloc_NewReference(PyObject *op)
         return -1;
     }
 
-    uintptr_t ptr;
     PyTypeObject *type = Py_TYPE(op);
-    if (PyType_IS_GC(type)) {
-        ptr = (uintptr_t)((char *)op - sizeof(PyGC_Head));
-    }
-    else {
-        ptr = (uintptr_t)op;
-    }
+    const size_t presize = _PyType_PreHeaderSize(type);
+    uintptr_t ptr = (uintptr_t)((char *)op - presize);
 
     int res = -1;
 
diff --git a/Objects/object.c b/Objects/object.c
index 7817c04ef5f5..446c7b1f5f03 100644
--- a/Objects/object.c
+++ b/Objects/object.c
@@ -2387,14 +2387,9 @@ _PyObject_AssertFailed(PyObject *obj, const char *expr, const char *msg,
         /* Display the traceback where the object has been allocated.
            Do it before dumping repr(obj), since repr() is more likely
            to crash than dumping the traceback. */
-        void *ptr;
         PyTypeObject *type = Py_TYPE(obj);
-        if (_PyType_IS_GC(type)) {
-            ptr = (void *)((char *)obj - sizeof(PyGC_Head));
-        }
-        else {
-            ptr = (void *)obj;
-        }
+        const size_t presize = _PyType_PreHeaderSize(type);
+        void *ptr = (void *)((char *)obj - presize);
         _PyMem_DumpTraceback(fileno(stderr), ptr);
 
         /* This might succeed or fail, but we're about to abort, so at least



More information about the Python-checkins mailing list