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 6 commits
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
7 changes: 7 additions & 0 deletions Include/descrobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,14 @@ PyAPI_FUNC(PyObject *) PyDescr_NewWrapper(PyTypeObject *,
#define PyDescr_IsData(d) (Py_TYPE(d)->tp_descr_set != NULL)
#endif

/* PyDictProxy should really have its own header/impl pair, but keeping
* it here for now... */

PyAPI_FUNC(PyObject *) PyDictProxy_New(PyObject *);
#ifdef Py_BUILD_CORE
PyAPI_DATA(PyTypeObject) PyDictProxy_Type;
#endif

PyAPI_FUNC(PyObject *) PyWrapper_New(PyObject *, PyObject *);


Expand Down
11 changes: 11 additions & 0 deletions Include/frameobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ PyAPI_FUNC(void) PyFrame_LocalsToFast(PyFrameObject *, int);

PyAPI_FUNC(int) PyFrame_FastToLocalsWithError(PyFrameObject *f);
PyAPI_FUNC(void) PyFrame_FastToLocals(PyFrameObject *);
#ifdef Py_BUILD_CORE
int _PyFrame_FastToLocalsInternal(PyFrameObject *f, int); /* For sys.settrace */
#endif

PyAPI_FUNC(int) PyFrame_ClearFreeList(void);

Expand All @@ -86,6 +89,14 @@ PyAPI_FUNC(void) _PyFrame_DebugMallocStats(FILE *out);
/* Return the line of code the frame is currently executing. */
PyAPI_FUNC(int) PyFrame_GetLineNumber(PyFrameObject *);


#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03080000
/* Fast locals proxy for reliable write-through from trace functions */
PyTypeObject PyFastLocalsProxy_Type;
#define _PyFastLocalsProxy_CheckExact(self) \
(Py_TYPE(self) == &PyFastLocalsProxy_Type)
#endif

#ifdef __cplusplus
}
#endif
Expand Down
144 changes: 144 additions & 0 deletions Lib/test/test_sys_settrace.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import sys
import difflib
import gc
import textwrap
from functools import wraps
import asyncio

Expand Down Expand Up @@ -1405,6 +1406,149 @@ def gen():
next(gen())
output.append(5)

class FrameLocalsTestCase(unittest.TestCase):
def setUp(self):
self.addCleanup(sys.settrace, sys.gettrace())
sys.settrace(None)

def test_closures_are_not_implicitly_reset_to_previous_state(self):
# See https://bugs.python.org/issue30744 for details
i_from_generator = []
x_from_generator = []
x_from_nested_ref = []
x_from_nested_locals = []
def outer():
x = 0

def update_nested_refs():
x_from_nested_ref.append(x)
x_from_nested_locals.append(locals()["x"])

yield update_nested_refs
for i in range(1, 10):
i_from_generator.append(i)
x += 1
yield x

incrementing_generator = outer()
update_nested_refs = next(incrementing_generator)

def tracefunc(frame, event, arg):
x_from_generator.append(next(incrementing_generator))
return tracefunc

sys.settrace(tracefunc)
try:
update_nested_refs()
update_nested_refs()
finally:
sys.settrace(None)
self.assertEqual(x_from_generator, i_from_generator)
self.assertEqual(x_from_nested_ref, [2, 6])
self.assertEqual(x_from_nested_locals, [3, 7])

def test_locals_writeback_support(self):
# To support interactive debuggers, trace functions are expected to
# be able to reliably modify function locals. However, this should
# NOT enable writebacks via locals() at function scope.
#
# Note: the sample values have numbers in them so mixing up variable
# names in the checks can't accidentally make the test pass -
# you'd have to get both the name *and* expected number wrong
self.maxDiff = None
code = textwrap.dedent("""
locals()['a_global'] = 'created1' # We expect this to be retained
another_global = 'original2' # Trace func will modify this

class C:
locals()['an_attr'] = 'created3' # We expect this to be retained
another_attr = 'original4' # Trace func will modify this
a_class_attribute = C.an_attr
another_class_attribute = C.another_attr
del C

def outer():
a_nonlocal = 'original5' # We expect this to be retained
another_nonlocal = 'original6' # Trace func will modify this
def inner():
nonlocal another_nonlocal
a_local = 'original7' # We expect this to be retained
another_local = 'original8' # Trace func will modify this
ns = locals()
ns['a_local'] = 'modified7' # We expect this to be reverted
ns['a_nonlocal'] = 'modified5' # We expect this to be reverted
ns['a_new_local'] = 'created9' # We expect this to be retained
return a_local, another_local, ns
outer_local = 'original10' # Trace func will modify this
# Trigger any updates from the inner function & trace function
inner_result = inner()
outer_result = a_nonlocal, another_nonlocal, outer_local, locals()
return outer_result, inner_result
outer_result, inner_result = outer()
a_nonlocal, another_nonlocal, outer_local, outer_ns = outer_result
a_nonlocal_via_ns = outer_ns['a_nonlocal']
another_nonlocal_via_ns = outer_ns['another_nonlocal']
outer_local_via_ns = outer_ns['outer_local']
a_local, another_local, inner_ns = inner_result
a_local_via_ns = inner_ns['a_local']
a_nonlocal_via_inner_ns = inner_ns['a_nonlocal']
another_nonlocal_via_inner_ns = inner_ns['another_nonlocal']
another_local_via_ns = inner_ns['another_local']
a_new_local_via_ns = inner_ns['a_new_local']
del outer, outer_result, outer_ns, inner_result, inner_ns
"""
)
def tracefunc(frame, event, arg):
if event == "return":
# We leave any state manipulation to the very end
ns = frame.f_locals
co_name = frame.f_code.co_name
if co_name == "C":
# Modify class attributes
ns["another_attr"] = "modified4"
elif co_name == "inner":
# Modify local and nonlocal variables
ns["another_nonlocal"] = "modified6"
ns["another_local"] = "modified8"
outer_ns = frame.f_back.f_locals
outer_ns["outer_local"] = "modified10"
elif co_name == "<module>":
# Modify globals
ns["another_global"] = "modified2"
return tracefunc
actual_ns = {}
sys.settrace(tracefunc)
try:
exec(code, actual_ns)
finally:
sys.settrace(None)
for k in list(actual_ns.keys()):
if k.startswith("_"):
del actual_ns[k]
# CURRENT FAILURE STATUS:
# - trace functions mutating state as expected
# - proxy is incorrectly being returned by locals(), so that also mutates
expected_ns = {
"a_global": "created1",
"another_global": "modified2",
"a_class_attribute": "created3",
"another_class_attribute": "modified4",
"a_nonlocal": "original5",
"a_nonlocal_via_ns": "original5",
"a_nonlocal_via_inner_ns": "original5",
"another_nonlocal": "modified6",
"another_nonlocal_via_ns": "modified6",
"another_nonlocal_via_inner_ns": "modified6",
"a_local": "original7",
"a_local_via_ns": "original7",
"another_local": "modified8",
"another_local_via_ns": "modified8",
"a_new_local_via_ns": "created9",
"outer_local": "modified10",
"outer_local_via_ns": "modified10",
}
self.assertEqual(actual_ns, expected_ns)


if __name__ == "__main__":
unittest.main()
25 changes: 25 additions & 0 deletions Objects/clinic/frameobject.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Objects/descrobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1004,6 +1004,7 @@ PyDictProxy_New(PyObject *mapping)
}



/* --- Wrapper object for "slot" methods --- */

/* This has no reason to be in this file except that adding new files is a
Expand Down Expand Up @@ -1569,7 +1570,9 @@ PyTypeObject PyDictProxy_Type = {
PyObject_GenericGetAttr, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Py_TPFLAGS_DEFAULT |
Py_TPFLAGS_HAVE_GC |
Py_TPFLAGS_BASETYPE, /* tp_flags */
0, /* tp_doc */
mappingproxy_traverse, /* tp_traverse */
0, /* tp_clear */
Expand Down
Loading