summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorSanyam Khurana <8039608+CuriousLearner@users.noreply.github.com>2018-01-20 03:12:22 (GMT)
committerNick Coghlan <ncoghlan@gmail.com>2018-01-20 03:12:22 (GMT)
commitd57f26c753dce61f72b52b96db3a3253d9f2fc3e (patch)
tree45edd2bbbd2a8abdfefacf29a52ad45e8a1dfbb4 /Objects
parent6690bb9f17d34eb3dec0aca8919d8d27d6c3c452 (diff)
downloadcpython-d57f26c753dce61f72b52b96db3a3253d9f2fc3e.zip
cpython-d57f26c753dce61f72b52b96db3a3253d9f2fc3e.tar.gz
cpython-d57f26c753dce61f72b52b96db3a3253d9f2fc3e.tar.bz2
bpo-32028: Fix suggestions for indented print statements (GH-4688)
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.
Diffstat (limited to 'Objects')
-rw-r--r--Objects/exceptions.c16
1 files changed, 11 insertions, 5 deletions
diff --git a/Objects/exceptions.c b/Objects/exceptions.c
index 4901eb1..d59abd1 100644
--- a/Objects/exceptions.c
+++ b/Objects/exceptions.c
@@ -2846,17 +2846,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;
}