diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2017-10-17 01:35:19 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-10-17 01:35:19 -0700 |
commit | 3c082a7fdb472f02bcac7a7f8fe1e3a34a11b70b (patch) | |
tree | d2718389a34a2ce5c5984c28e195f624d617af09 /Python | |
parent | 50cef52372381a9e2f3d760497d8db76254cffef (diff) | |
download | cpython-git-3c082a7fdb472f02bcac7a7f8fe1e3a34a11b70b.tar.gz |
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.
Diffstat (limited to 'Python')
-rw-r--r-- | Python/pythonrun.c | 27 |
1 files changed, 17 insertions, 10 deletions
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; /* |