mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-28 10:44:36 +08:00
Update py_compile from 3.14.2 (#6897)
This commit is contained in:
1 parent
8e9d591369
commit
75bcd24918
13 files changed
+286
-13
No files matched your search
Vendored
+1
-1
@@ -177,7 +177,7 @@ def main():
|
||||
import argparse
|
||||
|
||||
description = 'A simple command-line interface for py_compile module.'
|
||||
parser = argparse.ArgumentParser(description=description)
|
||||
parser = argparse.ArgumentParser(description=description, color=True)
|
||||
parser.add_argument(
|
||||
'-q', '--quiet',
|
||||
action='store_true',
|
||||
|
||||
Vendored
+37
-12
@@ -109,18 +109,22 @@ class PyCompileTestsBase:
|
||||
self.assertTrue(os.path.exists(self.pyc_path))
|
||||
self.assertFalse(os.path.exists(self.cache_path))
|
||||
|
||||
import platform
|
||||
@unittest.expectedFailureIf(sys.platform == "darwin" and int(platform.release().split(".")[0]) < 20, "TODO: RUSTPYTHON")
|
||||
@unittest.expectedFailureIf(
|
||||
sys.platform == "darwin" and int(
|
||||
__import__("platform").release().split(".")[0]
|
||||
) < 20,
|
||||
"TODO: RUSTPYTHON"
|
||||
)
|
||||
def test_relative_path(self):
|
||||
py_compile.compile(os.path.relpath(self.source_path),
|
||||
os.path.relpath(self.pyc_path))
|
||||
self.assertTrue(os.path.exists(self.pyc_path))
|
||||
self.assertFalse(os.path.exists(self.cache_path))
|
||||
|
||||
@unittest.skipIf(hasattr(os, 'geteuid') and os.geteuid() == 0,
|
||||
'non-root user required')
|
||||
@os_helper.skip_if_dac_override
|
||||
@unittest.skipIf(os.name == 'nt',
|
||||
'cannot control directory permissions on Windows')
|
||||
@os_helper.skip_unless_working_chmod
|
||||
def test_exceptions_propagate(self):
|
||||
# Make sure that exceptions raised thanks to issues with writing
|
||||
# bytecode.
|
||||
@@ -133,10 +137,11 @@ class PyCompileTestsBase:
|
||||
finally:
|
||||
os.chmod(self.directory, mode.st_mode)
|
||||
|
||||
# TODO: RUSTPYTHON
|
||||
@unittest.expectedFailure
|
||||
@unittest.expectedFailure # TODO: RUSTPYTHON
|
||||
def test_bad_coding(self):
|
||||
bad_coding = os.path.join(os.path.dirname(__file__), 'bad_coding2.py')
|
||||
bad_coding = os.path.join(os.path.dirname(__file__),
|
||||
'tokenizedata',
|
||||
'bad_coding2.py')
|
||||
with support.captured_stderr():
|
||||
self.assertIsNone(py_compile.compile(bad_coding, doraise=False))
|
||||
self.assertFalse(os.path.exists(
|
||||
@@ -198,6 +203,18 @@ class PyCompileTestsBase:
|
||||
fp.read(), 'test', {})
|
||||
self.assertEqual(flags, 0b1)
|
||||
|
||||
@unittest.expectedFailure # TODO: RUSTPYTHON
|
||||
def test_quiet(self):
|
||||
bad_coding = os.path.join(os.path.dirname(__file__),
|
||||
'tokenizedata',
|
||||
'bad_coding2.py')
|
||||
with support.captured_stderr() as stderr:
|
||||
self.assertIsNone(py_compile.compile(bad_coding, doraise=False, quiet=2))
|
||||
self.assertIsNone(py_compile.compile(bad_coding, doraise=True, quiet=2))
|
||||
self.assertEqual(stderr.getvalue(), '')
|
||||
with self.assertRaises(py_compile.PyCompileError):
|
||||
py_compile.compile(bad_coding, doraise=True, quiet=1)
|
||||
|
||||
|
||||
class PyCompileTestsWithSourceEpoch(PyCompileTestsBase,
|
||||
unittest.TestCase,
|
||||
@@ -218,27 +235,31 @@ class PyCompileCLITestCase(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.directory = tempfile.mkdtemp()
|
||||
self.source_path = os.path.join(self.directory, '_test.py')
|
||||
self.cache_path = importlib.util.cache_from_source(self.source_path)
|
||||
self.cache_path = importlib.util.cache_from_source(self.source_path,
|
||||
optimization='' if __debug__ else 1)
|
||||
with open(self.source_path, 'w') as file:
|
||||
file.write('x = 123\n')
|
||||
|
||||
def tearDown(self):
|
||||
os_helper.rmtree(self.directory)
|
||||
|
||||
@support.requires_subprocess()
|
||||
def pycompilecmd(self, *args, **kwargs):
|
||||
# assert_python_* helpers don't return proc object. We'll just use
|
||||
# subprocess.run() instead of spawn_python() and its friends to test
|
||||
# stdin support of the CLI.
|
||||
opts = '-m' if __debug__ else '-Om'
|
||||
if args and args[0] == '-' and 'input' in kwargs:
|
||||
return subprocess.run([sys.executable, '-m', 'py_compile', '-'],
|
||||
return subprocess.run([sys.executable, opts, 'py_compile', '-'],
|
||||
input=kwargs['input'].encode(),
|
||||
capture_output=True)
|
||||
return script_helper.assert_python_ok('-m', 'py_compile', *args, **kwargs)
|
||||
return script_helper.assert_python_ok(opts, 'py_compile', *args, **kwargs)
|
||||
|
||||
def pycompilecmd_failure(self, *args):
|
||||
return script_helper.assert_python_failure('-m', 'py_compile', *args)
|
||||
|
||||
def test_stdin(self):
|
||||
self.assertFalse(os.path.exists(self.cache_path))
|
||||
result = self.pycompilecmd('-', input=self.source_path)
|
||||
self.assertEqual(result.returncode, 0)
|
||||
self.assertEqual(result.stdout, b'')
|
||||
@@ -253,14 +274,18 @@ class PyCompileCLITestCase(unittest.TestCase):
|
||||
self.assertTrue(os.path.exists(self.cache_path))
|
||||
|
||||
def test_bad_syntax(self):
|
||||
bad_syntax = os.path.join(os.path.dirname(__file__), 'badsyntax_3131.py')
|
||||
bad_syntax = os.path.join(os.path.dirname(__file__),
|
||||
'tokenizedata',
|
||||
'badsyntax_3131.py')
|
||||
rc, stdout, stderr = self.pycompilecmd_failure(bad_syntax)
|
||||
self.assertEqual(rc, 1)
|
||||
self.assertEqual(stdout, b'')
|
||||
self.assertIn(b'SyntaxError', stderr)
|
||||
|
||||
def test_bad_syntax_with_quiet(self):
|
||||
bad_syntax = os.path.join(os.path.dirname(__file__), 'badsyntax_3131.py')
|
||||
bad_syntax = os.path.join(os.path.dirname(__file__),
|
||||
'tokenizedata',
|
||||
'badsyntax_3131.py')
|
||||
rc, stdout, stderr = self.pycompilecmd_failure('-q', bad_syntax)
|
||||
self.assertEqual(rc, 1)
|
||||
self.assertEqual(stdout, b'')
|
||||
|
||||
Whitespace-only changes.
Vendored
+1
@@ -0,0 +1 @@
|
||||
# -*- coding: uft-8 -*-
|
||||
Vendored
+2
@@ -0,0 +1,2 @@
|
||||
#coding: utf8
|
||||
print('我')
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
€ = 2
|
||||
+1
@@ -0,0 +1 @@
|
||||
print("böse")
|
||||
Vendored
+4
@@ -0,0 +1,4 @@
|
||||
#coding:latin1
|
||||
|
||||
|
||||
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
# -*- coding: latin1 -*-
|
||||
# IMPORTANT: this file has the utf-8 BOM signature '\xef\xbb\xbf'
|
||||
# at the start of it. Make sure this is preserved if any changes
|
||||
# are made! Also note that the coding cookie above conflicts with
|
||||
# the presence of a utf-8 BOM signature -- this is intended.
|
||||
|
||||
# Arbitrary encoded utf-8 text (stolen from test_doctest2.py).
|
||||
x = 'ЉЊЈЁЂ'
|
||||
def y():
|
||||
"""
|
||||
And again in a comment. ЉЊЈЁЂ
|
||||
"""
|
||||
pass
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
# IMPORTANT: this file has the utf-8 BOM signature '\xef\xbb\xbf'
|
||||
# at the start of it. Make sure this is preserved if any changes
|
||||
# are made!
|
||||
|
||||
# Arbitrary encoded utf-8 text (stolen from test_doctest2.py).
|
||||
x = 'ЉЊЈЁЂ'
|
||||
def y():
|
||||
"""
|
||||
And again in a comment. ЉЊЈЁЂ
|
||||
"""
|
||||
pass
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# IMPORTANT: unlike the other test_tokenize-*.txt files, this file
|
||||
# does NOT have the utf-8 BOM signature '\xef\xbb\xbf' at the start
|
||||
# of it. Make sure this is not added inadvertently by your editor
|
||||
# if any changes are made to this file!
|
||||
|
||||
# Arbitrary encoded utf-8 text (stolen from test_doctest2.py).
|
||||
x = 'ЉЊЈЁЂ'
|
||||
def y():
|
||||
"""
|
||||
And again in a comment. ЉЊЈЁЂ
|
||||
"""
|
||||
pass
|
||||
@@ -0,0 +1,12 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# IMPORTANT: this file has the utf-8 BOM signature '\xef\xbb\xbf'
|
||||
# at the start of it. Make sure this is preserved if any changes
|
||||
# are made!
|
||||
|
||||
# Arbitrary encoded utf-8 text (stolen from test_doctest2.py).
|
||||
x = 'ЉЊЈЁЂ'
|
||||
def y():
|
||||
"""
|
||||
And again in a comment. ЉЊЈЁЂ
|
||||
"""
|
||||
pass
|
||||
+189
@@ -0,0 +1,189 @@
|
||||
# Tests for the 'tokenize' module.
|
||||
# Large bits stolen from test_grammar.py.
|
||||
|
||||
# Comments
|
||||
"#"
|
||||
#'
|
||||
#"
|
||||
#\
|
||||
#
|
||||
# abc
|
||||
'''#
|
||||
#'''
|
||||
|
||||
x = 1 #
|
||||
|
||||
# Balancing continuation
|
||||
|
||||
a = (3, 4,
|
||||
5, 6)
|
||||
y = [3, 4,
|
||||
5]
|
||||
z = {'a':5,
|
||||
'b':6}
|
||||
x = (len(repr(y)) + 5*x - a[
|
||||
3 ]
|
||||
- x + len({
|
||||
}
|
||||
)
|
||||
)
|
||||
|
||||
# Backslash means line continuation:
|
||||
x = 1 \
|
||||
+ 1
|
||||
|
||||
# Backslash does not means continuation in comments :\
|
||||
x = 0
|
||||
|
||||
# Ordinary integers
|
||||
0xff != 255
|
||||
0o377 != 255
|
||||
2147483647 != 0o17777777777
|
||||
-2147483647-1 != 0o20000000000
|
||||
0o37777777777 != -1
|
||||
0xffffffff != -1; 0o37777777777 != -1; -0o1234567 == 0O001234567; 0b10101 == 0B00010101
|
||||
|
||||
# Long integers
|
||||
x = 0
|
||||
x = 0
|
||||
x = 0xffffffffffffffff
|
||||
x = 0xffffffffffffffff
|
||||
x = 0o77777777777777777
|
||||
x = 0B11101010111111111
|
||||
x = 123456789012345678901234567890
|
||||
x = 123456789012345678901234567890
|
||||
|
||||
# Floating-point numbers
|
||||
x = 3.14
|
||||
x = 314.
|
||||
x = 0.314
|
||||
# XXX x = 000.314
|
||||
x = .314
|
||||
x = 3e14
|
||||
x = 3E14
|
||||
x = 3e-14
|
||||
x = 3e+14
|
||||
x = 3.e14
|
||||
x = .3e14
|
||||
x = 3.1e4
|
||||
|
||||
# String literals
|
||||
x = ''; y = "";
|
||||
x = '\''; y = "'";
|
||||
x = '"'; y = "\"";
|
||||
x = "doesn't \"shrink\" does it"
|
||||
y = 'doesn\'t "shrink" does it'
|
||||
x = "does \"shrink\" doesn't it"
|
||||
y = 'does "shrink" doesn\'t it'
|
||||
x = """
|
||||
The "quick"
|
||||
brown fox
|
||||
jumps over
|
||||
the 'lazy' dog.
|
||||
"""
|
||||
y = '\nThe "quick"\nbrown fox\njumps over\nthe \'lazy\' dog.\n'
|
||||
y = '''
|
||||
The "quick"
|
||||
brown fox
|
||||
jumps over
|
||||
the 'lazy' dog.
|
||||
''';
|
||||
y = "\n\
|
||||
The \"quick\"\n\
|
||||
brown fox\n\
|
||||
jumps over\n\
|
||||
the 'lazy' dog.\n\
|
||||
";
|
||||
y = '\n\
|
||||
The \"quick\"\n\
|
||||
brown fox\n\
|
||||
jumps over\n\
|
||||
the \'lazy\' dog.\n\
|
||||
';
|
||||
x = r'\\' + R'\\'
|
||||
x = r'\'' + ''
|
||||
y = r'''
|
||||
foo bar \\
|
||||
baz''' + R'''
|
||||
foo'''
|
||||
y = r"""foo
|
||||
bar \\ baz
|
||||
""" + R'''spam
|
||||
'''
|
||||
x = b'abc' + B'ABC'
|
||||
y = b"abc" + B"ABC"
|
||||
x = br'abc' + Br'ABC' + bR'ABC' + BR'ABC'
|
||||
y = br"abc" + Br"ABC" + bR"ABC" + BR"ABC"
|
||||
x = rb'abc' + rB'ABC' + Rb'ABC' + RB'ABC'
|
||||
y = rb"abc" + rB"ABC" + Rb"ABC" + RB"ABC"
|
||||
x = br'\\' + BR'\\'
|
||||
x = rb'\\' + RB'\\'
|
||||
x = br'\'' + ''
|
||||
x = rb'\'' + ''
|
||||
y = br'''
|
||||
foo bar \\
|
||||
baz''' + BR'''
|
||||
foo'''
|
||||
y = Br"""foo
|
||||
bar \\ baz
|
||||
""" + bR'''spam
|
||||
'''
|
||||
y = rB"""foo
|
||||
bar \\ baz
|
||||
""" + Rb'''spam
|
||||
'''
|
||||
|
||||
# Indentation
|
||||
if 1:
|
||||
x = 2
|
||||
if 1:
|
||||
x = 2
|
||||
if 1:
|
||||
while 0:
|
||||
if 0:
|
||||
x = 2
|
||||
x = 2
|
||||
if 0:
|
||||
if 2:
|
||||
while 0:
|
||||
if 1:
|
||||
x = 2
|
||||
|
||||
# Operators
|
||||
|
||||
def d22(a, b, c=1, d=2): pass
|
||||
def d01v(a=1, *restt, **restd): pass
|
||||
|
||||
(x, y) != ({'a':1}, {'b':2})
|
||||
|
||||
# comparison
|
||||
if 1 < 1 > 1 == 1 >= 1 <= 1 != 1 != 1 in 1 not in 1 is 1 is not 1: pass
|
||||
|
||||
# binary
|
||||
x = 1 & 1
|
||||
x = 1 ^ 1
|
||||
x = 1 | 1
|
||||
|
||||
# shift
|
||||
x = 1 << 1 >> 1
|
||||
|
||||
# additive
|
||||
x = 1 - 1 + 1 - 1 + 1
|
||||
|
||||
# multiplicative
|
||||
x = 1 / 1 * 1 % 1
|
||||
|
||||
# unary
|
||||
x = ~1 ^ 1 & 1 | 1 & 1 ^ -1
|
||||
x = -1*1/1 + 1*1 - ---1*1
|
||||
|
||||
# selector
|
||||
import sys, time
|
||||
x = sys.modules['time'].time()
|
||||
|
||||
@staticmethod
|
||||
def foo(): pass
|
||||
|
||||
@staticmethod
|
||||
def foo(x:1)->1: pass
|
||||
|
||||
Reference in new issue
Block a user