summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2018-01-20 03:56:31 (GMT)
committerNick Coghlan <ncoghlan@gmail.com>2018-01-20 03:56:31 (GMT)
commit4002d5dbf4c058bbf2462f9f5dea057956d1caff (patch)
treea6da37135e5e5e0f9f830db514a8849597f4b608
parent051650ab8de620dd7e3c557f8227c43439ea8c6a (diff)
downloadcpython-4002d5dbf4c058bbf2462f9f5dea057956d1caff.zip
cpython-4002d5dbf4c058bbf2462f9f5dea057956d1caff.tar.gz
cpython-4002d5dbf4c058bbf2462f9f5dea057956d1caff.tar.bz2
[3.6] bpo-32028: Fix suggestions for indented print statements (GH-5249)
The suggested replacement for print statements previously failed to account for leading whitespace and hence could end up including unwanted text in the proposed call to the print builtin. Patch by Sanyam Khurana. (cherry picked from commit d57f26c753dce61f72b52b96db3a3253d9f2fc3e)
-rw-r--r--Lib/test/test_print.py9
-rw-r--r--Misc/NEWS.d/next/Core and Builtins/2017-12-03-22-29-13.bpo-32028.KC2w4Q.rst3
-rw-r--r--Objects/exceptions.c16
3 files changed, 23 insertions, 5 deletions
diff --git a/Lib/test/test_print.py b/Lib/test/test_print.py
index e6434fe..7bc23cf 100644
--- a/Lib/test/test_print.py
+++ b/Lib/test/test_print.py
@@ -156,6 +156,15 @@ class TestPy2MigrationHint(unittest.TestCase):
self.assertIn('print("Hello World", end=" ")', str(context.exception))
+ 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)
+
+ self.assertIn('print("Hello World")', 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:
diff --git a/Misc/NEWS.d/next/Core and Builtins/2017-12-03-22-29-13.bpo-32028.KC2w4Q.rst b/Misc/NEWS.d/next/Core and Builtins/2017-12-03-22-29-13.bpo-32028.KC2w4Q.rst
new file mode 100644
index 0000000..8e2b2e1
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2017-12-03-22-29-13.bpo-32028.KC2w4Q.rst
@@ -0,0 +1,3 @@
+Leading whitespace is now correctly ignored when generating suggestions
+for converting Py2 print statements to Py3 builtin print function calls.
+Patch by Sanyam Khurana.
diff --git a/Objects/exceptions.c b/Objects/exceptions.c
index df48993..0a75f1b 100644
--- a/Objects/exceptions.c
+++ b/Objects/exceptions.c
@@ -2865,17 +2865,23 @@ _set_legacy_print_statement_msg(PySyntaxErrorObject *self, Py_ssize_t start)
// PRINT_OFFSET is to remove `print ` word from the data.
const int PRINT_OFFSET = 6;
- Py_ssize_t text_len = PyUnicode_GET_LENGTH(self->text);
- PyObject *data = PyUnicode_Substring(self->text, PRINT_OFFSET, text_len);
-
+ const int STRIP_BOTH = 2;
+ // Issue 32028: Handle case when whitespace is used with print call
+ PyObject *initial_data = _PyUnicode_XStrip(self->text, STRIP_BOTH, strip_sep_obj);
+ if (initial_data == NULL) {
+ Py_DECREF(strip_sep_obj);
+ return -1;
+ }
+ Py_ssize_t text_len = PyUnicode_GET_LENGTH(initial_data);
+ PyObject *data = PyUnicode_Substring(initial_data, PRINT_OFFSET, text_len);
+ Py_DECREF(initial_data);
if (data == NULL) {
Py_DECREF(strip_sep_obj);
return -1;
}
- PyObject *new_data = _PyUnicode_XStrip(data, 2, strip_sep_obj);
+ PyObject *new_data = _PyUnicode_XStrip(data, STRIP_BOTH, strip_sep_obj);
Py_DECREF(data);
Py_DECREF(strip_sep_obj);
-
if (new_data == NULL) {
return -1;
}