|
1 | 1 | import unittest |
| 2 | +import sys |
2 | 3 | from io import StringIO |
3 | 4 |
|
4 | 5 | from test import support |
@@ -155,6 +156,38 @@ def test_string_with_excessive_whitespace(self): |
155 | 156 |
|
156 | 157 | self.assertIn('print("Hello World", end=" ")', str(context.exception)) |
157 | 158 |
|
| 159 | + def test_stream_redirection_hint_for_py2_migration(self): |
| 160 | + # Test correct hint produced for Py2 redirection syntax |
| 161 | + with self.assertRaises(TypeError) as context: |
| 162 | + print >> sys.stderr, "message" |
| 163 | + self.assertIn('Did you mean "print(<message>, ' |
| 164 | + 'file=<output_stream>)', str(context.exception)) |
| 165 | + |
| 166 | + # Test correct hint is produced in the case where RHS implements |
| 167 | + # __rrshift__ but returns NotImplemented |
| 168 | + with self.assertRaises(TypeError) as context: |
| 169 | + print >> 42 |
| 170 | + self.assertIn('Did you mean "print(<message>, ' |
| 171 | + 'file=<output_stream>)', str(context.exception)) |
| 172 | + |
| 173 | + # Test stream redirection hint is specific to print |
| 174 | + with self.assertRaises(TypeError) as context: |
| 175 | + max >> sys.stderr |
| 176 | + self.assertNotIn('Did you mean ', str(context.exception)) |
| 177 | + |
| 178 | + # Test stream redirection hint is specific to rshift |
| 179 | + with self.assertRaises(TypeError) as context: |
| 180 | + print << sys.stderr |
| 181 | + self.assertNotIn('Did you mean', str(context.exception)) |
| 182 | + |
| 183 | + # Ensure right operand implementing rrshift still works |
| 184 | + class OverrideRRShift: |
| 185 | + def __rrshift__(self, lhs): |
| 186 | + return 42 # Force result independent of LHS |
| 187 | + |
| 188 | + self.assertEqual(print >> OverrideRRShift(), 42) |
| 189 | + |
| 190 | + |
158 | 191 |
|
159 | 192 | if __name__ == "__main__": |
160 | 193 | unittest.main() |
0 commit comments