Skip to content

Update test_code.py code.py from CPython v3.11 #5035

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 26, 2023
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
5 changes: 3 additions & 2 deletions Lib/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import sys
import traceback
import argparse
from codeop import CommandCompiler, compile_command

__all__ = ["InteractiveInterpreter", "InteractiveConsole", "interact",
Expand Down Expand Up @@ -41,7 +40,7 @@ def runsource(self, source, filename="<input>", symbol="single"):

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
Expand Down Expand Up @@ -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")
Expand Down
53 changes: 37 additions & 16 deletions Lib/test/test_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,14 @@
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
Expand All @@ -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)))

Expand Down Expand Up @@ -357,18 +357,6 @@ def func():
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()
Expand Down Expand Up @@ -717,6 +705,38 @@ def test_lines(self):
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
Expand Down Expand Up @@ -811,6 +831,7 @@ def run(self):
tt.join()
self.assertEqual(LAST_FREED, 500)


def load_tests(loader, tests, pattern):
tests.addTest(doctest.DocTestSuite())
return tests
Expand Down