Skip to content
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

Run Selection/Line in Python Terminal gives syntax errors when running selection with function def or other blocks #259

Closed
arundeep78 opened this issue Nov 19, 2017 · 11 comments · Fixed by #1432 or #1515
Labels
area-terminal bug Issue identified by VS Code Team member as probable bug
Milestone

Comments

@arundeep78
Copy link

Environment data

VS Code version: 1.18.0
Python Extension version: 0.8.0
Python Version: 3.6.2 :: Continuum Analytics, Inc./ 2.7.13 :: Anaconda 4.4.0 (x86_64)
OS and version: OSX El Capitan 10.11.6

Actual behavior

Tries to execute a block and gives a syntax error

image

However, while executing those block as individual blocks. e.g. def separate and print separate it works fine.

Expected behavior

It should execute the whole selection without issues.

Steps to reproduce:

  • Set Python as default interpreter
  • Execute any of the sample blocks by "Run Selection/Line command"

Logs

Output from Python Terminal
image
image

@brettcannon brettcannon added awaiting 1-verification area-terminal bug Issue identified by VS Code Team member as probable bug labels Nov 21, 2017
@ericsnowcurrently ericsnowcurrently self-assigned this Jan 18, 2018
@ericsnowcurrently
Copy link
Member

Hi @arundeep78! Thanks for letting us know about this issue. I've been able to reproduce the problem (at least under linux). It appears to be related to the blank lines getting dropped somewhere along the line. We'll look into it as soon as we can. Thanks again!

@tebeka
Copy link

tebeka commented Jan 23, 2018

I'm hitting this problem as well, would love to see it solved.

@tebeka
Copy link

tebeka commented Jan 24, 2018

From what I can see codeExectuor is calling normalizeLines which removes empty lines.

IMO we can drop the call to normalizeLines and it'll work. Any ideas?

@tebeka
Copy link

tebeka commented Jan 29, 2018

IMO this was fixed in ad806fa (by @DonJayamanne), is there a release planned soon?

@brettcannon
Copy link
Member

brettcannon commented Jan 29, 2018

@tebeka we're hoping to release this week. If you want to help us validate that this got fixed, please install an insiders build and let us know if your issue was fixed.

@tebeka
Copy link

tebeka commented Jan 29, 2018

I've linked the current source tree (at 36b3050) to ~/.vscode/extension/vscode-pytho and tried the code - it worked.

If you prefer I'll check an exact versionn please LMK which one and I'll check.

@brettcannon
Copy link
Member

Nope, that does it! Thanks for the validation!

@tebeka
Copy link

tebeka commented Feb 2, 2018

Seems like if there are empty lines inside a function this still happens. I tried the below code with the new release and still get indentation error.

test.py

def add(x, y):
    """Adds x to y"""
    # Some comment

    return x + y

v = add(1, 7)
print(v)

terminal output

Python 3.6.4 |Anaconda, Inc.| (default, Jan 16 2018, 18:10:19)
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> def add(x, y):
...     """Adds x to y"""
...     # Some comment
...
>>>     return x + y
  File "<stdin>", line 1
    return x + y
    ^
IndentationError: unexpected indent
>>>
>>> v = add(1, 7)
>>> print(v)
None
>>>

@DonJayamanne
Copy link

Reopening as this issue was never fixed, i obviously made a mistake in closing this.

@DonJayamanne
Copy link

@brettcannon

Looks like blank lines within a block of code (if, while, for) needs to be stripped off or we should indent accordingly).
Each block of code should be separated with a blank line (i.e. different blocks of code should be separated with blank lines).

Sample 1

if True:
    print(1)

    print(2)

In order to get the above working in the terminal, we should either strip the blank line between the two print statements or ensure the blank line is indented (i.e. has 4 spaces in this case).

Sample 2

if True:
    print(1)

    print(2)
print(3)

For this code to work in the terminal, there must be a blank line between the last two print statements. I.e. we need a blank line to separate the if block from the rest of the code.

More information here https://stackoverflow.com/questions/8391633/blank-line-rule-at-interactive-prompt

@brettcannon
Copy link
Member

Either the leading newlines prepended to blank lines or stripping them are fine by me (I guess the blank prepending will lead to less surprise for people when looking at the REPL, but debugging that later will be a nightmare for any bug reports).

As for inserting the newline between blocks, we can't solve all problems at once. 😉

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-terminal bug Issue identified by VS Code Team member as probable bug
Projects
None yet
5 participants