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

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

Closed
Closed
Show file tree
Hide file tree
Changes from all 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
9 changes: 5 additions & 4 deletions Doc/c-api/cell.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,14 @@ Cell objects are not likely to be useful elsewhere.

.. c:function:: PyObject* PyCell_Get(PyObject *cell)

Return the contents of the cell *cell*.
Return a new reference to the contents of the cell *cell*.


.. c:function:: PyObject* PyCell_GET(PyObject *cell)

Return the contents of the cell *cell*, but without checking that *cell* is
non-``NULL`` and a cell object.
Borrow a reference to the contents of the cell *cell*. No reference counts are
adjusted, and no checks are made for safety; *cell* must be non-``NULL`` and must
be a cell object.


.. c:function:: int PyCell_Set(PyObject *cell, PyObject *value)
Expand All @@ -58,6 +59,6 @@ Cell objects are not likely to be useful elsewhere.

.. c:function:: void PyCell_SET(PyObject *cell, PyObject *value)

Sets the value of the cell object *cell* to *value*. No reference counts are
Sets the value of the cell object *cell* to *value*. No reference counts are
adjusted, and no checks are made for safety; *cell* must be non-``NULL`` and must
be a cell object.
9 changes: 9 additions & 0 deletions Doc/c-api/reflection.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,21 @@ Reflection
Return a dictionary of the builtins in the current execution frame,
or the interpreter of the thread state if no frame is currently executing.

.. c:function:: PyObject* PyLocals_Get(void)

Return a dictionary of the local variables in the current execution frame,
or ``NULL`` if no frame is currently executing.

Equivalent to calling the Python level ``locals()`` builtin.

.. TODO: cover the rest of the PEP 558 API here

.. c:function:: PyObject* PyEval_GetLocals(void)

Return a dictionary of the local variables in the current execution frame,
or ``NULL`` if no frame is currently executing.

TODO: Clarify just how this relates to PyLocals_Get().

.. c:function:: PyObject* PyEval_GetGlobals(void)

Expand Down
4 changes: 4 additions & 0 deletions Doc/data/stable_abi.dat
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,10 @@ function,PyList_SetSlice,3.2,
function,PyList_Size,3.2,
function,PyList_Sort,3.2,
var,PyList_Type,3.2,
function,PyLocals_Get,3.11,
function,PyLocals_GetCopy,3.11,
function,PyLocals_Kind,3.11,
function,PyLocals_GetView,3.11,
type,PyLongObject,3.2,
var,PyLongRangeIter_Type,3.2,
function,PyLong_AsDouble,3.2,
Expand Down
30 changes: 30 additions & 0 deletions Include/ceval.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,36 @@ PyAPI_FUNC(PyObject *) PyEval_GetGlobals(void);
PyAPI_FUNC(PyObject *) PyEval_GetLocals(void);
PyAPI_FUNC(PyFrameObject *) PyEval_GetFrame(void);

// TODO: Update PyEval_GetLocals() documentation as described in
// https://discuss.python.org/t/pep-558-defined-semantics-for-locals/2936/11

#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030B0000
/* Access the frame locals mapping in an implementation independent way */

/* PyLocals_Get() is equivalent to the Python locals() builtin.
* It returns a read/write reference or a shallow copy depending on the scope
* of the active frame.
*/
PyAPI_FUNC(PyObject *) PyLocals_Get(void);

/* PyLocals_GetCopy() returns a fresh shallow copy of the active local namespace */
PyAPI_FUNC(PyObject *) PyLocals_GetCopy(void);

/* PyLocals_GetView() returns a read-only proxy for the active local namespace */
PyAPI_FUNC(PyObject *) PyLocals_GetView(void);

/* PyLocals_GetKind()reports the behaviour of PyLocals_Get() in the active scope */
typedef enum {
PyLocals_UNDEFINED = -1, // Indicates error (e.g. no thread state defined)
PyLocals_DIRECT_REFERENCE = 0,
PyLocals_SHALLOW_COPY = 1,
_PyLocals_ENSURE_32BIT_ENUM = 2147483647
} PyLocals_Kind;

PyAPI_FUNC(PyLocals_Kind) PyLocals_GetKind(void);
#endif


PyAPI_FUNC(int) Py_AddPendingCall(int (*func)(void *), void *arg);
PyAPI_FUNC(int) Py_MakePendingCalls(void);

Expand Down
35 changes: 31 additions & 4 deletions Include/cpython/frameobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ struct _frame {
struct _frame *f_back; /* previous frame, or NULL */
PyObject **f_valuestack; /* points after the last local */
PyObject *f_trace; /* Trace function */
PyObject *f_fast_refs; /* Name -> index-or-cell lookup for fast locals */
/* Borrowed reference to a generator, or NULL */
PyObject *f_gen;
int f_stackdepth; /* Depth of value stack */
Expand Down Expand Up @@ -64,13 +65,39 @@ _PyFrame_New_NoTrack(PyThreadState *, PyFrameConstructor *, PyObject *, PyObject

/* The rest of the interface is specific for frame objects */

/* Conversions between "fast locals" and locals in dictionary */

PyAPI_FUNC(void) PyFrame_LocalsToFast(PyFrameObject *, int);

/* Legacy conversions between "fast locals" and locals in dictionary */
PyAPI_FUNC(int) PyFrame_FastToLocalsWithError(PyFrameObject *f);
PyAPI_FUNC(void) PyFrame_FastToLocals(PyFrameObject *);

/* This always raises RuntimeError now (use the PyLocals_* API instead) */
PyAPI_FUNC(void) PyFrame_LocalsToFast(PyFrameObject *, int);

/* Frame object memory management */
PyAPI_FUNC(int) PyFrame_ClearFreeList(void);
PyAPI_FUNC(void) _PyFrame_DebugMallocStats(FILE *out);

PyAPI_FUNC(PyFrameObject *) PyFrame_GetBack(PyFrameObject *frame);

/* Fast locals proxy allows for reliable write-through from trace functions */
// TODO: Perhaps this should be hidden, and API users told to query for
// PyFrame_GetLocalsKind() instead. Having this available
// seems like a nice way to let folks write some useful debug assertions,
// though.
PyAPI_DATA(PyTypeObject) _PyFastLocalsProxy_Type;
#define _PyFastLocalsProxy_CheckExact(op) Py_IS_TYPE(op, &_PyFastLocalsProxy_Type)


// Underlying implementation API supporting the stable PyLocals_*() APIs
// TODO: Add specific test cases for these (as any PyLocals_* tests won't cover
// checking the status of a frame other than the currently active one)
PyAPI_FUNC(PyObject *) PyFrame_GetLocals(PyFrameObject *);
PyAPI_FUNC(PyObject *) PyFrame_GetLocalsCopy(PyFrameObject *);
PyAPI_FUNC(PyObject *) PyFrame_GetLocalsView(PyFrameObject *);
PyAPI_FUNC(PyLocals_Kind) PyFrame_GetLocalsKind(PyFrameObject *);

// Underlying API supporting PyEval_GetLocals()
PyAPI_FUNC(PyObject *) _PyFrame_BorrowLocals(PyFrameObject *);

#ifdef __cplusplus
}
#endif
7 changes: 7 additions & 0 deletions Include/descrobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,14 @@ PyAPI_FUNC(PyObject *) PyDescr_NewWrapper(PyTypeObject *,
PyAPI_FUNC(int) PyDescr_IsData(PyObject *);
#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
10 changes: 5 additions & 5 deletions Include/internal/pycore_code.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,19 +164,19 @@ extern Py_ssize_t _Py_QuickenedCount;
#define CO_FAST_CELL 0x40
#define CO_FAST_FREE 0x80

typedef unsigned char _PyLocals_Kind;
typedef unsigned char _PyLocal_VarKind;

static inline _PyLocals_Kind
_PyLocals_GetKind(PyObject *kinds, int i)
static inline _PyLocal_VarKind
_PyLocal_GetVarKind(PyObject *kinds, int i)
{
assert(PyBytes_Check(kinds));
assert(0 <= i && i < PyBytes_GET_SIZE(kinds));
char *ptr = PyBytes_AS_STRING(kinds);
return (_PyLocals_Kind)(ptr[i]);
return (_PyLocal_VarKind)(ptr[i]);
}

static inline void
_PyLocals_SetKind(PyObject *kinds, int i, _PyLocals_Kind kind)
_PyLocal_SetVarKind(PyObject *kinds, int i, _PyLocal_VarKind kind)
{
assert(PyBytes_Check(kinds));
assert(0 <= i && i < PyBytes_GET_SIZE(kinds));
Expand Down
17 changes: 17 additions & 0 deletions Include/internal/pycore_frameobject.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef Py_INTERNAL_FRAMEOBJECT_H
#define Py_INTERNAL_FRAMEOBJECT_H
#ifdef __cplusplus
extern "C" {
#endif

#ifndef Py_BUILD_CORE
# error "this header requires Py_BUILD_CORE define"
#endif

struct _frame;
typedef struct _frame PyFrameObject;

#ifdef __cplusplus
}
#endif
#endif /* !Py_INTERNAL_FRAMEOBJECT_H */
Loading