Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Check sys.xoptions *before* we delete it.
  • Loading branch information
ericsnowcurrently committed Sep 12, 2017
commit fd6830a7b68a12a9222d2c9438c1990d1f9b45b9
3 changes: 1 addition & 2 deletions Include/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -727,14 +727,13 @@ PyAPI_FUNC(Py_ssize_t) _Py_GetRefTotal(void);
/* Py_REF_DEBUG also controls the display of refcounts and memory block
* allocations at the interactive prompt and at interpreter shutdown
*/
PyAPI_FUNC(PyObject *) _PyDebug_XOptionShowRefCount(void);
PyAPI_FUNC(void) _PyDebug_PrintTotalRefs(void);
#define _PY_DEBUG_PRINT_TOTAL_REFS() _PyDebug_PrintTotalRefs()
#else
#define _Py_INC_REFTOTAL
#define _Py_DEC_REFTOTAL
#define _Py_REF_DEBUG_COMMA
#define _Py_CHECK_REFCNT(OP) /* a semicolon */;
#define _PY_DEBUG_PRINT_TOTAL_REFS()
#endif /* Py_REF_DEBUG */

#ifdef COUNT_ALLOCS
Expand Down
27 changes: 15 additions & 12 deletions Objects/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,23 @@ _Py_GetRefTotal(void)
return total;
}

void
_PyDebug_PrintTotalRefs(void) {
PyObject *xoptions, *value;
PyObject *
_PyDebug_XOptionShowRefCount(void)
{
PyObject *xoptions = PySys_GetXOptions();
if (xoptions == NULL)
return NULL;

_Py_IDENTIFIER(showrefcount);
return _PyDict_GetItemId(xoptions, &PyId_showrefcount);
}

xoptions = PySys_GetXOptions();
if (xoptions == NULL)
return;
value = _PyDict_GetItemId(xoptions, &PyId_showrefcount);
if (value == Py_True)
fprintf(stderr,
"[%" PY_FORMAT_SIZE_T "d refs, "
"%" PY_FORMAT_SIZE_T "d blocks]\n",
_Py_GetRefTotal(), _Py_GetAllocatedBlocks());
void
_PyDebug_PrintTotalRefs(void) {
fprintf(stderr,
"[%" PY_FORMAT_SIZE_T "d refs, "
"%" PY_FORMAT_SIZE_T "d blocks]\n",
_Py_GetRefTotal(), _Py_GetAllocatedBlocks());
}
#endif /* Py_REF_DEBUG */

Expand Down
10 changes: 9 additions & 1 deletion Python/pylifecycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -1006,6 +1006,11 @@ Py_FinalizeEx(void)
while (_PyGC_CollectIfEnabled() > 0)
/* nothing */;
#endif

#ifdef Py_REF_DEBUG
PyObject *showrefcount = _PyDebug_XOptionShowRefCount();
#endif

/* Destroy all modules */
PyImport_Cleanup();

Expand Down Expand Up @@ -1053,7 +1058,10 @@ Py_FinalizeEx(void)
/* dump hash stats */
_PyHash_Fini();

_PY_DEBUG_PRINT_TOTAL_REFS();
#ifdef Py_REF_DEBUG
if (showrefcount == Py_True)
_PyDebug_PrintTotalRefs();
#endif

#ifdef Py_TRACE_REFS
/* Display all objects still alive -- this can invoke arbitrary
Expand Down
5 changes: 4 additions & 1 deletion Python/pythonrun.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,10 @@ PyRun_InteractiveLoopFlags(FILE *fp, const char *filename_str, PyCompilerFlags *
err = -1;
for (;;) {
ret = PyRun_InteractiveOneObject(fp, filename, flags);
_PY_DEBUG_PRINT_TOTAL_REFS();
#ifdef Py_REF_DEBUG
if (_PyDebug_XOptionShowRefCount() == Py_True)
_PyDebug_PrintTotalRefs();
#endif
if (ret == E_EOF) {
err = 0;
break;
Expand Down