FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

bpo-32028: Fix custom print suggestion having leading whitespace in print statement by CuriousLearner · Pull Request #4688 · python/cpython · GitHub

Repository navigation

bpo-32028: Fix custom print suggestion having leading whitespace in print statement - #4688

Merged
ncoghlan merged 7 commits into
python:masterfrom
CuriousLearner:fix-issue32028
Jan 20, 2018
Merged

ncoghlan merged 7 commits into
python:masterfrom
CuriousLearner:fix-issue32028

Conversation

CuriousLearner commented Dec 3, 2017 •
edited by bedevere-bot
Loading

Copy link
Copy Markdown
Member

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

ncoghlan left a comment •
edited
Loading

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

The new test case is good, and the implementation changes are headed in the right general direction. However, it should be sufficient to reorder the existing operations, rather than calling _PyUnicode_XStrip twice.

Comment thread Objects/exceptions.c Outdated
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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Rather than adding a new call to _PyUnicode_XStrip, just reorder the existing operations:

  1. First strip any surrounding ASCII whitespace (i.e. do this first, instead of last)
  2. Then get the length of the result (rather than the length of the original text)
  3. Then skip over PRINT_OFFSET characters at the beginning

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

+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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

@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?

cc @serhiy-storchaka

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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.

Comment thread Lib/test/test_print.py Outdated

def test_string_with_leading_whitespace(self):
python2_print_str = '''if 1:
print "Hello World"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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)

Copy link
Copy Markdown

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 I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Don't forget to check for errors and count references.

Comment thread Objects/exceptions.c
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);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

initial_data can be NULL.

Comment thread Objects/exceptions.c Outdated
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), \

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

The result of PyUnicode_Substring() can be NULL.
Otherwise it will be leaked.

Copy link
Copy Markdown
Member Author

@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 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Please remove redundant empty lines.

Copy link
Copy Markdown
Member Author

@serhiy-storchaka Fixed :)

ncoghlan left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I'd missed that the second _PyUnicode_XStrip call handled excess whitespace between print and the expression being printed, so I was wrong about that being redundant (and the regression test suite was right).

I've adjusted the NEWS entry wording, so +1 from me (I'll merge once CI finishes).

Copy link
Copy Markdown
Member Author

Thank you so much @ncoghlan & @serhiy-storchaka :)

CuriousLearner commented Dec 16, 2017 •
edited
Loading

Copy link
Copy Markdown
Member Author

@serhiy-storchaka @ncoghlan I guess we can merge this now :)

Also needs a backport to 3.6 label on this one.

Copy link
Copy Markdown
Member Author

Hi @serhiy-storchaka @ncoghlan

Do we need something else here? Please let me know and I'll do that :)

Copy link
Copy Markdown
Contributor

Just closing & reopending to restart the CI (Appveyor's a required check now, and it didn't run properly)

ncoghlan closed this Jan 18, 2018
ncoghlan reopened this Jan 18, 2018
ncoghlan merged commit d57f26c into python:master Jan 20, 2018

Copy link
Copy Markdown
Contributor

Thanks @CuriousLearner for the PR, and @ncoghlan for merging it 🌮🎉.. I'm working now to backport this PR to: 3.6.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jan 20, 2018
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)

Copy link
Copy Markdown

GH-5249 is a backport of this pull request to the 3.6 branch.

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants


Back | FazBrowse Home | New Git URL