8000 Merge branch 'main' into mypy-tomllib · python/cpython@318ffa7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 318ffa7

Browse files
authored
Merge branch 'main' into mypy-tomllib
2 parents 3eec25a + 208d06f commit 318ffa7

32 files changed

+1047
-468
lines changed

.github/workflows/mypy.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ on:
88
pull_request:
99
paths:
1010
- ".github/workflows/mypy.yml"
11+
- "Lib/_colorize.py"
1112
- "Lib/_pyrepl/**"
1213
- "Lib/test/libregrtest/**"
1314
- "Lib/tomllib/**"
15+
- "Misc/mypy/**"
1416
- "Tools/build/generate_sbom.py"
1517
- "Tools/cases_generator/**"
1618
- "Tools/clinic/**"

Include/internal/pycore_code.h

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -177,12 +177,14 @@ typedef struct {
177177
*/
178178

179179
// Note that these all fit within a byte, as do combinations.
180-
// Later, we will use the smaller numbers to differentiate the different
181-
// kinds of locals (e.g. pos-only arg, varkwargs, local-only).
182-
#define CO_FAST_HIDDEN 0x10
183-
#define CO_FAST_LOCAL 0x20
184-
#define CO_FAST_CELL 0x40
185-
#define CO_FAST_FREE 0x80
180+
#define CO_FAST_ARG_POS (0x02) // pos-only, pos-or-kw, varargs
181+
#define CO_FAST_ARG_KW (0x04) // kw-only, pos-or-kw, varkwargs
182+
#define CO_FAST_ARG_VAR (0x08) // varargs, varkwargs
183+
#define CO_FAST_ARG (CO_FAST_ARG_POS | CO_FAST_ARG_KW | CO_FAST_ARG_VAR)
184+
#define CO_FAST_HIDDEN (0x10)
185+
#define CO_FAST_LOCAL (0x20)
186+
#define CO_FAST_CELL (0x40)
187+
#define CO_FAST_FREE (0x80)
186188

187189
typedef unsigned char _PyLocals_Kind;
188190

@@ -315,6 +317,7 @@ extern void _Py_Specialize_ForIter(_PyStackRef iter, _Py_CODEUNIT *instr, int op
315317
extern void _Py_Specialize_Send(_PyStackRef receiver, _Py_CODEUNIT *instr);
316318
extern void _Py_Specialize_ToBool(_PyStackRef value, _Py_CODEUNIT *instr);
317319
extern void _Py_Specialize_ContainsOp(_PyStackRef value, _Py_CODEUNIT *instr);
320+
extern void _Py_GatherStats_GetIter(_PyStackRef iterable);
318321

319322
// Utility functions for reading/writing 32/64-bit values in the inline caches.
320323
// Great care should be taken to ensure that these functions remain correct and
@@ -561,6 +564,10 @@ extern void _Py_ClearTLBCIndex(_PyThreadStateImpl *tstate);
561564
extern int _Py_ClearUnusedTLBC(PyInterpreterState *interp);
562565
#endif
563566

567+
568+
PyAPI_FUNC(int) _PyCode_ReturnsOnlyNone(PyCodeObject *);
569+
570+
564571
#ifdef __cplusplus
565572
}
566573
#endif

Include/internal/pycore_crossinterp.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,13 @@ PyAPI_FUNC(_PyBytes_data_t *) _PyBytes_GetXIDataWrapped(
171171
xid_newobjfunc,
172172
_PyXIData_t *);
173173

174+
// _PyObject_GetXIData() for marshal
175+
PyAPI_FUNC(PyObject *) _PyMarshal_ReadObjectFromXIData(_PyXIData_t *);
176+
PyAPI_FUNC(int) _PyMarshal_GetXIData(
177+
PyThreadState *,
178+
PyObject *,
179+
_PyXIData_t *);
180+
174181

175182
/* using cross-interpreter data */
176183

Include/internal/pycore_interp_structs.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -667,11 +667,8 @@ struct _Py_interp_cached_objects {
667667

668668
/* object.__reduce__ */
669669
PyObject *objreduce;
670-
#ifndef Py_GIL_DISABLED
671-
/* resolve_slotdups() */
672670
PyObject *type_slots_pname;
673671
pytype_slotdef *type_slots_ptrs[MAX_EQUIV];
674-
#endif
675672

676673
/* TypeVar and related types */
677674
PyTypeObject *generic_type;

Include/internal/pycore_object.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ extern int _PyDict_CheckConsistency(PyObject *mp, int check_content);
313313
// Fast inlined version of PyType_HasFeature()
314314
static inline int
315315
_PyType_HasFeature(PyTypeObject *type, unsigned long feature) {
316-
return ((type->tp_flags) & feature) != 0;
316+
return ((FT_ATOMIC_LOAD_ULONG_RELAXED(type->tp_flags) & feature) != 0);
317317
}
318318

319319
extern void _PyType_InitCache(PyInterpreterState *interp);

Include/internal/pycore_opcode_utils.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ extern "C" {
5454
(opcode) == RAISE_VARARGS || \
5555
(opcode) == RERAISE)
5656

57+
#define IS_RETURN_OPCODE(opcode) \
58+
(opcode == RETURN_VALUE)
59+
5760

5861
/* Flags used in the oparg for MAKE_FUNCTION */
5962
#define MAKE_FUNCTION_DEFAULTS 0x01

Include/internal/pycore_typeobject.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ extern int _PyType_AddMethod(PyTypeObject *, PyMethodDef *);
134134
extern void _PyType_SetFlagsRecursive(PyTypeObject *self, unsigned long mask,
135135
unsigned long flags);
136136

137+
extern unsigned int _PyType_GetVersionForCurrentState(PyTypeObject *tp);
137138
PyAPI_FUNC(void) _PyType_SetVersion(PyTypeObject *tp, unsigned int version);
138139
PyTypeObject *_PyType_LookupByVersion(unsigned int version);
139140

Include/object.h

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -620,12 +620,6 @@ given type object has a specified feature.
620620
#define Py_TPFLAGS_HAVE_FINALIZE (1UL << 0)
621621
#define Py_TPFLAGS_HAVE_VERSION_TAG (1UL << 18)
622622

623-
// Flag values for ob_flags (16 bits available, if SIZEOF_VOID_P > 4).
624-
#define _Py_IMMORTAL_FLAGS (1 << 0)
625-
#define _Py_STATICALLY_ALLOCATED_FLAG (1 << 2)
626-
#if defined(Py_GIL_DISABLED) && defined(Py_DEBUG)
627-
#define _Py_TYPE_REVEALED_FLAG (1 << 3)
628-
#endif
629623

630624
#define Py_CONSTANT_NONE 0
631625
#define Py_CONSTANT_FALSE 1
@@ -782,7 +776,11 @@ PyType_HasFeature(PyTypeObject *type, unsigned long feature)
782776
// PyTypeObject is opaque in the limited C API
783777
flags = PyType_GetFlags(type);
784778
#else
785-
flags = type->tp_flags;
779+
# ifdef Py_GIL_DISABLED
780+
flags = _Py_atomic_load_ulong_relaxed(&type->tp_flags);
781+
# else
782+
flags = type->tp_flags;
783+
# endif
786784
#endif
787785
return ((flags & feature) != 0);
788786
}

Include/refcount.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ immortal. The latter should be the only instances that require
1919
cleanup during runtime finalization.
2020
*/
2121

22+
#define _Py_STATICALLY_ALLOCATED_FLAG 4
23+
#define _Py_IMMORTAL_FLAGS 1
24+
2225
#if SIZEOF_VOID_P > 4
2326
/*
2427
In 64+ bit systems, any object whose 32 bit reference count is >= 2**31

Lib/statistics.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -810,8 +810,7 @@ def deco(builder):
810810
@register('normal', 'gauss')
811811
def normal_kernel():
812812
sqrt2pi = sqrt(2 * pi)
813-
sqrt2 = sqrt(2)
814-
neg_sqrt2 = -sqrt2
813+
neg_sqrt2 = -sqrt(2)
815814
pdf = lambda t: exp(-1/2 * t * t) / sqrt2pi
816815
cdf = lambda t: 1/2 * erfc(t / neg_sqrt2)
817816
invcdf = lambda t: _normal_dist_inv_cdf(t, 0.0, 1.0)

Lib/test/_code_definitions.py

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
2+
3+
def spam_minimal():
4+
# no arg defaults or kwarg defaults
5+
# no annotations
6+
# no local vars
7+
# no free vars
8+
# no globals
9+
# no builtins
10+
# no attr access (names)
11+
# no code
12+
return
13+
14+
15+
def spam_full(a, b, /, c, d:int=1, *args, e, f:object=None, **kwargs) -> tuple:
16+
# arg defaults, kwarg defaults
17+
# annotations
18+
# all kinds of local vars, except cells
19+
# no free vars
20+
# some globals
21+
# some builtins
22+
# some attr access (names)
23+
x = args
24+
y = kwargs
25+
z = (a, b, c, d)
26+
kwargs['e'] = e
27+
kwargs['f'] = f
28+
extras = list((x, y, z, spam, spam.__name__))
29+
return tuple(a, b, c, d, e, f, args, kwargs), extras
30+
31+
32+
def spam(x):
33+
return x, None
34+
35+
36+
def spam_N(x):
37+
def eggs_nested(y):
38+
return None, y
39+
return eggs_nested, x
40+
41+
42+
def spam_C(x):
43+
a = 1
44+
def eggs_closure(y):
45+
return None, y, a, x
46+
return eggs_closure, a, x
47+
48+
49+
def spam_NN(x):
50+
def eggs_nested_N(y):
51+
def ham_nested(z):
52+
return None, z
53+
return ham_nested, y
54+
return eggs_nested_N, x
55+
56+
57+
def spam_NC(x):
58+
a = 1
59+
def eggs_nested_C(y):
60+
def ham_closure(z):
61+
return None, z, y, a, x
62+
return ham_closure, y
63+
return eggs_nested_C, a, x
64+
65+
66+
def spam_CN(x):
67+
a = 1
68+
def eggs_closure_N(y):
69+
def ham_C_nested(z):
70+
return None, z
71+
return ham_C_nested, y, a, x
72+
return eggs_closure_N, a, x
73+
74+
75+
def spam_CC(x):
76+
a = 1
77+
def eggs_closure_C(y):
78+
b = 2
79+
def ham_C_closure(z):
80+
return None, z, b, y, a, x
81+
return ham_C_closure, b, y, a, x
82+
return eggs_closure_C, a, x
83+
84+
85+
eggs_nested, *_ = spam_N(1)
86+
eggs_closure, *_ = spam_C(1)
87+
eggs_nested_N, *_ = spam_NN(1)
88+
eggs_nested_C, *_ = spam_NC(1)
89+
eggs_closure_N, *_ = spam_CN(1)
90+
eggs_closure_C, *_ = spam_CC(1)
91+
92+
ham_nested, *_ = eggs_nested_N(2)
93+
ham_closure, *_ = eggs_nested_C(2)
94+
ham_C_nested, *_ = eggs_closure_N(2)
95+
ham_C_closure, *_ = eggs_closure_C(2)
96+
97+
98+
TOP_FUNCTIONS = [
99+
# shallow
100+
spam_minimal,
101+
spam_full,
102+
spam,
103+
# outer func
104+
spam_N,
105+
spam_C,
106+
spam_NN,
107+
spam_NC,
108+
spam_CN,
109+
spam_CC,
110+
]
111+
NESTED_FUNCTIONS = [
112+
# inner func
113+
eggs_nested,
114+
eggs_closure,
115+
eggs_nested_N,
116+
eggs_nested_C,
117+
eggs_closure_N,
118+
eggs_closure_C,
119+
# inner inner func
120+
ham_nested,
121+
ham_closure,
122+
ham_C_nested,
123+
ham_C_closure,
124+
]
125+
FUNCTIONS = [
126+
*TOP_FUNCTIONS,
127+
*NESTED_FUNCTIONS,
128+
]
129+
130+
131+
# generators
132+
133+
def gen_spam_1(*args):
134+
for arg in args:
135+
yield arg
136+
137+
138+
def gen_spam_2(*args):
139+
yield from args
140+
141+
142+
async def async_spam():
143+
pass
144+
coro_spam = async_spam()
145+
coro_spam.close()
146+
147+
148+
async def asyncgen_spam(*args):
149+
for arg in args:
150+
yield arg
151+
asynccoro_spam = asyncgen_spam(1, 2, 3)
152+
153+
154+
FUNCTION_LIKE = [
155+
gen_spam_1,
156+
gen_spam_2,
157+
async_spam,
158+
asyncgen_spam,
159+
]
160+
FUNCTION_LIKE_APPLIED = [
161+
coro_spam, # actually FunctionType?
162+
asynccoro_spam, # actually FunctionType?
163+
]

0 commit comments

Comments
 (0)
0