[ Web Proxy ]
URL:
Viewing:
https://github-redirect.dependabot.com/python/cpython/blob/main/Python/intrinsics.c
[Back]
[Original]
cpython/Python/intrinsics.c at main python/cpython GitHub
Skip to content
Navigation Menu
Sign in
Products
Platform
[]
[]
GitHub
Repository hosting, code review, and collaboration
[]
[]
Copilot
AI coding agent for tasks, projects and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Events
Changelog
[]
[]
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
[]
[]
GitHub Universe
The event for developers, by developers talks, workshops, and whats next for GitHub.
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.3k
(2.3k)
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
cpython
/
Python
/
intrinsics.c
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
339 lines (308 loc) 11 KB
main
Breadcrumbs
cpython
/
Python
/
intrinsics.c
Copy path
Top
File metadata and controls
Code
Blame
339 lines (308 loc) 11 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#define
_PY_INTERPRETER
#include
"Python.h"
#include
"pycore_compile.h"
// _PyCompile_GetUnaryIntrinsicName
#include
"pycore_function.h"
// _Py_set_function_type_params()
#include
"pycore_genobject.h"
// _PyAsyncGenValueWrapperNew
#include
"pycore_interpframe.h"
// _PyFrame_GetLocals()
#include
"pycore_intrinsics.h"
// INTRINSIC_PRINT
#include
"pycore_list.h"
// _PyList_AsTupleAndClear()
#include
"pycore_object.h"
// _PyObject_IsUniquelyReferenced()
#include
"pycore_setobject.h"
// _PySet_Freeze()
#include
"pycore_pyerrors.h"
// _PyErr_SetString()
#include
"pycore_runtime.h"
// _Py_ID()
#include
"pycore_typevarobject.h"
// _Py_make_typevar()
#include
"pycore_unicodeobject.h"
// _PyUnicode_FromASCII()
/******** Unary functions ********/
static
PyObject
*
no_intrinsic1
(
PyThreadState
*
tstate
,
PyObject
*
unused
)
{
_PyErr_SetString
(
tstate
,
PyExc_SystemError
,
"invalid intrinsic function"
);
return
NULL
;
}
static
PyObject
*
print_expr
(
PyThreadState
*
Py_UNUSED
(
ignored
),
PyObject
*
value
)
{
PyObject
*
hook
=
PySys_GetAttr
(
&
_Py_ID
(
displayhook
));
if
(
hook
==
NULL
) {
return
NULL
;
}
PyObject
*
res
=
PyObject_CallOneArg
(
hook
,
value
);
Py_DECREF
(
hook
);
return
res
;
}
static
int
import_all_from
(
PyThreadState
*
tstate
,
PyObject
*
locals
,
PyObject
*
v
)
{
PyObject
*
all
,
*
dict
,
*
name
,
*
value
;
int
skip_leading_underscores
=
0
;
int
pos
,
err
;
if
(
PyObject_GetOptionalAttr
(
v
,
&
_Py_ID
(
__all__
),
&
all
)
<
0
) {
return
-1
;
/* Unexpected error */
}
if
(
all
==
NULL
) {
if
(
PyObject_GetOptionalAttr
(
v
,
&
_Py_ID
(
__dict__
),
&
dict
)
<
0
) {
return
-1
;
}
if
(
dict
==
NULL
) {
_PyErr_SetString
(
tstate
,
PyExc_ImportError
,
"from-import-* object has no __dict__ and no __all__"
);
return
-1
;
}
all
=
PyMapping_Keys
(
dict
);
Py_DECREF
(
dict
);
if
(
all
==
NULL
)
return
-1
;
skip_leading_underscores
=
1
;
}
for
(
pos
=
0
,
err
=
0
; ;
pos
++
) {
name
=
PySequence_GetItem
(
all
,
pos
);
if
(
name
==
NULL
) {
if
(!
_PyErr_ExceptionMatches
(
tstate
,
PyExc_IndexError
)) {
err
=
-1
;
}
else
{
_PyErr_Clear
(
tstate
);
}
break
;
}
if
(!
PyUnicode_Check
(
name
)) {
PyObject
*
modname
=
PyObject_GetAttr
(
v
,
&
_Py_ID
(
__name__
));
if
(
modname
==
NULL
) {
Py_DECREF
(
name
);
err
=
-1
;
break
;
}
if
(!
PyUnicode_Check
(
modname
)) {
_PyErr_Format
(
tstate
,
PyExc_TypeError
,
"module __name__ must be a string, not %.100s"
,
Py_TYPE
(
modname
)
->
tp_name
);
}
else
{
_PyErr_Format
(
tstate
,
PyExc_TypeError
,
"%s in %U.%s must be str, not %.100s"
,
skip_leading_underscores
?
"Key"
:
"Item"
,
modname
,
skip_leading_underscores
?
"__dict__"
:
"__all__"
,
Py_TYPE
(
name
)
->
tp_name
);
}
Py_DECREF
(
modname
);
Py_DECREF
(
name
);
err
=
-1
;
break
;
}
if
(
skip_leading_underscores
) {
if
(
PyUnicode_READ_CHAR
(
name
,
0
)
==
'_'
) {
Py_DECREF
(
name
);
continue
;
}
}
value
=
PyObject_GetAttr
(
v
,
name
);
if
(
value
==
NULL
)
err
=
-1
;
else
if
(
PyDict_CheckExact
(
locals
))
err
=
PyDict_SetItem
(
locals
,
name
,
value
);
else
err
=
PyObject_SetItem
(
locals
,
name
,
value
);
Py_DECREF
(
name
);
Py_XDECREF
(
value
);
if
(
err
<
0
)
break
;
}
Py_DECREF
(
all
);
return
err
;
}
static
PyObject
*
import_star
(
PyThreadState
*
tstate
,
PyObject
*
from
)
{
_PyInterpreterFrame
*
frame
=
tstate
->
current_frame
;
PyObject
*
locals
=
_PyFrame_GetLocals
(
frame
);
if
(
locals
==
NULL
) {
_PyErr_SetString
(
tstate
,
PyExc_SystemError
,
"no locals found during 'import *'"
);
return
NULL
;
}
int
err
=
import_all_from
(
tstate
,
locals
,
from
);
Py_DECREF
(
locals
);
if
(
err
<
0
) {
return
NULL
;
}
Py_RETURN_NONE
;
}
static
PyObject
*
stopiteration_error
(
PyThreadState
*
tstate
,
PyObject
*
exc
)
{
_PyInterpreterFrame
*
frame
=
tstate
->
current_frame
;
assert
(
frame
->
owner
==
FRAME_OWNED_BY_GENERATOR
);
assert
(
PyExceptionInstance_Check
(
exc
));
const
char
*
msg
=
NULL
;
if
(
PyErr_GivenExceptionMatches
(
exc
,
PyExc_StopIteration
)) {
msg
=
"generator raised StopIteration"
;
if
(
_PyFrame_GetCode
(
frame
)
->
co_flags
&
CO_ASYNC_GENERATOR
) {
msg
=
"async generator raised StopIteration"
;
}
else
if
(
_PyFrame_GetCode
(
frame
)
->
co_flags
&
CO_COROUTINE
) {
msg
=
"coroutine raised StopIteration"
;
}
}
else
if
((
_PyFrame_GetCode
(
frame
)
->
co_flags
&
CO_ASYNC_GENERATOR
)
&&
PyErr_GivenExceptionMatches
(
exc
,
PyExc_StopAsyncIteration
))
{
/* code in `gen` raised a StopAsyncIteration error:
raise a RuntimeError.
*/
msg
=
"async generator raised StopAsyncIteration"
;
}
if
(
msg
!=
NULL
) {
PyObject
*
message
=
_PyUnicode_FromASCII
(
msg
,
strlen
(
msg
));
if
(
message
==
NULL
) {
return
NULL
;
}
PyObject
*
error
=
PyObject_CallOneArg
(
PyExc_RuntimeError
,
message
);
if
(
error
==
NULL
) {
Py_DECREF
(
message
);
return
NULL
;
}
assert
(
PyExceptionInstance_Check
(
error
));
PyException_SetCause
(
error
,
Py_NewRef
(
exc
));
// Steal exc reference, rather than Py_NewRef+Py_DECREF
PyException_SetContext
(
error
,
Py_NewRef
(
exc
));
Py_DECREF
(
message
);
return
error
;
}
return
Py_NewRef
(
exc
);
}
static
PyObject
*
unary_pos
(
PyThreadState
*
unused
,
PyObject
*
value
)
{
return
PyNumber_Positive
(
value
);
}
static
PyObject
*
list_to_tuple
(
PyThreadState
*
unused
,
PyObject
*
v
)
{
/* INTRINSIC_LIST_TO_TUPLE is only emitted by the compiler for a
freshly-built, uniquely-referenced temporary list, so steal its items
into the tuple instead of copying them. */
assert
(
PyList_CheckExact
(
v
));
assert
(
_PyObject_IsUniquelyReferenced
(
v
));
return
_PyList_AsTupleAndClear
((
PyListObject
*
)
v
);
}
static
PyObject
*
make_typevar
(
PyThreadState
*
Py_UNUSED
(
ignored
),
PyObject
*
v
)
{
assert
(
PyUnicode_Check
(
v
));
return
_Py_make_typevar
(
v
,
NULL
,
NULL
);
}
static
PyObject
*
make_frozenset
(
PyThreadState
*
Py_UNUSED
(
ignored
),
PyObject
*
set
)
{
assert
(
PySet_CheckExact
(
set
));
assert
(
_PyObject_IsUniquelyReferenced
(
set
));
return
_PySet_Freeze
(
set
);
}
#define
INTRINSIC_FUNC_ENTRY
(
N
,
F
) \
[N] = {F, #N},
const
intrinsic_func1_info
_PyIntrinsics_UnaryFunctions
[]
=
{
INTRINSIC_FUNC_ENTRY
(
INTRINSIC_1_INVALID
,
no_intrinsic1
)
INTRINSIC_FUNC_ENTRY
(
INTRINSIC_PRINT
,
print_expr
)
INTRINSIC_FUNC_ENTRY
(
INTRINSIC_IMPORT_STAR
,
import_star
)
INTRINSIC_FUNC_ENTRY
(
INTRINSIC_STOPITERATION_ERROR
,
stopiteration_error
)
INTRINSIC_FUNC_ENTRY
(
INTRINSIC_ASYNC_GEN_WRAP
,
_PyAsyncGenValueWrapperNew
)
INTRINSIC_FUNC_ENTRY
(
INTRINSIC_UNARY_POSITIVE
,
unary_pos
)
INTRINSIC_FUNC_ENTRY
(
INTRINSIC_LIST_TO_TUPLE
,
list_to_tuple
)
INTRINSIC_FUNC_ENTRY
(
INTRINSIC_TYPEVAR
,
make_typevar
)
INTRINSIC_FUNC_ENTRY
(
INTRINSIC_PARAMSPEC
,
_Py_make_paramspec
)
INTRINSIC_FUNC_ENTRY
(
INTRINSIC_TYPEVARTUPLE
,
_Py_make_typevartuple
)
INTRINSIC_FUNC_ENTRY
(
INTRINSIC_SUBSCRIPT_GENERIC
,
_Py_subscript_generic
)
INTRINSIC_FUNC_ENTRY
(
INTRINSIC_TYPEALIAS
,
_Py_make_typealias
)
INTRINSIC_FUNC_ENTRY
(
INTRINSIC_BUILD_FROZENSET
,
make_frozenset
)
};
/******** Binary functions ********/
static
PyObject
*
no_intrinsic2
(
PyThreadState
*
tstate
,
PyObject
*
unused1
,
PyObject
*
unused2
)
{
_PyErr_SetString
(
tstate
,
PyExc_SystemError
,
"invalid intrinsic function"
);
return
NULL
;
}
static
PyObject
*
prep_reraise_star
(
PyThreadState
*
unused
,
PyObject
*
orig
,
PyObject
*
excs
)
{
assert
(
PyList_Check
(
excs
));
return
_PyExc_PrepReraiseStar
(
orig
,
excs
);
}
static
PyObject
*
make_typevar_with_bound
(
PyThreadState
*
Py_UNUSED
(
ignored
),
PyObject
*
name
,
PyObject
*
evaluate_bound
)
{
assert
(
PyUnicode_Check
(
name
));
return
_Py_make_typevar
(
name
,
evaluate_bound
,
NULL
);
}
static
PyObject
*
make_typevar_with_constraints
(
PyThreadState
*
Py_UNUSED
(
ignored
),
PyObject
*
name
,
PyObject
*
evaluate_constraints
)
{
assert
(
PyUnicode_Check
(
name
));
return
_Py_make_typevar
(
name
,
NULL
,
evaluate_constraints
);
}
static
PyObject
*
add_conditional_annotation
(
PyThreadState
*
tstate
,
PyObject
*
conditional_annotations
,
PyObject
*
index
)
{
// gh-154902: user code can rebind __conditional_annotations__ to any object
if
(!
PySet_CheckExact
(
conditional_annotations
)) {
_PyErr_Format
(
tstate
,
PyExc_TypeError
,
"__conditional_annotations__ must be a set, not %T"
,
conditional_annotations
);
return
NULL
;
}
if
(
PySet_Add
(
conditional_annotations
,
index
)
<
0
) {
return
NULL
;
}
Py_RETURN_NONE
;
}
static
PyObject
*
match_class_isinstance
(
PyThreadState
*
tstate
,
PyObject
*
subject
,
PyObject
*
type
)
{
/* Fast path for class patterns with no sub-patterns, e.g. `case C():`
Equivalent to the isinstance check performed by _PyEval_MatchClass,
including the same TypeError when the pattern does not refer to a
class. */
if
(!
PyType_Check
(
type
)) {
_PyErr_SetString
(
tstate
,
PyExc_TypeError
,
"class pattern must refer to a class"
);
return
NULL
;
}
int
res
=
PyObject_IsInstance
(
subject
,
type
);
if
(
res
<
0
) {
return
NULL
;
}
return
res
?
Py_True
:
Py_False
;
}
const
intrinsic_func2_info
_PyIntrinsics_BinaryFunctions
[]
=
{
INTRINSIC_FUNC_ENTRY
(
INTRINSIC_2_INVALID
,
no_intrinsic2
)
INTRINSIC_FUNC_ENTRY
(
INTRINSIC_PREP_RERAISE_STAR
,
prep_reraise_star
)
INTRINSIC_FUNC_ENTRY
(
INTRINSIC_TYPEVAR_WITH_BOUND
,
make_typevar_with_bound
)
INTRINSIC_FUNC_ENTRY
(
INTRINSIC_TYPEVAR_WITH_CONSTRAINTS
,
make_typevar_with_constraints
)
INTRINSIC_FUNC_ENTRY
(
INTRINSIC_SET_FUNCTION_TYPE_PARAMS
,
_Py_set_function_type_params
)
INTRINSIC_FUNC_ENTRY
(
INTRINSIC_SET_TYPEPARAM_DEFAULT
,
_Py_set_typeparam_default
)
INTRINSIC_FUNC_ENTRY
(
INTRINSIC_ADD_CONDITIONAL_ANNOTATION
,
add_conditional_annotation
)
INTRINSIC_FUNC_ENTRY
(
INTRINSIC_MATCH_CLASS_ISINSTANCE
,
match_class_isinstance
)
};
#undef
INTRINSIC_FUNC_ENTRY
PyObject
*
_PyCompile_GetUnaryIntrinsicName
(
int
index
)
{
if
(
index
<
0
||
index
>
MAX_INTRINSIC_1
) {
return
NULL
;
}
return
PyUnicode_FromString
(
_PyIntrinsics_UnaryFunctions
[
index
].
name
);
}
PyObject
*
_PyCompile_GetBinaryIntrinsicName
(
int
index
)
{
if
(
index
<
0
||
index
>
MAX_INTRINSIC_2
) {
return
NULL
;
}
return
PyUnicode_FromString
(
_PyIntrinsics_BinaryFunctions
[
index
].
name
);
}
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