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

inspect.getsourcelines() is buggy on 3.12 #104866

Closed
JelleZijlstra opened this issue May 24, 2023 · 1 comment
Closed

inspect.getsourcelines() is buggy on 3.12 #104866

JelleZijlstra opened this issue May 24, 2023 · 1 comment
Labels
3.12 bugs and security fixes 3.13 bugs and security fixes type-bug An unexpected behavior, bug, or error

Comments

@JelleZijlstra
Copy link
Member

JelleZijlstra commented May 24, 2023

% cat gsl.py
import inspect

def test_list_add(self):
    def capybara() -> None:
        assert_is_value(
            [x] + [y],
            z,
        )
        assert_is_value(
            # in the binop implementation
            a,
        )

print(inspect.getsourcelines(test_list_add))
% python3.11 gsl.py 
(['def test_list_add(self):\n', '    def capybara() -> None:\n', '        assert_is_value(\n', '            [x] + [y],\n', '            z,\n', '        )\n', '        assert_is_value(\n', '            # in the binop implementation\n', '            a,\n', '        )\n'], 3)
% ~/py/cpython/python.exe gsl.py 
(['def test_list_add(self):\n', '    def capybara() -> None:\n', '        assert_is_value(\n', '            [x] + [y],\n', '            z,\n', '        )\n', '        assert_is_value(\n', '            # in the binop implementation\n'], 3)
% ~/py/cpython/python.exe -V
Python 3.12.0b1+

On 3.12, the last two lines (a, )) are not returned as part of the source lines. When I tried to minify by removing the first assert_is_value() call, it instead returned the print() line as part of the function.

This looks related to the tokenizer, cc @pablogsal @lysnikolaou.

Linked PRs

@JelleZijlstra JelleZijlstra added type-bug An unexpected behavior, bug, or error 3.12 bugs and security fixes 3.13 bugs and security fixes labels May 24, 2023
@AlexWaygood AlexWaygood added stdlib Python modules in the Lib dir and removed stdlib Python modules in the Lib dir labels May 24, 2023
@lysnikolaou
Copy link
Contributor

This is a bug in the implementation of the new tokenize module. The tokenizer checks whether something is in a parenthesized block in order to emit a NL token instead of a NEWLINE token. It also sets and unsets a flag about a comment-only line, in order to do the same. When both are true (a comment inside a parenthesized block), it nevers resets the comment flag. Here's a minimal example:

cpython onmain [$?] via C v14.0.3-clang via 🐍 pyenv 3.11.3cat hello.py              
[
    # hello
    1
]

cpython onmain [$?] via C v14.0.3-clang via 🐍 pyenv 3.11.3
❯ ./python.exe -m tokenize hello.py
0,0-0,0:            ENCODING       'utf-8'        
1,0-1,1:            OP             '['            
1,1-1,2:            NL             '\n'           
2,4-2,11:           COMMENT        '# hello'      
2,11-2,12:          NL             '\n'           
3,4-3,5:            NUMBER         '1'            
3,5-3,6:            NL             '\n'           
4,0-4,1:            OP             ']'            
4,1-4,2:            NL             '\n'       # This should be a NEWLINE instead    
5,0-5,0:            ENDMARKER      ''             

I'm opening a PR shortly.

lysnikolaou added a commit to lysnikolaou/cpython that referenced this issue May 24, 2023
miss-islington pushed a commit to miss-islington/cpython that referenced this issue May 24, 2023
…h comment (pythonGH-104870)

(cherry picked from commit c90a862)

Co-authored-by: Lysandros Nikolaou <lisandrosnik@gmail.com>
terryjreedy pushed a commit that referenced this issue May 24, 2023
…th comment (GH-104870) (#104872)

gh-104866: Tokenize should emit NEWLINE after exiting block with comment (GH-104870)
(cherry picked from commit c90a862)

Co-authored-by: Lysandros Nikolaou <lisandrosnik@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.12 bugs and security fixes 3.13 bugs and security fixes type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

4 participants