bpo-30860: Fix a refleak. (#3567) · pythoncapi/cpython@dae0276 · GitHub
Skip to content

Commit dae0276

Browse files
bpo-30860: Fix a refleak. (python#3567)
Resolves bpo-31420. (This was accidentally reverted when in python#3565.)
1 parent 93c92f7 commit dae0276

7 files changed

Lines changed: 47 additions & 43 deletions

File tree

Include/object.h

Lines changed: 1 addition & 2 deletions

Include/pystate.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@ typedef struct _is {
6161

6262
/* Used in Python/sysmodule.c. */
6363
int check_interval;
64-
PyObject *warnoptions;
65-
PyObject *xoptions;
6664

6765
/* Used in Modules/_threadmodule.c. */
6866
long num_threads;

Objects/object.c

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,23 @@ _Py_GetRefTotal(void)
2929
return total;
3030
}
3131

32-
void
33-
_PyDebug_PrintTotalRefs(void) {
34-
PyObject *xoptions, *value;
32+
PyObject *
33+
_PyDebug_XOptionShowRefCount(void)
34+
{
35+
PyObject *xoptions = PySys_GetXOptions();
36+
if (xoptions == NULL)
37+
return NULL;
38+
3539
_Py_IDENTIFIER(showrefcount);
40+
return _PyDict_GetItemId(xoptions, &PyId_showrefcount);
41+
}
3642

37-
xoptions = PySys_GetXOptions();
38-
if (xoptions == NULL)
39-
return;
40-
value = _PyDict_GetItemId(xoptions, &PyId_showrefcount);
41-
if (value == Py_True)
42-
fprintf(stderr,
43-
"[%" PY_FORMAT_SIZE_T "d refs, "
44-
"%" PY_FORMAT_SIZE_T "d blocks]\n",
45-
_Py_GetRefTotal(), _Py_GetAllocatedBlocks());
43+
void
44+
_PyDebug_PrintTotalRefs(void) {
45+
fprintf(stderr,
46+
"[%" PY_FORMAT_SIZE_T "d refs, "
47+
"%" PY_FORMAT_SIZE_T "d blocks]\n",
48+
_Py_GetRefTotal(), _Py_GetAllocatedBlocks());
4649
}
4750
#endif /* Py_REF_DEBUG */
4851

Python/pylifecycle.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1011,6 +1011,11 @@ Py_FinalizeEx(void)
10111011
while (_PyGC_CollectIfEnabled() > 0)
10121012
/* nothing */;
10131013
#endif
1014+
1015+
#ifdef Py_REF_DEBUG
1016+
PyObject *showrefcount = _PyDebug_XOptionShowRefCount();
1017+
#endif
1018+
10141019
/* Destroy all modules */
10151020
PyImport_Cleanup();
10161021

@@ -1058,7 +1063,10 @@ Py_FinalizeEx(void)
10581063
/* dump hash stats */
10591064
_PyHash_Fini();
10601065

1061-
_PY_DEBUG_PRINT_TOTAL_REFS();
1066+
#ifdef Py_REF_DEBUG
1067+
if (showrefcount == Py_True)
1068+
_PyDebug_PrintTotalRefs();
1069+
#endif
10621070

10631071
#ifdef Py_TRACE_REFS
10641072
/* Display all objects still alive -- this can invoke arbitrary

Python/pystate.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,6 @@ PyInterpreterState_New(void)
9797
interp->builtins_copy = NULL;
9898
interp->tstate_head = NULL;
9999
interp->check_interval = 100;
100-
interp->warnoptions = NULL;
101-
interp->xoptions = NULL;
102100
interp->num_threads = 0;
103101
interp->pythread_stacksize = 0;
104102
interp->codec_search_path = NULL;

Python/pythonrun.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,10 @@ PyRun_InteractiveLoopFlags(FILE *fp, const char *filename_str, PyCompilerFlags *
113113
err = -1;
114114
for (;;) {
115115
ret = PyRun_InteractiveOneObject(fp, filename, flags);
116-
_PY_DEBUG_PRINT_TOTAL_REFS();
116+
#ifdef Py_REF_DEBUG
117+
if (_PyDebug_XOptionShowRefCount() == Py_True)
118+
_PyDebug_PrintTotalRefs();
119+
#endif
117120
if (ret == E_EOF) {
118121
err = 0;
119122
break;

Python/sysmodule.c

Lines changed: 18 additions & 23 deletions

0 commit comments

Comments
 (0)