From 82a34d5ef303da1b77acc96fbcb027cd76047bd1 Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Tue, 17 Aug 2021 13:05:41 +0900 Subject: [PATCH] bpo-44895: Address Victor's code review --- Doc/using/cmdline.rst | 7 +++-- Include/cpython/initconfig.h | 2 +- .../2021-08-14-20-13-21.bpo-44895.Ic9m90.rst | 2 +- Python/initconfig.c | 8 ++--- Python/pylifecycle.c | 29 +++++++++++-------- 5 files changed, 27 insertions(+), 21 deletions(-) diff --git a/Doc/using/cmdline.rst b/Doc/using/cmdline.rst index 3120efbb00b351..b541678ee70eaf 100644 --- a/Doc/using/cmdline.rst +++ b/Doc/using/cmdline.rst @@ -976,10 +976,11 @@ Debug-mode variables Need Python configured with the :option:`--with-trace-refs` build option. -.. envvar:: PYTHONDUMPFILE +.. envvar:: PYTHONDUMPREFSFILE - If set, Python will create a file named `$PYTHONDUMPFILE` as the dump file - which is generated by :envvar:`PYTHONDUMPREFS`. + If set, Python will dump objects and reference counts still alive + as file named `$PYTHONDUMPREFSFILE` still alive after shutting down + the interpreter. Need Python configured with the :option:`--with-trace-refs` build option. diff --git a/Include/cpython/initconfig.h b/Include/cpython/initconfig.h index ae7d5cd558393c..22ad0f14e58004 100644 --- a/Include/cpython/initconfig.h +++ b/Include/cpython/initconfig.h @@ -143,8 +143,8 @@ typedef struct PyConfig { int no_debug_ranges; int show_ref_count; int dump_refs; + wchar_t *dump_refs_file; int malloc_stats; - wchar_t *python_dump_file; wchar_t *filesystem_encoding; wchar_t *filesystem_errors; wchar_t *pycache_prefix; diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-08-14-20-13-21.bpo-44895.Ic9m90.rst b/Misc/NEWS.d/next/Core and Builtins/2021-08-14-20-13-21.bpo-44895.Ic9m90.rst index 195bb330fd0d26..b4a1a69d9852a6 100644 --- a/Misc/NEWS.d/next/Core and Builtins/2021-08-14-20-13-21.bpo-44895.Ic9m90.rst +++ b/Misc/NEWS.d/next/Core and Builtins/2021-08-14-20-13-21.bpo-44895.Ic9m90.rst @@ -1,2 +1,2 @@ -A debug variable :envvar:`PYTHONDUMPFILE` is added for creating a dump file +A debug variable :envvar:`PYTHONDUMPREFSFILE` is added for creating a dump file which is generated by :envvar:`PYTHONDUMPREFS`. Patch by Dong-hee Na. diff --git a/Python/initconfig.c b/Python/initconfig.c index 6cd16045b277be..61cd0e6213ed17 100644 --- a/Python/initconfig.c +++ b/Python/initconfig.c @@ -898,7 +898,7 @@ _PyConfig_Copy(PyConfig *config, const PyConfig *config2) COPY_ATTR(no_debug_ranges); COPY_ATTR(show_ref_count); COPY_ATTR(dump_refs); - COPY_ATTR(python_dump_file); + COPY_ATTR(dump_refs_file); COPY_ATTR(malloc_stats); COPY_WSTR_ATTR(pycache_prefix); @@ -1702,9 +1702,9 @@ config_read_env_vars(PyConfig *config) config->malloc_stats = 1; } - if (config->python_dump_file == NULL) { - status = CONFIG_GET_ENV_DUP(config, &config->python_dump_file, - L"PYTHONDUMPFILE", "PYTHONDUMPFILE"); + if (config->dump_refs_file == NULL) { + status = CONFIG_GET_ENV_DUP(config, &config->dump_refs_file, + L"PYTHONDUMPREFSFILE", "PYTHONDUMPREFSFILE"); if (_PyStatus_EXCEPTION(status)) { return status; } diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index 3e045845eee2a1..36ffcb60e7b8ed 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -1736,8 +1736,8 @@ Py_FinalizeEx(void) int show_ref_count = tstate->interp->config.show_ref_count; #endif #ifdef Py_TRACE_REFS - wchar_t *python_dump_file = tstate->interp->config.python_dump_file; int dump_refs = tstate->interp->config.dump_refs; + wchar_t *dump_refs_file = tstate->interp->config.dump_refs_file; #endif #ifdef WITH_PYMALLOC int malloc_stats = tstate->interp->config.malloc_stats; @@ -1837,12 +1837,13 @@ Py_FinalizeEx(void) * up later. */ - FILE *fp = stderr; - if (python_dump_file != NULL) { - fp = _Py_wfopen(python_dump_file, L"w"); - } - if (dump_refs) { - _Py_PrintReferences(fp); + FILE *fp = dump_refs_file ? _Py_wfopen(dump_refs_file, L"w") : stderr; + if (dump_refs || dump_refs_file != NULL) { + if (fp == NULL) { + fprintf(stderr, "Can not log all live objects.\n"); + } else { + _Py_PrintReferences(fp); + } } #endif /* Py_TRACE_REFS */ @@ -1854,11 +1855,15 @@ Py_FinalizeEx(void) * An address can be used to find the repr of the object, printed * above by _Py_PrintReferences. */ - if (dump_refs) { - _Py_PrintReferenceAddresses(fp); - } - if (fp != NULL && fp != stderr) { - fclose(fp); + if (dump_refs || dump_refs_file) { + if (fp == NULL) { + fprintf(stderr, "Can not log the addresses of all live objects.\n"); + } else { + _Py_PrintReferenceAddresses(fp); + if (fp != stderr) { + fclose(fp); + } + } } #endif /* Py_TRACE_REFS */ #ifdef WITH_PYMALLOC