Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-44895: Introduce PYTHONDUMPREFSFILE variable for refcount dumping #27767

Merged
merged 9 commits into from Aug 17, 2021
9 changes: 9 additions & 0 deletions Doc/using/cmdline.rst
Expand Up @@ -975,3 +975,12 @@ Debug-mode variables
shutting down the interpreter.

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

.. envvar:: PYTHONDUMPFILE
corona10 marked this conversation as resolved.
Show resolved Hide resolved

If set, Python will create a file named `$PYTHONDUMPFILE` as the dump file
which is generated by :envvar:`PYTHONDUMPREFS`.

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

.. versionadded:: 3.11
1 change: 1 addition & 0 deletions Include/cpython/initconfig.h
Expand Up @@ -144,6 +144,7 @@ typedef struct PyConfig {
int show_ref_count;
int dump_refs;
corona10 marked this conversation as resolved.
Show resolved Hide resolved
int malloc_stats;
wchar_t *python_dump_file;
wchar_t *filesystem_encoding;
wchar_t *filesystem_errors;
wchar_t *pycache_prefix;
Expand Down
@@ -0,0 +1,2 @@
A debug variable :envvar:`PYTHONDUMPFILE` is added for creating a dump file
which is generated by :envvar:`PYTHONDUMPREFS`. Patch by Dong-hee Na.
9 changes: 9 additions & 0 deletions Python/initconfig.c
Expand Up @@ -898,6 +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(malloc_stats);

COPY_WSTR_ATTR(pycache_prefix);
Expand Down Expand Up @@ -1701,6 +1702,14 @@ 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 (_PyStatus_EXCEPTION(status)) {
return status;
}
}

if (config->pythonpath_env == NULL) {
status = CONFIG_GET_ENV_DUP(config, &config->pythonpath_env,
L"PYTHONPATH", "PYTHONPATH");
Expand Down
13 changes: 11 additions & 2 deletions Python/pylifecycle.c
Expand Up @@ -1736,6 +1736,7 @@ 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;
#endif
#ifdef WITH_PYMALLOC
Expand Down Expand Up @@ -1835,8 +1836,13 @@ Py_FinalizeEx(void)
* Alas, a lot of stuff may still be alive now that will be cleaned
* up later.
*/

FILE *fp = stderr;
if (python_dump_file != NULL) {
fp = _Py_wfopen(python_dump_file, L"w");
corona10 marked this conversation as resolved.
Show resolved Hide resolved
}
if (dump_refs) {
_Py_PrintReferences(stderr);
_Py_PrintReferences(fp);
}
#endif /* Py_TRACE_REFS */

Expand All @@ -1849,7 +1855,10 @@ Py_FinalizeEx(void)
* above by _Py_PrintReferences.
*/
if (dump_refs) {
_Py_PrintReferenceAddresses(stderr);
_Py_PrintReferenceAddresses(fp);
}
if (fp != NULL && fp != stderr) {
fclose(fp);
}
#endif /* Py_TRACE_REFS */
#ifdef WITH_PYMALLOC
Expand Down