8000 bpo-1635741: Clean sysdict and builtins of interpreter by shihai1991 · Pull Request #21605 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-1635741: Clean sysdict and builtins of interpreter #21605

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

Merged
merged 5 commits into from
Aug 12, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
update code according victor's comment
  • Loading branch information
shihai1991 committed Aug 4, 2020
commit 2910570accce70fcdfd3af398456e2f53b066c2d
11 changes: 9 additions & 2 deletions Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,8 @@ PyInterpreterState_Clear(PyInterpreterState *interp)
}
HEAD_UNLOCK(runtime);

PyDict_Clear(interp->sysdict);
PyDict_Clear(interp->builtins);
PyObject *sysdict = interp->sysdict;
PyObject *builtins = interp->builtins;

Py_CLEAR(interp->audit_hooks);

Expand All @@ -311,6 +311,13 @@ PyInterpreterState_Clear(PyInterpreterState *interp)
if (_PyRuntimeState_GetFinalizing(runtime) == NULL) {
_PyWarnings_Fini(interp);
}

/* We don't clear sysdict and builtins until the end of this function.
Because clearing other attributes can execute arbitrary Python code
which reuqires sysdict and builtins. */
PyDict_Clear(sysdict);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see the point of adding two local variables, why not accessing directly the structure member?

Suggested change
PyDict_Clear(sysdict);
PyDict_Clear(interp->sysdict);

Same remark on folowing line.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interp->sysdict and interp->builtins will be cleared before this line. https://github.com/python/cpython/blob/master/Python/pystate.c#L297-L298

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aha, I didn't notice. In this case, I suggest to move Py_CLEAR(interp->sysdict) and Py_CLEAR(interp->builtins) at the end. Something like:

PyDict_Clear(interp->sysdict);
PyDict_Clear(interp->builtins);
Py_CLEAR(interp->sysdict)
Py_CLEAR(interp->builtins);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated. And I delete the description info.

PyDict_Clear(builtins);

// XXX Once we have one allocator per interpreter (i.e.
// per-interpreter GC) we must ensure that all of the interpreter's
// objects have been cleaned up at the point.
Expand Down
0