Skip to content

__futures__ parsing can be simplified using complete source locations for error detection. Also the error's offset is off by one sometimes. #98811

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

Closed
iritkatriel opened this issue Oct 28, 2022 · 0 comments · Fixed by #98812
Assignees
Labels
3.12 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error type-feature A feature request or enhancement

Comments

@iritkatriel
Copy link
Member

iritkatriel commented Oct 28, 2022

The check for whether there are __future__ imports which are not in the beginning of the script appears in two places:

in future.c, future_parse, the ff_lineno is stored (last lineno of a __future__ import) and there is a check for bad __future__ imports on the same line .

Then in compile.c, compiler_from_import(), any import from __future__ which is in a line after c->c_future->ff_lineno is rejected.

If we replace ff_lineno by ff_location (the complete location information) then the check in compile.c can use the column info to detect the same-line case, and we no longer need to check for this in future.c. The code of future_parse will become much simpler.

There is also a bug in the future.c exception - the offset is off by 1, and this results in incorrect hilighting of the bad import:

python.exe -c "import test.badsyntax_future7.py"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/Users/iritkatriel/src/cpython/Lib/test/badsyntax_future7.py", line 3
    from __future__ import nested_scopes; import string; from __future__ import \
                                                        ^
SyntaxError: from __future__ imports must occur at the beginning of the file

My patch will result in this output instead:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/Users/iritkatriel/src/cpython-654/Lib/test/badsyntax_future7.py", line 3
    from __future__ import nested_scopes; import string; from __future__ import \
                                                         ^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: from __future__ imports must occur at the beginning of the file
@iritkatriel iritkatriel added type-bug An unexpected behavior, bug, or error interpreter-core (Objects, Python, Grammar, and Parser dirs) 3.12 only security fixes labels Oct 28, 2022
@iritkatriel iritkatriel self-assigned this Oct 28, 2022
iritkatriel added a commit to iritkatriel/cpython that referenced this issue Oct 28, 2022
…ts error checking. Also fixes the offset of the error in one case
iritkatriel added a commit to iritkatriel/cpython that referenced this issue Oct 28, 2022
…ts error checking. Also fixes the offset of the error in one case
@iritkatriel iritkatriel changed the title __futures__ parsing can be simplified using complete source locations for error detection __futures__ parsing can be simplified using complete source locations for error detection. Also the error's offset is off by one sometimes. Oct 28, 2022
@iritkatriel iritkatriel added the type-feature A feature request or enhancement label Oct 28, 2022
iritkatriel added a commit that referenced this issue Oct 31, 2022
…or checking. This also fixes an incorrect error offset. (GH-98812)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.12 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error type-feature A feature request or enhancement
Projects
None yet
1 participant