Skip to content

Commit 7c722c2

Browse files
authored
Merge pull request RustPython#5174 from Blues-star/code
Update code.py and test_code.py from CPython v3.12
2 parents d297e73 + 572053d commit 7c722c2

File tree

2 files changed

+64
-6
lines changed

2 files changed

+64
-6
lines changed

Lib/code.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ def showsyntaxerror(self, filename=None):
106106
107107
"""
108108
type, value, tb = sys.exc_info()
109+
sys.last_exc = value
109110
sys.last_type = type
110111
sys.last_value = value
111112
sys.last_traceback = tb
@@ -119,7 +120,7 @@ def showsyntaxerror(self, filename=None):
119120
else:
120121
# Stuff in the right filename
121122
value = SyntaxError(msg, (filename, lineno, offset, line))
122-
sys.last_value = value
123+
sys.last_exc = sys.last_value = value
123124
if sys.excepthook is sys.__excepthook__:
124125
lines = traceback.format_exception_only(type, value)
125126
self.write(''.join(lines))
@@ -138,6 +139,7 @@ def showtraceback(self):
138139
"""
139140
sys.last_type, sys.last_value, last_tb = ei = sys.exc_info()
140141
sys.last_traceback = last_tb
142+
sys.last_exc = ei[1]
141143
try:
142144
lines = traceback.format_exception(ei[0], ei[1], last_tb.tb_next)
143145
if sys.excepthook is sys.__excepthook__:

Lib/test/test_code.py

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@
150150
gc_collect)
151151
from test.support.script_helper import assert_python_ok
152152
from test.support import threading_helper
153-
from opcode import opmap
153+
from opcode import opmap, opname
154154
COPY_FREE_VARS = opmap['COPY_FREE_VARS']
155155

156156

@@ -357,6 +357,32 @@ def func():
357357
new_code = code = func.__code__.replace(co_linetable=b'')
358358
self.assertEqual(list(new_code.co_lines()), [])
359359

360+
# TODO: RUSTPYTHON
361+
@unittest.expectedFailure
362+
def test_co_lnotab_is_deprecated(self): # TODO: remove in 3.14
363+
def func():
364+
pass
365+
366+
with self.assertWarns(DeprecationWarning):
367+
func.__code__.co_lnotab
368+
369+
# TODO: RUSTPYTHON
370+
@unittest.expectedFailure
371+
def test_invalid_bytecode(self):
372+
def foo():
373+
pass
374+
375+
# assert that opcode 229 is invalid
376+
self.assertEqual(opname[229], '<229>')
377+
378+
# change first opcode to 0xeb (=229)
379+
foo.__code__ = foo.__code__.replace(
380+
co_code=b'\xe5' + foo.__code__.co_code[1:])
381+
382+
msg = "unknown opcode 229"
383+
with self.assertRaisesRegex(SystemError, msg):
384+
foo()
385+
360386
# TODO: RUSTPYTHON
361387
@unittest.expectedFailure
362388
# @requires_debug_ranges()
@@ -479,6 +505,34 @@ def f():
479505
self.assertNotEqual(code_b, code_d)
480506
self.assertNotEqual(code_c, code_d)
481507

508+
def test_code_hash_uses_firstlineno(self):
509+
c1 = (lambda: 1).__code__
510+
c2 = (lambda: 1).__code__
511+
self.assertNotEqual(c1, c2)
512+
self.assertNotEqual(hash(c1), hash(c2))
513+
c3 = c1.replace(co_firstlineno=17)
514+
self.assertNotEqual(c1, c3)
515+
self.assertNotEqual(hash(c1), hash(c3))
516+
517+
def test_code_hash_uses_order(self):
518+
# Swapping posonlyargcount and kwonlyargcount should change the hash.
519+
c = (lambda x, y, *, z=1, w=1: 1).__code__
520+
self.assertEqual(c.co_argcount, 2)
521+
self.assertEqual(c.co_posonlyargcount, 0)
522+
self.assertEqual(c.co_kwonlyargcount, 2)
523+
swapped = c.replace(co_posonlyargcount=2, co_kwonlyargcount=0)
524+
self.assertNotEqual(c, swapped)
525+
self.assertNotEqual(hash(c), hash(swapped))
526+
527+
# TODO: RUSTPYTHON
528+
@unittest.expectedFailure
529+
def test_code_hash_uses_bytecode(self):
530+
c = (lambda x, y: x + y).__code__
531+
d = (lambda x, y: x * y).__code__
532+
c1 = c.replace(co_code=d.co_code)
533+
self.assertNotEqual(c, c1)
534+
self.assertNotEqual(hash(c), hash(c1))
535+
482536

483537
def isinterned(s):
484538
return s is sys.intern(('_' + s + '_')[1:-1])
@@ -692,7 +746,8 @@ def test_positions(self):
692746

693747
def check_lines(self, func):
694748
co = func.__code__
695-
lines1 = list(dedup(l for (_, _, l) in co.co_lines()))
749+
lines1 = [line for _, _, line in co.co_lines()]
750+
self.assertEqual(lines1, list(dedup(lines1)))
696751
lines2 = list(lines_from_postions(positions_from_location_table(co)))
697752
for l1, l2 in zip(lines1, lines2):
698753
self.assertEqual(l1, l2)
@@ -714,6 +769,7 @@ def f():
714769
pass
715770
PY_CODE_LOCATION_INFO_NO_COLUMNS = 13
716771
f.__code__ = f.__code__.replace(
772+
co_stacksize=1,
717773
co_firstlineno=42,
718774
co_code=bytes(
719775
[
@@ -742,15 +798,15 @@ def f():
742798
py = ctypes.pythonapi
743799
freefunc = ctypes.CFUNCTYPE(None,ctypes.c_voidp)
744800

745-
RequestCodeExtraIndex = py._PyEval_RequestCodeExtraIndex
801+
RequestCodeExtraIndex = py.PyUnstable_Eval_RequestCodeExtraIndex
746802
RequestCodeExtraIndex.argtypes = (freefunc,)
747803
RequestCodeExtraIndex.restype = ctypes.c_ssize_t
748804

749-
SetExtra = py._PyCode_SetExtra
805+
SetExtra = py.PyUnstable_Code_SetExtra
750806
SetExtra.argtypes = (ctypes.py_object, ctypes.c_ssize_t, ctypes.c_voidp)
751807
SetExtra.restype = ctypes.c_int
752808

753-
GetExtra = py._PyCode_GetExtra
809+
GetExtra = py.PyUnstable_Code_GetExtra
754810
GetExtra.argtypes = (ctypes.py_object, ctypes.c_ssize_t,
755811
ctypes.POINTER(ctypes.c_voidp))
756812
GetExtra.restype = ctypes.c_int

0 commit comments

Comments
 (0)