Skip to content

Commit 0fbe57f

Browse files
zhangblyouknowone
authored andcommitted
Update test_code.py code.py from CPython v3.11.
1 parent d5a6284 commit 0fbe57f

File tree

2 files changed

+40
-18
lines changed

2 files changed

+40
-18
lines changed

Lib/code.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
import sys
99
import traceback
10-
import argparse
1110
from codeop import CommandCompiler, compile_command
1211

1312
__all__ = ["InteractiveInterpreter", "InteractiveConsole", "interact",
@@ -41,7 +40,7 @@ def runsource(self, source, filename="<input>", symbol="single"):
4140
4241
Arguments are as for compile_command().
4342
44-
One several things can happen:
43+
One of several things can happen:
4544
4645
1) The input is incorrect; compile_command() raised an
4746
exception (SyntaxError or OverflowError). A syntax traceback
@@ -303,6 +302,8 @@ def interact(banner=None, readfunc=None, local=None, exitmsg=None):
303302

304303

305304
if __name__ == "__main__":
305+
import argparse
306+
306307
parser = argparse.ArgumentParser()
307308
parser.add_argument('-q', action='store_true',
308309
help="don't print version and copyright messages")

Lib/test/test_code.py

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,14 @@
139139
import unittest
140140
import textwrap
141141
import weakref
142+
import dis
142143

143144
try:
144145
import ctypes
145146
except ImportError:
146147
ctypes = None
147148
from test.support import (cpython_only,
148-
check_impl_detail,
149+
check_impl_detail, requires_debug_ranges,
149150
gc_collect)
150151
from test.support.script_helper import assert_python_ok
151152
from test.support import threading_helper
@@ -165,9 +166,8 @@ def consts(t):
165166
def dump(co):
166167
"""Print out a text representation of a code object."""
167168
for attr in ["name", "argcount", "posonlyargcount",
168-
"kwonlyargcount", "names", "varnames",]:
169-
# TODO: RUSTPYTHON
170-
# "cellvars","freevars", "nlocals", "flags"]:
169+
"kwonlyargcount", "names", "varnames",
170+
"cellvars", "freevars", "nlocals", "flags"]:
171171
print("%s: %s" % (attr, getattr(co, "co_" + attr)))
172172
print("consts:", tuple(consts(co.co_consts)))
173173

@@ -357,18 +357,6 @@ 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_invalid_bytecode(self):
363-
def foo(): pass
364-
foo.__code__ = co = foo.__code__.replace(co_code=b'\xee\x00d\x00S\x00')
365-
366-
with self.assertRaises(SystemError) as se:
367-
foo()
368-
self.assertEqual(
369-
f"{co.co_filename}:{co.co_firstlineno}: unknown opcode 238",
370-
str(se.exception))
371-
372360
# TODO: RUSTPYTHON
373361
@unittest.expectedFailure
374362
# @requires_debug_ranges()
@@ -717,6 +705,38 @@ def test_lines(self):
717705
self.check_lines(misshappen)
718706
self.check_lines(bug93662)
719707

708+
@cpython_only
709+
def test_code_new_empty(self):
710+
# If this test fails, it means that the construction of PyCode_NewEmpty
711+
# needs to be modified! Please update this test *and* PyCode_NewEmpty,
712+
# so that they both stay in sync.
713+
def f():
714+
pass
715+
PY_CODE_LOCATION_INFO_NO_COLUMNS = 13
716+
f.__code__ = f.__code__.replace(
717+
co_firstlineno=42,
718+
co_code=bytes(
719+
[
720+
dis.opmap["RESUME"], 0,
721+
dis.opmap["LOAD_ASSERTION_ERROR"], 0,
722+
dis.opmap["RAISE_VARARGS"], 1,
723+
]
724+
),
725+
co_linetable=bytes(
726+
[
727+
(1 << 7)
728+
| (PY_CODE_LOCATION_INFO_NO_COLUMNS << 3)
729+
| (3 - 1),
730+
0,
731+
]
732+
),
733+
)
734+
self.assertRaises(AssertionError, f)
735+
self.assertEqual(
736+
list(f.__code__.co_positions()),
737+
3 * [(42, 42, None, None)],
738+
)
739+
720740

721741
if check_impl_detail(cpython=True) and ctypes is not None:
722742
py = ctypes.pythonapi
@@ -811,6 +831,7 @@ def run(self):
811831
tt.join()
812832
self.assertEqual(LAST_FREED, 500)
813833

834+
814835
def load_tests(loader, tests, pattern):
815836
tests.addTest(doctest.DocTestSuite())
816837
return tests

0 commit comments

Comments
 (0)