Skip to content

Conversation

@ammaraskar
Copy link
Member

@ammaraskar ammaraskar commented Jun 24, 2018

Most of the change involves fixing up the test suite, which previously made the assumption that there wouldn't be a new line if the input didn't end in one.

https://bugs.python.org/issue33899

line = b''
while True: # loop over lines in stream
try:
last_line = line
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't last_line only be set after StopIteration is caught? ISTM that in other cases we wouldn't want to be adding the newline at the end.

Copy link
Member Author

Choose a reason for hiding this comment

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

readline is one of the ancient APIs that existed before generators. There's two ways of stopping iteration, either raising StopIteration or returning the empty string, the latter gets caught all the way down here https://github.com/python/cpython/blob/master/Lib/tokenize.py#L528

How you're describing it is what I had initially but there's a few places in the loop where iteration can stop so I thought this would be simpler.

Copy link
Contributor

Choose a reason for hiding this comment

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

Then perhaps add a comment to that end? It seems rather crucial to understanding that particular piece of the code.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point, added.

Lib/tokenize.py Outdated
try:
# This loop has multiple points where it can break out so we
# pick up the value for the last_line here, at one unified
# point to keep things simple.
Copy link
Contributor

Choose a reason for hiding this comment

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

"at one unified point to keep things simple" should be removed IMO (unnecessary).

I suggest mentioning briefly why last_line is kept (to the best of my understanding: the last line is needed for the newline check after the loop, but line will be overridden at the last loop iteration).

@bedevere-bot
Copy link

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.

@ammaraskar
Copy link
Member Author

You're right, on a second read the comment didn't really explain much. Reworded.

I have made the requested changes; please review again

@bedevere-bot
Copy link

Thanks for making the requested changes!

@taleinat: please review the changes made to this pull request.

Copy link
Contributor

@taleinat taleinat left a comment

Choose a reason for hiding this comment

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

There's a whitespace issue in one of the files. For details, run make patchcheck or take a look at the output on Travis.

@bedevere-bot
Copy link

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.

@ammaraskar
Copy link
Member Author

Whoops, that's what I get for being lazy and using the github web editor.

I have made the requested changes; please review again

@bedevere-bot
Copy link

Thanks for making the requested changes!

@taleinat: please review the changes made to this pull request.

Copy link
Contributor

@taleinat taleinat left a comment

Choose a reason for hiding this comment

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

I've reviewed the whole PR again and a have a few final remarks. After these are fixed it would be good to go IMO.

Lib/tokenize.py Outdated
pos += 1

# Add an implicit NEWLINE if the input doesn't end in one
if len(last_line) > 0 and last_line[-1] not in '\r\n':
Copy link
Contributor

Choose a reason for hiding this comment

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

This should begin if last_line and ....

for type, token, start, end, line in tokenize(f.readline):
if type == ENDMARKER:
break
if s[-1] not in '\r\n' and type == NEWLINE and end[0] == num_lines:
Copy link
Contributor

Choose a reason for hiding this comment

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

This check is quite unreadable. Not simple to make more readable though. A short comment could help.

if type == ENDMARKER:
break
if s[-1] not in '\r\n' and type == NEWLINE and end[0] == num_lines:
continue
Copy link
Contributor

Choose a reason for hiding this comment

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

The check_tokenize() logic is repeated exactly, but it is now becoming rather involved. This should be a common function used by both classes.

@bedevere-bot
Copy link

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.

@ammaraskar
Copy link
Member Author

Thanks for the reviews Tal, I really appreciate it! I refactored out the common logic, does this seem fine? I also added a comment and added a variable in the check to convey the meaning better.

I have made the requested changes; please review again

@ammaraskar ammaraskar closed this Jul 6, 2018
@ammaraskar ammaraskar reopened this Jul 6, 2018
@taleinat taleinat merged commit c4ef489 into python:master Jul 6, 2018
@miss-islington
Copy link
Contributor

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

@miss-islington
Copy link
Contributor

Sorry, @ammaraskar and @taleinat, I could not cleanly backport this to 3.7 due to a conflict.
Please backport using cherry_picker on command line.
cherry_picker c4ef4896eac86a6759901c8546e26de4695a1389 3.7

@miss-islington
Copy link
Contributor

Sorry, @ammaraskar and @taleinat, I could not cleanly backport this to 2.7 due to a conflict.
Please backport using cherry_picker on command line.
cherry_picker c4ef4896eac86a6759901c8546e26de4695a1389 2.7

@miss-islington
Copy link
Contributor

Sorry, @ammaraskar and @taleinat, I could not cleanly backport this to 3.6 due to a conflict.
Please backport using cherry_picker on command line.
cherry_picker c4ef4896eac86a6759901c8546e26de4695a1389 3.6

@ammaraskar
Copy link
Member Author

Will backport by hand in a bit

@taleinat
Copy link
Contributor

taleinat commented Jul 6, 2018

@ammaraskar, ask me (via GitHub's "Reviewers" feature) to review the backport PRs when they're ready.

ammaraskar added a commit to ammaraskar/cpython that referenced this pull request Jul 6, 2018
…ne behavior (pythonGH-7891) Most of the change involves fixing up the test suite, which previously made the assumption that there wouldn't be a new line if the input didn't end in one. Contributed by Ammar Askar.. (cherry picked from commit c4ef489) Co-authored-by: Ammar Askar <ammar_askar@hotmail.com>
ammaraskar added a commit to ammaraskar/cpython that referenced this pull request Jul 6, 2018
…ne behavior (pythonGH-7891) Most of the change involves fixing up the test suite, which previously made the assumption that there wouldn't be a new line if the input didn't end in one. Contributed by Ammar Askar.. (cherry picked from commit c4ef489) Co-authored-by: Ammar Askar <ammar_askar@hotmail.com>
ammaraskar added a commit to ammaraskar/cpython that referenced this pull request Jul 6, 2018
…ne behavior (pythonGH-7891) Most of the change involves fixing up the test suite, which previously made the assumption that there wouldn't be a new line if the input didn't end in one. Contributed by Ammar Askar.. (cherry picked from commit c4ef489) Co-authored-by: Ammar Askar <ammar_askar@hotmail.com>
@ammaraskar
Copy link
Member Author

@taleinat
It seems like I can't touch the reviewers/labels etc, only people with access to the repo can.

2.7: #8133
3.6: #8134
3.7: #8132

@taleinat
Copy link
Contributor

taleinat commented Jul 6, 2018

Thanks, @ammaraskar.

taleinat pushed a commit that referenced this pull request Jul 6, 2018
…ne behavior (GH-7891) (GH-8132) Most of the change involves fixing up the test suite, which previously made the assumption that there wouldn't be a new line if the input didn't end in one. Contributed by Ammar Askar. (cherry picked from commit c4ef489)
taleinat pushed a commit that referenced this pull request Jul 6, 2018
…ne behavior (GH-7891) (GH-8134) Most of the change involves fixing up the test suite, which previously made the assumption that there wouldn't be a new line if the input didn't end in one. Contributed by Ammar Askar. (cherry picked from commit c4ef489)
taleinat pushed a commit that referenced this pull request Jul 6, 2018
…ne behavior (GH-7891) (#8133) Most of the change involves fixing up the test suite, which previously made the assumption that there wouldn't be a new line if the input didn't end in one. Contributed by Ammar Askar. (cherry picked from commit c4ef489)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

5 participants