8000 [PEP 558 - WIP] bpo-30744: Trace hooks no longer reset closure state by ncoghlan · Pull Request #3640 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

[PEP 558 - WIP] bpo-30744: Trace hooks no longer reset closure state #3640

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

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
80 commits
Select commit Hold shift + click to select a range
7626a0e
bpo-30744: Trace hooks no longer reset closure state
ncoghlan Sep 18, 2017
3cbb73c
Disable the current broken writeback logic
ncoghlan Nov 5, 2017
01f3f34
Failing test case for writeback functionality
ncoghlan Nov 5, 2017
4f6dd93
Initial skeleton for a write-through proxy
ncoghlan Nov 5, 2017
cd45da7
Merge remote-tracking branch 'origin/master' into bpo-30744-make-loca…
ncoghlan Apr 21, 2019
acbf587
Finish adding the write-through proxy
ncoghlan Apr 21, 2019
d1a9014
Merge remote-tracking branch 'origin/master' into bpo-30744-make-loca…
ncoghlan May 21, 2019
5ea8bcf
Add test case for the PEP 558 locals() behaviour
ncoghlan May 22, 2019
fe92891
Actually implement most of the PEP and fix the tests
ncoghlan May 22, 2019
ac9e0bf
Fix segfault on cleared frames
ncoghlan May 27, 2019
6774e71
Use correct printf formatting code
ncoghlan May 27, 2019
74c51e4
Initial skeleton for other mutable mapping methods
ncoghlan May 28, 2019
0e5fbf3
Break ref cycle when frame finishes executing
ncoghlan May 30, 2019
9e3ce53
Remove implicit frame locals update
ncoghlan May 30, 2019
9479557
Merge remote-tracking branch 'origin/master' into bpo-30744-make-loca…
ncoghlan May 30, 2019
8e886ef
Avoid double DECREF on error
ncoghlan May 30, 2019
5b63e7c
Attempt to make dealloc more robust under gc
ncoghlan May 30, 2019
f0ecee3
Merge remote-tracking branch 'origin/master' into bpo-30744-make-loca…
ncoghlan Dec 29, 2019
1752b54
Fix post-merge compilation errors
ncoghlan Dec 29, 2019
348a56d
Implement flp.pop()
ncoghlan Dec 29, 2019
93f512c
Merge remote-tracking branch 'origin/master' into bpo-30744-make-loca…
ncoghlan Dec 29, 2019
39ec4d8
Refactor ref map creation
ncoghlan Dec 29, 2019
7078632
Correctly manage fast local refcounts
ncoghlan Dec 29, 2019
ed5f86e
Allow closure updates after frame termination
ncoghlan Dec 29, 2019
a216747
Refactor frame post-eval cleanup
ncoghlan Dec 29, 2019
a0dc787
Use full word in API name
ncoghlan Dec 29, 2019
7b02bed
Update to match latest PEP draft
ncoghlan Dec 29, 2019
e9876b5
Update test_scope for snapshot semantics
ncoghlan Dec 29, 2019
0033c60
Update test_sys_settrace for snapshot semantics
ncoghlan Dec 29, 2019
a5a8b19
Fix pop/delete locals proxy bug
ncoghlan Dec 29, 2019
6c98f48
Update argument clinic output
ncoghlan Dec 30, 2019
617f6ab
Merge remote-tracking branch 'origin/master' into bpo-30744-make-loca…
ncoghlan Dec 30, 2019
729f121
Merge remote-tracking branch 'origin/master' into bpo-30744-make-loca…
ncoghlan Jan 18, 2020
619fb75
Merge remote-tracking branch 'origin/master' into bpo-30744-make-loca…
ncoghlan Feb 2, 2020
1fe964e
Migrate to revised public API design
ncoghlan Feb 2, 2020
b047ae4
Rename PyFrame_LocalsIsSnapshot to PyFrame_GetLocalsReturnsSnapshot
ncoghlan Feb 2, 2020
d1a8420
Mark fast locals proxy as an internal type
ncoghlan Feb 2, 2020
82108ff
Merge remote-tracking branch 'origin/master' into bpo-30744-make-loca…
ncoghlan Feb 15, 2020
eccb1ea
Update draft C API to match latest PEP text
ncoghlan Feb 16, 2020
c1933e7
Migrate exec() and eval() to PyLocals_Get()
ncoghlan Feb 16, 2020
161ad47
Merge remote-tracking branch 'origin/master' into bpo-30744-make-loca…
ncoghlan Feb 22, 2020
68f10ce
Avoid circular reference between locals proxy and frame
ncoghlan Feb 22, 2020
29ce344
Add back implicit view refresh in Python trace hook
ncoghlan Feb 22, 2020
ed6e53b
Attempt to tidy up Mac OS X compile warnings/errors
ncoghlan Feb 22, 2020
96c77cb
Merge remote-tracking branch 'origin/main' into bpo-30744-make-locals…
ncoghlan Jul 3, 2021
a348d08
Fix Argument Clinic checksum
ncoghlan Jul 3, 2021
69c8f19
Fix stable ABI minimum version
ncoghlan Jul 3, 2021
caeaf66
Bring implementation into line with latest PEP version
ncoghlan Jul 10, 2021
7ec5d26
Register new stable ABI additions
ncoghlan Jul 10, 2021
7ddc3eb
Add FLP str(), fix various value lookup issues
ncoghlan Jul 10, 2021
7400a46
Uninitialised fields will get you every time
ncoghlan Jul 10, 2021
5eae0d5
Fix refcounting, bdb segfault, pdb functionality
ncoghlan Jul 10, 2021
40db4e7
Delegate more operations to the full dynamic snapshot
ncoghlan Jul 10, 2021
74b97a3
Add TODO item for false positives in containment checks
ncoghlan Jul 10, 2021
9f16513
Only ensure frame snapshot is up to date in O(n) proxy operations
ncoghlan Jul 10, 2021
c477e24
Keep locals snapshot up to date when reading/writing individual keys
ncoghlan Jul 11, 2021
dd94608
Avoid false positives in FLP contains method
ncoghlan Jul 11, 2021
b7eb662
Merge remote-tracking branch 'origin/main' into bpo-30744-make-locals…
ncoghlan Jul 17, 2021
1484c10
Finish public C API, start dict API tests
ncoghlan Jul 17, 2021
3891c13
Merge remote-tracking branch 'origin/main' into bpo-30744-make-locals…
ncoghlan Jul 17, 2021
760ffa9
Remove debugging print statement
ncoghlan Jul 17, 2021
ae6b013
Regenerated stable ABI files
ncoghlan Jul 18, 2021
b03309b
Rename _PyLocals_Kind APIs to avoid potential confusion
ncoghlan Jul 21, 2021
cde5035
Merge remote-tracking branch 'origin/main' into bpo-30744-make-locals…
ncoghlan Jul 21, 2021
66d058c
PyLocals_GetReturnsCopy -> PyLocals_GetKind()
ncoghlan Jul 21, 2021
67c3958
Share fast_refs mapping between proxy objects
ncoghlan Aug 21, 2021
034345f
Remove debugging print
ncoghlan Aug 21, 2021
3c49ff8
Defer value cache refresh until needed, start fleshing out dict API t…
ncoghlan Aug 21, 2021
ea5f943
Add dict union operations to proxy
ncoghlan Aug 21, 2021
fcf99ca
Implement and test locals proxy clear() method
ncoghlan Aug 21, 2021
16e0581
Remove pointless print() call
ncoghlan Aug 21, 2021
c356949
Implement proxy pop() tests
ncoghlan Aug 21, 2021
8a4e788
Implement and test proxy popitem()
ncoghlan Aug 21, 2021
706eec4
Test popitem with cells and extra variables
ncoghlan Aug 21, 2021
35a017c
Implement and test setdefault()
ncoghlan Aug 21, 2021
06c406c
Implement and test proxy __sizeof__()
ncoghlan Aug 21, 2021
31493b9
Add C API test for the LocalsToFast exception
ncoghlan Aug 21, 2021
b587a41
Force enum size
ncoghlan Aug 23, 2021
e1b505d
Clarify code comment
ncoghlan Aug 23, 2021
2b27389
Keep track of defined names even on cleared frames
ncoghlan Aug 26, 2021
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
Avoid circular reference between locals proxy and frame
Mark Shannon pointed out that if every access to the Python
level frame f_locals attributes creates a new write-through
proxy instance, there's no need to actually store the proxy
itself on the frame.

Instead, we can continue to store only the shared mapping
returned by PyEval_GetLocals(), and avoid creating a
circular reference that we then need to break when frame
evaluation ends.
  • Loading branch information
ncoghlan committed Feb 22, 2020
commit 68f10ced11a24166269cef8416bb98715d9bd200
5 changes: 1 addition & 4 deletions Include/internal/pycore_ceval.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ struct _ceval_runtime_state;
struct _frame;

#include "pycore_pystate.h" /* PyInterpreterState.eval_frame */
#include "pycore_frameobject.h" /* _PyFrame_PostEvalCleanup */

PyAPI_FUNC(void) _Py_FinishPendingCalls(PyThreadState *tstate);
PyAPI_FUNC(void) _PyEval_Initialize(struct _ceval_runtime_state *);
Expand All @@ -41,9 +40,7 @@ void _PyEval_Fini(void);
static inline PyObject*
_PyEval_EvalFrame(PyThreadState *tstate, struct _frame *f, int throwflag)
{
PyObject * result = tstate->interp->eval_frame(f, throwflag);
_PyFrame_PostEvalCleanup(f); // Notify frame that execution has completed
return result;
return tstate->interp->eval_frame(f, throwflag);
}

extern PyObject *_PyEval_EvalCode(
Expand Down
2 changes: 0 additions & 2 deletions Include/internal/pycore_frameobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ extern "C" {
struct _frame;
typedef struct _frame PyFrameObject;

void _PyFrame_PostEvalCleanup(PyFrameObject *f); /* For _PyEval_EvalFrame() */

#ifdef __cplusplus
}
#endif
Expand Down
105 changes: 39 additions & 66 deletions Objects/frameobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
#define OFF(x) offsetof(PyFrameObject, x)

static PyObject *_PyFastLocalsProxy_New(PyObject *frame);
static PyObject *_PyFastLocalsProxy_BorrowLocals(PyObject *flp);
static void _PyFastLocalsProxy_BreakReferenceCycle(PyObject *flp);

static PyMemberDef frame_memberlist[] = {
{"f_back", T_OBJECT, OFF(f_back), READONLY},
Expand All @@ -35,40 +33,23 @@ _frame_get_updated_locals(PyFrameObject *f)
return f->f_locals;
}

void _PyFrame_PostEvalCleanup(PyFrameObject *f)
{
// Don't clean up still running coroutines and generators
if (f->f_executing) {
return;
}

// Ensure that any reference cycle between the frame and f_locals gets
// broken when the frame finishes execution.
if (f->f_locals && _PyFastLocalsProxy_CheckExact(f->f_locals)) {
_PyFastLocalsProxy_BreakReferenceCycle(f->f_locals);
}
}

PyObject *
_PyFrame_BorrowLocals(PyFrameObject *f)
{
// This is called by PyEval_GetLocals(), which has historically returned
// a borrowed reference, so this does the same
PyObject *updated_locals = _frame_get_updated_locals(f);
if (_PyFastLocalsProxy_CheckExact(updated_locals)) {
updated_locals = _PyFastLocalsProxy_BorrowLocals(updated_locals);
}
return updated_locals;
return _frame_get_updated_locals(f);
}

PyObject *
PyFrame_GetLocals(PyFrameObject *f)
{
// This API implements the Python level locals() builtin
PyObject *updated_locals = _frame_get_updated_locals(f);
if (_PyFastLocalsProxy_CheckExact(updated_locals)) {
// Take a snapshot of optimised scopes to avoid quirky side effects
PyObject *d = _PyFastLocalsProxy_BorrowLocals(updated_locals);
updated_locals = PyDict_Copy(d);
assert(f->f_code);
if (f->f_code->co_flags & CO_OPTIMIZED) {
// PEP 558: Make a copy of optimised scopes to avoid quirky side effects
updated_locals = PyDict_Copy(updated_locals);
} else {
// Share a direct locals reference for class and module scopes
Py_INCREF(updated_locals);
Expand All @@ -80,9 +61,21 @@ PyFrame_GetLocals(PyFrameObject *f)
static PyObject *
frame_getlocals(PyFrameObject *f, void *__unused)
{
PyObject *updated_locals = _frame_get_updated_locals(f);
Py_INCREF(updated_locals);
return updated_locals;
// This API implements the Python level frame.f_locals descriptor
PyObject *f_locals_attr = NULL;
assert(f->f_code);
if (f->f_code->co_flags & CO_OPTIMIZED) {
/* PEP 558: If this is an optimized frame, ensure f_locals at the Python
* layer is a new fastlocalsproxy instance, while f_locals at the C
* layer still refers to the underlying shared namespace mapping.
*/
f_locals_attr = _PyFastLocalsProxy_New((PyObject *) f);
} else {
// Share a direct locals reference for class and module scopes
f_locals_attr = _frame_get_updated_locals(f);
Py_INCREF(f_locals_attr);
}
return f_locals_attr;
}

int
Expand Down Expand Up @@ -465,6 +458,7 @@ frame_setlineno(PyFrameObject *f, PyObject* p_new_lineno, void *Py_UNUSED(ignore


codetracker tracker;
assert(f->f_code);
init_codetracker(&tracker, f->f_code);
move_to_addr(&tracker, f->f_lasti);
int current_line = tracker.line;
Expand Down Expand Up @@ -702,6 +696,7 @@ frame_dealloc(PyFrameObject *f)
Py_CLEAR(f->f_trace);

co = f->f_code;
assert(co);
if (co->co_zombieframe == NULL)
co->co_zombieframe = f;
else if (numfree < PyFrame_MAXFREELIST) {
Expand Down Expand Up @@ -730,6 +725,7 @@ frame_traverse(PyFrameObject *f, visitproc visit, void *arg)
Py_VISIT(f->f_trace);

/* locals */
assert(f->f_code);
slots = f->f_code->co_nlocals + PyTuple_GET_SIZE(f->f_code->co_cellvars) + PyTuple_GET_SIZE(f->f_code->co_freevars);
fastlocals = f->f_localsplus;
for (i = slots; --i >= 0; ++fastlocals)
Expand Down Expand Up @@ -800,6 +796,7 @@ frame_sizeof(PyFrameObject *f, PyObject *Py_UNUSED(ignored))
{
Py_ssize_t res, extras, ncells, nfrees;

assert(f->f_code);
ncells = PyTuple_GET_SIZE(f->f_code->co_cellvars);
nfrees = PyTuple_GET_SIZE(f->f_code->co_freevars);
extras = f->f_code->co_stacksize + f->f_code->co_nlocals +
Expand Down Expand Up @@ -1091,25 +1088,13 @@ PyFrame_FastToLocalsWithError(PyFrameObject *f)
return -1;
}
co = f->f_code;
assert(co);
locals = f->f_locals;
if (locals == NULL) {
if (co->co_flags & CO_OPTIMIZED) {
/* PEP 558: If this is an optimized frame, ensure f_locals is a
* fastlocalsproxy instance, while locals refers to the underlying mapping.
*/
PyObject *flp = _PyFastLocalsProxy_New((PyObject *) f);
if (flp == NULL) {
return -1;
}
f->f_locals = flp;
locals = _PyFastLocalsProxy_BorrowLocals(flp);
} else {
locals = f->f_locals = PyDict_New();
if (locals == NULL)
return -1;
locals = f->f_locals = PyDict_New();
if (locals == NULL) {
return -1;
}
} else if (_PyFastLocalsProxy_CheckExact(locals)) {
locals = _PyFastLocalsProxy_BorrowLocals(locals);
}

map = co->co_varnames;
Expand Down Expand Up @@ -1173,8 +1158,8 @@ PyFrame_LocalsToFast(PyFrameObject *f, int clear)
PyErr_SetString(
PyExc_RuntimeError,
"PyFrame_LocalsToFast is no longer supported. "
"Use PyFrame_GetLocals(), PyFrame_GetLocalsCopy(), "
"or PyFrame_GetLocalsView() instead."
"Use PyObject_GetAttrString(frame, \"f_locals\") "
"to obtain a write-through mapping proxy instead."
);
}

Expand Down Expand Up @@ -1247,6 +1232,7 @@ _PyFrame_BuildFastRefs(PyFrameObject *f)
* to the corresponding cell objects
*/
co = f->f_code;
assert(co);
if (!(co->co_flags & CO_OPTIMIZED)) {
PyErr_SetString(PyExc_SystemError,
"attempted to build fast refs lookup table for non-optimized scope");
Expand Down Expand Up @@ -1348,6 +1334,8 @@ typedef struct {
PyObject *fast_refs; /* Cell refs and local variable indices */
} fastlocalsproxyobject;

// TODO: Implement correct Python sizeof() support for fastlocalsproxyobject

/* Provide __setitem__() and __delitem__() implementations that not only
* write to the namespace returned by locals(), but also write to the frame
* storage directly (either the closure cells or the fast locals array)
Expand Down Expand Up @@ -1631,20 +1619,22 @@ _PyFastLocalsProxy_New(PyObject *frame)
fastlocalsproxyobject *flp;
PyObject *mapping, *fast_refs;

if (fastlocalsproxy_check_frame(frame) == -1)
if (fastlocalsproxy_check_frame(frame) == -1) {
return NULL;
}

flp = PyObject_GC_New(fastlocalsproxyobject, &_PyFastLocalsProxy_Type);
if (flp == NULL)
return NULL;
mapping = PyDict_New();
mapping = _frame_get_updated_locals((PyFrameObject *) frame);
if (mapping == NULL) {
Py_DECREF(flp);
return NULL;
}
flp->mapping = mapping;
Py_INCREF(frame);
Py_INCREF(flp->mapping);
flp->frame = (PyFrameObject *) frame;
Py_INCREF(flp->frame);
fast_refs = _PyFrame_BuildFastRefs(flp->frame);
if (fast_refs == NULL) {
Py_DECREF(flp); // Also handles DECREF for mapping and frame
Expand All @@ -1655,23 +1645,6 @@ _PyFastLocalsProxy_New(PyObject *frame)
return (PyObject *)flp;
}

static PyObject *
_PyFastLocalsProxy_BorrowLocals(PyObject *self)
{
assert(_PyFastLocalsProxy_CheckExact(self));
return ((fastlocalsproxyobject *) self)->mapping;
}

static void
_PyFastLocalsProxy_BreakReferenceCycle(PyObject *self)
{
assert(_PyFastLocalsProxy_CheckExact(self));
fastlocalsproxyobject *flp = (fastlocalsproxyobject *) self;
Py_CLEAR(flp->frame);
// We keep flp->fast_refs alive to allow updating of closure variables
}


/*[clinic input]
@classmethod
fastlocalsproxy.__new__ as fastlocalsproxy_new
Expand Down
0