-
-
Notifications
You must be signed in to change notification settings - Fork 30.4k
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
Crash on AST with misordered linenos #92597
Comments
Simple repro: from ast import *
tree = Module(body=[
Import(names=[alias(name='builtins', lineno=1, col_offset=0)], lineno=1, col_offset=0),
Import(names=[alias(name='traceback', lineno=0, col_offset=0)], lineno=0, col_offset=1)
], type_ignores=[])
compile(tree, "x", "exec", dont_inherit=True)
|
944fffe is the first bad commit
|
Looks like the ast defaults the end_lineno to None, which then becomes 0 in the compiler. Maybe the ast should default it be equal to lineno? Or should None become -1 in c? |
Both, perhaps. |
cc @pablogsal |
That's what we do in some parts that deal with these information. I can prepare a PR |
The question is if we should manage this in the AST creation routines or in the compiler. I assume that the first is more resilient but what do others think? |
FWIW, this seems to affect pytest's assertion rewriting as well. As soon as I'm running pytest against e.g.: def test_foo():
pass with a Python 3.11.0b1 configured with
which is here: and indeed
Turning pytest's assertion rewriting off with Passing the diff --git i/src/_pytest/assertion/rewrite.py w/src/_pytest/assertion/rewrite.py
index 81096764e..5d7392dd4 100644
--- i/src/_pytest/assertion/rewrite.py
+++ w/src/_pytest/assertion/rewrite.py
@@ -727,7 +727,7 @@ def run(self, mod: ast.Module) -> None:
ast.alias("_pytest.assertion.rewrite", "@pytest_ar"),
]
imports = [
- ast.Import([alias], lineno=lineno, col_offset=0) for alias in aliases
+ ast.Import([alias], lineno=lineno, end_lineno=item.end_lineno, col_offset=0) for alias in aliases
]
mod.body[pos:pos] = imports
cc @bluetech - though I suppose we don't necessarily need to fix this in pytest, hence I didn' t open an issue over there. |
Another project affected in the wild seems to be flask:
|
I'm working on a patch, will have a draft PR ready soon |
@pablogsal Do you have time to work on this, or should I fix it? |
I have opened a PR for this. |
…can be compiled (pythonGH-93359) (cherry picked from commit 705eaec) Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com>
…itions can be compiled (cherry picked from commit 705eaec)
…es (pythonGH-93398) (cherry picked from commit 8a221a8) Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com>
./python.exe -m ensurepip
./python.exe -m pip install pytest-cov
./python.exe -m pytest
Crashes with
Assertion failed: (i->i_end_lineno >= i->i_lineno), function write_location_info_long_form, file compile.c, line 7532.
lldb stack trace:
Your environment
I put print statements in that show the file it crashes on is
'/usr/local/lib/python3.11/site-packages/pytest_cov/plugin.py'
, but just running Python on that file doesn't reproduce the crash. I'll try to debug some more.The text was updated successfully, but these errors were encountered: