8

Maybe I've gotten rusty with Python. Why is this not acceptable when pasted into a Python shell?

hdr_filenames = [
    "20210311_105300_HDR.jpg",
    "20210311_105306_HDR.jpg",
    "20210311_105310_HDR.jpg",
    "20210311_105314_HDR.jpg",
    "20210311_105341_HDR.jpg",    # order of last two have reversed exposures   
    "20210311_105323_HDR.jpg"
    ]

When this is copied to the prompt in an xterm running python3, I get (never mind the quaint retro term):

enter image description here

EDIT: silly of me to forget to report some basic obvious info: This is Python version 3.9.1, on Arch Linux updated about a month or so ago.

DarenW
  • 16,549
  • 7
  • 63
  • 102
  • 5
    Works fine for me, looks like you could have a strange character mixed in or something? Try to copy/paste from the version you posted here on SO. – Masklinn Mar 12 '21 at 07:52
  • 1
    Works for me... I cannot reproduce. I copy-and-pasted this right into my REPL no problem, I'm using iterm2 on OSx though... maybe something to do with xterm specifically? – juanpa.arrivillaga Mar 12 '21 at 07:53
  • I can reproduce this in my `python3` REPL though it works fine in `ipython3`. I think it's just some line continuation/indent issue. – tdy Mar 12 '21 at 07:55
  • I can reproduce it as well. If I move the first string up to the first line, following the `[`, the problem goes away. Another strange thing...if I copy/paste the example in two chunks, first pasting just the first line, and then the rest of the expression, the problem goes away as well. I have a similar setup to @juanpa.arrivillaga, so it's strange that I reproduce the problem and they do not. – CryptoFool Mar 12 '21 at 08:08
  • For people that can reproduce, can you tell us what terminal you are using? – juanpa.arrivillaga Mar 12 '21 at 08:10
  • @juanpa.arrivillaga iTerm2, just like you. Python 3.9.1. OSX Catalina. – CryptoFool Mar 12 '21 at 08:11
  • I think this has to do with indentation of the list items and the closing `]` charachter. Try increasing the indent of the list items, and using no indentation for the closing `]` character. – Attila Viniczai Mar 12 '21 at 08:13
  • Happens for me on 3.9, but not 3.7!...nor 3.8. It's something that's just arrived in 3.9. – CryptoFool Mar 12 '21 at 08:14
  • 1
    Can confirm, happens on Python 3.9.1 for me in OSX, iterm2! – juanpa.arrivillaga Mar 12 '21 at 08:15
  • @juanpa.arrivillaga - nice. The world is back on its axis :) – CryptoFool Mar 12 '21 at 08:15
  • @DarenW - can you confirm that you are on 3.9? – CryptoFool Mar 12 '21 at 08:18
  • @CryptoFool it's the new parser! See my answer... if you pass `-X oldparser` I don't get the error anymore! Think we have an actual bug on our hands... – juanpa.arrivillaga Mar 12 '21 at 08:19
  • Yeah. you da man @juanpa.arrivillaga! – CryptoFool Mar 12 '21 at 08:21
  • Can you please swap the screenshot of the error to text formatted as code? – MisterMiyagi Mar 12 '21 at 08:47
  • If anyone is wondering, I'm using cool-retro-term. When I have too many windows of all kinds up, the old amber or green CRTlook helps me find the terms quickly. – DarenW Mar 13 '21 at 20:23
  • This video shows the problem, clean and straightforward: https://www.youtube.com/watch?v=CrTzBpVdcVM – DarenW Mar 19 '21 at 02:05

2 Answers2

11

So, I don't have a complete answer here, but this has something to do with the new PEG parser that debuted in Python 3.9, because when I use it (it is the default parser on Python 3.9), I get the same error:

Python 3.9.1 (default, Dec 11 2020, 06:28:49)
[Clang 10.0.0 ] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> hdr_filenames = [
    "20210311_105300_HDR.jpg",
    "20210311_105306_HDR.jpg",
    "20210311_105310_HDR.jpg",
    "20210311_105314_HDR.jpg",
    "20210311_105341_HDR.jpg",    # order of last two have reversed exposures
    "20210311_105323_HDR.jpg"
    ]
  File "<stdin>", line 1
        ]
         ^
SyntaxError: multiple statements found while compiling a single statement

BUT You can revert to the old, LL(1) parser by passing a command line option, an voila! No error:

(py39) juanarrivillaga@50-254-139-253-static % python -X oldparser
Python 3.9.1 (default, Dec 11 2020, 06:28:49)
[Clang 10.0.0 ] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> hdr_filenames = [
    "20210311_105300_HDR.jpg",
    "20210311_105306_HDR.jpg",
    "20210311_105310_HDR.jpg",
    "20210311_105314_HDR.jpg",
    "20210311_105341_HDR.jpg",    # order of last two have reversed exposures
    "20210311_105323_HDR.jpg"
    ]
>>> exit()

For those interested, the relevant PEP 617 about the new parser.

EDIT

So, this no-longer seems to be a problem on Python 3.9.2 (the latest version currently, I believe). So perhaps upgrade?

Python 3.9.2 (default, Mar  3 2021, 11:58:52)
[Clang 10.0.0 ] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> hdr_filenames = [
...     "20210311_105300_HDR.jpg",
...     "20210311_105306_HDR.jpg",
...     "20210311_105310_HDR.jpg",
...     "20210311_105314_HDR.jpg",
...     "20210311_105341_HDR.jpg",    # order of last two have reversed exposures
...     "20210311_105323_HDR.jpg"
...     ]
>>>
juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
  • 2
    Wow. Dude. That's some killer sleuthing. I'm impressed with your knowledge. I'm clueless as to PEG vs LL(1) vs paper-and-pencil. Most excellent. - In fact...I'd call this a pretty darn complete answer. I think it's unreasonable to expect you to find the exact line of code that is to blame, LOL. What else can you do? – CryptoFool Mar 12 '21 at 08:20
  • @CryptoFool I have no deep knowledge of computer science topics like this, I came to programming / software engineering relatively late in life, but I *do* keep up with new features in Python releases, and I remembered seeing this change and thinking "neat, wish I understood this more deeply". I just tried this out on a hunch. – juanpa.arrivillaga Mar 12 '21 at 08:25
  • It works for me in Python 3.9.0 (on Windows). Shouldn't it also fail then? – Manuel Mar 12 '21 at 08:26
  • @Manuel *interesting*. Try it with 3.9.1, which is what I used. EDIT: confirmed to error on Python 3.9.0 for me on Mac OSX, iterm2. Cna you give more info? – juanpa.arrivillaga Mar 12 '21 at 08:27
  • Here's a more granular change log: https://docs.python.org/3/whatsnew/changelog.html – juanpa.arrivillaga Mar 12 '21 at 08:29
  • @Manuel - interesting. Things regarding the terminal are often different on Windows than Mac and Linux, so I'm not too surprised. I bet it happens on Linux. - UPDATE: Maybe not. I just tried it under `docker run -it python:3.9` and it doesn't happen for me there. – CryptoFool Mar 12 '21 at 08:29
  • @juanpa.arrivillaga I have `Python 3.9.0 (tags/v3.9.0:9cf6752, Oct 5 2020, 15:34:40) [MSC v.1927 64 bit (AMD64)] on win32` and `Microsoft Windows [Version 10.0.19041.804]`. Works fine both in an IDLE shell and in a Python shell in a `cmd` window. – Manuel Mar 12 '21 at 08:35
  • 2
    Your error slightly differs from the OP's screenshot, where there's an empty line above the line with `^`. – Manuel Mar 12 '21 at 08:49
  • Just thought to add a datapoint: I *cannot* reproduce this on MacOS 10.14.6, Python 3.9.0 (default, Dec 6 2020, 20:20:21) [Clang 11.0.0 (clang-1100.0.33.17)] on darwin, ITerm2 Build 3.4.4. – MisterMiyagi Mar 12 '21 at 08:51
  • [This issue](https://bugs.python.org/issue43379) sounds like that. – Manuel Mar 12 '21 at 08:53
  • @MisterMiyagi *interesting*, I could reproduce it on Python 3.9.0, and Python 3.9.1 MacOS 10.15.5, iterm2. But not in Python 3.9.2 – juanpa.arrivillaga Mar 12 '21 at 08:57
  • Nice, I can confirm on linux -- broken in 3.9.0, fixed in 3.9.2 – tdy Mar 12 '21 at 19:39
  • I'm using 3.9.1 from Arch Linux, which is fairly close to bleeding edge. That command line option didn't help. Apparently it's time to upgrade! – DarenW Mar 13 '21 at 20:20
  • 1
    This made the problem go away: echo "set enable-bracketed-paste off" >> ~/.inputrc – DarenW Mar 19 '21 at 02:12
-3

yes maybe just needs an upgrade, I have ran into this problems too for me the problem was the interpreter was wrong and tutorial solved here's the link maybe it will help you as well as it did helped me: https://youtu.be/RvbUqf3Tb1s The Fix