10000 Catch up with main · python/cpython@dc308ba · GitHub
[go: up one dir, main page]

Skip to content

Commit dc308ba

Browse files
committed
Catch up with main
2 parents a98d4fe + 909adb5 commit dc308ba

File tree

17 files changed

+180
-37
lines changed

17 files changed

+180
-37
lines changed

Doc/c-api/intro.rst

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,30 @@ defined closer to where they are useful (e.g. :c:macro:`Py_RETURN_NONE`).
105105
Others of a more general utility are defined here. This is not necessarily a
106106
complete listing.
107107

108+
.. c:macro:: PyMODINIT_FUNC
109+
110+
Declare an extension module ``PyInit`` initialization function. The function
111+
return type is :c:expr:`PyObject*`. The macro declares any special linkage
112+
declarations required by the platform, and for C++ declares the function as
113+
``extern "C"``.
114+
115+
The initialization function must be named :samp:`PyInit_{name}`, where
116+
*name* is the name of the module, and should be the only non-\ ``static``
117+
item defined in the module file. Example::
118+
119+
static struct PyModuleDef spam_module = {
120+
PyModuleDef_HEAD_INIT,
121+
.m_name = "spam",
122+
...
123+
};
124+
125+
PyMODINIT_FUNC
126+
PyInit_spam(void)
127+
{
128+
return PyModule_Create(&spam_module);
129+
}
130+
131+
108132
.. c:macro:: Py_ABS(x)
109133
110134
Return the absolute value of ``x``.

Doc/using/configure.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -855,8 +855,8 @@ Example on Linux x86-64::
855855
At the beginning of the files, C extensions are built as built-in modules.
856856
Extensions defined after the ``*shared*`` marker are built as dynamic libraries.
857857

858-
The :c:macro:`PyAPI_FUNC()`, :c:macro:`PyAPI_DATA()` and
859-
:c:macro:`PyMODINIT_FUNC` macros of :file:`Include/pyport.h` are defined
858+
The :c:macro:`!PyAPI_FUNC()`, :c:macro:`!PyAPI_DATA()` and
859+
:c:macro:`PyMODINIT_FUNC` macros of :file:`Include/exports.h` are defined
860860
differently depending if the ``Py_BUILD_CORE_MODULE`` macro is defined:
861861

862862
* Use ``Py_EXPORTED_SYMBOL`` if the ``Py_BUILD_CORE_MODULE`` is defined

Doc/whatsnew/2.3.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1889,7 +1889,7 @@ Changes to Python's build process and to the C API include:
18891889
* The :c:macro:`!DL_EXPORT` and :c:macro:`!DL_IMPORT` macros are now deprecated.
18901890
Initialization functions for Python extension modules should now be declared
18911891
using the new macro :c:macro:`PyMODINIT_FUNC`, while the Python core will
1892-
generally use the :c:macro:`PyAPI_FUNC` and :c:macro:`PyAPI_DATA` macros.
1892+
generally use the :c:macro:`!PyAPI_FUNC` and :c:macro:`!PyAPI_DATA` macros.
18931893

18941894
* The interpreter can be compiled without any docstrings for the built-in
18951895
functions and modules by supplying :option:`!--without-doc-strings` to the

Lib/test/libregrtest/findtests.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,19 @@ def findtests(*, testdir: StrPath | None = None, exclude=(),
3838
mod, ext = os.path.splitext(name)
3939
if (not mod.startswith("test_")) or (mod in exclude):
4040
continue
41-
if mod in split_test_dirs:
41+
if base_mod:
42+
fullname = f"{base_mod}.{mod}"
43+
else:
44+
fullname = mod
45+
if fullname in split_test_dirs:
4246
subdir = os.path.join(testdir, mod)
43-
mod = f"{base_mod or 'test'}.{mod}"
47+
if not base_mod:
48+
fullname = f"test.{mod}"
4449
tests.extend(findtests(testdir=subdir, exclude=exclude,
4550
split_test_dirs=split_test_dirs,
46-
base_mod=mod))
51+
base_mod=fullname))
4752
elif ext in (".py", ""):
48-
tests.append(f"{base_mod}.{mod}" if base_mod else mod)
53+
tests.append(fullname)
4954
return sorted(tests)
5055

5156

Lib/test/test_dis.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,8 @@ def _tryfinallyconst(b):
642642
CALL 0
643643
POP_TOP
644644
RERAISE 0
645-
>> COPY 3
645+
646+
None >> COPY 3
646647
POP_EXCEPT
647648
RERAISE 1
648649
ExceptionTable:
@@ -674,7 +675,8 @@ def _tryfinallyconst(b):
674675
CALL 0
675676
POP_TOP
676677
RERAISE 0
677-
>> COPY 3
678+
679+
None >> COPY 3
678680
POP_EXCEPT
679681
RERAISE 1
680682
ExceptionTable:
@@ -1822,9 +1824,9 @@ def _prepare_test_cases():
18221824
Instruction(opname='CALL', opcode=53, arg=1, argval=1, argrepr='', offset=414, start_offset=414, starts_line=False, line_number=28, is_jump_target=False, positions=None),
18231825
Instruction(opname='POP_TOP', opcode=32, arg=None, argval=None, argrepr='', offset=422, start_offset=422, starts_line=False, line_number=28, is_jump_target=False, positions=None),
18241826
Instruction(opname='RERAISE', opcode=102, arg=0, argval=0, argrepr='', offset=424, start_offset=424, starts_line=False, line_number=28, is_jump_target=False, positions=None),
1825-
Instruction(opname='COPY', opcode=61, arg=3, argval=3, argrepr='', offset=426, start_offset=426, starts_line=False, line_number=28, is_jump_target=False, positions=None),
1826-
Instruction(opname='POP_EXCEPT', opcode=31, arg=None, argval=None, argrepr='', offset=428, start_offset=428, starts_line=False, line_number=28, is_jump_target=False, positions=None),
1827-
Instruction(opname='RERAISE', opcode=102, arg=1, argval=1, argrepr='', offset=430, start_offset=430, starts_line=False, line_number=28, is_jump_target=False, positions=None),
1827+
Instruction(opname='COPY', opcode=61, arg=3, argval=3, argrepr='', offset=426, start_offset=426, starts_line=True, line_number=None, is_jump_target=False, positions=None),
1828+
Instruction(opname='POP_EXCEPT', opcode=31, arg=None, argval=None, argrepr='', offset=428, start_offset=428, starts_line=False, line_number=None, is_jump_target=False, positions=None),
1829+
Instruction(opname='RERAISE', opcode=102, arg=1, argval=1, argrepr='', offset=430, start_offset=430, starts_line=False, line_number=None, is_jump_target=False, positions=None),
18281830
]
18291831

18301832
# One last piece of inspect fodder to check the default line number handling

Lib/test/test_pydoc.py

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from io import StringIO
2525
from collections import namedtuple
2626
from urllib.request import urlopen, urlcleanup
27+
from test import support
2728
from test.support import import_helper
2829
from test.support import os_helper
2930
from test.support.script_helper import (assert_python_ok,
@@ -1236,22 +1237,56 @@ def test_bound_builtin_classmethod_o(self):
12361237
self.assertEqual(self._get_summary_line(dict.__class_getitem__),
12371238
"__class_getitem__(object, /) method of builtins.type instance")
12381239

1240+
@support.cpython_only
12391241
def test_module_level_callable_unrepresentable_default(self):
1240-
self.assertEqual(self._get_summary_line(getattr),
1241-
"getattr(...)")
1242+
import _testcapi
1243+
builtin = _testcapi.func_with_unrepresentable_signature
1244+
self.assertEqual(self._get_summary_line(builtin),
1245+
"func_with_unrepresentable_signature(a, b=<x>)")
12421246

1247+
@support.cpython_only
12431248
def test_builtin_staticmethod_unrepresentable_default(self):
12441249
self.assertEqual(self._get_summary_line(str.maketrans),
12451250
"maketrans(x, y=<unrepresentable>, z=<unrepresentable>, /)")
1251+
import _testcapi
1252+
cls = _testcapi.DocStringUnrepresentableSignatureTest
1253+
self.assertEqual(self._get_summary_line(cls.staticmeth),
1254+
"staticmeth(a, b=<x>)")
12461255

1256+
@support.cpython_only
12471257
def test_unbound_builtin_method_unrepresentable_default(self):
12481258
self.assertEqual(self._get_summary_line(dict.pop),
12491259
"pop(self, key, default=<unrepresentable>, /)")
1260+
import _testcapi
1261+
cls = _testcapi.DocStringUnrepresentableSignatureTest
1262+
self.assertEqual(self._get_summary_line(cls.meth),
1263+
"meth(self, /, a, b=<x>)")
12501264

1265+
@support.cpython_only
12511266
def test_bound_builtin_method_unrepresentable_default(self):
12521267
self.assertEqual(self._get_summary_line({}.pop),
12531268
"pop(key, default=<unrepresentable>, /) "
12541269
"method of builtins.dict instance")
1270+
import _testcapi
1271+
obj = _testcapi.DocStringUnrepresentableSignatureTest()
1272+
self.assertEqual(self._get_summary_line(obj.meth),
1273+
"meth(a, b=<x>) "
1274+
"method of _testcapi.DocStringUnrepresentableSignatureTest instance")
1275+
1276+
@support.cpython_only
1277+
def test_unbound_builtin_classmethod_unrepresentable_default(self):
1278+
import _testcapi
1279+
cls = _testcapi.DocStringUnrepresentableSignatureTest
1280+
descr = cls.__dict__['classmeth']
1281+
self.assertEqual(self._get_summary_line(descr),
1282+
"classmeth(type, /, a, b=<x>)")
1283+
1284+
@support.cpython_only
1285+
def test_bound_builtin_classmethod_unrepresentable_default(self):
1286+
import _testcapi
1287+
cls = _testcapi.DocStringUnrepresentableSignatureTest
1288+
self.assertEqual(self._get_summary_line(cls.classmeth),
1289+
"classmeth(a, b=<x>) method of builtins.type instance")
12551290

12561291
def test_overridden_text_signature(self):
12571292
class C:

Lib/test/test_sys_settrace.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -929,6 +929,35 @@ def func():
929929
(6, 'line'),
930930
(6, 'return')])
931931

932+
def test_finally_with_conditional(self):
933+
934+
# See gh-105658
935+
condition = True
936+
def func():
937+
try:
938+
try:
939+
raise Exception
940+
finally:
941+
if condition:
942+
result = 1
943+
result = 2
944+
except:
945+
result = 3
946+
return result
947+
948+
self.run_and_compare(func,
949+
[(0, 'call'),
950+
(1, 'line'),
951+
(2, 'line'),
952+
(3, 'line'),
953+
(3, 'exception'),
954+
(5, 'line'),
955+
(6, 'line'),
956+
(8, 'line'),
957+
(9, 'line'),
958+
(10, 'line'),
959+
(10, 'return')])
960+
932961
def test_break_to_continue1(self):
933962

934963
def func():
@@ -2123,7 +2152,7 @@ def test_jump_in_nested_finally_3(output):
21232152
output.append(11)
21242153
output.append(12)
21252154

2126-
@jump_test(5, 11, [2, 4], (ValueError, 'exception'))
2155+
@jump_test(5, 11, [2, 4], (ValueError, 'comes after the current code block'))
21272156
def test_no_jump_over_return_try_finally_in_finally_block(output):
21282157
try:
21292158
output.append(2)

Lib/test/test_type_params.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,19 @@ class Cls:
694694
cls = ns["outer"]()
695695
self.assertEqual(cls.Alias.__value__, "class")
696696

697+
def test_nested_free(self):
698+
ns = run_code("""
699+
def f():
700+
T = str
701+
class C:
702+
T = int
703+
class D[U](T):
704+
x = T
705+
return C
706+
""")
707+
C = ns["f"]()
708+
self.assertIn(int, C.D.__bases__)
709+
self.assertIs(C.D.x, str)
697710

698711
class TypeParamsManglingTest(unittest.TestCase):
699712
def test_mangling(self):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix compiling type param scopes that use a name which is also free in an
2+
inner scope.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix bug where the line trace of an except block ending with a conditional
2+
includes an excess event with the line of the conditional expression.

Modules/_testcapi/docstring.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,13 @@ static PyMethodDef test_methods[] = {
100100
{"test_with_docstring",
101101
test_with_docstring, METH_VARARGS,
102102
PyDoc_STR("This is a pretty normal docstring.")},
103+
{"func_with_unrepresentable_signature",
104+
(PyCFunction)test_with_docstring, METH_VARARGS,
105+
PyDoc_STR(
106+
"func_with_unrepresentable_signature($module, /, a, b=<x>)\n"
107+
"--\n\n"
108+
"This docstring has a signature with unrepresentable default."
109+
)},
103110
{NULL},
104111
};
105112

@@ -140,6 +147,40 @@ static PyTypeObject DocStringNoSignatureTest = {
140147
.tp_new = PyType_GenericNew,
141148
};
142149

150+
static PyMethodDef DocStringUnrepresentableSignatureTest_methods[] = {
151+
{"meth",
152+
(PyCFunction)test_with_docstring, METH_VARARGS,
153+
PyDoc_STR(
154+
"meth($self, /, a, b=<x>)\n"
155+
"--\n\n"
156+
"This docstring has a signature with unrepresentable default."
157+
)},
158+
{"classmeth",
159+
(PyCFunction)test_with_docstring, METH_VARARGS|METH_CLASS,
160+
PyDoc_STR(
161+
"classmeth($type, /, a, b=<x>)\n"
162+
"--\n\n"
163+
"This docstring has a signature with unrepresentable default."
164+
)},
165+
{"staticmeth",
166+
(PyCFunction)test_with_docstring, METH_VARARGS|METH_STATIC,
167+
PyDoc_STR(
168+
"staticmeth(a, b=<x>)\n"
169+
"--\n\n"
170+
"This docstring has a signature with unrepresentable default."
171+
)},
172+
{NULL},
173+
};
174+
175+
static PyTypeObject DocStringUnrepresentableSignatureTest = {
176+
PyVarObject_HEAD_INIT(NULL, 0)
177+
.tp_name = "_testcapi.DocStringUnrepresentableSignatureTest",
178+
.tp_basicsize = sizeof(PyObject),
179+
.tp_flags = Py_TPFLAGS_DEFAULT,
180+
.tp_methods = DocStringUnrepresentableSignatureTest_methods,
181+
.tp_new = PyType_GenericNew,
182+
};
183+
143184
int
144185
_PyTestCapi_Init_Docstring(PyObject *mod)
145186
{
@@ -149,5 +190,8 @@ _PyTestCapi_Init_Docstring(PyObject *mod)
149190
if (PyModule_AddType(mod, &DocStringNoSignatureTest) < 0) {
150191
return -1;
151192
}
193+
if (PyModule_AddType(mod, &DocStringUnrepresentableSignatureTest) < 0) {
194+
return -1;
195+
}
152196
return 0;
153197
}

Objects/genericaliasobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,7 @@ ga_unpacked_tuple_args(PyObject *self, void *unused)
814814
}
815815

816816
static PyGetSetDef ga_properties[] = {
817-
{"__parameters__", ga_parameters, (setter)NULL, "Type variables in the GenericAlias.", NULL},
817+
{"__parameters__", ga_parameters, (setter)NULL, PyDoc_STR("Type variables in the GenericAlias."), NULL},
818818
{"__typing_unpacked_tuple_args__", ga_unpacked_tuple_args, (setter)NULL, NULL},
819819
{0}
820820
};

Python/compile.c

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3261,18 +3261,6 @@ compiler_continue(struct compiler *c, location loc)
32613261
}
32623262

32633263

3264-
static location
3265-
location_of_last_executing_statement(asdl_stmt_seq *stmts)
3266-
{
3267-
for (Py_ssize_t i = asdl_seq_LEN(stmts) - 1; i >= 0; i++) {
3268-
location loc = LOC((stmt_ty)asdl_seq_GET(stmts, i));
3269-
if (loc.lineno > 0) {
3270-
return loc;
3271-
}
3272-
}
3273-
return NO_LOCATION;
3274-
}
3275-
32763264
/* Code generated for "try: <body> finally: <finalbody>" is as follows:
32773265
32783266
SETUP_FINALLY L
@@ -3341,9 +3329,9 @@ compiler_try_finally(struct compiler *c, stmt_ty s)
33413329
RETURN_IF_ERROR(
33423330
compiler_push_fblock(c, loc, FINALLY_END, end, NO_LABEL, NULL));
33433331
VISIT_SEQ(c, stmt, s->v.Try.finalbody);
3344-
loc = location_of_last_executing_statement(s->v.Try.finalbody);
33453332
compiler_pop_fblock(c, FINALLY_END, end);
33463333

3334+
loc = NO_LOCATION;
33473335
ADDOP_I(c, loc, RERAISE, 0);
33483336

33493337
USE_LABEL(c, cleanup);
@@ -3392,9 +3380,9 @@ compiler_try_star_finally(struct compiler *c, stmt_ty s)
33923380
compiler_push_fblock(c, loc, FINALLY_END, end, NO_LABEL, NULL));
33933381

33943382
VISIT_SEQ(c, stmt, s->v.TryStar.finalbody);
3395-
loc = location_of_last_executing_statement(s->v.Try.finalbody);
33963383

33973384
compiler_pop_fblock(c, FINALLY_END, end);
3385+
loc = NO_LOCATION;
33983386
ADDOP_I(c, loc, RERAISE, 0);
33993387

34003388
USE_LABEL(c, cleanup);

Python/flowgraph.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,8 @@ dump_instr(cfg_instr *i)
261261
if (HAS_TARGET(i->i_opcode)) {
262262
sprintf(arg, "target: %p [%d] ", i->i_target, i->i_oparg);
263263
}
264-
fprintf(stderr, "line: %d, opcode: %d %s%s\n",
265-
i->i_loc.lineno, i->i_opcode, arg, jump);
264+
fprintf(stderr, "line: %d, %s (%d) %s%s\n",
265+
i->i_loc.lineno, _PyOpcode_OpName[i->i_opcode], i->i_opcode, arg, jump);
266266
}
267267

268268
static inline int
@@ -2661,4 +2661,3 @@ _PyCfg_OptimizedCfgToInstructionSequence(cfg_builder *g,
26612661

26622662
return SUCCESS;
26632663
}
2664-

Python/symtable.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -836,8 +836,7 @@ update_symbols(PyObject *symbols, PyObject *scopes,
836836
the class that has the same name as a local
837837
or global in the class scope.
838838
*/
839-
if (classflag &&
840-
PyLong_AS_LONG(v) & (DEF_BOUND | DEF_GLOBAL)) {
839+
if (classflag) {
841840
long flags = PyLong_AS_LONG(v) | DEF_FREE_CLASS;
842841
v_new = PyLong_FromLong(flags);
843842
if (!v_new) {
@@ -1078,7 +1077,7 @@ analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
10781077
goto error;
10791078
/* Records the results of the analysis in the symbol table entry */
10801079
if (!update_symbols(ste->ste_symbols, scopes, bound, newfree, inlined_cells,
1081-
ste->ste_type == ClassBlock))
1080+
(ste->ste_type == ClassBlock) || ste->ste_can_see_class_scope))
10821081
goto error;
10831082

10841083
temp = PyNumber_InPlaceOr(free, newfree);

0 commit comments

Comments
 (0)
0