Skip to content

Commit 10a7144

Browse files
committed
Merge branch 'main' of https://github.com/python/cpython into add-bytecode-specialization-guide
2 parents a023a52 + b6769e9 commit 10a7144

File tree

10 files changed

+113
-5
lines changed

10 files changed

+113
-5
lines changed

Doc/whatsnew/3.14.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,10 @@ pdb
785785
(if any).
786786
(Contributed by Tian Gao in :gh:`130493`.)
787787

788+
* ``<tab>`` at the beginning of the line in :mod:`pdb` multi-line input will
789+
fill in a 4-space indentation now, instead of inserting a ``\t`` character.
790+
(Contributed by Tian Gao in :gh:`130471`.)
791+
788792

789793
pickle
790794
------

Lib/pdb.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -652,10 +652,10 @@ def displayhook(self, obj):
652652
self.message(repr(obj))
653653

654654
@contextmanager
655-
def _disable_command_completion(self):
655+
def _enable_multiline_completion(self):
656656
completenames = self.completenames
657657
try:
658-
self.completenames = self.completedefault
658+
self.completenames = self.complete_multiline_names
659659
yield
660660
finally:
661661
self.completenames = completenames
@@ -753,7 +753,7 @@ def default(self, line):
753753
buffer = line
754754
if (code := codeop.compile_command(line + '\n', '<stdin>', 'single')) is None:
755755
# Multi-line mode
756-
with self._disable_command_completion():
756+
with self._enable_multiline_completion():
757757
buffer = line
758758
continue_prompt = "... "
759759
while (code := codeop.compile_command(buffer, '<stdin>', 'single')) is None:
@@ -996,6 +996,21 @@ def _complete_expression(self, text, line, begidx, endidx):
996996
# Complete a simple name.
997997
return [n for n in ns.keys() if n.startswith(text)]
998998

999+
def _complete_indentation(self, text, line, begidx, endidx):
1000+
try:
1001+
import readline
1002+
except ImportError:
1003+
return []
1004+
# Fill in spaces to form a 4-space indent
1005+
return [' ' * (4 - readline.get_begidx() % 4)]
1006+
1007+
def complete_multiline_names(self, text, line, begidx, endidx):
1008+
# If text is space-only, the user entered <tab> before any text.
1009+
# That normally means they want to indent the current line.
1010+
if not text.strip():
1011+
return self._complete_indentation(text, line, begidx, endidx)
1012+
return self.completedefault(text, line, begidx, endidx)
1013+
9991014
def completedefault(self, text, line, begidx, endidx):
10001015
if text.startswith("$"):
10011016
# Complete convenience variables

Lib/test/test_capi/test_opt.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1499,6 +1499,37 @@ def f(n):
14991499
# But all of the appends we care about are still there:
15001500
self.assertEqual(uops.count("_CALL_LIST_APPEND"), len("ABCDEFG"))
15011501

1502+
def test_narrow_type_to_constant_int_zero(self):
1503+
def f(n):
1504+
trace = []
1505+
for i in range(n):
1506+
# zero is always (int) 0, but we can only prove that it's a integer:
1507+
false = i == TIER2_THRESHOLD # this will always be false, while hopefully still fooling optimizer improvements
1508+
zero = false + 0 # this should always set the variable zero equal to 0
1509+
trace.append("A")
1510+
if not zero: # Kept.
1511+
trace.append("B")
1512+
if not zero: # Removed!
1513+
trace.append("C")
1514+
trace.append("D")
1515+
if zero: # Removed!
1516+
trace.append("X")
1517+
trace.append("E")
1518+
trace.append("F")
1519+
if zero: # Removed!
1520+
trace.append("X")
1521+
trace.append("G")
1522+
return trace
1523+
1524+
trace, ex = self._run_with_optimizer(f, TIER2_THRESHOLD)
1525+
self.assertEqual(trace, list("ABCDEFG") * TIER2_THRESHOLD)
1526+
self.assertIsNotNone(ex)
1527+
uops = get_opnames(ex)
1528+
# Only one guard remains:
1529+
self.assertEqual(uops.count("_GUARD_IS_FALSE_POP"), 1)
1530+
self.assertEqual(uops.count("_GUARD_IS_TRUE_POP"), 0)
1531+
# But all of the appends we care about are still there:
1532+
self.assertEqual(uops.count("_CALL_LIST_APPEND"), len("ABCDEFG"))
15021533

15031534
def global_identity(x):
15041535
return x

Lib/test/test_pdb.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4342,6 +4342,32 @@ def test_quit(self):
43424342
# The quit prompt should be printed exactly twice
43434343
self.assertEqual(stdout.count("Quit anyway"), 2)
43444344

4345+
def test_quit_after_interact(self):
4346+
"""
4347+
interact command will set sys.ps1 temporarily, we need to make sure
4348+
that it's restored and pdb does not believe it's in interactive mode
4349+
after interact is done.
4350+
"""
4351+
script = """
4352+
x = 1
4353+
breakpoint()
4354+
"""
4355+
4356+
commands = """
4357+
interact
4358+
quit()
4359+
q
4360+
y
4361+
"""
4362+
4363+
stdout, stderr = self._run_script(script, commands)
4364+
# Normal exit should not print anything to stderr
4365+
self.assertEqual(stderr, "")
4366+
# The quit prompt should be printed exactly once
4367+
self.assertEqual(stdout.count("Quit anyway"), 1)
4368+
# BdbQuit should not be printed
4369+
self.assertNotIn("BdbQuit", stdout)
4370+
43454371
def test_set_trace_with_skip(self):
43464372
"""GH-82897
43474373
Inline set_trace() should break unconditionally. This example is a
@@ -4487,6 +4513,32 @@ def test_multiline_completion(self):
44874513

44884514
self.assertIn(b'42', output)
44894515

4516+
def test_multiline_indent_completion(self):
4517+
script = textwrap.dedent("""
4518+
import pdb; pdb.Pdb().set_trace()
4519+
""")
4520+
4521+
# \t should always complete a 4-space indent
4522+
# This piece of code will raise an IndentationError or a SyntaxError
4523+
# if the completion is not working as expected
4524+
input = textwrap.dedent("""\
4525+
def func():
4526+
\ta = 1
4527+
\ta += 1
4528+
\ta += 1
4529+
\tif a > 0:
4530+
a += 1
4531+
\t\treturn a
4532+
4533+
func()
4534+
c
4535+
""").encode()
4536+
4537+
output = run_pty(script, input)
4538+
4539+
self.assertIn(b'4', output)
4540+
self.assertNotIn(b'Error', output)
4541+
44904542

44914543
def load_tests(loader, tests, pattern):
44924544
from test import test_pdb

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,6 +1061,7 @@ Detlef Lannert
10611061
Rémi Lapeyre
10621062
Soren Larsen
10631063
Amos Latteier
1064+
Keenan Lau
10641065
Piers Lauder
10651066
Ben Laurie
10661067
Yoni Lavi
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improve JIT understanding of integers in boolean context.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
``<tab>`` at the beginning of the line in :mod:`pdb` multi-line input will fill in a 4-space indentation now, instead of inserting a ``\t`` character.

Python/optimizer_bytecodes.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ dummy_func(void) {
406406
op(_TO_BOOL_INT, (value -- res)) {
407407
if (!optimize_to_bool(this_instr, ctx, value, &res)) {
408408
sym_set_type(value, &PyLong_Type);
409-
res = sym_new_type(ctx, &PyBool_Type);
409+
res = sym_new_truthiness(ctx, value, true);
410410
}
411411
}
412412

Python/optimizer_cases.c.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/optimizer_symbols.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,9 @@ _Py_uop_sym_set_const(JitOptContext *ctx, JitOptSymbol *sym, PyObject *const_val
299299
else if (type == &PyBool_Type) {
300300
_Py_uop_sym_set_const(ctx, value, Py_False);
301301
}
302+
else if (type == &PyLong_Type) {
303+
_Py_uop_sym_set_const(ctx, value, Py_GetConstant(Py_CONSTANT_ZERO));
304+
}
302305
// TODO: More types (GH-130415)!
303306
make_const(sym, const_val);
304307
return;

0 commit comments

Comments
 (0)