Eric Smith | 799c300 | 2008-03-18 15:35:27 +0000 | [diff] [blame] | 1 | import unittest |
Sanyam Khurana | 5e2eb35 | 2017-08-18 16:07:36 +0530 | [diff] [blame] | 2 | import sys |
Andrew Svetlov | ace34dd | 2013-04-04 22:32:28 +0300 | [diff] [blame] | 3 | from io import StringIO |
Eric Smith | 799c300 | 2008-03-18 15:35:27 +0000 | [diff] [blame] | 4 | |
Andrew Svetlov | ace34dd | 2013-04-04 22:32:28 +0300 | [diff] [blame] | 5 | from test import support |
Eric Smith | 01a3e2b | 2008-03-18 23:48:28 +0000 | [diff] [blame] | 6 | |
Eric Smith | 799c300 | 2008-03-18 15:35:27 +0000 | [diff] [blame] | 7 | NotDefined = object() |
| 8 | |
| 9 | # A dispatch table all 8 combinations of providing |
Andrew Svetlov | ace34dd | 2013-04-04 22:32:28 +0300 | [diff] [blame] | 10 | # sep, end, and file. |
Eric Smith | 799c300 | 2008-03-18 15:35:27 +0000 | [diff] [blame] | 11 | # I use this machinery so that I'm not just passing default |
Andrew Svetlov | ace34dd | 2013-04-04 22:32:28 +0300 | [diff] [blame] | 12 | # values to print, I'm either passing or not passing in the |
| 13 | # arguments. |
Eric Smith | 799c300 | 2008-03-18 15:35:27 +0000 | [diff] [blame] | 14 | dispatch = { |
| 15 | (False, False, False): |
Andrew Svetlov | ace34dd | 2013-04-04 22:32:28 +0300 | [diff] [blame] | 16 | lambda args, sep, end, file: print(*args), |
Eric Smith | 799c300 | 2008-03-18 15:35:27 +0000 | [diff] [blame] | 17 | (False, False, True): |
Andrew Svetlov | ace34dd | 2013-04-04 22:32:28 +0300 | [diff] [blame] | 18 | lambda args, sep, end, file: print(file=file, *args), |
Eric Smith | 799c300 | 2008-03-18 15:35:27 +0000 | [diff] [blame] | 19 | (False, True, False): |
Andrew Svetlov | ace34dd | 2013-04-04 22:32:28 +0300 | [diff] [blame] | 20 | lambda args, sep, end, file: print(end=end, *args), |
Eric Smith | 799c300 | 2008-03-18 15:35:27 +0000 | [diff] [blame] | 21 | (False, True, True): |
Andrew Svetlov | ace34dd | 2013-04-04 22:32:28 +0300 | [diff] [blame] | 22 | lambda args, sep, end, file: print(end=end, file=file, *args), |
Eric Smith | 799c300 | 2008-03-18 15:35:27 +0000 | [diff] [blame] | 23 | (True, False, False): |
Andrew Svetlov | ace34dd | 2013-04-04 22:32:28 +0300 | [diff] [blame] | 24 | lambda args, sep, end, file: print(sep=sep, *args), |
Eric Smith | 799c300 | 2008-03-18 15:35:27 +0000 | [diff] [blame] | 25 | (True, False, True): |
Andrew Svetlov | ace34dd | 2013-04-04 22:32:28 +0300 | [diff] [blame] | 26 | lambda args, sep, end, file: print(sep=sep, file=file, *args), |
Eric Smith | 799c300 | 2008-03-18 15:35:27 +0000 | [diff] [blame] | 27 | (True, True, False): |
Andrew Svetlov | ace34dd | 2013-04-04 22:32:28 +0300 | [diff] [blame] | 28 | lambda args, sep, end, file: print(sep=sep, end=end, *args), |
Eric Smith | 799c300 | 2008-03-18 15:35:27 +0000 | [diff] [blame] | 29 | (True, True, True): |
Andrew Svetlov | ace34dd | 2013-04-04 22:32:28 +0300 | [diff] [blame] | 30 | lambda args, sep, end, file: print(sep=sep, end=end, file=file, *args), |
| 31 | } |
| 32 | |
Eric Smith | 799c300 | 2008-03-18 15:35:27 +0000 | [diff] [blame] | 33 | |
Eric Smith | 799c300 | 2008-03-18 15:35:27 +0000 | [diff] [blame] | 34 | # Class used to test __str__ and print |
| 35 | class ClassWith__str__: |
| 36 | def __init__(self, x): |
| 37 | self.x = x |
Andrew Svetlov | ace34dd | 2013-04-04 22:32:28 +0300 | [diff] [blame] | 38 | |
Eric Smith | 799c300 | 2008-03-18 15:35:27 +0000 | [diff] [blame] | 39 | def __str__(self): |
| 40 | return self.x |
| 41 | |
Eric Smith | 799c300 | 2008-03-18 15:35:27 +0000 | [diff] [blame] | 42 | |
Andrew Svetlov | ace34dd | 2013-04-04 22:32:28 +0300 | [diff] [blame] | 43 | class TestPrint(unittest.TestCase): |
| 44 | """Test correct operation of the print function.""" |
| 45 | |
| 46 | def check(self, expected, args, |
| 47 | sep=NotDefined, end=NotDefined, file=NotDefined): |
| 48 | # Capture sys.stdout in a StringIO. Call print with args, |
| 49 | # and with sep, end, and file, if they're defined. Result |
| 50 | # must match expected. |
| 51 | |
| 52 | # Look up the actual function to call, based on if sep, end, |
| 53 | # and file are defined. |
Eric Smith | 799c300 | 2008-03-18 15:35:27 +0000 | [diff] [blame] | 54 | fn = dispatch[(sep is not NotDefined, |
| 55 | end is not NotDefined, |
| 56 | file is not NotDefined)] |
| 57 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 58 | with support.captured_stdout() as t: |
Eric Smith | 799c300 | 2008-03-18 15:35:27 +0000 | [diff] [blame] | 59 | fn(args, sep, end, file) |
| 60 | |
| 61 | self.assertEqual(t.getvalue(), expected) |
| 62 | |
| 63 | def test_print(self): |
Eric Smith | 01a3e2b | 2008-03-18 23:48:28 +0000 | [diff] [blame] | 64 | def x(expected, args, sep=NotDefined, end=NotDefined): |
Eric Smith | 799c300 | 2008-03-18 15:35:27 +0000 | [diff] [blame] | 65 | # Run the test 2 ways: not using file, and using |
Andrew Svetlov | ace34dd | 2013-04-04 22:32:28 +0300 | [diff] [blame] | 66 | # file directed to a StringIO. |
Eric Smith | 799c300 | 2008-03-18 15:35:27 +0000 | [diff] [blame] | 67 | |
| 68 | self.check(expected, args, sep=sep, end=end) |
| 69 | |
| 70 | # When writing to a file, stdout is expected to be empty |
Eric Smith | 01a3e2b | 2008-03-18 23:48:28 +0000 | [diff] [blame] | 71 | o = StringIO() |
Eric Smith | 799c300 | 2008-03-18 15:35:27 +0000 | [diff] [blame] | 72 | self.check('', args, sep=sep, end=end, file=o) |
| 73 | |
| 74 | # And o will contain the expected output |
| 75 | self.assertEqual(o.getvalue(), expected) |
| 76 | |
| 77 | x('\n', ()) |
| 78 | x('a\n', ('a',)) |
| 79 | x('None\n', (None,)) |
| 80 | x('1 2\n', (1, 2)) |
Eric Smith | 01a3e2b | 2008-03-18 23:48:28 +0000 | [diff] [blame] | 81 | x('1 2\n', (1, ' ', 2)) |
Eric Smith | 799c300 | 2008-03-18 15:35:27 +0000 | [diff] [blame] | 82 | x('1*2\n', (1, 2), sep='*') |
| 83 | x('1 s', (1, 's'), end='') |
| 84 | x('a\nb\n', ('a', 'b'), sep='\n') |
| 85 | x('1.01', (1.0, 1), sep='', end='') |
| 86 | x('1*a*1.3+', (1, 'a', 1.3), sep='*', end='+') |
Eric Smith | 01a3e2b | 2008-03-18 23:48:28 +0000 | [diff] [blame] | 87 | x('a\n\nb\n', ('a\n', 'b'), sep='\n') |
| 88 | x('\0+ +\0\n', ('\0', ' ', '\0'), sep='+') |
| 89 | |
| 90 | x('a\n b\n', ('a\n', 'b')) |
| 91 | x('a\n b\n', ('a\n', 'b'), sep=None) |
| 92 | x('a\n b\n', ('a\n', 'b'), end=None) |
| 93 | x('a\n b\n', ('a\n', 'b'), sep=None, end=None) |
Eric Smith | 799c300 | 2008-03-18 15:35:27 +0000 | [diff] [blame] | 94 | |
| 95 | x('*\n', (ClassWith__str__('*'),)) |
Eric Smith | 01a3e2b | 2008-03-18 23:48:28 +0000 | [diff] [blame] | 96 | x('abc 1\n', (ClassWith__str__('abc'), 1)) |
| 97 | |
| 98 | # errors |
| 99 | self.assertRaises(TypeError, print, '', sep=3) |
| 100 | self.assertRaises(TypeError, print, '', end=3) |
| 101 | self.assertRaises(AttributeError, print, '', file='') |
Eric Smith | 799c300 | 2008-03-18 15:35:27 +0000 | [diff] [blame] | 102 | |
Georg Brandl | bc3b682 | 2012-01-13 19:41:25 +0100 | [diff] [blame] | 103 | def test_print_flush(self): |
| 104 | # operation of the flush flag |
Andrew Svetlov | ace34dd | 2013-04-04 22:32:28 +0300 | [diff] [blame] | 105 | class filelike: |
Georg Brandl | bc3b682 | 2012-01-13 19:41:25 +0100 | [diff] [blame] | 106 | def __init__(self): |
| 107 | self.written = '' |
| 108 | self.flushed = 0 |
Andrew Svetlov | ace34dd | 2013-04-04 22:32:28 +0300 | [diff] [blame] | 109 | |
Georg Brandl | bc3b682 | 2012-01-13 19:41:25 +0100 | [diff] [blame] | 110 | def write(self, str): |
| 111 | self.written += str |
Andrew Svetlov | ace34dd | 2013-04-04 22:32:28 +0300 | [diff] [blame] | 112 | |
Georg Brandl | bc3b682 | 2012-01-13 19:41:25 +0100 | [diff] [blame] | 113 | def flush(self): |
| 114 | self.flushed += 1 |
| 115 | |
| 116 | f = filelike() |
| 117 | print(1, file=f, end='', flush=True) |
| 118 | print(2, file=f, end='', flush=True) |
| 119 | print(3, file=f, flush=False) |
| 120 | self.assertEqual(f.written, '123\n') |
| 121 | self.assertEqual(f.flushed, 2) |
| 122 | |
| 123 | # ensure exceptions from flush are passed through |
Andrew Svetlov | ace34dd | 2013-04-04 22:32:28 +0300 | [diff] [blame] | 124 | class noflush: |
Georg Brandl | bc3b682 | 2012-01-13 19:41:25 +0100 | [diff] [blame] | 125 | def write(self, str): |
| 126 | pass |
Andrew Svetlov | ace34dd | 2013-04-04 22:32:28 +0300 | [diff] [blame] | 127 | |
Georg Brandl | bc3b682 | 2012-01-13 19:41:25 +0100 | [diff] [blame] | 128 | def flush(self): |
| 129 | raise RuntimeError |
| 130 | self.assertRaises(RuntimeError, print, 1, file=noflush(), flush=True) |
| 131 | |
Sanyam Khurana | 3a7f035 | 2017-06-20 19:01:32 +0530 | [diff] [blame] | 132 | |
| 133 | class TestPy2MigrationHint(unittest.TestCase): |
| 134 | """Test that correct hint is produced analogous to Python3 syntax, |
| 135 | if print statement is executed as in Python 2. |
| 136 | """ |
| 137 | |
| 138 | def test_normal_string(self): |
| 139 | python2_print_str = 'print "Hello World"' |
| 140 | with self.assertRaises(SyntaxError) as context: |
| 141 | exec(python2_print_str) |
| 142 | |
| 143 | self.assertIn('print("Hello World")', str(context.exception)) |
| 144 | |
| 145 | def test_string_with_soft_space(self): |
| 146 | python2_print_str = 'print "Hello World",' |
| 147 | with self.assertRaises(SyntaxError) as context: |
| 148 | exec(python2_print_str) |
| 149 | |
| 150 | self.assertIn('print("Hello World", end=" ")', str(context.exception)) |
| 151 | |
| 152 | def test_string_with_excessive_whitespace(self): |
| 153 | python2_print_str = 'print "Hello World", ' |
| 154 | with self.assertRaises(SyntaxError) as context: |
| 155 | exec(python2_print_str) |
| 156 | |
| 157 | self.assertIn('print("Hello World", end=" ")', str(context.exception)) |
| 158 | |
Sanyam Khurana | d57f26c | 2018-01-20 08:42:22 +0530 | [diff] [blame] | 159 | def test_string_with_leading_whitespace(self): |
| 160 | python2_print_str = '''if 1: |
| 161 | print "Hello World" |
| 162 | ''' |
| 163 | with self.assertRaises(SyntaxError) as context: |
| 164 | exec(python2_print_str) |
| 165 | |
| 166 | self.assertIn('print("Hello World")', str(context.exception)) |
| 167 | |
Nitish Chandra | 43c0f1a | 2018-01-28 16:26:02 +0530 | [diff] [blame] | 168 | # bpo-32685: Suggestions for print statement should be proper when |
| 169 | # it is in the same line as the header of a compound statement |
| 170 | # and/or followed by a semicolon |
| 171 | def test_string_with_semicolon(self): |
| 172 | python2_print_str = 'print p;' |
| 173 | with self.assertRaises(SyntaxError) as context: |
| 174 | exec(python2_print_str) |
| 175 | |
| 176 | self.assertIn('print(p)', str(context.exception)) |
| 177 | |
| 178 | def test_string_in_loop_on_same_line(self): |
| 179 | python2_print_str = 'for i in s: print i' |
| 180 | with self.assertRaises(SyntaxError) as context: |
| 181 | exec(python2_print_str) |
| 182 | |
| 183 | self.assertIn('print(i)', str(context.exception)) |
| 184 | |
Sanyam Khurana | 5e2eb35 | 2017-08-18 16:07:36 +0530 | [diff] [blame] | 185 | def test_stream_redirection_hint_for_py2_migration(self): |
| 186 | # Test correct hint produced for Py2 redirection syntax |
| 187 | with self.assertRaises(TypeError) as context: |
| 188 | print >> sys.stderr, "message" |
| 189 | self.assertIn('Did you mean "print(<message>, ' |
Sanyam Khurana | a7c449b | 2017-08-18 17:48:14 +0530 | [diff] [blame] | 190 | 'file=<output_stream>)"?', str(context.exception)) |
Sanyam Khurana | 5e2eb35 | 2017-08-18 16:07:36 +0530 | [diff] [blame] | 191 | |
| 192 | # Test correct hint is produced in the case where RHS implements |
| 193 | # __rrshift__ but returns NotImplemented |
| 194 | with self.assertRaises(TypeError) as context: |
| 195 | print >> 42 |
| 196 | self.assertIn('Did you mean "print(<message>, ' |
Sanyam Khurana | a7c449b | 2017-08-18 17:48:14 +0530 | [diff] [blame] | 197 | 'file=<output_stream>)"?', str(context.exception)) |
Sanyam Khurana | 5e2eb35 | 2017-08-18 16:07:36 +0530 | [diff] [blame] | 198 | |
| 199 | # Test stream redirection hint is specific to print |
| 200 | with self.assertRaises(TypeError) as context: |
| 201 | max >> sys.stderr |
| 202 | self.assertNotIn('Did you mean ', str(context.exception)) |
| 203 | |
| 204 | # Test stream redirection hint is specific to rshift |
| 205 | with self.assertRaises(TypeError) as context: |
| 206 | print << sys.stderr |
| 207 | self.assertNotIn('Did you mean', str(context.exception)) |
| 208 | |
| 209 | # Ensure right operand implementing rrshift still works |
| 210 | class OverrideRRShift: |
| 211 | def __rrshift__(self, lhs): |
| 212 | return 42 # Force result independent of LHS |
| 213 | |
| 214 | self.assertEqual(print >> OverrideRRShift(), 42) |
| 215 | |
| 216 | |
Sanyam Khurana | 3a7f035 | 2017-06-20 19:01:32 +0530 | [diff] [blame] | 217 | |
Eric Smith | 799c300 | 2008-03-18 15:35:27 +0000 | [diff] [blame] | 218 | if __name__ == "__main__": |
Andrew Svetlov | ace34dd | 2013-04-04 22:32:28 +0300 | [diff] [blame] | 219 | unittest.main() |