From 3c082a7fdb472f02bcac7a7f8fe1e3a34a11b70b Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 17 Oct 2017 01:35:19 -0700 Subject: bpo-31733: Add PYTHONSHOWREFCOUNT env var (GH-3932) Add a new PYTHONSHOWREFCOUNT environment variable. In debug mode, Python now only print the total reference count if PYTHONSHOWREFCOUNT is set. --- Python/pythonrun.c | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) (limited to 'Python') diff --git a/Python/pythonrun.c b/Python/pythonrun.c index 2ffecc722d..677f6e4811 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -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 @@ -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; /* -- cgit v1.2.1