Skip to content

Update test_opcodes.py, test_codeop.py and codeop.py from CPython v3.12 #5115

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 9 commits into from
Nov 10, 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
209 changes: 48 additions & 161 deletions Cargo.lock

Large diffs are not rendered by default.

20 changes: 14 additions & 6 deletions Lib/codeop.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ def _maybe_compile(compiler, source, filename, symbol):
return None
# fallthrough

return compiler(source, filename, symbol)

return compiler(source, filename, symbol, incomplete_input=False)

def _is_syntax_error(err1, err2):
rep1 = repr(err1)
Expand All @@ -82,8 +81,13 @@ def _is_syntax_error(err1, err2):
return True
return False

def _compile(source, filename, symbol):
return compile(source, filename, symbol, PyCF_DONT_IMPLY_DEDENT | PyCF_ALLOW_INCOMPLETE_INPUT)
def _compile(source, filename, symbol, incomplete_input=True):
flags = 0
if incomplete_input:
flags |= PyCF_ALLOW_INCOMPLETE_INPUT
flags |= PyCF_DONT_IMPLY_DEDENT
return compile(source, filename, symbol, flags)


def compile_command(source, filename="<input>", symbol="single"):
r"""Compile a command and determine whether it is incomplete.
Expand Down Expand Up @@ -114,8 +118,12 @@ class Compile:
def __init__(self):
self.flags = PyCF_DONT_IMPLY_DEDENT | PyCF_ALLOW_INCOMPLETE_INPUT

def __call__(self, source, filename, symbol):
codeob = compile(source, filename, symbol, self.flags, True)
def __call__(self, source, filename, symbol, **kwargs):
flags = self.flags
if kwargs.get('incomplete_input', True) is False:
flags &= ~PyCF_DONT_IMPLY_DEDENT
flags &= ~PyCF_ALLOW_INCOMPLETE_INPUT
codeob = compile(source, filename, symbol, flags, True)
for feature in _features:
if codeob.co_flags & feature.compiler_flag:
self.flags |= feature.compiler_flag
Expand Down
1 change: 0 additions & 1 deletion Lib/test/test_atexit.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import atexit
import os
import sys
import textwrap
import unittest
from test import support
Expand Down
150 changes: 77 additions & 73 deletions Lib/test/test_codeop.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,19 @@
Test cases for codeop.py
Nick Mathewson
"""
import sys
import unittest
import warnings
from test import support
from test.support import warnings_helper
from textwrap import dedent

from codeop import compile_command, PyCF_DONT_IMPLY_DEDENT
import io

if support.is_jython:

def unify_callables(d):
for n, v in d.items():
if hasattr(v, '__call__'):
d[n] = True
return d


class CodeopTests(unittest.TestCase):

def assertValid(self, str, symbol='single'):
'''succeed iff str is a valid piece of code'''
if support.is_jython:
code = compile_command(str, "<input>", symbol)
self.assertTrue(code)
if symbol == "single":
d, r = {}, {}
saved_stdout = sys.stdout
sys.stdout = io.StringIO()
try:
exec(code, d)
exec(compile(str, "<input>", "single"), r)
finally:
sys.stdout = saved_stdout
elif symbol == 'eval':
ctx = {'a': 2}
d = {'value': eval(code, ctx)}
r = {'value': eval(str, ctx)}
self.assertEqual(unify_callables(r), unify_callables(d))
else:
expected = compile(str, "<input>", symbol, PyCF_DONT_IMPLY_DEDENT)
self.assertEqual(compile_command(str, "<input>", symbol), expected)
expected = compile(str, "<input>", symbol, PyCF_DONT_IMPLY_DEDENT)
self.assertEqual(compile_command(str, "<input>", symbol), expected)

def assertIncomplete(self, str, symbol='single'):
'''succeed iff str is the start of a valid piece of code'''
Expand All @@ -52,30 +23,25 @@ def assertIncomplete(self, str, symbol='single'):
def assertInvalid(self, str, symbol='single', is_syntax=1):
'''succeed iff str is the start of an invalid piece of code'''
try:
compile_command(str, symbol=symbol)
compile_command(str,symbol=symbol)
self.fail("No exception raised for invalid code")
except SyntaxError:
self.assertTrue(is_syntax)
except OverflowError:
self.assertTrue(not is_syntax)

# TODO: RUSTPYTHON

@unittest.expectedFailure
def test_valid(self):
av = self.assertValid

# special case
if not support.is_jython:
self.assertEqual(compile_command(""),
compile("pass", "<input>", 'single',
PyCF_DONT_IMPLY_DEDENT))
self.assertEqual(compile_command("\n"),
compile("pass", "<input>", 'single',
PyCF_DONT_IMPLY_DEDENT))
else:
av("")
av("\n")
self.assertEqual(compile_command(""),
compile("pass", "<input>", 'single',
PyCF_DONT_IMPLY_DEDENT))
self.assertEqual(compile_command("\n"),
compile("pass", "<input>", 'single',
PyCF_DONT_IMPLY_DEDENT))

av("a = 1")
av("\na = 1")
Expand Down Expand Up @@ -104,25 +70,25 @@ def test_valid(self):
av("a=3\n\n")
av("a = 9+ \\\n3")

av("3**3", "eval")
av("(lambda z: \n z**3)", "eval")
av("3**3","eval")
av("(lambda z: \n z**3)","eval")

av("9+ \\\n3", "eval")
av("9+ \\\n3\n", "eval")
av("9+ \\\n3","eval")
av("9+ \\\n3\n","eval")

av("\n\na**3", "eval")
av("\n \na**3", "eval")
av("#a\n#b\na**3", "eval")
av("\n\na**3","eval")
av("\n \na**3","eval")
av("#a\n#b\na**3","eval")

av("\n\na = 1\n\n")
av("\n\nif 1: a=1\n\n")

av("if 1:\n pass\n if 1:\n pass\n else:\n pass\n")
av("#a\n\n \na=3\n\n")

av("\n\na**3", "eval")
av("\n \na**3", "eval")
av("#a\n#b\na**3", "eval")
av("\n\na**3","eval")
av("\n \na**3","eval")
av("#a\n#b\na**3","eval")

av("def f():\n try: pass\n finally: [x for x in (1,2)]\n")
av("def f():\n pass\n#foo\n")
Expand All @@ -141,6 +107,10 @@ def test_incomplete(self):
ai("a = {")
ai("b + {")

ai("print([1,\n2,")
ai("print({1:1,\n2:3,")
ai("print((1,\n2,")

ai("if 9==3:\n pass\nelse:")
ai("if 9==3:\n pass\nelse:\n")
ai("if 9==3:\n pass\nelse:\n pass")
Expand All @@ -163,13 +133,12 @@ def test_incomplete(self):
ai("a = 'a\\")
ai("a = '''xy")

ai("", "eval")
ai("\n", "eval")
ai("(", "eval")
ai("(\n\n\n", "eval")
ai("(9+", "eval")
ai("9+ \\", "eval")
ai("lambda z: \\", "eval")
ai("","eval")
ai("\n","eval")
ai("(","eval")
ai("(9+","eval")
ai("9+ \\","eval")
ai("lambda z: \\","eval")

ai("if True:\n if True:\n if True: \n")

Expand Down Expand Up @@ -277,14 +246,13 @@ def test_invalid(self):
ai("a = 'a\\ ")
ai("a = 'a\\\n")

ai("a = 1", "eval")
ai("a = (", "eval")
ai("]", "eval")
ai("())", "eval")
ai("[}", "eval")
ai("9+", "eval")
ai("lambda z:", "eval")
ai("a b", "eval")
ai("a = 1","eval")
ai("]","eval")
ai("())","eval")
ai("[}","eval")
ai("9+","eval")
ai("lambda z:","eval")
ai("a b","eval")

ai("return 2.3")
ai("if (a == 1 and b = 2): pass")
Expand Down Expand Up @@ -314,11 +282,11 @@ def test_filename(self):
# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_warning(self):
# Teswarnings_helper.check_warningsonly returned once.
# Test that the warning is only returned once.
with warnings_helper.check_warnings(
(".*literal", SyntaxWarning),
(".*invalid", DeprecationWarning),
) as w:
('"is" with \'str\' literal', SyntaxWarning),
("invalid escape sequence", SyntaxWarning),
) as w:
compile_command(r"'\e' is 0")
self.assertEqual(len(w.warnings), 2)

Expand All @@ -327,6 +295,42 @@ def test_warning(self):
warnings.simplefilter('error', SyntaxWarning)
compile_command('1 is 1', symbol='exec')

# Check SyntaxWarning treated as an SyntaxError
with warnings.catch_warnings(), self.assertRaises(SyntaxError):
warnings.simplefilter('error', SyntaxWarning)
compile_command(r"'\e'", symbol='exec')

# TODO: RUSTPYTHON
#def test_incomplete_warning(self):
# with warnings.catch_warnings(record=True) as w:
# warnings.simplefilter('always')
# self.assertIncomplete("'\\e' + (")
# self.assertEqual(w, [])

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_invalid_warning(self):
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter('always')
self.assertInvalid("'\\e' 1")
self.assertEqual(len(w), 1)
self.assertEqual(w[0].category, SyntaxWarning)
self.assertRegex(str(w[0].message), 'invalid escape sequence')
self.assertEqual(w[0].filename, '<input>')

def assertSyntaxErrorMatches(self, code, message):
with self.subTest(code):
with self.assertRaisesRegex(SyntaxError, message):
compile_command(code, symbol='exec')

def test_syntax_errors(self):
self.assertSyntaxErrorMatches(
dedent("""\
def foo(x,x):
pass
"""), "duplicate argument 'x' in function definition")



if __name__ == "__main__":
unittest.main()
Loading