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

Type Annotation of nested function with positional only arguments triggers SystemError #83396

Closed
asottile mannequin opened this issue Jan 5, 2020 · 4 comments
Closed
Labels
3.8 only security fixes 3.9 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs)

Comments

@asottile
Copy link
Mannequin

asottile mannequin commented Jan 5, 2020

BPO 39215
Nosy @asottile, @pablogsal
PRs
  • bpo-39215: Fix SystemError when nested function has annotation on positional-only argument #17826
  • [3.8] bpo-39215: Fix SystemError when nested function has annotation on positional-only argument (GH-17826) #17827
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2020-01-05.02:15:30.380>
    created_at = <Date 2020-01-05.00:30:24.549>
    labels = ['interpreter-core', '3.8', '3.9']
    title = 'Type Annotation of nested function with positional only arguments triggers SystemError'
    updated_at = <Date 2020-01-05.02:15:30.373>
    user = 'https://github.com/asottile'

    bugs.python.org fields:

    activity = <Date 2020-01-05.02:15:30.373>
    actor = 'pablogsal'
    assignee = 'none'
    closed = True
    closed_date = <Date 2020-01-05.02:15:30.380>
    closer = 'pablogsal'
    components = ['Interpreter Core']
    creation = <Date 2020-01-05.00:30:24.549>
    creator = 'Anthony Sottile'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 39215
    keywords = ['patch']
    message_count = 4.0
    messages = ['359312', '359313', '359315', '359319']
    nosy_count = 2.0
    nosy_names = ['Anthony Sottile', 'pablogsal']
    pr_nums = ['17826', '17827']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue39215'
    versions = ['Python 3.8', 'Python 3.9']

    @asottile
    Copy link
    Mannequin Author

    asottile mannequin commented Jan 5, 2020

    def f():
        def g(arg: int, /):
            pass
    
    f()
    $ python3.9 t2.py
    Traceback (most recent call last):
      File "/home/asottile/workspace/t2.py", line 5, in <module>
        f()
      File "/home/asottile/workspace/t2.py", line 2, in f
        def g(arg: int, /):
    SystemError: no locals when loading 'int'

    Originally from this StackOverflow post: https://stackoverflow.com/q/59594494/812183

    @asottile asottile mannequin added 3.8 only security fixes 3.9 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) labels Jan 5, 2020
    @asottile
    Copy link
    Mannequin Author

    asottile mannequin commented Jan 5, 2020

    Looks as though annotations are using LOAD_NAME when they should probably use LOAD_GLOBAL?

    $ diff -u <(python3.9 -m dis t2.py | sed 's/0x[a-f0-9]*/0xdeadbeef/g;s/t2\.py/FILENAME/g') <(python3.9 -m dis t3.py | sed 's/0x[a-f0-9]*/0xdeadbeef/g;s/t3\.py/FILENAME/g')
    --- /dev/fd/63	2020-01-04 16:34:27.372004436 -0800
    +++ /dev/fd/62	2020-01-04 16:34:27.372004436 -0800
    @@ -10,7 +10,7 @@
                  16 RETURN_VALUE
     
     Disassembly of <code object f at 0xdeadbeef, file "FILENAME", line 1>:
    -  2           0 LOAD_NAME                0 (int)
    +  2           0 LOAD_GLOBAL              0 (int)
                   2 LOAD_CONST               1 (('arg',))
                   4 BUILD_CONST_KEY_MAP      1
                   6 LOAD_CONST               2 (<code object g at 0xdeadbeef, file "FILENAME", line 2>)
    

    t3.py is the same as t2.py but without positional-only arguments

    @asottile
    Copy link
    Mannequin Author

    asottile mannequin commented Jan 5, 2020

    this triggers an assertion when run with debug enabled:

    $ gdb ./python 
    GNU gdb (Ubuntu 8.1-0ubuntu3.2) 8.1.0.20180409-git
    Copyright (C) 2018 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
    This is free software: you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
    and "show warranty" for details.
    This GDB was configured as "x86_64-linux-gnu".
    Type "show configuration" for configuration details.
    For bug reporting instructions, please see:
    <http://www.gnu.org/software/gdb/bugs/>.
    Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.
    For help, type "help".
    Type "apropos word" to search for commands related to "word"...
    Reading symbols from ./python...done.
    warning: File "/home/asottile/workspace/cpython/python-gdb.py" auto-loading has been declined by your `auto-load safe-path' set to "$debugdir:$datadir/auto-load".
    To enable execution of this file add
    	add-auto-load-safe-path /home/asottile/workspace/cpython/python-gdb.py
    line to your configuration file "/home/asottile/.gdbinit".
    To completely disable this security protection add
    	set auto-load safe-path /
    line to your configuration file "/home/asottile/.gdbinit".
    For more information about this security protection see the
    "Auto-loading safe path" section in the GDB manual.  E.g., run from the shell:
    	info "(gdb)Auto-loading safe path"
    (gdb) run t2.py
    Starting program: /home/asottile/workspace/cpython/python t2.py
    [Thread debugging using libthread_db enabled]
    Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
    python: Python/compile.c:3545: compiler_nameop: Assertion `scope || PyUnicode_READ_CHAR(name, 0) == '_'' failed.

    Program received signal SIGABRT, Aborted.
    __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
    51 ../sysdeps/unix/sysv/linux/raise.c: No such file or directory.
    (gdb) bt
    #0 __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
    #1 0x00007ffff7060801 in __GI_abort () at abort.c:79
    #2 0x00007ffff705039a in __assert_fail_base (
    fmt=0x7ffff71d77d8 "%s%s%s:%u: %s%sAssertion `%s' failed.\n%n",
    assertion=assertion@entry=0x555555815190 "scope || PyUnicode_READ_CHAR(name, 0) == '_'", file=file@entry=0x555555814233 "Python/compile.c",
    line=line@entry=3545,
    function=function@entry=0x555555814180 <PRETTY_FUNCTION.17589> "compiler_nameop") at assert.c:92
    #3 0x00007ffff7050412 in __GI___assert_fail (
    assertion=assertion@entry=0x555555815190 "scope || PyUnicode_READ_CHAR(name, 0) == '_'", file=file@entry=0x555555814233 "Python/compile.c",
    line=line@entry=3545,
    function=function@entry=0x555555814180 <PRETTY_FUNCTION.17589> "compiler_nameop") at assert.c:101
    #4 0x000055555568e946 in compiler_nameop (c=0x7fffffffdb40,
    name=<optimized out>, ctx=Load) at Python/compile.c:3545
    #5 0x000055555569020e in compiler_visit_expr1 (c=c@entry=0x7fffffffdb40,
    e=0x555555b739d0) at Python/compile.c:5027
    #6 0x000055555569027f in compiler_visit_expr (c=0x7fffffffdb40,
    e=<optimized out>) at Python/compile.c:5052
    #7 0x0000555555693570 in compiler_visit_argannotation (
    c=c@entry=0x7fffffffdb40, id=0x7ffff7f95d60, annotation=<optimized out>,
    names=names@entry=0x7ffff66217d0) at Python/compile.c:2045
    #8 0x000055555569363e in compiler_visit_argannotations (
    c=c@entry=0x7fffffffdb40, args=0x555555b739c0,
    names=names@entry=0x7ffff66217d0) at Python/compile.c:2066
    #9 0x00005555556936b2 in compiler_visit_annotations (
    c=c@entry=0x7fffffffdb40, args=args@entry=0x555555b73a28,
    returns=returns@entry=0x0) at Python/compile.c:2094
    #10 0x00005555556939fc in compiler_function (c=0x7fffffffdb40,
    s=0x555555b73ab8, is_async=is_async@entry=0) at Python/compile.c:2214
    #11 0x0000555555693dad in compiler_visit_stmt (c=c@entry=0x7fffffffdb40,
    s=0x555555b73ab8) at Python/compile.c:3332
    ---Type <return> to continue, or q <return> to quit---
    #12 0x0000555555693b8f in compiler_function (c=0x7fffffffdb40,
    s=<optimized out>, is_async=is_async@entry=0) at Python/compile.c:2238
    #13 0x0000555555693dad in compiler_visit_stmt (c=c@entry=0x7fffffffdb40,
    s=0x555555b73b00) at Python/compile.c:3332
    #14 0x0000555555694022 in compiler_body (c=c@entry=0x7fffffffdb40,
    stmts=0x555555b73960) at Python/compile.c:1816
    #15 0x0000555555694754 in compiler_mod (c=c@entry=0x7fffffffdb40,
    mod=mod@entry=0x555555b73c00) at Python/compile.c:1836
    #16 0x000055555569682b in PyAST_CompileObject (mod=mod@entry=0x555555b73c00,
    filename=filename@entry=0x7ffff662a7b0, flags=flags@entry=0x7fffffffddc0,
    optimize=optimize@entry=-1, arena=arena@entry=0x7ffff7e69640)
    at Python/compile.c:364
    #17 0x00005555556c20cc in run_mod (mod=mod@entry=0x555555b73c00,
    filename=filename@entry=0x7ffff662a7b0,
    globals=globals@entry=0x7ffff7ecf3b0, locals=locals@entry=0x7ffff7ecf3b0,
    flags=flags@entry=0x7fffffffddc0, arena=arena@entry=0x7ffff7e69640)
    at Python/pythonrun.c:1138
    #18 0x00005555556c530c in PyRun_FileExFlags (fp=fp@entry=0x555555b6d000,
    filename_str=filename_str@entry=0x7ffff7ecf720 "/home/asottile/workspace/cpython/t2.py", start=start@entry=257, globals=globals@entry=0x7ffff7ecf3b0,
    locals=locals@entry=0x7ffff7ecf3b0, closeit=closeit@entry=1,
    flags=0x7fffffffddc0) at Python/pythonrun.c:1063
    #19 0x00005555556c56e6 in PyRun_SimpleFileExFlags (fp=fp@entry=0x555555b6d000,
    filename=<optimized out>,
    filename@entry=0x7ffff7ecf720 "/home/asottile/workspace/cpython/t2.py",
    closeit=closeit@entry=1, flags=flags@entry=0x7fffffffddc0)
    at Python/pythonrun.c:428
    #20 0x00005555556c58e9 in PyRun_AnyFileExFlags (fp=fp@entry=0x555555b6d000,
    filename=0x7ffff7ecf720 "/home/asottile/workspace/cpython/t2.py",
    closeit=closeit@entry=1, flags=flags@entry=0x7fffffffddc0)
    at Python/pythonrun.c:86
    #21 0x00005555555aceca in pymain_run_file (config=config@entry=0x555555b2f190,
    cf=cf@entry=0x7fffffffddc0) at Modules/main.c:369
    #22 0x00005555555ad698 in pymain_run_python (
    exitcode=exitcode@entry=0x7fffffffddf4) at Modules/main.c:553
    #23 0x00005555555ad703 in Py_RunMain () at Modules/main.c:632
    #24 0x00005555555ad77d in pymain_main (args=args@entry=0x7fffffffde50)
    at Modules/main.c:662
    #25 0x00005555555ad843 in Py_BytesMain (argc=<optimized out>,
    argv=<optimized out>) at Modules/main.c:686
    ---Type <return> to continue, or q <return> to quit---
    #26 0x00005555555ac053 in main (argc=<optimized out>, argv=<optimized out>)
    at ./Programs/python.c:16

    @pablogsal
    Copy link
    Member

    Thanks, Anthony, for the quick fix and the investigation! :)

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.8 only security fixes 3.9 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs)
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant