|
|
f948d41 |
diff --git a/Doc/using/cmdline.rst b/Doc/using/cmdline.rst
|
|
|
f948d41 |
index 55bc128..15d5830 100644
|
|
|
f948d41 |
--- a/Doc/using/cmdline.rst
|
|
|
f948d41 |
+++ b/Doc/using/cmdline.rst
|
|
|
f948d41 |
@@ -664,6 +664,13 @@ if Python was configured with the ``--with-pydebug`` build option.
|
|
|
f948d41 |
If set, Python will print memory allocation statistics every time a new
|
|
|
f948d41 |
object arena is created, and on shutdown.
|
|
|
f948d41 |
|
|
|
f948d41 |
+.. envvar:: PYTHONSHOWREFCOUNT
|
|
|
f948d41 |
+
|
|
|
f948d41 |
+ If set, Python will print the total reference count when the program
|
|
|
f948d41 |
+ finishes or after each statement in the interactive interpreter.
|
|
|
f948d41 |
+
|
|
|
f948d41 |
+ .. versionadded:: 2.7.15
|
|
|
f948d41 |
+
|
|
|
f948d41 |
.. envvar:: PYTHONSHOWALLOCCOUNT
|
|
|
f948d41 |
|
|
|
f948d41 |
If set and Python was compiled with ``COUNT_ALLOCS`` defined, Python will
|
|
|
f948d41 |
diff --git a/Doc/whatsnew/2.7.rst b/Doc/whatsnew/2.7.rst
|
|
|
f948d41 |
index f0d2428..b29593a 100644
|
|
|
f948d41 |
--- a/Doc/whatsnew/2.7.rst
|
|
|
f948d41 |
+++ b/Doc/whatsnew/2.7.rst
|
|
|
f948d41 |
@@ -2548,6 +2548,10 @@ longer dumped by default anymore: the :envvar:`PYTHONSHOWALLOCCOUNT` environment
|
|
|
f948d41 |
variable must now also be set. Moreover, allocation counts are now dumped into
|
|
|
f948d41 |
stderr, rather than stdout. (Contributed by Victor Stinner; :issue:`31692`.)
|
|
|
f948d41 |
|
|
|
f948d41 |
+In debug mode, the ``[xxx refs]`` statistic is not written by default, the
|
|
|
f948d41 |
+:envvar:`PYTHONSHOWREFCOUNT` environment variable now must also be set.
|
|
|
f948d41 |
+(Contributed by Victor Stinner; :issue:`31733`.)
|
|
|
f948d41 |
+
|
|
|
f948d41 |
.. versionadded:: 2.7.15
|
|
|
f948d41 |
|
|
|
f948d41 |
|
|
|
f948d41 |
diff --git a/Python/pythonrun.c b/Python/pythonrun.c
|
|
|
f948d41 |
index d17f7f3..eb31e34 100644
|
|
|
f948d41 |
--- a/Python/pythonrun.c
|
|
|
f948d41 |
+++ b/Python/pythonrun.c
|
|
|
f948d41 |
@@ -37,14 +37,6 @@
|
|
|
f948d41 |
#include "windows.h"
|
|
|
f948d41 |
#endif
|
|
|
f948d41 |
|
|
|
f948d41 |
-#ifndef Py_REF_DEBUG
|
|
|
f948d41 |
-#define PRINT_TOTAL_REFS()
|
|
|
f948d41 |
-#else /* Py_REF_DEBUG */
|
|
|
f948d41 |
-#define PRINT_TOTAL_REFS() fprintf(stderr, \
|
|
|
f948d41 |
- "[%" PY_FORMAT_SIZE_T "d refs]\n", \
|
|
|
f948d41 |
- _Py_GetRefTotal())
|
|
|
f948d41 |
-#endif
|
|
|
f948d41 |
-
|
|
|
f948d41 |
#ifdef __cplusplus
|
|
|
f948d41 |
extern "C" {
|
|
|
f948d41 |
#endif
|
|
|
f948d41 |
@@ -104,6 +96,21 @@ PyModule_GetWarningsModule(void)
|
|
|
f948d41 |
return PyImport_ImportModule("warnings");
|
|
|
f948d41 |
}
|
|
|
f948d41 |
|
|
|
f948d41 |
+static void
|
|
|
f948d41 |
+_PyDebug_PrintTotalRefs(void)
|
|
|
f948d41 |
+{
|
|
|
f948d41 |
+#ifdef Py_REF_DEBUG
|
|
|
f948d41 |
+ Py_ssize_t total;
|
|
|
f948d41 |
+
|
|
|
f948d41 |
+ if (!Py_GETENV("PYTHONSHOWREFCOUNT")) {
|
|
|
f948d41 |
+ return;
|
|
|
f948d41 |
+ }
|
|
|
f948d41 |
+
|
|
|
f948d41 |
+ total = _Py_GetRefTotal();
|
|
|
f948d41 |
+ fprintf(stderr, "[%" PY_FORMAT_SIZE_T "d refs]\n", total);
|
|
|
f948d41 |
+#endif
|
|
|
f948d41 |
+}
|
|
|
f948d41 |
+
|
|
|
f948d41 |
static int initialized = 0;
|
|
|
f948d41 |
|
|
|
f948d41 |
/* API to access the initialized flag -- useful for esoteric use */
|
|
|
f948d41 |
@@ -486,7 +493,7 @@ Py_Finalize(void)
|
|
|
f948d41 |
}
|
|
|
f948d41 |
#endif
|
|
|
f948d41 |
|
|
|
f948d41 |
- PRINT_TOTAL_REFS();
|
|
|
f948d41 |
+ _PyDebug_PrintTotalRefs();
|
|
|
f948d41 |
|
|
|
f948d41 |
#ifdef Py_TRACE_REFS
|
|
|
f948d41 |
/* Display all objects still alive -- this can invoke arbitrary
|
|
|
f948d41 |
@@ -777,7 +784,7 @@ PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flag
|
|
|
f948d41 |
}
|
|
|
f948d41 |
for (;;) {
|
|
|
f948d41 |
ret = PyRun_InteractiveOneFlags(fp, filename, flags);
|
|
|
f948d41 |
- PRINT_TOTAL_REFS();
|
|
|
f948d41 |
+ _PyDebug_PrintTotalRefs();
|
|
|
f948d41 |
if (ret == E_EOF)
|
|
|
f948d41 |
return 0;
|
|
|
f948d41 |
/*
|