[ Web Proxy ]
URL:
Viewing:
https://github-redirect.dependabot.com/python/cpython/blob/main/Python/frame.c
[Back]
[Original]
cpython/Python/frame.c at main python/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 }}
python
cpython
Repository navigation
Code
Issues
5k+
(5k+)
Pull requests
2.7k
(2.7k)
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
cpython
/
Python
/
frame.c
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
158 lines (145 loc) 5.45 KB
main
Breadcrumbs
cpython
/
Python
/
frame.c
Copy path
Top
File metadata and controls
Code
Blame
158 lines (145 loc) 5.45 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
154
155
156
157
158
#define
_PY_INTERPRETER
#include
"Python.h"
#include
"pycore_frame.h"
// _PyFrame_New_NoTrack()
#include
"pycore_interpframe.h"
// _PyFrame_GetCode()
#include
"pycore_genobject.h"
// _PyGen_GetGeneratorFromFrame()
#include
"pycore_stackref.h"
// _Py_VISIT_STACKREF()
int
_PyFrame_Traverse
(
_PyInterpreterFrame
*
frame
,
visitproc
visit
,
void
*
arg
)
{
Py_VISIT
(
frame
->
frame_obj
);
Py_VISIT
(
frame
->
f_locals
);
_Py_VISIT_STACKREF
(
frame
->
f_funcobj
);
_Py_VISIT_STACKREF
(
frame
->
f_executable
);
return
_PyGC_VisitFrameStack
(
frame
,
visit
,
arg
);
}
PyFrameObject
*
_PyFrame_MakeAndSetFrameObject
(
_PyInterpreterFrame
*
frame
)
{
PyObject
*
exc
=
PyErr_GetRaisedException
();
PyFrameObject
*
f
=
_PyFrame_New_NoTrack
(
_PyFrame_GetCode
(
frame
));
if
(
f
==
NULL
) {
Py_XDECREF
(
exc
);
return
NULL
;
}
PyErr_SetRaisedException
(
exc
);
// GH-97002: There was a time when a frame object could be created when we
// are allocating the new frame object f above, so frame->frame_obj would
// be assigned already. That path does not exist anymore. We won't call any
// Python code in this function and garbage collection will not run.
// Notice that _PyFrame_New_NoTrack() can potentially raise a MemoryError,
// but it won't allocate a traceback until the frame unwinds, so we are safe
// here.
assert
(
frame
->
owner
!=
FRAME_OWNED_BY_FRAME_OBJECT
);
f
->
f_frame
=
frame
;
#ifdef
Py_GIL_DISABLED
PyFrameObject
*
expected
=
NULL
;
if
(!
_Py_atomic_compare_exchange_ptr
(
&
frame
->
frame_obj
,
&
expected
,
f
)) {
Py_DECREF
(
f
);
return
expected
;
}
#else
assert
(
frame
->
frame_obj
==
NULL
);
frame
->
frame_obj
=
f
;
#endif
return
f
;
}
static
void
take_ownership
(
PyFrameObject
*
f
,
_PyInterpreterFrame
*
frame
)
{
Py_BEGIN_CRITICAL_SECTION
(
f
);
assert
(
frame
->
owner
<
FRAME_OWNED_BY_INTERPRETER
);
assert
(
frame
->
owner
!=
FRAME_OWNED_BY_FRAME_OBJECT
);
_PyInterpreterFrame
*
new_frame
=
(
_PyInterpreterFrame
*
)
f
->
_f_frame_data
;
_PyFrame_Copy
(
frame
,
new_frame
);
// _PyFrame_Copy takes the reference to the executable,
// so we need to restore it.
new_frame
->
f_executable
=
PyStackRef_DUP
(
new_frame
->
f_executable
);
f
->
f_frame
=
new_frame
;
new_frame
->
owner
=
FRAME_OWNED_BY_FRAME_OBJECT
;
if
(
_PyFrame_IsIncomplete
(
new_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
=
_PyFrame_GetCode
(
new_frame
);
new_frame
->
instr_ptr
=
_PyFrame_GetBytecode
(
new_frame
)
+
code
->
_co_firsttraceable
+
1
;
}
assert
(!
_PyFrame_IsIncomplete
(
new_frame
));
assert
(
f
->
f_back
==
NULL
);
_PyInterpreterFrame
*
prev
=
_PyFrame_GetFirstComplete
(
frame
->
previous
);
if
(
prev
) {
assert
(
prev
->
owner
<
FRAME_OWNED_BY_INTERPRETER
);
PyObject
*
exc
=
PyErr_GetRaisedException
();
/* 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
);
}
PyErr_SetRaisedException
(
exc
);
}
if
(!
_PyObject_GC_IS_TRACKED
((
PyObject
*
)
f
)) {
_PyObject_GC_TRACK
((
PyObject
*
)
f
);
}
Py_END_CRITICAL_SECTION
();
}
void
_PyFrame_ClearLocals
(
_PyInterpreterFrame
*
frame
)
{
assert
(
frame
->
stackpointer
!=
NULL
);
_PyStackRef
*
sp
=
frame
->
stackpointer
;
_PyStackRef
*
locals
=
frame
->
localsplus
;
frame
->
stackpointer
=
locals
;
while
(
sp
>
locals
) {
sp
--
;
PyStackRef_XCLOSE
(
*
sp
);
}
Py_CLEAR
(
frame
->
f_locals
);
}
void
_PyFrame_ClearExceptCode
(
_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
||
FT_ATOMIC_LOAD_INT8_RELAXED
(
_PyGen_GetGeneratorFromFrame
(
frame
)
->
gi_frame_state
)
==
FRAME_CLEARED
);
// GH-99729: Clearing this frame can expose the stack (via finalizers). It's
// crucial that this frame has been unlinked, and is no longer visible:
assert
(
_PyThreadState_GET
()
->
current_frame
!=
frame
);
PyFrameObject
*
f
=
FT_ATOMIC_LOAD_PTR_RELAXED
(
frame
->
frame_obj
);
if
(
f
!=
NULL
) {
FT_ATOMIC_STORE_PTR_RELAXED
(
frame
->
frame_obj
,
NULL
);
if
(!
_PyObject_IsUniquelyReferenced
((
PyObject
*
)
f
)) {
take_ownership
(
f
,
frame
);
Py_DECREF
(
f
);
return
;
}
Py_DECREF
(
f
);
}
_PyFrame_ClearLocals
(
frame
);
PyStackRef_CLEAR
(
frame
->
f_funcobj
);
}
/* Unstable API functions */
PyObject
*
PyUnstable_InterpreterFrame_GetCode
(
struct
_PyInterpreterFrame
*
frame
)
{
return
PyStackRef_AsPyObjectNew
(
frame
->
f_executable
);
}
// NOTE: We allow racy accesses to the instruction pointer from other threads
// for sys._current_frames() and similar APIs.
int
_Py_NO_SANITIZE_THREAD
PyUnstable_InterpreterFrame_GetLasti
(
struct
_PyInterpreterFrame
*
frame
)
{
return
_PyInterpreterFrame_LASTI
(
frame
)
*
sizeof
(
_Py_CODEUNIT
);
}
int
_Py_NO_SANITIZE_THREAD
PyUnstable_InterpreterFrame_GetLine
(
_PyInterpreterFrame
*
frame
)
{
int
addr
=
_PyInterpreterFrame_LASTI
(
frame
)
*
sizeof
(
_Py_CODEUNIT
);
return
PyCode_Addr2Line
(
_PyFrame_GetCode
(
frame
),
addr
);
}
Footer
© 2026 GitHub, Inc.
Footer navigation
Terms
Privacy
Security
Status
Community
Docs
Contact
Manage cookies
Do not share my personal information
You cant perform that action at this time.
Web Proxy Viewer |
New URL
|
Original Page