]> granicus.if.org Git - python/commitdiff
bpo-31733: Add PYTHONSHOWREFCOUNT env var (GH-3932)
authorVictor Stinner <victor.stinner@gmail.com>
Tue, 17 Oct 2017 08:35:19 +0000 (01:35 -0700)
committerGitHub <noreply@github.com>
Tue, 17 Oct 2017 08:35:19 +0000 (01:35 -0700)
Add a new PYTHONSHOWREFCOUNT environment variable. In debug mode,
Python now only print the total reference count if PYTHONSHOWREFCOUNT
is set.

Doc/using/cmdline.rst
Misc/NEWS.d/next/Core and Builtins/2017-10-09-15-46-37.bpo-31733.pIf17N.rst [new file with mode: 0644]
Python/pythonrun.c

index f00f7f6026a0febad21ea2ee559b35ce52433eab..65988bc32c162d45bdab01fd0a75b9741c6d22ac 100644 (file)
@@ -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
diff --git a/Misc/NEWS.d/next/Core and Builtins/2017-10-09-15-46-37.bpo-31733.pIf17N.rst b/Misc/NEWS.d/next/Core and Builtins/2017-10-09-15-46-37.bpo-31733.pIf17N.rst
new file mode 100644 (file)
index 0000000..08f0205
--- /dev/null
@@ -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.
index 2ffecc722dc317f91147d6f4f39bed5ff6e74225..677f6e48111be44900da4afd99059cbafc511b1a 100644 (file)
 #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
@@ -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 */
@@ -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
@@ -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;
         /*