Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9bf1644

Browse files
committed
A few miscellaneous helpers.
PyObject_Dump(): New function that is useful when debugging Python's C runtime. In something like gdb it can be a pain to get some useful information out of PyObject*'s. This function prints the str() of the object to stderr, along with the object's refcount and hex address. PyGC_Dump(): Similar to PyObject_Dump() but knows how to cast from the garbage collector prefix back to the PyObject* structure. [See Misc/gdbinit for some useful gdb hooks] none_dealloc(): Rather than SEGV if we accidentally decref None out of existance, we assign None's and NotImplemented's destructor slot to this function, which just calls abort().
1 parent 9667ed2 commit 9bf1644

1 file changed

Lines changed: 26 additions & 2 deletions

File tree

Objects/object.c

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,19 @@ PyObject_Print(PyObject *op, FILE *fp, int flags)
220220
return ret;
221221
}
222222

223+
/* For debugging convenience. See Misc/gdbinit for some useful gdb hooks */
224+
void PyObject_Dump(PyObject* op)
225+
{
226+
(void)PyObject_Print(op, stderr, 0);
227+
fprintf(stderr, "\nrefcounts: %d\n", op->ob_refcnt);
228+
fprintf(stderr, "address : %x\n", op);
229+
}
230+
void PyGC_Dump(PyGC_Head* op)
231+
{
232+
PyObject_Dump(PyObject_FROM_GC(op));
233+
}
234+
235+
223236
PyObject *
224237
PyObject_Repr(PyObject *v)
225238
{
@@ -1213,13 +1226,24 @@ none_repr(PyObject *op)
12131226
return PyString_FromString("None");
12141227
}
12151228

1229+
/* ARGUSED */
1230+
static void
1231+
none_dealloc(PyObject* ignore)
1232+
{
1233+
/* This should never get called, but we also don't want to SEGV if
1234+
* we accidently decref None out of existance.
1235+
*/
1236+
abort();
1237+
}
1238+
1239+
12161240
static PyTypeObject PyNothing_Type = {
12171241
PyObject_HEAD_INIT(&PyType_Type)
12181242
0,
12191243
"None",
12201244
0,
12211245
0,
1222-
0, /*tp_dealloc*/ /*never called*/
1246+
(destructor)none_dealloc, /*tp_dealloc*/ /*never called*/
12231247
0, /*tp_print*/
12241248
0, /*tp_getattr*/
12251249
0, /*tp_setattr*/
@@ -1250,7 +1274,7 @@ static PyTypeObject PyNotImplemented_Type = {
12501274
"NotImplemented",
12511275
0,
12521276
0,
1253-
0, /*tp_dealloc*/ /*never called*/
1277+
(destructor)none_dealloc, /*tp_dealloc*/ /*never called*/
12541278
0, /*tp_print*/
12551279
0, /*tp_getattr*/
12561280
0, /*tp_setattr*/

0 commit comments

Comments
 (0)