summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorNick Coghlan <ncoghlan@gmail.com>2017-08-19 06:59:38 (GMT)
committerGitHub <noreply@github.com>2017-08-19 06:59:38 (GMT)
commit1a05e87ec75436d818f05a5dabcecaea67334cbd (patch)
tree1ce98f474172605ce4ca1916159a4f99e2a5a824 /Lib
parentb50e7683acac36ff16e6c6c2c32d9a15e46b5174 (diff)
downloadcpython-1a05e87ec75436d818f05a5dabcecaea67334cbd.zip
cpython-1a05e87ec75436d818f05a5dabcecaea67334cbd.tar.gz
cpython-1a05e87ec75436d818f05a5dabcecaea67334cbd.tar.bz2
[3.6] bpo-31232: Backport custom print rshift message (GH-3155)
bpo-30721 added a "Did you mean ...?" suggestion to rshift TypeError messages that triggers when the LHS is a Python C function called "print". Since this custom error message is expected to be triggered primarily by attempts to use Python 2 print redirection syntax in Python 3, and is incredibly unlikely to be encountered otherwise, it is also being backported to the next 3.6 maintenance release. Initial patch by Sanyam Khurana.
Diffstat (limited to 'Lib')
-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..e6434fe 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()