Skip to content

Commit

Permalink
bpo-44895: Address Victor's code review
Browse files Browse the repository at this point in the history
  • Loading branch information
corona10 committed Aug 17, 2021
1 parent 1895e69 commit 82a34d5
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 21 deletions.
7 changes: 4 additions & 3 deletions Doc/using/cmdline.rst
Expand Up @@ -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.

Expand Down
2 changes: 1 addition & 1 deletion Include/cpython/initconfig.h
Expand Up @@ -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;
Expand Down
@@ -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.
8 changes: 4 additions & 4 deletions Python/initconfig.c
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down
29 changes: 17 additions & 12 deletions Python/pylifecycle.c
Expand Up @@ -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;
Expand Down Expand Up @@ -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 */

Expand All @@ -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
Expand Down

0 comments on commit 82a34d5

Please sign in to comment.