summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorSanyam Khurana <sanyam@sanyamkhurana.com>2017-08-18 10:37:36 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2017-08-18 10:37:36 (GMT)
commit5e2eb35bbed3e84079165e576cdb50ef36e13493 (patch)
treea2f513631279ac12788b04a56f7f61fb63c81068 /Lib/test
parentad7eaed54382b346784e51a6f0122ce81e8842b5 (diff)
downloadcpython-5e2eb35bbed3e84079165e576cdb50ef36e13493.zip
cpython-5e2eb35bbed3e84079165e576cdb50ef36e13493.tar.gz
cpython-5e2eb35bbed3e84079165e576cdb50ef36e13493.tar.bz2
bpo-30721: Show correct syntax hint in Py3 when using Py2 redirection syntax (#2345)
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_print.py33
1 files changed, 33 insertions, 0 deletions
diff --git a/Lib/test/test_print.py b/Lib/test/test_print.py
index 03f13b4..8099327 100644
--- a/Lib/test/test_print.py
+++ b/Lib/test/test_print.py
@@ -1,4 +1,5 @@
import unittest
+import sys
from io import StringIO
from test import support
@@ -155,6 +156,38 @@ class TestPy2MigrationHint(unittest.TestCase):
self.assertIn('print("Hello World", end=" ")', 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()