Skip to content

Commit 82a34d5

Browse files
committed
bpo-44895: Address Victor's code review
1 parent 1895e69 commit 82a34d5

File tree

5 files changed

+27
-21
lines changed

5 files changed

+27
-21
lines changed

Doc/using/cmdline.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -976,10 +976,11 @@ Debug-mode variables
976976

977977
Need Python configured with the :option:`--with-trace-refs` build option.
978978

979-
.. envvar:: PYTHONDUMPFILE
979+
.. envvar:: PYTHONDUMPREFSFILE
980980

981-
If set, Python will create a file named `$PYTHONDUMPFILE` as the dump file
982-
which is generated by :envvar:`PYTHONDUMPREFS`.
981+
If set, Python will dump objects and reference counts still alive
982+
as file named `$PYTHONDUMPREFSFILE` still alive after shutting down
983+
the interpreter.
983984

984985
Need Python configured with the :option:`--with-trace-refs` build option.
985986

Include/cpython/initconfig.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,8 @@ typedef struct PyConfig {
143143
int no_debug_ranges;
144144
int show_ref_count;
145145
int dump_refs;
146+
wchar_t *dump_refs_file;
146147
int malloc_stats;
147-
wchar_t *python_dump_file;
148148
wchar_t *filesystem_encoding;
149149
wchar_t *filesystem_errors;
150150
wchar_t *pycache_prefix;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
A debug variable :envvar:`PYTHONDUMPFILE` is added for creating a dump file
1+
A debug variable :envvar:`PYTHONDUMPREFSFILE` is added for creating a dump file
22
which is generated by :envvar:`PYTHONDUMPREFS`. Patch by Dong-hee Na.

Python/initconfig.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -898,7 +898,7 @@ _PyConfig_Copy(PyConfig *config, const PyConfig *config2)
898898
COPY_ATTR(no_debug_ranges);
899899
COPY_ATTR(show_ref_count);
900900
COPY_ATTR(dump_refs);
901-
COPY_ATTR(python_dump_file);
901+
COPY_ATTR(dump_refs_file);
902902
COPY_ATTR(malloc_stats);
903903

904904
COPY_WSTR_ATTR(pycache_prefix);
@@ -1702,9 +1702,9 @@ config_read_env_vars(PyConfig *config)
17021702
config->malloc_stats = 1;
17031703
}
17041704

1705-
if (config->python_dump_file == NULL) {
1706-
status = CONFIG_GET_ENV_DUP(config, &config->python_dump_file,
1707-
L"PYTHONDUMPFILE", "PYTHONDUMPFILE");
1705+
if (config->dump_refs_file == NULL) {
1706+
status = CONFIG_GET_ENV_DUP(config, &config->dump_refs_file,
1707+
L"PYTHONDUMPREFSFILE", "PYTHONDUMPREFSFILE");
17081708
if (_PyStatus_EXCEPTION(status)) {
17091709
return status;
17101710
}

Python/pylifecycle.c

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1736,8 +1736,8 @@ Py_FinalizeEx(void)
17361736
int show_ref_count = tstate->interp->config.show_ref_count;
17371737
#endif
17381738
#ifdef Py_TRACE_REFS
1739-
wchar_t *python_dump_file = tstate->interp->config.python_dump_file;
17401739
int dump_refs = tstate->interp->config.dump_refs;
1740+
wchar_t *dump_refs_file = tstate->interp->config.dump_refs_file;
17411741
#endif
17421742
#ifdef WITH_PYMALLOC
17431743
int malloc_stats = tstate->interp->config.malloc_stats;
@@ -1837,12 +1837,13 @@ Py_FinalizeEx(void)
18371837
* up later.
18381838
*/
18391839

1840-
FILE *fp = stderr;
1841-
if (python_dump_file != NULL) {
1842-
fp = _Py_wfopen(python_dump_file, L"w");
1843-
}
1844-
if (dump_refs) {
1845-
_Py_PrintReferences(fp);
1840+
FILE *fp = dump_refs_file ? _Py_wfopen(dump_refs_file, L"w") : stderr;
1841+
if (dump_refs || dump_refs_file != NULL) {
1842+
if (fp == NULL) {
1843+
fprintf(stderr, "Can not log all live objects.\n");
1844+
} else {
1845+
_Py_PrintReferences(fp);
1846+
}
18461847
}
18471848
#endif /* Py_TRACE_REFS */
18481849

@@ -1854,11 +1855,15 @@ Py_FinalizeEx(void)
18541855
* An address can be used to find the repr of the object, printed
18551856
* above by _Py_PrintReferences.
18561857
*/
1857-
if (dump_refs) {
1858-
_Py_PrintReferenceAddresses(fp);
1859-
}
1860-
if (fp != NULL && fp != stderr) {
1861-
fclose(fp);
1858+
if (dump_refs || dump_refs_file) {
1859+
if (fp == NULL) {
1860+
fprintf(stderr, "Can not log the addresses of all live objects.\n");
1861+
} else {
1862+
_Py_PrintReferenceAddresses(fp);
1863+
if (fp != stderr) {
1864+
fclose(fp);
1865+
}
1866+
}
18621867
}
18631868
#endif /* Py_TRACE_REFS */
18641869
#ifdef WITH_PYMALLOC

0 commit comments

Comments
 (0)