Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 7 additions & 0 deletions Doc/using/cmdline.rst
Original file line number Diff line number Diff line change
Expand Up @@ -663,3 +663,10 @@ if Python was configured with the ``--with-pydebug`` build option.

If set, Python will print memory allocation statistics every time a new
object arena is created, and on shutdown.

.. envvar:: PYTHONSHOWREFCOUNT

If set, Python will print the total reference count when the program
finishes or after each statement in the interactive interpreter.

.. versionadded:: 2.7.15
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add a new PYTHONSHOWREFCOUNT environment variable. In debug mode, Python now
only print the total reference count if PYTHONSHOWREFCOUNT is set.
27 changes: 17 additions & 10 deletions Python/pythonrun.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,6 @@
#include "windows.h"
#endif

#ifndef Py_REF_DEBUG
#define PRINT_TOTAL_REFS()
#else /* Py_REF_DEBUG */
#define PRINT_TOTAL_REFS() fprintf(stderr, \
"[%" PY_FORMAT_SIZE_T "d refs]\n", \
_Py_GetRefTotal())
#endif

#ifdef __cplusplus
extern "C" {
#endif
Expand Down Expand Up @@ -104,6 +96,21 @@ PyModule_GetWarningsModule(void)
return PyImport_ImportModule("warnings");
}

static void
_PyDebug_PrintTotalRefs(void)
{
#ifdef Py_REF_DEBUG
Py_ssize_t total;

if (!Py_GETENV("PYTHONSHOWREFCOUNT")) {
return;
}

total = _Py_GetRefTotal();
fprintf(stderr, "[%" PY_FORMAT_SIZE_T "d refs]\n", total);
#endif
}

static int initialized = 0;

/* API to access the initialized flag -- useful for esoteric use */
Expand Down Expand Up @@ -484,7 +491,7 @@ Py_Finalize(void)
dump_counts(stdout);
#endif

PRINT_TOTAL_REFS();
_PyDebug_PrintTotalRefs();

#ifdef Py_TRACE_REFS
/* Display all objects still alive -- this can invoke arbitrary
Expand Down Expand Up @@ -775,7 +782,7 @@ PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flag
}
for (;;) {
ret = PyRun_InteractiveOneFlags(fp, filename, flags);
PRINT_TOTAL_REFS();
_PyDebug_PrintTotalRefs();
if (ret == E_EOF)
return 0;
/*
Expand Down