Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Lib/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ def showsyntaxerror(self, filename=None):

"""
type, value, tb = sys.exc_info()
sys.last_exc = value
sys.last_type = type
sys.last_value = value
sys.last_traceback = tb
Expand All @@ -119,7 +120,7 @@ def showsyntaxerror(self, filename=None):
else:
# Stuff in the right filename
value = SyntaxError(msg, (filename, lineno, offset, line))
sys.last_value = value
sys.last_exc = sys.last_value = value
if sys.excepthook is sys.__excepthook__:
lines = traceback.format_exception_only(type, value)
self.write(''.join(lines))
Expand All @@ -138,6 +139,7 @@ def showtraceback(self):
"""
sys.last_type, sys.last_value, last_tb = ei = sys.exc_info()
sys.last_traceback = last_tb
sys.last_exc = ei[1]
try:
lines = traceback.format_exception(ei[0], ei[1], last_tb.tb_next)
if sys.excepthook is sys.__excepthook__:
Expand Down
66 changes: 61 additions & 5 deletions Lib/test/test_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@
gc_collect)
from test.support.script_helper import assert_python_ok
from test.support import threading_helper
from opcode import opmap
from opcode import opmap, opname
COPY_FREE_VARS = opmap['COPY_FREE_VARS']


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

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_co_lnotab_is_deprecated(self): # TODO: remove in 3.14
def func():
pass

with self.assertWarns(DeprecationWarning):
func.__code__.co_lnotab

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_invalid_bytecode(self):
def foo():
pass

# assert that opcode 229 is invalid
self.assertEqual(opname[229], '<229>')

# change first opcode to 0xeb (=229)
foo.__code__ = foo.__code__.replace(
co_code=b'\xe5' + foo.__code__.co_code[1:])

msg = "unknown opcode 229"
with self.assertRaisesRegex(SystemError, msg):
foo()

# TODO: RUSTPYTHON
@unittest.expectedFailure
# @requires_debug_ranges()
Expand Down Expand Up @@ -479,6 +505,34 @@ def f():
self.assertNotEqual(code_b, code_d)
self.assertNotEqual(code_c, code_d)

def test_code_hash_uses_firstlineno(self):
c1 = (lambda: 1).__code__
c2 = (lambda: 1).__code__
self.assertNotEqual(c1, c2)
self.assertNotEqual(hash(c1), hash(c2))
c3 = c1.replace(co_firstlineno=17)
self.assertNotEqual(c1, c3)
self.assertNotEqual(hash(c1), hash(c3))

def test_code_hash_uses_order(self):
# Swapping posonlyargcount and kwonlyargcount should change the hash.
c = (lambda x, y, *, z=1, w=1: 1).__code__
self.assertEqual(c.co_argcount, 2)
self.assertEqual(c.co_posonlyargcount, 0)
self.assertEqual(c.co_kwonlyargcount, 2)
swapped = c.replace(co_posonlyargcount=2, co_kwonlyargcount=0)
self.assertNotEqual(c, swapped)
self.assertNotEqual(hash(c), hash(swapped))

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_code_hash_uses_bytecode(self):
c = (lambda x, y: x + y).__code__
d = (lambda x, y: x * y).__code__
c1 = c.replace(co_code=d.co_code)
self.assertNotEqual(c, c1)
self.assertNotEqual(hash(c), hash(c1))


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

def check_lines(self, func):
co = func.__code__
lines1 = list(dedup(l for (_, _, l) in co.co_lines()))
lines1 = [line for _, _, line in co.co_lines()]
self.assertEqual(lines1, list(dedup(lines1)))
lines2 = list(lines_from_postions(positions_from_location_table(co)))
for l1, l2 in zip(lines1, lines2):
self.assertEqual(l1, l2)
Expand All @@ -714,6 +769,7 @@ def f():
pass
PY_CODE_LOCATION_INFO_NO_COLUMNS = 13
f.__code__ = f.__code__.replace(
co_stacksize=1,
co_firstlineno=42,
co_code=bytes(
[
Expand Down Expand Up @@ -742,15 +798,15 @@ def f():
py = ctypes.pythonapi
freefunc = ctypes.CFUNCTYPE(None,ctypes.c_voidp)

RequestCodeExtraIndex = py._PyEval_RequestCodeExtraIndex
RequestCodeExtraIndex = py.PyUnstable_Eval_RequestCodeExtraIndex
RequestCodeExtraIndex.argtypes = (freefunc,)
RequestCodeExtraIndex.restype = ctypes.c_ssize_t

SetExtra = py._PyCode_SetExtra
SetExtra = py.PyUnstable_Code_SetExtra
SetExtra.argtypes = (ctypes.py_object, ctypes.c_ssize_t, ctypes.c_voidp)
SetExtra.restype = ctypes.c_int

GetExtra = py._PyCode_GetExtra
GetExtra = py.PyUnstable_Code_GetExtra
GetExtra.argtypes = (ctypes.py_object, ctypes.c_ssize_t,
ctypes.POINTER(ctypes.c_voidp))
GetExtra.restype = ctypes.c_int
Expand Down