Skip to content

Commit be4c122

Browse files
authored
Merge pull request #528 from python/main
Sync Fork from Upstream Repo
2 parents e7747ec + 919ad53 commit be4c122

File tree

10 files changed

+4475
-4346
lines changed

10 files changed

+4475
-4346
lines changed

Lib/test/test_dis.py

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,7 +1092,7 @@ def _prepare_test_cases():
10921092
Instruction(opname='PUSH_EXC_INFO', opcode=35, arg=None, argval=None, argrepr='', offset=116, starts_line=None, is_jump_target=False, positions=None),
10931093
Instruction(opname='DUP_TOP', opcode=4, arg=None, argval=None, argrepr='', offset=118, starts_line=22, is_jump_target=False, positions=None),
10941094
Instruction(opname='LOAD_GLOBAL', opcode=116, arg=2, argval='ZeroDivisionError', argrepr='ZeroDivisionError', offset=120, starts_line=None, is_jump_target=False, positions=None),
1095-
Instruction(opname='JUMP_IF_NOT_EXC_MATCH', opcode=121, arg=109, argval=218, argrepr='to 218', offset=122, starts_line=None, is_jump_target=False, positions=None),
1095+
Instruction(opname='JUMP_IF_NOT_EXC_MATCH', opcode=121, arg=124, argval=248, argrepr='to 248', offset=122, starts_line=None, is_jump_target=False, positions=None),
10961096
Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=124, starts_line=None, is_jump_target=False, positions=None),
10971097
Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=126, starts_line=None, is_jump_target=False, positions=None),
10981098
Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=128, starts_line=None, is_jump_target=False, positions=None),
@@ -1101,7 +1101,7 @@ def _prepare_test_cases():
11011101
Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=134, starts_line=None, is_jump_target=False, positions=None),
11021102
Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=136, starts_line=None, is_jump_target=False, positions=None),
11031103
Instruction(opname='POP_EXCEPT', opcode=89, arg=None, argval=None, argrepr='', offset=138, starts_line=None, is_jump_target=False, positions=None),
1104-
Instruction(opname='JUMP_FORWARD', opcode=110, arg=25, argval=192, argrepr='to 192', offset=140, starts_line=None, is_jump_target=False, positions=None),
1104+
Instruction(opname='JUMP_FORWARD', opcode=110, arg=46, argval=234, argrepr='to 234', offset=140, starts_line=None, is_jump_target=False, positions=None),
11051105
Instruction(opname='POP_EXCEPT_AND_RERAISE', opcode=37, arg=None, argval=None, argrepr='', offset=142, starts_line=None, is_jump_target=False, positions=None),
11061106
Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=144, starts_line=25, is_jump_target=True, positions=None),
11071107
Instruction(opname='BEFORE_WITH', opcode=53, arg=None, argval=None, argrepr='', offset=146, starts_line=None, is_jump_target=False, positions=None),
@@ -1115,7 +1115,7 @@ def _prepare_test_cases():
11151115
Instruction(opname='DUP_TOP', opcode=4, arg=None, argval=None, argrepr='', offset=162, starts_line=None, is_jump_target=False, positions=None),
11161116
Instruction(opname='CALL_FUNCTION', opcode=131, arg=3, argval=3, argrepr='', offset=164, starts_line=None, is_jump_target=False, positions=None),
11171117
Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=166, starts_line=None, is_jump_target=False, positions=None),
1118-
Instruction(opname='JUMP_FORWARD', opcode=110, arg=11, argval=192, argrepr='to 192', offset=168, starts_line=None, is_jump_target=False, positions=None),
1118+
Instruction(opname='JUMP_FORWARD', opcode=110, arg=25, argval=220, argrepr='to 220', offset=168, starts_line=None, is_jump_target=False, positions=None),
11191119
Instruction(opname='PUSH_EXC_INFO', opcode=35, arg=None, argval=None, argrepr='', offset=170, starts_line=None, is_jump_target=False, positions=None),
11201120
Instruction(opname='WITH_EXCEPT_START', opcode=49, arg=None, argval=None, argrepr='', offset=172, starts_line=None, is_jump_target=False, positions=None),
11211121
Instruction(opname='POP_JUMP_IF_TRUE', opcode=115, arg=90, argval=180, argrepr='to 180', offset=174, starts_line=None, is_jump_target=False, positions=None),
@@ -1127,20 +1127,35 @@ def _prepare_test_cases():
11271127
Instruction(opname='POP_EXCEPT', opcode=89, arg=None, argval=None, argrepr='', offset=186, starts_line=None, is_jump_target=False, positions=None),
11281128
Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=188, starts_line=None, is_jump_target=False, positions=None),
11291129
Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=190, starts_line=None, is_jump_target=False, positions=None),
1130-
Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=192, starts_line=28, is_jump_target=True, positions=None),
1131-
Instruction(opname='LOAD_CONST', opcode=100, arg=10, argval="OK, now we're done", argrepr='"OK, now we\'re done"', offset=194, starts_line=None, is_jump_target=False, positions=None),
1132-
Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=196, starts_line=None, is_jump_target=False, positions=None),
1133-
Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=198, starts_line=None, is_jump_target=False, positions=None),
1134-
Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=200, starts_line=None, is_jump_target=False, positions=None),
1135-
Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=202, starts_line=None, is_jump_target=False, positions=None),
1136-
Instruction(opname='PUSH_EXC_INFO', opcode=35, arg=None, argval=None, argrepr='', offset=204, starts_line=None, is_jump_target=False, positions=None),
1137-
Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=206, starts_line=None, is_jump_target=False, positions=None),
1138-
Instruction(opname='LOAD_CONST', opcode=100, arg=10, argval="OK, now we're done", argrepr='"OK, now we\'re done"', offset=208, starts_line=None, is_jump_target=False, positions=None),
1139-
Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=210, starts_line=None, is_jump_target=False, positions=None),
1140-
Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=212, starts_line=None, is_jump_target=False, positions=None),
1141-
Instruction(opname='RERAISE', opcode=119, arg=0, argval=0, argrepr='', offset=214, starts_line=None, is_jump_target=False, positions=None),
1142-
Instruction(opname='POP_EXCEPT_AND_RERAISE', opcode=37, arg=None, argval=None, argrepr='', offset=216, starts_line=None, is_jump_target=False, positions=None),
1143-
Instruction(opname='RERAISE', opcode=119, arg=0, argval=0, argrepr='', offset=218, starts_line=22, is_jump_target=True, positions=None),
1130+
Instruction(opname='NOP', opcode=9, arg=None, argval=None, argrepr='', offset=192, starts_line=None, is_jump_target=False, positions=None),
1131+
Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=194, starts_line=28, is_jump_target=False, positions=None),
1132+
Instruction(opname='LOAD_CONST', opcode=100, arg=10, argval="OK, now we're done", argrepr='"OK, now we\'re done"', offset=196, starts_line=None, is_jump_target=False, positions=None),
1133+
Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=198, starts_line=None, is_jump_target=False, positions=None),
1134+
Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=200, starts_line=None, is_jump_target=False, positions=None),
1135+
Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=202, starts_line=None, is_jump_target=False, positions=None),
1136+
Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=204, starts_line=None, is_jump_target=False, positions=None),
1137+
Instruction(opname='PUSH_EXC_INFO', opcode=35, arg=None, argval=None, argrepr='', offset=206, starts_line=None, is_jump_target=False, positions=None),
1138+
Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=208, starts_line=None, is_jump_target=False, positions=None),
1139+
Instruction(opname='LOAD_CONST', opcode=100, arg=10, argval="OK, now we're done", argrepr='"OK, now we\'re done"', offset=210, starts_line=None, is_jump_target=False, positions=None),
1140+
Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=212, starts_line=None, is_jump_target=False, positions=None),
1141+
Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=214, starts_line=None, is_jump_target=False, positions=None),
1142+
Instruction(opname='RERAISE', opcode=119, arg=0, argval=0, argrepr='', offset=216, starts_line=None, is_jump_target=False, positions=None),
1143+
Instruction(opname='POP_EXCEPT_AND_RERAISE', opcode=37, arg=None, argval=None, argrepr='', offset=218, starts_line=None, is_jump_target=False, positions=None),
1144+
Instruction(opname='NOP', opcode=9, arg=None, argval=None, argrepr='', offset=220, starts_line=25, is_jump_target=True, positions=None),
1145+
Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=222, starts_line=28, is_jump_target=False, positions=None),
1146+
Instruction(opname='LOAD_CONST', opcode=100, arg=10, argval="OK, now we're done", argrepr='"OK, now we\'re done"', offset=224, starts_line=None, is_jump_target=False, positions=None),
1147+
Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=226, starts_line=None, is_jump_target=False, positions=None),
1148+
Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=228, starts_line=None, is_jump_target=False, positions=None),
1149+
Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=230, starts_line=None, is_jump_target=False, positions=None),
1150+
Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=232, starts_line=None, is_jump_target=False, positions=None),
1151+
Instruction(opname='NOP', opcode=9, arg=None, argval=None, argrepr='', offset=234, starts_line=23, is_jump_target=True, positions=None),
1152+
Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=236, starts_line=28, is_jump_target=False, positions=None),
1153+
Instruction(opname='LOAD_CONST', opcode=100, arg=10, argval="OK, now we're done", argrepr='"OK, now we\'re done"', offset=238, starts_line=None, is_jump_target=False, positions=None),
1154+
Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=240, starts_line=None, is_jump_target=False, positions=None),
1155+
Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=242, starts_line=None, is_jump_target=False, positions=None),
1156+
Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=244, starts_line=None, is_jump_target=False, positions=None),
1157+
Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=246, starts_line=None, is_jump_target=False, positions=None),
1158+
Instruction(opname='RERAISE', opcode=119, arg=0, argval=0, argrepr='', offset=248, starts_line=22, is_jump_target=True, positions=None),
11441159
]
11451160

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

Lib/test/test_httpservers.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -593,9 +593,18 @@ def test_html_escape_filename(self):
593593
#!%s
594594
import os
595595
596-
print("Content-type: text/plain")
596+
print("X-ambv: was here")
597+
print("Content-type: text/html")
597598
print()
598-
print(repr(os.environ))
599+
print("<pre>")
600+
for k, v in os.environ.items():
601+
try:
602+
k.encode('ascii')
603+
v.encode('ascii')
604+
except UnicodeEncodeError:
605+
continue # see: BPO-44647
606+
print(f"{k}={v}")
607+
print("</pre>")
599608
"""
600609

601610

@@ -850,8 +859,8 @@ def test_accept(self):
850859
with self.subTest(headers):
851860
res = self.request('/cgi-bin/file6.py', 'GET', headers=headers)
852861
self.assertEqual(http.HTTPStatus.OK, res.status)
853-
expected = f"'HTTP_ACCEPT': {expected!r}"
854-
self.assertIn(expected.encode('ascii'), res.read())
862+
expected = f"HTTP_ACCEPT={expected}".encode('ascii')
863+
self.assertIn(expected, res.read())
855864

856865

857866
class SocketlessRequestHandler(SimpleHTTPRequestHandler):

Lib/test/test_sys_settrace.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -986,14 +986,29 @@ def func():
986986
except Exception:
987987
pass
988988

989-
# This doesn't conform to PEP 626
990989
self.run_and_compare(func,
991990
[(0, 'call'),
992991
(1, 'line'),
993992
(2, 'line'),
994993
(3, 'line'),
995-
(5, 'line'),
996-
(5, 'return')])
994+
(3, 'return')])
995+
996+
def test_if_in_if_in_if(self):
997+
def func(a=0, p=1, z=1):
998+
if p:
999+
if a:
1000+
if z:
1001+
pass
1002+
else:
1003+
pass
1004+
else:
1005+
pass
1006+
1007+
self.run_and_compare(func,
1008+
[(0, 'call'),
1009+
(1, 'line'),
1010+
(2, 'line'),
1011+
(2, 'return')])
9971012

9981013
def test_early_exit_with(self):
9991014

Lib/test/test_traceback.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,44 @@ def test_traceback_specialization_with_syntax_error(self):
485485
)
486486
self.assertEqual(result_lines, expected_error.splitlines())
487487

488+
def assertSpecialized(self, func, expected_specialization):
489+
result_lines = self.get_exception(func)
490+
specialization_line = result_lines[-1]
491+
self.assertEqual(specialization_line.lstrip(), expected_specialization)
492+
493+
def test_specialization_variations(self):
494+
self.assertSpecialized(lambda: 1/0,
495+
"~^~")
496+
self.assertSpecialized(lambda: 1/0/3,
497+
"~^~")
498+
self.assertSpecialized(lambda: 1 / 0,
499+
"~~^~~")
500+
self.assertSpecialized(lambda: 1 / 0 / 3,
501+
"~~^~~")
502+
self.assertSpecialized(lambda: 1/ 0,
503+
"~^~~")
504+
self.assertSpecialized(lambda: 1/ 0/3,
505+
"~^~~")
506+
self.assertSpecialized(lambda: 1 / 0,
507+
"~~~~~^~~~")
508+
self.assertSpecialized(lambda: 1 / 0 / 5,
509+
"~~~~~^~~~")
510+
self.assertSpecialized(lambda: 1 /0,
511+
"~~^~")
512+
self.assertSpecialized(lambda: 1//0,
513+
"~^^~")
514+
self.assertSpecialized(lambda: 1//0//4,
515+
"~^^~")
516+
self.assertSpecialized(lambda: 1 // 0,
517+
"~~^^~~")
518+
self.assertSpecialized(lambda: 1 // 0 // 4,
519+
"~~^^~~")
520+
self.assertSpecialized(lambda: 1 //0,
521+
"~~^^~")
522+
self.assertSpecialized(lambda: 1// 0,
523+
"~^^~~")
524+
525+
488526
@cpython_only
489527
@requires_debug_ranges()
490528
class CPythonTracebackErrorCaretTests(TracebackErrorLocationCaretTests):

Lib/traceback.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ def format(self):
496496

497497
try:
498498
anchors = _extract_caret_anchors_from_line_segment(
499-
frame._original_line[colno - 1:end_colno]
499+
frame._original_line[colno - 1:end_colno - 1]
500500
)
501501
except Exception:
502502
anchors = None

Objects/typeobject.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1368,14 +1368,22 @@ subtype_dealloc(PyObject *self)
13681368
/* Extract the type again; tp_del may have changed it */
13691369
type = Py_TYPE(self);
13701370

1371+
// Don't read type memory after calling basedealloc() since basedealloc()
1372+
// can deallocate the type and free its memory.
1373+
int type_needs_decref = (type->tp_flags & Py_TPFLAGS_HEAPTYPE
1374+
&& !(base->tp_flags & Py_TPFLAGS_HEAPTYPE));
1375+
13711376
/* Call the base tp_dealloc() */
13721377
assert(basedealloc);
13731378
basedealloc(self);
13741379

1375-
/* Only decref if the base type is not already a heap allocated type.
1376-
Otherwise, basedealloc should have decref'd it already */
1377-
if (type->tp_flags & Py_TPFLAGS_HEAPTYPE && !(base->tp_flags & Py_TPFLAGS_HEAPTYPE))
1380+
/* Can't reference self beyond this point. It's possible tp_del switched
1381+
our type from a HEAPTYPE to a non-HEAPTYPE, so be careful about
1382+
reference counting. Only decref if the base type is not already a heap
1383+
allocated type. Otherwise, basedealloc should have decref'd it already */
1384+
if (type_needs_decref) {
13781385
Py_DECREF(type);
1386+
}
13791387

13801388
/* Done */
13811389
return;

Python/compile.c

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7521,10 +7521,11 @@ makecode(struct compiler *c, struct assembler *a, PyObject *constslist,
75217521
/* For debugging purposes only */
75227522
#if 0
75237523
static void
7524-
dump_instr(const struct instr *i)
7524+
dump_instr(struct instr *i)
75257525
{
7526-
const char *jrel = (is_relative_jump(instr)) ? "jrel " : "";
7527-
const char *jabs = (is_jump(instr) && !is_relative_jump(instr))? "jabs " : "";
7526+
const char *jrel = (is_relative_jump(i)) ? "jrel " : "";
7527+
const char *jabs = (is_jump(i) && !is_relative_jump(i))? "jabs " : "";
7528+
75287529
char arg[128];
75297530

75307531
*arg = '\0';
@@ -7561,6 +7562,9 @@ optimize_cfg(struct compiler *c, struct assembler *a, PyObject *consts);
75617562
static int
75627563
ensure_exits_have_lineno(struct compiler *c);
75637564

7565+
static int
7566+
extend_block(basicblock *bb);
7567+
75647568
static int *
75657569
build_cellfixedoffsets(struct compiler *c)
75667570
{
@@ -7783,6 +7787,12 @@ assemble(struct compiler *c, int addNone)
77837787
}
77847788
}
77857789

7790+
for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
7791+
if (extend_block(b)) {
7792+
return NULL;
7793+
}
7794+
}
7795+
77867796
if (ensure_exits_have_lineno(c)) {
77877797
return NULL;
77887798
}
@@ -7839,6 +7849,7 @@ assemble(struct compiler *c, int addNone)
78397849
if (consts == NULL) {
78407850
goto error;
78417851
}
7852+
78427853
if (optimize_cfg(c, &a, consts)) {
78437854
goto error;
78447855
}
@@ -8246,19 +8257,14 @@ optimize_basic_block(struct compiler *c, basicblock *bb, PyObject *consts)
82468257
goto error;
82478258
}
82488259
break;
8249-
default:
8250-
if (inst->i_target->b_exit && inst->i_target->b_iused <= MAX_COPY_SIZE) {
8251-
basicblock *to_copy = inst->i_target;
8252-
inst->i_opcode = NOP;
8253-
for (i = 0; i < to_copy->b_iused; i++) {
8254-
int index = compiler_next_instr(bb);
8255-
if (index < 0) {
8256-
return -1;
8257-
}
8258-
bb->b_instr[index] = to_copy->b_instr[i];
8259-
}
8260-
bb->b_exit = 1;
8261-
}
8260+
}
8261+
break;
8262+
case FOR_ITER:
8263+
assert (i == bb->b_iused-1);
8264+
if (target->i_opcode == JUMP_FORWARD) {
8265+
if (eliminate_jump_to_jump(bb, inst->i_opcode)) {
8266+
goto error;
8267+
}
82628268
}
82638269
break;
82648270
case ROT_N:
@@ -8288,6 +8294,32 @@ optimize_basic_block(struct compiler *c, basicblock *bb, PyObject *consts)
82888294
return -1;
82898295
}
82908296

8297+
/* If this block ends with an unconditional jump to an exit block,
8298+
* then remove the jump and extend this block with the target.
8299+
*/
8300+
static int
8301+
extend_block(basicblock *bb) {
8302+
if (bb->b_iused == 0) {
8303+
return 0;
8304+
}
8305+
struct instr *last = &bb->b_instr[bb->b_iused-1];
8306+
if (last->i_opcode != JUMP_ABSOLUTE && last->i_opcode != JUMP_FORWARD) {
8307+
return 0;
8308+
}
8309+
if (last->i_target->b_exit && last->i_target->b_iused <= MAX_COPY_SIZE) {
8310+
basicblock *to_copy = last->i_target;
8311+
last->i_opcode = NOP;
8312+
for (int i = 0; i < to_copy->b_iused; i++) {
8313+
int index = compiler_next_instr(bb);
8314+
if (index < 0) {
8315+
return -1;
8316+
}
8317+
bb->b_instr[index] = to_copy->b_instr[i];
8318+
}
8319+
bb->b_exit = 1;
8320+
}
8321+
return 0;
8322+
}
82918323

82928324
static void
82938325
clean_basic_block(basicblock *bb) {

0 commit comments

Comments
 (0)