summaryrefslogtreecommitdiffstats
path: root/Objects/exceptions.c
diff options
context:
space:
mode:
authorNitish Chandra <nitishchandrachinta@gmail.com>2018-01-28 10:56:02 (GMT)
committerNick Coghlan <ncoghlan@gmail.com>2018-01-28 10:56:02 (GMT)
commit43c0f1ac5ed8bc9c3bd048d2ce4de4c98a83de99 (patch)
treee2a5ab622dbb25f3ec3b6f66099ad1aaaab37acd /Objects/exceptions.c
parent255f7a26da47ca6b7bd1d375b8a04920f68c119c (diff)
downloadcpython-43c0f1ac5ed8bc9c3bd048d2ce4de4c98a83de99.zip
cpython-43c0f1ac5ed8bc9c3bd048d2ce4de4c98a83de99.tar.gz
cpython-43c0f1ac5ed8bc9c3bd048d2ce4de4c98a83de99.tar.bz2
bpo-32685: Improve suggestion for print statement (GH-5375)
Better account for single-line compound statements and semi-colon separated statements when suggesting Py3 replacements for Py2 print statements. Initial patch by Nitish Chandra.
Diffstat (limited to 'Objects/exceptions.c')
-rw-r--r--Objects/exceptions.c33
1 files changed, 19 insertions, 14 deletions
diff --git a/Objects/exceptions.c b/Objects/exceptions.c
index d59abd1..2cce40f 100644
--- a/Objects/exceptions.c
+++ b/Objects/exceptions.c
@@ -2840,26 +2840,31 @@ _PyErr_TrySetFromCause(const char *format, ...)
static int
_set_legacy_print_statement_msg(PySyntaxErrorObject *self, Py_ssize_t start)
{
- PyObject *strip_sep_obj = PyUnicode_FromString(" \t\r\n");
- if (strip_sep_obj == NULL)
- return -1;
-
- // PRINT_OFFSET is to remove `print ` word from the data.
+ // PRINT_OFFSET is to remove the `print ` prefix from the data.
const int PRINT_OFFSET = 6;
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 start_pos = start + PRINT_OFFSET;
+ Py_ssize_t text_len = PyUnicode_GET_LENGTH(self->text);
+ Py_UCS4 semicolon = ';';
+ Py_ssize_t end_pos = PyUnicode_FindChar(self->text, semicolon,
+ start_pos, text_len, 1);
+ if (end_pos < -1) {
+ return -1;
+ } else if (end_pos == -1) {
+ end_pos = text_len;
}
- Py_ssize_t text_len = PyUnicode_GET_LENGTH(initial_data);
- PyObject *data = PyUnicode_Substring(initial_data, PRINT_OFFSET, text_len);
- Py_DECREF(initial_data);
+
+ PyObject *data = PyUnicode_Substring(self->text, start_pos, end_pos);
if (data == NULL) {
- Py_DECREF(strip_sep_obj);
return -1;
}
+
+ PyObject *strip_sep_obj = PyUnicode_FromString(" \t\r\n");
+ if (strip_sep_obj == NULL) {
+ Py_DECREF(data);
+ return -1;
+ }
+
PyObject *new_data = _PyUnicode_XStrip(data, STRIP_BOTH, strip_sep_obj);
Py_DECREF(data);
Py_DECREF(strip_sep_obj);