From 0fbe57f96b12f711e003b80ea54eeba291fe3d5d Mon Sep 17 00:00:00 2001 From: zhangbl Date: Mon, 7 Aug 2023 10:19:55 +0800 Subject: [PATCH] Update test_code.py code.py from CPython v3.11. --- Lib/code.py | 5 ++-- Lib/test/test_code.py | 53 ++++++++++++++++++++++++++++++------------- 2 files changed, 40 insertions(+), 18 deletions(-) diff --git a/Lib/code.py b/Lib/code.py index 23295f4cf..76000f8c8 100644 --- a/Lib/code.py +++ b/Lib/code.py @@ -7,7 +7,6 @@ import sys import traceback -import argparse from codeop import CommandCompiler, compile_command __all__ = ["InteractiveInterpreter", "InteractiveConsole", "interact", @@ -41,7 +40,7 @@ class InteractiveInterpreter: Arguments are as for compile_command(). - One several things can happen: + One of several things can happen: 1) The input is incorrect; compile_command() raised an exception (SyntaxError or OverflowError). A syntax traceback @@ -303,6 +302,8 @@ def interact(banner=None, readfunc=None, local=None, exitmsg=None): if __name__ == "__main__": + import argparse + parser = argparse.ArgumentParser() parser.add_argument('-q', action='store_true', help="don't print version and copyright messages") diff --git a/Lib/test/test_code.py b/Lib/test/test_code.py index 65630a271..2661cbaa1 100644 --- a/Lib/test/test_code.py +++ b/Lib/test/test_code.py @@ -139,13 +139,14 @@ import doctest import unittest import textwrap import weakref +import dis try: import ctypes except ImportError: ctypes = None from test.support import (cpython_only, - check_impl_detail, + check_impl_detail, requires_debug_ranges, gc_collect) from test.support.script_helper import assert_python_ok from test.support import threading_helper @@ -165,9 +166,8 @@ def consts(t): def dump(co): """Print out a text representation of a code object.""" for attr in ["name", "argcount", "posonlyargcount", - "kwonlyargcount", "names", "varnames",]: - # TODO: RUSTPYTHON - # "cellvars","freevars", "nlocals", "flags"]: + "kwonlyargcount", "names", "varnames", + "cellvars", "freevars", "nlocals", "flags"]: print("%s: %s" % (attr, getattr(co, "co_" + attr))) print("consts:", tuple(consts(co.co_consts))) @@ -357,18 +357,6 @@ class CodeTest(unittest.TestCase): new_code = code = func.__code__.replace(co_linetable=b'') self.assertEqual(list(new_code.co_lines()), []) - # TODO: RUSTPYTHON - @unittest.expectedFailure - def test_invalid_bytecode(self): - def foo(): pass - foo.__code__ = co = foo.__code__.replace(co_code=b'\xee\x00d\x00S\x00') - - with self.assertRaises(SystemError) as se: - foo() - self.assertEqual( - f"{co.co_filename}:{co.co_firstlineno}: unknown opcode 238", - str(se.exception)) - # TODO: RUSTPYTHON @unittest.expectedFailure # @requires_debug_ranges() @@ -717,6 +705,38 @@ class CodeLocationTest(unittest.TestCase): self.check_lines(misshappen) self.check_lines(bug93662) + @cpython_only + def test_code_new_empty(self): + # If this test fails, it means that the construction of PyCode_NewEmpty + # needs to be modified! Please update this test *and* PyCode_NewEmpty, + # so that they both stay in sync. + def f(): + pass + PY_CODE_LOCATION_INFO_NO_COLUMNS = 13 + f.__code__ = f.__code__.replace( + co_firstlineno=42, + co_code=bytes( + [ + dis.opmap["RESUME"], 0, + dis.opmap["LOAD_ASSERTION_ERROR"], 0, + dis.opmap["RAISE_VARARGS"], 1, + ] + ), + co_linetable=bytes( + [ + (1 << 7) + | (PY_CODE_LOCATION_INFO_NO_COLUMNS << 3) + | (3 - 1), + 0, + ] + ), + ) + self.assertRaises(AssertionError, f) + self.assertEqual( + list(f.__code__.co_positions()), + 3 * [(42, 42, None, None)], + ) + if check_impl_detail(cpython=True) and ctypes is not None: py = ctypes.pythonapi @@ -811,6 +831,7 @@ if check_impl_detail(cpython=True) and ctypes is not None: tt.join() self.assertEqual(LAST_FREED, 500) + def load_tests(loader, tests, pattern): tests.addTest(doctest.DocTestSuite()) return tests