Skip to content

Commit 0d3d500

Browse files
committed
2 parents 46f461b + acc2f3b commit 0d3d500

File tree

9 files changed

+158
-142
lines changed

9 files changed

+158
-142
lines changed

Doc/library/asyncio-task.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -1097,7 +1097,7 @@ Task Object
10971097
The *limit* argument is passed to :meth:`get_stack` directly.
10981098

10991099
The *file* argument is an I/O stream to which the output
1100-
is written; by default output is written to :data:`sys.stderr`.
1100+
is written; by default output is written to :data:`sys.stdout`.
11011101

11021102
.. method:: get_coro()
11031103

Doc/requirements.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ sphinx==4.5.0
88
blurb
99

1010
sphinx-lint==0.6.7
11-
sphinxext-opengraph>=0.7.1
11+
sphinxext-opengraph==0.7.5
1212

1313
# The theme used by the documentation is stored separately, so we need
1414
# to install that as well.

Doc/whatsnew/3.12.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ APIs:
479479
* :class:`webbrowser.MacOSX` (:gh:`86421`)
480480

481481
Pending Removal in Python 3.14
482-
==============================
482+
------------------------------
483483

484484
* Deprecated the following :mod:`importlib.abc` classes, scheduled for removal in
485485
Python 3.14:

Lib/test/test_fileio.py

+6-7
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
from test.support import (
1313
cpython_only, swap_attr, gc_collect, is_emscripten, is_wasi
1414
)
15-
from test.support.os_helper import (TESTFN, TESTFN_UNICODE, make_bad_fd)
15+
from test.support.os_helper import (
16+
TESTFN, TESTFN_ASCII, TESTFN_UNICODE, make_bad_fd,
17+
)
1618
from test.support.warnings_helper import check_warnings
1719
from collections import UserList
1820

@@ -431,18 +433,15 @@ def testUnicodeOpen(self):
431433

432434
def testBytesOpen(self):
433435
# Opening a bytes filename
434-
try:
435-
fn = TESTFN.encode("ascii")
436-
except UnicodeEncodeError:
437-
self.skipTest('could not encode %r to ascii' % TESTFN)
436+
fn = TESTFN_ASCII.encode("ascii")
438437
f = self.FileIO(fn, "w")
439438
try:
440439
f.write(b"abc")
441440
f.close()
442-
with open(TESTFN, "rb") as f:
441+
with open(TESTFN_ASCII, "rb") as f:
443442
self.assertEqual(f.read(), b"abc")
444443
finally:
445-
os.unlink(TESTFN)
444+
os.unlink(TESTFN_ASCII)
446445

447446
@unittest.skipIf(sys.getfilesystemencoding() != 'utf-8',
448447
"test only works for utf-8 filesystems")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix :meth:`asyncio.Task.print_stack` description for ``file=None``.
2+
Patch by Oleg Iarygin.

Modules/_testcapimodule.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -3185,11 +3185,11 @@ eval_eval_code_ex(PyObject *mod, PyObject *pos_args)
31853185
globals,
31863186
locals,
31873187
c_args,
3188-
c_args_len,
3188+
(int)c_args_len,
31893189
c_kwargs,
3190-
c_kwargs_len,
3190+
(int)c_kwargs_len,
31913191
c_defaults,
3192-
c_defaults_len,
3192+
(int)c_defaults_len,
31933193
kw_defaults,
31943194
closure
31953195
);

Python/bytecodes.c

+58-62
Original file line numberDiff line numberDiff line change
@@ -859,41 +859,31 @@ dummy_func(
859859
}
860860
}
861861

862-
// stack effect: (__0 -- __array[oparg])
863-
inst(UNPACK_SEQUENCE) {
862+
inst(UNPACK_SEQUENCE, (unused/1, seq -- unused[oparg])) {
864863
#if ENABLE_SPECIALIZATION
865864
_PyUnpackSequenceCache *cache = (_PyUnpackSequenceCache *)next_instr;
866865
if (ADAPTIVE_COUNTER_IS_ZERO(cache->counter)) {
867866
assert(cframe.use_tracing == 0);
868-
PyObject *seq = TOP();
869867
next_instr--;
870868
_Py_Specialize_UnpackSequence(seq, next_instr, oparg);
871869
DISPATCH_SAME_OPARG();
872870
}
873871
STAT_INC(UNPACK_SEQUENCE, deferred);
874872
DECREMENT_ADAPTIVE_COUNTER(cache->counter);
875873
#endif /* ENABLE_SPECIALIZATION */
876-
PyObject *seq = POP();
877-
PyObject **top = stack_pointer + oparg;
878-
if (!unpack_iterable(tstate, seq, oparg, -1, top)) {
879-
Py_DECREF(seq);
880-
goto error;
881-
}
882-
STACK_GROW(oparg);
874+
PyObject **top = stack_pointer + oparg - 1;
875+
int res = unpack_iterable(tstate, seq, oparg, -1, top);
883876
Py_DECREF(seq);
884-
JUMPBY(INLINE_CACHE_ENTRIES_UNPACK_SEQUENCE);
877+
ERROR_IF(res == 0, error);
885878
}
886879

887-
// stack effect: (__0 -- __array[oparg])
888-
inst(UNPACK_SEQUENCE_TWO_TUPLE) {
889-
PyObject *seq = TOP();
880+
inst(UNPACK_SEQUENCE_TWO_TUPLE, (unused/1, seq -- v1, v0)) {
890881
DEOPT_IF(!PyTuple_CheckExact(seq), UNPACK_SEQUENCE);
891882
DEOPT_IF(PyTuple_GET_SIZE(seq) != 2, UNPACK_SEQUENCE);
892883
STAT_INC(UNPACK_SEQUENCE, hit);
893-
SET_TOP(Py_NewRef(PyTuple_GET_ITEM(seq, 1)));
894-
PUSH(Py_NewRef(PyTuple_GET_ITEM(seq, 0)));
884+
v1 = Py_NewRef(PyTuple_GET_ITEM(seq, 1));
885+
v0 = Py_NewRef(PyTuple_GET_ITEM(seq, 0));
895886
Py_DECREF(seq);
896-
JUMPBY(INLINE_CACHE_ENTRIES_UNPACK_SEQUENCE);
897887
}
898888

899889
// stack effect: (__0 -- __array[oparg])
@@ -926,17 +916,12 @@ dummy_func(
926916
JUMPBY(INLINE_CACHE_ENTRIES_UNPACK_SEQUENCE);
927917
}
928918

929-
// error: UNPACK_EX has irregular stack effect
930-
inst(UNPACK_EX) {
919+
inst(UNPACK_EX, (seq -- unused[oparg & 0xFF], unused, unused[oparg >> 8])) {
931920
int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
932-
PyObject *seq = POP();
933-
PyObject **top = stack_pointer + totalargs;
934-
if (!unpack_iterable(tstate, seq, oparg & 0xFF, oparg >> 8, top)) {
935-
Py_DECREF(seq);
936-
goto error;
937-
}
938-
STACK_GROW(totalargs);
921+
PyObject **top = stack_pointer + totalargs - 1;
922+
int res = unpack_iterable(tstate, seq, oparg & 0xFF, oparg >> 8, top);
939923
Py_DECREF(seq);
924+
ERROR_IF(res == 0, error);
940925
}
941926

942927
family(store_attr, INLINE_CACHE_ENTRIES_STORE_ATTR) = {
@@ -2066,27 +2051,35 @@ dummy_func(
20662051
PREDICT(LOAD_CONST);
20672052
}
20682053

2069-
// stack effect: ( -- __0)
2070-
inst(FOR_ITER) {
2054+
// Most members of this family are "secretly" super-instructions.
2055+
// When the loop is exhausted, they jump, and the jump target is
2056+
// always END_FOR, which pops two values off the stack.
2057+
// This is optimized by skipping that instruction and combining
2058+
// its effect (popping 'iter' instead of pushing 'next'.)
2059+
2060+
family(for_iter, INLINE_CACHE_ENTRIES_FOR_ITER) = {
2061+
FOR_ITER,
2062+
FOR_ITER_LIST,
2063+
FOR_ITER_TUPLE,
2064+
FOR_ITER_RANGE,
2065+
FOR_ITER_GEN,
2066+
};
2067+
2068+
inst(FOR_ITER, (unused/1, iter -- iter, next)) {
20712069
#if ENABLE_SPECIALIZATION
20722070
_PyForIterCache *cache = (_PyForIterCache *)next_instr;
20732071
if (ADAPTIVE_COUNTER_IS_ZERO(cache->counter)) {
20742072
assert(cframe.use_tracing == 0);
20752073
next_instr--;
2076-
_Py_Specialize_ForIter(TOP(), next_instr, oparg);
2074+
_Py_Specialize_ForIter(iter, next_instr, oparg);
20772075
DISPATCH_SAME_OPARG();
20782076
}
20792077
STAT_INC(FOR_ITER, deferred);
20802078
DECREMENT_ADAPTIVE_COUNTER(cache->counter);
20812079
#endif /* ENABLE_SPECIALIZATION */
2082-
/* before: [iter]; after: [iter, iter()] *or* [] */
2083-
PyObject *iter = TOP();
2084-
PyObject *next = (*Py_TYPE(iter)->tp_iternext)(iter);
2085-
if (next != NULL) {
2086-
PUSH(next);
2087-
JUMPBY(INLINE_CACHE_ENTRIES_FOR_ITER);
2088-
}
2089-
else {
2080+
/* before: [iter]; after: [iter, iter()] *or* [] (and jump over END_FOR.) */
2081+
next = (*Py_TYPE(iter)->tp_iternext)(iter);
2082+
if (next == NULL) {
20902083
if (_PyErr_Occurred(tstate)) {
20912084
if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
20922085
goto error;
@@ -2098,70 +2091,74 @@ dummy_func(
20982091
}
20992092
/* iterator ended normally */
21002093
assert(_Py_OPCODE(next_instr[INLINE_CACHE_ENTRIES_FOR_ITER + oparg]) == END_FOR);
2101-
STACK_SHRINK(1);
21022094
Py_DECREF(iter);
2103-
/* Skip END_FOR */
2095+
STACK_SHRINK(1);
2096+
/* Jump forward oparg, then skip following END_FOR instruction */
21042097
JUMPBY(INLINE_CACHE_ENTRIES_FOR_ITER + oparg + 1);
2098+
DISPATCH();
21052099
}
2100+
// Common case: no jump, leave it to the code generator
21062101
}
21072102

2108-
// stack effect: ( -- __0)
2109-
inst(FOR_ITER_LIST) {
2103+
inst(FOR_ITER_LIST, (unused/1, iter -- iter, next)) {
21102104
assert(cframe.use_tracing == 0);
2111-
_PyListIterObject *it = (_PyListIterObject *)TOP();
2112-
DEOPT_IF(Py_TYPE(it) != &PyListIter_Type, FOR_ITER);
2105+
DEOPT_IF(Py_TYPE(iter) != &PyListIter_Type, FOR_ITER);
2106+
_PyListIterObject *it = (_PyListIterObject *)iter;
21132107
STAT_INC(FOR_ITER, hit);
21142108
PyListObject *seq = it->it_seq;
21152109
if (seq) {
21162110
if (it->it_index < PyList_GET_SIZE(seq)) {
2117-
PyObject *next = PyList_GET_ITEM(seq, it->it_index++);
2118-
PUSH(Py_NewRef(next));
2119-
JUMPBY(INLINE_CACHE_ENTRIES_FOR_ITER);
2111+
next = Py_NewRef(PyList_GET_ITEM(seq, it->it_index++));
21202112
goto end_for_iter_list; // End of this instruction
21212113
}
21222114
it->it_seq = NULL;
21232115
Py_DECREF(seq);
21242116
}
2117+
Py_DECREF(iter);
21252118
STACK_SHRINK(1);
2126-
Py_DECREF(it);
2119+
/* Jump forward oparg, then skip following END_FOR instruction */
21272120
JUMPBY(INLINE_CACHE_ENTRIES_FOR_ITER + oparg + 1);
2121+
DISPATCH();
21282122
end_for_iter_list:
2123+
// Common case: no jump, leave it to the code generator
21292124
}
21302125

2131-
// stack effect: ( -- __0)
2132-
inst(FOR_ITER_TUPLE) {
2126+
inst(FOR_ITER_TUPLE, (unused/1, iter -- iter, next)) {
21332127
assert(cframe.use_tracing == 0);
2134-
_PyTupleIterObject *it = (_PyTupleIterObject *)TOP();
2128+
_PyTupleIterObject *it = (_PyTupleIterObject *)iter;
21352129
DEOPT_IF(Py_TYPE(it) != &PyTupleIter_Type, FOR_ITER);
21362130
STAT_INC(FOR_ITER, hit);
21372131
PyTupleObject *seq = it->it_seq;
21382132
if (seq) {
21392133
if (it->it_index < PyTuple_GET_SIZE(seq)) {
2140-
PyObject *next = PyTuple_GET_ITEM(seq, it->it_index++);
2141-
PUSH(Py_NewRef(next));
2142-
JUMPBY(INLINE_CACHE_ENTRIES_FOR_ITER);
2134+
next = Py_NewRef(PyTuple_GET_ITEM(seq, it->it_index++));
21432135
goto end_for_iter_tuple; // End of this instruction
21442136
}
21452137
it->it_seq = NULL;
21462138
Py_DECREF(seq);
21472139
}
2140+
Py_DECREF(iter);
21482141
STACK_SHRINK(1);
2149-
Py_DECREF(it);
2142+
/* Jump forward oparg, then skip following END_FOR instruction */
21502143
JUMPBY(INLINE_CACHE_ENTRIES_FOR_ITER + oparg + 1);
2144+
DISPATCH();
21512145
end_for_iter_tuple:
2146+
// Common case: no jump, leave it to the code generator
21522147
}
21532148

2154-
// stack effect: ( -- __0)
2155-
inst(FOR_ITER_RANGE) {
2149+
// This is slightly different, when the loop isn't terminated we
2150+
// jump over the immediately following STORE_FAST instruction.
2151+
inst(FOR_ITER_RANGE, (unused/1, iter -- iter, unused)) {
21562152
assert(cframe.use_tracing == 0);
2157-
_PyRangeIterObject *r = (_PyRangeIterObject *)TOP();
2153+
_PyRangeIterObject *r = (_PyRangeIterObject *)iter;
21582154
DEOPT_IF(Py_TYPE(r) != &PyRangeIter_Type, FOR_ITER);
21592155
STAT_INC(FOR_ITER, hit);
21602156
_Py_CODEUNIT next = next_instr[INLINE_CACHE_ENTRIES_FOR_ITER];
21612157
assert(_PyOpcode_Deopt[_Py_OPCODE(next)] == STORE_FAST);
21622158
if (r->len <= 0) {
21632159
STACK_SHRINK(1);
21642160
Py_DECREF(r);
2161+
// Jump over END_FOR instruction.
21652162
JUMPBY(INLINE_CACHE_ENTRIES_FOR_ITER + oparg + 1);
21662163
}
21672164
else {
@@ -2174,11 +2171,13 @@ dummy_func(
21742171
// The STORE_FAST is already done.
21752172
JUMPBY(INLINE_CACHE_ENTRIES_FOR_ITER + 1);
21762173
}
2174+
DISPATCH();
21772175
}
21782176

2179-
inst(FOR_ITER_GEN) {
2177+
// This is *not* a super-instruction, unique in the family.
2178+
inst(FOR_ITER_GEN, (unused/1, iter -- iter, unused)) {
21802179
assert(cframe.use_tracing == 0);
2181-
PyGenObject *gen = (PyGenObject *)TOP();
2180+
PyGenObject *gen = (PyGenObject *)iter;
21822181
DEOPT_IF(Py_TYPE(gen) != &PyGen_Type, FOR_ITER);
21832182
DEOPT_IF(gen->gi_frame_state >= FRAME_EXECUTING, FOR_ITER);
21842183
STAT_INC(FOR_ITER, hit);
@@ -3168,9 +3167,6 @@ family(call, INLINE_CACHE_ENTRIES_CALL) = {
31683167
CALL_NO_KW_LIST_APPEND, CALL_NO_KW_METHOD_DESCRIPTOR_FAST, CALL_NO_KW_METHOD_DESCRIPTOR_NOARGS,
31693168
CALL_NO_KW_METHOD_DESCRIPTOR_O, CALL_NO_KW_STR_1, CALL_NO_KW_TUPLE_1,
31703169
CALL_NO_KW_TYPE_1 };
3171-
family(for_iter, INLINE_CACHE_ENTRIES_FOR_ITER) = {
3172-
FOR_ITER, FOR_ITER_LIST,
3173-
FOR_ITER_RANGE };
31743170
family(store_fast) = { STORE_FAST, STORE_FAST__LOAD_FAST, STORE_FAST__STORE_FAST };
31753171
family(unpack_sequence, INLINE_CACHE_ENTRIES_UNPACK_SEQUENCE) = {
31763172
UNPACK_SEQUENCE, UNPACK_SEQUENCE_LIST,

0 commit comments

Comments
 (0)