From: Victor Stinner Date: Tue, 17 Oct 2017 08:35:19 +0000 (-0700) Subject: bpo-31733: Add PYTHONSHOWREFCOUNT env var (GH-3932) X-Git-Tag: v2.7.15rc1~167 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=3c082a7fdb472f02bcac7a7f8fe1e3a34a11b70b;p=python 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. --- diff --git a/Doc/using/cmdline.rst b/Doc/using/cmdline.rst index f00f7f6026..65988bc32c 100644 --- a/Doc/using/cmdline.rst +++ b/Doc/using/cmdline.rst @@ -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 index 0000000000..08f02053a5 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2017-10-09-15-46-37.bpo-31733.pIf17N.rst @@ -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. 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; /*