Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
cpython/Python/frame.c at patch-11 · sobolevn/cpython · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
sobolevn
/
cpython
Public
forked from
python/cpython
Notifications
You must be signed in to change notification settings
Fork
1
Star
11
Code
Pull requests
14
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Files
Expand file tree
patch-11
Breadcrumbs
cpython
/
Python
/
frame.c
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
153 lines (143 loc) · 5.08 KB
patch-11
Breadcrumbs
cpython
/
Python
/
frame.c
Copy path
Top
File metadata and controls
Code
Blame
153 lines (143 loc) · 5.08 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#define
_PY_INTERPRETER
#include
"Python.h"
#include
"frameobject.h"
#include
"pycore_code.h"
// stats
#include
"pycore_frame.h"
#include
"pycore_object.h"
// _PyObject_GC_UNTRACK()
#include
"opcode.h"
int
_PyFrame_Traverse
(
_PyInterpreterFrame
*
frame
,
visitproc
visit
,
void
*
arg
)
{
Py_VISIT
(
frame
->
frame_obj
);
Py_VISIT
(
frame
->
f_locals
);
Py_VISIT
(
frame
->
f_funcobj
);
Py_VISIT
(
frame
->
f_code
);
/* locals */
PyObject
*
*
locals
=
_PyFrame_GetLocalsArray
(
frame
);
int
i
=
0
;
/* locals and stack */
for
(;
i
<
frame
->
stacktop
;
i
++
) {
Py_VISIT
(
locals
[
i
]);
}
return
0
;
}
PyFrameObject
*
_PyFrame_MakeAndSetFrameObject
(
_PyInterpreterFrame
*
frame
)
{
assert
(
frame
->
frame_obj
==
NULL
);
PyObject
*
error_type
,
*
error_value
,
*
error_traceback
;
PyErr_Fetch
(
&
error_type
,
&
error_value
,
&
error_traceback
);
PyFrameObject
*
f
=
_PyFrame_New_NoTrack
(
frame
->
f_code
);
if
(
f
==
NULL
) {
Py_XDECREF
(
error_type
);
Py_XDECREF
(
error_value
);
Py_XDECREF
(
error_traceback
);
return
NULL
;
}
PyErr_Restore
(
error_type
,
error_value
,
error_traceback
);
if
(
frame
->
frame_obj
) {
// GH-97002: How did we get into this horrible situation? Most likely,
// allocating f triggered a GC collection, which ran some code that
// *also* created the same frame... while we were in the middle of
// creating it! See test_sneaky_frame_object in test_frame.py for a
// concrete example.
//
// Regardless, just throw f away and use that frame instead, since it's
// already been exposed to user code. It's actually a bit tricky to do
// this, since we aren't backed by a real _PyInterpreterFrame anymore.
// Just pretend that we have an owned, cleared frame so frame_dealloc
// doesn't make the situation worse:
f
->
f_frame
=
(
_PyInterpreterFrame
*
)
f
->
_f_frame_data
;
f
->
f_frame
->
owner
=
FRAME_CLEARED
;
f
->
f_frame
->
frame_obj
=
f
;
Py_DECREF
(
f
);
return
frame
->
frame_obj
;
}
assert
(
frame
->
owner
!=
FRAME_OWNED_BY_FRAME_OBJECT
);
assert
(
frame
->
owner
!=
FRAME_CLEARED
);
f
->
f_frame
=
frame
;
frame
->
frame_obj
=
f
;
return
f
;
}
void
_PyFrame_Copy
(
_PyInterpreterFrame
*
src
,
_PyInterpreterFrame
*
dest
)
{
assert
(
src
->
stacktop
>=
src
->
f_code
->
co_nlocalsplus
);
Py_ssize_t
size
=
((
char
*
)
&
src
->
localsplus
[
src
->
stacktop
])
-
(
char
*
)
src
;
memcpy
(
dest
,
src
,
size
);
// Don't leave a dangling pointer to the old frame when creating generators
// and coroutines:
dest
->
previous
=
NULL
;
}
static
void
take_ownership
(
PyFrameObject
*
f
,
_PyInterpreterFrame
*
frame
)
{
assert
(
frame
->
owner
!=
FRAME_OWNED_BY_FRAME_OBJECT
);
assert
(
frame
->
owner
!=
FRAME_CLEARED
);
Py_ssize_t
size
=
((
char
*
)
&
frame
->
localsplus
[
frame
->
stacktop
])
-
(
char
*
)
frame
;
memcpy
((
_PyInterpreterFrame
*
)
f
->
_f_frame_data
,
frame
,
size
);
frame
=
(
_PyInterpreterFrame
*
)
f
->
_f_frame_data
;
f
->
f_frame
=
frame
;
frame
->
owner
=
FRAME_OWNED_BY_FRAME_OBJECT
;
if
(
_PyFrame_IsIncomplete
(
frame
)) {
// This may be a newly-created generator or coroutine frame. Since it's
// dead anyways, just pretend that the first RESUME ran:
PyCodeObject
*
code
=
frame
->
f_code
;
frame
->
prev_instr
=
_PyCode_CODE
(
code
)
+
code
->
_co_firsttraceable
;
}
assert
(!
_PyFrame_IsIncomplete
(
frame
));
assert
(
f
->
f_back
==
NULL
);
_PyInterpreterFrame
*
prev
=
frame
->
previous
;
while
(
prev
&&
_PyFrame_IsIncomplete
(
prev
)) {
prev
=
prev
->
previous
;
}
if
(
prev
) {
/* Link PyFrameObjects.f_back and remove link through _PyInterpreterFrame.previous */
PyFrameObject
*
back
=
_PyFrame_GetFrameObject
(
prev
);
if
(
back
==
NULL
) {
/* Memory error here. */
assert
(
PyErr_ExceptionMatches
(
PyExc_MemoryError
));
/* Nothing we can do about it */
PyErr_Clear
();
}
else
{
f
->
f_back
=
(
PyFrameObject
*
)
Py_NewRef
(
back
);
}
frame
->
previous
=
NULL
;
}
if
(!
_PyObject_GC_IS_TRACKED
((
PyObject
*
)
f
)) {
_PyObject_GC_TRACK
((
PyObject
*
)
f
);
}
}
void
_PyFrame_Clear
(
_PyInterpreterFrame
*
frame
)
{
/* It is the responsibility of the owning generator/coroutine
* to have cleared the enclosing generator, if any. */
assert
(
frame
->
owner
!=
FRAME_OWNED_BY_GENERATOR
||
_PyFrame_GetGenerator
(
frame
)
->
gi_frame_state
==
FRAME_CLEARED
);
if
(
frame
->
frame_obj
) {
PyFrameObject
*
f
=
frame
->
frame_obj
;
frame
->
frame_obj
=
NULL
;
if
(
Py_REFCNT
(
f
)
>
1
) {
take_ownership
(
f
,
frame
);
Py_DECREF
(
f
);
return
;
}
Py_DECREF
(
f
);
}
assert
(
frame
->
stacktop
>=
0
);
for
(
int
i
=
0
;
i
<
frame
->
stacktop
;
i
++
) {
Py_XDECREF
(
frame
->
localsplus
[
i
]);
}
Py_XDECREF
(
frame
->
frame_obj
);
Py_XDECREF
(
frame
->
f_locals
);
Py_DECREF
(
frame
->
f_funcobj
);
Py_DECREF
(
frame
->
f_code
);
}
int
_PyInterpreterFrame_GetLine
(
_PyInterpreterFrame
*
frame
)
{
int
addr
=
_PyInterpreterFrame_LASTI
(
frame
)
*
sizeof
(
_Py_CODEUNIT
);
return
PyCode_Addr2Line
(
frame
->
f_code
,
addr
);
}
You can’t perform that action at this time.