Add some Python 3.7 backports (#306)

* make.com now uses stack size of 2mb
* optimize _PyCFunction_FastCallKeywords
* backport python@cpython/7fc252adfbedece75f2330bcfdadbf84dee7836f
This commit is contained in:
Gautham 2021-10-30 11:24:14 +05:30 committed by GitHub
parent 903cc38c37
commit d7ff346b52
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 700 additions and 504 deletions

View file

@ -1,5 +1,4 @@
import unittest
import sys
from io import StringIO
from test import support
@ -129,91 +128,5 @@ class TestPrint(unittest.TestCase):
raise RuntimeError
self.assertRaises(RuntimeError, print, 1, file=noflush(), flush=True)
class TestPy2MigrationHint(unittest.TestCase):
"""Test that correct hint is produced analogous to Python3 syntax,
if print statement is executed as in Python 2.
"""
def test_normal_string(self):
python2_print_str = 'print "Hello World"'
with self.assertRaises(SyntaxError) as context:
exec(python2_print_str)
self.assertIn('print("Hello World")', str(context.exception))
def test_string_with_soft_space(self):
python2_print_str = 'print "Hello World",'
with self.assertRaises(SyntaxError) as context:
exec(python2_print_str)
self.assertIn('print("Hello World", end=" ")', str(context.exception))
def test_string_with_excessive_whitespace(self):
python2_print_str = 'print "Hello World", '
with self.assertRaises(SyntaxError) as context:
exec(python2_print_str)
self.assertIn('print("Hello World", end=" ")', str(context.exception))
def test_string_with_leading_whitespace(self):
python2_print_str = '''if 1:
print "Hello World"
'''
with self.assertRaises(SyntaxError) as context:
exec(python2_print_str)
self.assertIn('print("Hello World")', str(context.exception))
# bpo-32685: Suggestions for print statement should be proper when
# it is in the same line as the header of a compound statement
# and/or followed by a semicolon
def test_string_with_semicolon(self):
python2_print_str = 'print p;'
with self.assertRaises(SyntaxError) as context:
exec(python2_print_str)
self.assertIn('print(p)', str(context.exception))
def test_string_in_loop_on_same_line(self):
python2_print_str = 'for i in s: print i'
with self.assertRaises(SyntaxError) as context:
exec(python2_print_str)
self.assertIn('print(i)', str(context.exception))
def test_stream_redirection_hint_for_py2_migration(self):
# Test correct hint produced for Py2 redirection syntax
with self.assertRaises(TypeError) as context:
print >> sys.stderr, "message"
self.assertIn('Did you mean "print(<message>, '
'file=<output_stream>)"?', str(context.exception))
# Test correct hint is produced in the case where RHS implements
# __rrshift__ but returns NotImplemented
with self.assertRaises(TypeError) as context:
print >> 42
self.assertIn('Did you mean "print(<message>, '
'file=<output_stream>)"?', str(context.exception))
# Test stream redirection hint is specific to print
with self.assertRaises(TypeError) as context:
max >> sys.stderr
self.assertNotIn('Did you mean ', str(context.exception))
# Test stream redirection hint is specific to rshift
with self.assertRaises(TypeError) as context:
print << sys.stderr
self.assertNotIn('Did you mean', str(context.exception))
# Ensure right operand implementing rrshift still works
class OverrideRRShift:
def __rrshift__(self, lhs):
return 42 # Force result independent of LHS
self.assertEqual(print >> OverrideRRShift(), 42)
if __name__ == "__main__":
unittest.main()