Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
| @@ -2847,7 +2847,10 @@ _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); | |||
| // Issue 32028: Handle case when whitespace is used with print call | |||
| PyObject *initial_data = _PyUnicode_XStrip(self->text, 2, strip_sep_obj); | |||
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Rather than adding a new call to _PyUnicode_XStrip, just reorder the existing operations:
PRINT_OFFSET characters at the beginningThere was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
A minor point I missed when reviewing the original PR: defining and using const int STRIP_BOTH = 2; will make the _PyUnicode_XStrip call more self explanatory.
So we may as well make that change now, since the code is being modified anyway.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
+1 for the const int STRIP_BOTH = 2;. I was explaining my patch to a few folks and I had to consult my blog for that particular thing. Few months down the line I didn't even remember that.
So, yes that would be great.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
@ncoghlan So, while I modified the code, one of the test cases fails with re-ordering the code. That test case is:
test_string_with_excessive_whitespace. Since we're only stripping the the data at the beginning, and then directly trying to extract the sub-string.
I think we may need to strip the leading chars to make the previous test case pass. What do you say?
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I've pushed my current code which re-orders the existing operations & the test_string_with_excessive_whitespace is failing. I'm waiting for your reply on this. I think we might want to strip the leading whitespace from data.
| @@ -156,6 +156,15 @@ def test_string_with_excessive_whitespace(self): | |||
|
|
|||
| self.assertIn('print("Hello World", end=" ")', str(context.exception)) | |||
|
|
|||
| def test_string_with_leading_whitespace(self): | |||
| python2_print_str = '''if 1: | |||
| print "Hello World" | |||
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Since we're stripping all leading whitespace, you may as well indent this 4 spaces relative to the target variable name (so 12 leading spaces total)
|
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
| 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); | ||
| Py_ssize_t text_len = PyUnicode_GET_LENGTH(initial_data); |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
initial_data can be NULL.
| PyObject *initial_data = _PyUnicode_XStrip(self->text, STRIP_BOTH, strip_sep_obj); | ||
| Py_ssize_t text_len = PyUnicode_GET_LENGTH(initial_data); | ||
| PyObject *data = _PyUnicode_XStrip( \ | ||
| PyUnicode_Substring(initial_data, PRINT_OFFSET, text_len), \ |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
The result of PyUnicode_Substring() can be NULL.
Otherwise it will be leaked.
|
@serhiy-storchaka Nice catch! Sorry, I missed those memory leaks. I've tried to address those issues. Can you please take a pass. |
|
@serhiy-storchaka Fixed :) |
|
Thank you so much @ncoghlan & @serhiy-storchaka :) |
|
@serhiy-storchaka @ncoghlan I guess we can merge this now :) Also needs a |
|
Hi @serhiy-storchaka @ncoghlan Do we need something else here? Please let me know and I'll do that :) |
|
Just closing & reopending to restart the CI (Appveyor's a required check now, and it didn't run properly) |
|
Thanks @CuriousLearner for the PR, and @ncoghlan for merging it |
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 d57f26c)
|
GH-5249 is a backport of this pull request to the 3.6 branch. |
This fixes the newly added print suggestion for cases when there is leading whitespace in the initial data.
I've also added a test case for this. @ncoghlan Can you please check this?
https://bugs.python.org/issue32028