-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
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
Enhancements to running code in a terminal #1432
Enhancements to running code in a terminal #1432
Conversation
Codecov Report
@@ Coverage Diff @@
## master #1432 +/- ##
===========================================
- Coverage 70.8% 45.29% -25.52%
===========================================
Files 271 271
Lines 12519 12550 +31
Branches 2224 2231 +7
===========================================
- Hits 8864 5684 -3180
- Misses 3512 6716 +3204
- Partials 143 150 +7
Continue to review full report at Codecov.
|
@brettcannon Please review. See #259 for details. Oh yes, this will require a lot of testing. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would also add a test that includes UTF-8 Unicode characters.
unicode = str | ||
|
||
|
||
def normalizeLines(content): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
normalize_lines
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
aaarg..
|
||
|
||
def normalizeLines(content): | ||
"""Removes empty lines and adds empty lines only to sepaparate |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Docstrings always have a single line summary that fits within the 80 column limit, then a newline, then anything extra. E.g.
def normalize_lines(content):
"""Normalize blank lines for sending to the terminal.
Blank lines within a statement block are removed to prevent the REPL
from thinking the block is finished. Newlines are added to separate
top-level statements so that the REPL does not think there is a syntax
error.
"""
lines = content.splitlines(False)
# ...
|
||
lines = content.splitlines(False) | ||
|
||
# Find out if we have any trailing blank lines |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing a period.
# Find out if we have any trailing blank lines | ||
has_blank_lines = len(lines[-1].strip()) == 0 or content.endswith(os.linesep) | ||
|
||
# Remove empty lines |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing a period.
for line_index in reversed(new_lines_to_remove): | ||
lines.pop(line_index) | ||
|
||
# Add new lines just before every dedent |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing a period.
|
||
|
||
if __name__ == '__main__': | ||
contents = unicode(sys.argv[1]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What are you trying to accomplish here? This is a rather dramatic decoding if you were handed bytes through sys.argv.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When sending via stdin for python2, bytes get sent in stdin.
has_blank_lines = len(lines[-1].strip()) == 0 or content.endswith(os.linesep) | ||
|
||
# Remove empty lines | ||
tokens = tokenize.generate_tokens(io.StringIO(content).readline) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI that is undocumented in Python 3. Is there a reason you aren't using tokenize.tokenize()
?
Fixes #1207
Fixes #1316
Fixes #1349
Fixes #259
This pull request: