| 
									
										
										
										
											2008-03-18 15:35:27 +00:00
										 |  |  | import unittest | 
					
						
							| 
									
										
										
										
											2017-08-18 16:07:36 +05:30
										 |  |  | import sys | 
					
						
							| 
									
										
										
										
											2013-04-04 22:32:28 +03:00
										 |  |  | from io import StringIO | 
					
						
							| 
									
										
										
										
											2008-03-18 15:35:27 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-04 22:32:28 +03:00
										 |  |  | from test import support | 
					
						
							| 
									
										
										
										
											2008-03-18 23:48:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-18 15:35:27 +00:00
										 |  |  | NotDefined = object() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # A dispatch table all 8 combinations of providing | 
					
						
							| 
									
										
										
										
											2013-04-04 22:32:28 +03:00
										 |  |  | # sep, end, and file. | 
					
						
							| 
									
										
										
										
											2008-03-18 15:35:27 +00:00
										 |  |  | # I use this machinery so that I'm not just passing default | 
					
						
							| 
									
										
										
										
											2013-04-04 22:32:28 +03:00
										 |  |  | # values to print, I'm either passing or not passing in the | 
					
						
							|  |  |  | # arguments. | 
					
						
							| 
									
										
										
										
											2008-03-18 15:35:27 +00:00
										 |  |  | dispatch = { | 
					
						
							|  |  |  |     (False, False, False): | 
					
						
							| 
									
										
										
										
											2013-04-04 22:32:28 +03:00
										 |  |  |         lambda args, sep, end, file: print(*args), | 
					
						
							| 
									
										
										
										
											2008-03-18 15:35:27 +00:00
										 |  |  |     (False, False, True): | 
					
						
							| 
									
										
										
										
											2013-04-04 22:32:28 +03:00
										 |  |  |         lambda args, sep, end, file: print(file=file, *args), | 
					
						
							| 
									
										
										
										
											2008-03-18 15:35:27 +00:00
										 |  |  |     (False, True,  False): | 
					
						
							| 
									
										
										
										
											2013-04-04 22:32:28 +03:00
										 |  |  |         lambda args, sep, end, file: print(end=end, *args), | 
					
						
							| 
									
										
										
										
											2008-03-18 15:35:27 +00:00
										 |  |  |     (False, True,  True): | 
					
						
							| 
									
										
										
										
											2013-04-04 22:32:28 +03:00
										 |  |  |         lambda args, sep, end, file: print(end=end, file=file, *args), | 
					
						
							| 
									
										
										
										
											2008-03-18 15:35:27 +00:00
										 |  |  |     (True,  False, False): | 
					
						
							| 
									
										
										
										
											2013-04-04 22:32:28 +03:00
										 |  |  |         lambda args, sep, end, file: print(sep=sep, *args), | 
					
						
							| 
									
										
										
										
											2008-03-18 15:35:27 +00:00
										 |  |  |     (True,  False, True): | 
					
						
							| 
									
										
										
										
											2013-04-04 22:32:28 +03:00
										 |  |  |         lambda args, sep, end, file: print(sep=sep, file=file, *args), | 
					
						
							| 
									
										
										
										
											2008-03-18 15:35:27 +00:00
										 |  |  |     (True,  True,  False): | 
					
						
							| 
									
										
										
										
											2013-04-04 22:32:28 +03:00
										 |  |  |         lambda args, sep, end, file: print(sep=sep, end=end, *args), | 
					
						
							| 
									
										
										
										
											2008-03-18 15:35:27 +00:00
										 |  |  |     (True,  True,  True): | 
					
						
							| 
									
										
										
										
											2013-04-04 22:32:28 +03:00
										 |  |  |         lambda args, sep, end, file: print(sep=sep, end=end, file=file, *args), | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-18 15:35:27 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | # Class used to test __str__ and print | 
					
						
							|  |  |  | class ClassWith__str__: | 
					
						
							|  |  |  |     def __init__(self, x): | 
					
						
							|  |  |  |         self.x = x | 
					
						
							| 
									
										
										
										
											2013-04-04 22:32:28 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-18 15:35:27 +00:00
										 |  |  |     def __str__(self): | 
					
						
							|  |  |  |         return self.x | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-04 22:32:28 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-18 15:35:27 +00:00
										 |  |  | class TestPrint(unittest.TestCase): | 
					
						
							| 
									
										
										
										
											2013-04-04 22:32:28 +03:00
										 |  |  |     """Test correct operation of the print function.""" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-18 23:48:28 +00:00
										 |  |  |     def check(self, expected, args, | 
					
						
							| 
									
										
										
										
											2013-04-04 22:32:28 +03:00
										 |  |  |               sep=NotDefined, end=NotDefined, file=NotDefined): | 
					
						
							| 
									
										
										
										
											2008-03-18 15:35:27 +00:00
										 |  |  |         # Capture sys.stdout in a StringIO.  Call print with args, | 
					
						
							| 
									
										
										
										
											2013-04-04 22:32:28 +03:00
										 |  |  |         # and with sep, end, and file, if they're defined.  Result | 
					
						
							|  |  |  |         # must match expected. | 
					
						
							| 
									
										
										
										
											2008-03-18 15:35:27 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-04 22:32:28 +03:00
										 |  |  |         # Look up the actual function to call, based on if sep, end, | 
					
						
							|  |  |  |         # and file are defined. | 
					
						
							| 
									
										
										
										
											2008-03-18 15:35:27 +00:00
										 |  |  |         fn = dispatch[(sep is not NotDefined, | 
					
						
							|  |  |  |                        end is not NotDefined, | 
					
						
							|  |  |  |                        file is not NotDefined)] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-05-20 21:35:26 +00:00
										 |  |  |         with support.captured_stdout() as t: | 
					
						
							| 
									
										
										
										
											2008-03-18 15:35:27 +00:00
										 |  |  |             fn(args, sep, end, file) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.assertEqual(t.getvalue(), expected) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_print(self): | 
					
						
							| 
									
										
										
										
											2008-03-18 23:48:28 +00:00
										 |  |  |         def x(expected, args, sep=NotDefined, end=NotDefined): | 
					
						
							| 
									
										
										
										
											2008-03-18 15:35:27 +00:00
										 |  |  |             # Run the test 2 ways: not using file, and using | 
					
						
							| 
									
										
										
										
											2013-04-04 22:32:28 +03:00
										 |  |  |             # file directed to a StringIO. | 
					
						
							| 
									
										
										
										
											2008-03-18 15:35:27 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |             self.check(expected, args, sep=sep, end=end) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             # When writing to a file, stdout is expected to be empty | 
					
						
							| 
									
										
										
										
											2008-03-18 23:48:28 +00:00
										 |  |  |             o = StringIO() | 
					
						
							| 
									
										
										
										
											2008-03-18 15:35:27 +00:00
										 |  |  |             self.check('', args, sep=sep, end=end, file=o) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             # And o will contain the expected output | 
					
						
							|  |  |  |             self.assertEqual(o.getvalue(), expected) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         x('\n', ()) | 
					
						
							|  |  |  |         x('a\n', ('a',)) | 
					
						
							|  |  |  |         x('None\n', (None,)) | 
					
						
							|  |  |  |         x('1 2\n', (1, 2)) | 
					
						
							| 
									
										
										
										
											2008-03-18 23:48:28 +00:00
										 |  |  |         x('1   2\n', (1, ' ', 2)) | 
					
						
							| 
									
										
										
										
											2008-03-18 15:35:27 +00:00
										 |  |  |         x('1*2\n', (1, 2), sep='*') | 
					
						
							|  |  |  |         x('1 s', (1, 's'), end='') | 
					
						
							|  |  |  |         x('a\nb\n', ('a', 'b'), sep='\n') | 
					
						
							|  |  |  |         x('1.01', (1.0, 1), sep='', end='') | 
					
						
							|  |  |  |         x('1*a*1.3+', (1, 'a', 1.3), sep='*', end='+') | 
					
						
							| 
									
										
										
										
											2008-03-18 23:48:28 +00:00
										 |  |  |         x('a\n\nb\n', ('a\n', 'b'), sep='\n') | 
					
						
							|  |  |  |         x('\0+ +\0\n', ('\0', ' ', '\0'), sep='+') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         x('a\n b\n', ('a\n', 'b')) | 
					
						
							|  |  |  |         x('a\n b\n', ('a\n', 'b'), sep=None) | 
					
						
							|  |  |  |         x('a\n b\n', ('a\n', 'b'), end=None) | 
					
						
							|  |  |  |         x('a\n b\n', ('a\n', 'b'), sep=None, end=None) | 
					
						
							| 
									
										
										
										
											2008-03-18 15:35:27 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         x('*\n', (ClassWith__str__('*'),)) | 
					
						
							| 
									
										
										
										
											2008-03-18 23:48:28 +00:00
										 |  |  |         x('abc 1\n', (ClassWith__str__('abc'), 1)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # errors | 
					
						
							|  |  |  |         self.assertRaises(TypeError, print, '', sep=3) | 
					
						
							|  |  |  |         self.assertRaises(TypeError, print, '', end=3) | 
					
						
							|  |  |  |         self.assertRaises(AttributeError, print, '', file='') | 
					
						
							| 
									
										
										
										
											2008-03-18 15:35:27 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-01-13 19:41:25 +01:00
										 |  |  |     def test_print_flush(self): | 
					
						
							|  |  |  |         # operation of the flush flag | 
					
						
							| 
									
										
										
										
											2013-04-04 22:32:28 +03:00
										 |  |  |         class filelike: | 
					
						
							| 
									
										
										
										
											2012-01-13 19:41:25 +01:00
										 |  |  |             def __init__(self): | 
					
						
							|  |  |  |                 self.written = '' | 
					
						
							|  |  |  |                 self.flushed = 0 | 
					
						
							| 
									
										
										
										
											2013-04-04 22:32:28 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-01-13 19:41:25 +01:00
										 |  |  |             def write(self, str): | 
					
						
							|  |  |  |                 self.written += str | 
					
						
							| 
									
										
										
										
											2013-04-04 22:32:28 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-01-13 19:41:25 +01:00
										 |  |  |             def flush(self): | 
					
						
							|  |  |  |                 self.flushed += 1 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         f = filelike() | 
					
						
							|  |  |  |         print(1, file=f, end='', flush=True) | 
					
						
							|  |  |  |         print(2, file=f, end='', flush=True) | 
					
						
							|  |  |  |         print(3, file=f, flush=False) | 
					
						
							|  |  |  |         self.assertEqual(f.written, '123\n') | 
					
						
							|  |  |  |         self.assertEqual(f.flushed, 2) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # ensure exceptions from flush are passed through | 
					
						
							| 
									
										
										
										
											2013-04-04 22:32:28 +03:00
										 |  |  |         class noflush: | 
					
						
							| 
									
										
										
										
											2012-01-13 19:41:25 +01:00
										 |  |  |             def write(self, str): | 
					
						
							|  |  |  |                 pass | 
					
						
							| 
									
										
										
										
											2013-04-04 22:32:28 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-01-13 19:41:25 +01:00
										 |  |  |             def flush(self): | 
					
						
							|  |  |  |                 raise RuntimeError | 
					
						
							|  |  |  |         self.assertRaises(RuntimeError, print, 1, file=noflush(), flush=True) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-20 19:01:32 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  | 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) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-27 21:30:32 +01:00
										 |  |  |         self.assertIn("Missing parentheses in call to 'print'. Did you mean print(...)", | 
					
						
							|  |  |  |                 str(context.exception)) | 
					
						
							| 
									
										
										
										
											2017-06-20 19:01:32 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_string_with_soft_space(self): | 
					
						
							|  |  |  |         python2_print_str = 'print "Hello World",' | 
					
						
							|  |  |  |         with self.assertRaises(SyntaxError) as context: | 
					
						
							|  |  |  |             exec(python2_print_str) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-27 21:30:32 +01:00
										 |  |  |         self.assertIn("Missing parentheses in call to 'print'. Did you mean print(...)", | 
					
						
							|  |  |  |                 str(context.exception)) | 
					
						
							| 
									
										
										
										
											2017-06-20 19:01:32 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_string_with_excessive_whitespace(self): | 
					
						
							|  |  |  |         python2_print_str = 'print  "Hello World", ' | 
					
						
							|  |  |  |         with self.assertRaises(SyntaxError) as context: | 
					
						
							|  |  |  |             exec(python2_print_str) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-27 21:30:32 +01:00
										 |  |  |         self.assertIn("Missing parentheses in call to 'print'. Did you mean print(...)", | 
					
						
							|  |  |  |                 str(context.exception)) | 
					
						
							| 
									
										
										
										
											2017-06-20 19:01:32 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-20 08:42:22 +05:30
										 |  |  |     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) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-27 21:30:32 +01:00
										 |  |  |         self.assertIn("Missing parentheses in call to 'print'. Did you mean print(...)", | 
					
						
							|  |  |  |                 str(context.exception)) | 
					
						
							| 
									
										
										
										
											2018-01-20 08:42:22 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-28 16:26:02 +05:30
										 |  |  |     # 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) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-27 21:30:32 +01:00
										 |  |  |         self.assertIn("Missing parentheses in call to 'print'. Did you mean print(...)", | 
					
						
							|  |  |  |                 str(context.exception)) | 
					
						
							| 
									
										
										
										
											2018-01-28 16:26:02 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |     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) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-27 21:30:32 +01:00
										 |  |  |         self.assertIn("Missing parentheses in call to 'print'. Did you mean print(...)", | 
					
						
							|  |  |  |                 str(context.exception)) | 
					
						
							| 
									
										
										
										
											2018-01-28 16:26:02 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-18 16:07:36 +05:30
										 |  |  |     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>, ' | 
					
						
							| 
									
										
										
										
											2017-08-18 17:48:14 +05:30
										 |  |  |                 'file=<output_stream>)"?', str(context.exception)) | 
					
						
							| 
									
										
										
										
											2017-08-18 16:07:36 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |         # 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>, ' | 
					
						
							| 
									
										
										
										
											2017-08-18 17:48:14 +05:30
										 |  |  |                 'file=<output_stream>)"?', str(context.exception)) | 
					
						
							| 
									
										
										
										
											2017-08-18 16:07:36 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |         # 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) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-20 19:01:32 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-18 15:35:27 +00:00
										 |  |  | if __name__ == "__main__": | 
					
						
							| 
									
										
										
										
											2013-04-04 22:32:28 +03:00
										 |  |  |     unittest.main() |