classification
Title: Py_CompileString no longer allows to tell "incomplete input" from "invalid input"
Type: Stage:
Components: C API Versions: Python 3.11, Python 3.10, Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: iritkatriel, lys.nikolaou, mloskot, pablogsal
Priority: normal Keywords:

Created on 2022-01-24 15:48 by mloskot, last changed 2022-01-25 15:10 by pablogsal.

Files
File name Uploaded Description Edit
Py_CompileString-Py36-vs-Py310.png mloskot, 2022-01-24 15:48 VS 2022 debugging session of the sample program from the FAQ presenting the difference in the Py_CompileString behaviour between Python 3.6 and 3.10 on Windows.
Messages (6)
msg411484 - (view) Author: Mateusz Loskot (mloskot) Date: 2022-01-24 15:48
Something has changed in Python 3.7 through 3.10 (I'm observing it in 3.10) in behaviour of the Python C API function Py_CompileString such that for an incomplete input it no longer raises

SyntaxError: "unexpected EOF while parsing"

but

IndentationError: expected an indented block after ...

The new behaviour makes the sample program from the "How do I tell “incomplete input” from “invalid input”?" at https://docs.python.org/3/faq/extending.html no longer work as described there.

For example:

```
for i in []:
```

raises

IndentationError: expected an indented block after 'for' statement on line 1


```
if True:
```

raises

IndentationError: expected an indented block after 'if' statement on line 1

instead of 

SyntaxError: unexpected EOF while parsing

This effectively makes it impossible to detect incomplete input using the Py_CompileString in applications where it is not possible to use PyRun_InteractiveLoop.

I have failed to identify what could be related changes in the release notes and the documentation does not seem to offer any update on that.
So, I'm assuming the new behaviour is not desired or expected.

Attached, is the VS 2022 screenshot with debugging session of the sample program from the FAQ presenting the difference in the behaviour between Python 3.6 and 3.10 on Windows.
msg411591 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2022-01-25 13:59
The python parser was completely rewritten in version 3.9, so it's likely that this caused changes to error messages.

Adding @pablogsal and @lys.nikolaou in case they can say more.
msg411596 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2022-01-25 14:32
Indeed, these new errors are due to the new parser. Unfortunately, the new parser doesn't allow to check against incomplete input the way the old one did so that piece of documentation is unfortunately outdated.

We should remove it to avoid further confusion.
msg411597 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2022-01-25 14:36
Mateusz, would you like to submit a patch to remove this section from the faq?
msg411605 - (view) Author: Mateusz Loskot (mloskot) Date: 2022-01-25 15:04
Irit, I can and I will submit a patch.

However, I'm very keen to learn what is an alternative solution then to distinguish hard invalid from incomplete input. IOW, what would be the new way of achieving what's described in the old FAQ?

I believe it would be more useful to the community if I updated the sample in the FAQ instead of just removing it.
msg411609 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2022-01-25 15:10
> However, I'm very keen to learn what is an alternative solution then to distinguish hard invalid from incomplete input. IOW, what would be the new way of achieving what's described in the old FAQ?

Unfortunately, there is no new way to do this from C due to how the new parser works. The only supported way is to manually import the codeop module from C (https://docs.python.org/3/library/codeop.html#module-codeop) and use its API as IDLE does, but this API can differ and will present its own complications, as unfortunately is implemented in a bit hacky way.
History
Date User Action Args
2022-01-25 15:10:18pablogsalsetmessages: + msg411609
2022-01-25 15:04:49mloskotsetmessages: + msg411605
2022-01-25 14:36:46iritkatrielsetversions: + Python 3.9, Python 3.11
2022-01-25 14:36:35iritkatrielsetmessages: + msg411597
2022-01-25 14:32:05pablogsalsetmessages: + msg411596
2022-01-25 13:59:38iritkatrielsetnosy: + iritkatriel, pablogsal, lys.nikolaou
messages: + msg411591
2022-01-24 15:48:56mloskotcreate