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

SystemError when method has both super() & closure #48610

Closed
kaizhu mannequin opened this issue Nov 20, 2008 · 8 comments
Closed

SystemError when method has both super() & closure #48610

kaizhu mannequin opened this issue Nov 20, 2008 · 8 comments
Assignees
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) release-blocker type-bug An unexpected behavior, bug, or error

Comments

@kaizhu
Copy link
Mannequin

kaizhu mannequin commented Nov 20, 2008

BPO 4360
Nosy @warsaw, @brettcannon, @amauryfa, @tiran
Files
  • super-withcell.patch
  • 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 = 'https://github.com/warsaw'
    closed_at = <Date 2008-11-20.20:02:17.826>
    created_at = <Date 2008-11-20.10:29:19.374>
    labels = ['interpreter-core', 'type-bug', 'release-blocker']
    title = 'SystemError when method has both super() & closure'
    updated_at = <Date 2008-11-20.20:02:17.825>
    user = 'https://bugs.python.org/kaizhu'

    bugs.python.org fields:

    activity = <Date 2008-11-20.20:02:17.825>
    actor = 'barry'
    assignee = 'barry'
    closed = True
    closed_date = <Date 2008-11-20.20:02:17.826>
    closer = 'barry'
    components = ['Interpreter Core']
    creation = <Date 2008-11-20.10:29:19.374>
    creator = 'kaizhu'
    dependencies = []
    files = ['12070']
    hgrepos = []
    issue_num = 4360
    keywords = ['patch']
    message_count = 8.0
    messages = ['76093', '76094', '76095', '76096', '76103', '76104', '76129', '76133']
    nosy_count = 5.0
    nosy_names = ['barry', 'brett.cannon', 'amaury.forgeotdarc', 'christian.heimes', 'kaizhu']
    pr_nums = []
    priority = 'release blocker'
    resolution = 'accepted'
    stage = 'commit review'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue4360'
    versions = ['Python 3.0']

    @kaizhu
    Copy link
    Mannequin Author

    kaizhu mannequin commented Nov 20, 2008

    ################################
    # super_closure.py
    class A(object):
      def foo(self):
        return super()
        # remove the closure below
        # & SystemError goes away ???
        lambda: self
    A().foo()
    ################################

    when run on 3.0rc1 & 3.0rc2:

    hpc-login2 3 ~/work/py3to2: python3.0 super_closure.py
    Traceback (most recent call last):
      File "super_closure.py", line 9, in <module>
        A().foo()
      File "super_closure.py", line 5, in foo
        return super()
    SystemError: super(): __class__ is not a type (A)

    SystemError seems to b raised from typeobject.c (line6155):

    static int
    super_init(PyObject *self, PyObject *args, PyObject *kwds)
    {...
            if (!PyType_Check(type)) {
                PyErr_Format(PyExc_SystemError,
                  "super(): __class__ is not a type (%s)",
                  Py_TYPE(type)->tp_name);
                return -1;
            }
            break;

    @kaizhu kaizhu mannequin added build The build process and cross-build interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Nov 20, 2008
    @kaizhu
    Copy link
    Mannequin Author

    kaizhu mannequin commented Nov 20, 2008

    here's a printout of bytecode from script

    >>>
    >>> s = open("super_closure.py").read()
    >>> c = compile(s, "super_closure.py", "exec")
    >>> t = py3to2.codetree(c)
    >>> print( t )
    codetree(
    co_argcount =     0,
    co_cellvars =     (),
    co_code =        
    b'Gd\x00\x00\x84\x00\x00d\x01\x00e\x00\x00\x83\x03\x00Z\x01\x00e\x01\x00\x83\x00\x00j\x02\x00\x83\x00\x00\x01d\x02\x00S',
    co_filename =     'super_closure.py',
    co_firstlineno =  3,
    co_flags =        64,
    co_freevars =     (),
    co_kwonlyargcount =0,
    co_lnotab =       b'\x13\x06',
    co_name =         '<module>',
    co_names =        ('object', 'A', 'foo'),
    co_nlocals =      0,
    co_stacksize =    4,
    co_varnames =     (),
    depth =           0,
    co_consts = (
     codetree(
     co_argcount =     1,
     co_cellvars =     ('__class__',),
     co_code =        
    b'|\x00\x00Ee\x00\x00Z\x01\x00\x87\x00\x00f\x01\x00d\x00\x00\x86\x00\x00Z\x02\x00\x87\x00\x00S',
     co_filename =     'super_closure.py',
     co_firstlineno =  3,
     co_flags =        2,
     co_freevars =     (),
     co_kwonlyargcount =0,
     co_lnotab =       b'\n\x01',
     co_name =         'A',
     co_names =        ('__name__', '__module__', 'foo'),
     co_nlocals =      1,
     co_stacksize =    2,
     co_varnames =     ('__locals__',),
     depth =           1,
     co_consts = (
      codetree(
      co_argcount =     1,
      co_cellvars =     ('self',),
      co_code =        
    b't\x00\x00\x83\x00\x00S\x87\x00\x00f\x01\x00d\x01\x00\x86\x00\x00\x01',
      co_filename =     'super_closure.py',
      co_firstlineno =  4,
      co_flags =        3,
      co_freevars =     ('__class__',),
      co_kwonlyargcount =0,
      co_lnotab =       b'\x00\x01\x07\x03',
      co_name =         'foo',
      co_names =        ('super',),
      co_nlocals =      1,
      co_stacksize =    2,
      co_varnames =     ('self',),
      depth =           2,
      co_consts = (
       None,
       codetree(
       co_argcount =     0,
       co_cellvars =     (),
       co_code =         b'\x88\x00\x00S',
       co_filename =     'super_closure.py',
       co_firstlineno =  8,
       co_flags =        19,
       co_freevars =     ('self',),
       co_kwonlyargcount =0,
       co_lnotab =       b'',
       co_name =         '<lambda>',
       co_names =        (),
       co_nlocals =      0,
       co_stacksize =    1,
       co_varnames =     (),
       depth =           3,
       co_consts = (
        )),
       )),
      )),
     A,
     None,
     ))
    >>>

    and disassembly:

    >>>
    >>> print( t.dis() )
      3           0 LOAD_BUILD_CLASS
                  1 LOAD_CONST               0 (<code object A at
    0x2a987af9b0, file "super_closure.py", line 3>)
                  4 MAKE_FUNCTION            0
                  7 LOAD_CONST               1 ('A')
                 10 LOAD_NAME                0 (object)
                 13 CALL_FUNCTION            3
                 16 STORE_NAME               1 (A)

    9 19 LOAD_NAME 1 (A)
    22 CALL_FUNCTION 0
    25 LOAD_ATTR 2 (foo)
    28 CALL_FUNCTION 0
    31 POP_TOP
    32 LOAD_CONST 2 (None)
    35 RETURN_VALUE

      3           0 LOAD_FAST                0 (__locals__)
                  3 STORE_LOCALS
                  4 LOAD_NAME                0 (__name__)
                  7 STORE_NAME               1 (__module__)
    
      4          10 LOAD_CLOSURE             0 (__class__)
                 13 BUILD_TUPLE              1
                 16 LOAD_CONST               0 (\<code object foo at
    

    0x2a987afd30, file "super_closure.py", line 4>)
    19 MAKE_CLOSURE 0
    22 STORE_NAME 2 (foo)
    25 LOAD_CLOSURE 0 (class)
    28 RETURN_VALUE

          5           0 LOAD_GLOBAL              0 (super)
                      3 CALL_FUNCTION            0
                      6 RETURN_VALUE
    
          8           7 LOAD_CLOSURE             0 (self)
                     10 BUILD_TUPLE              1
                     13 LOAD_CONST               1 (\<code object
    

    <lambda> at 0x2a984c0530, file "super_closure.py", line 8>)
    16 MAKE_CLOSURE 0
    19 POP_TOP

              8           0 LOAD_DEREF               0 (self)
                          3 RETURN_VALUE
    

    >>

    @kaizhu
    Copy link
    Mannequin Author

    kaizhu mannequin commented Nov 20, 2008

    same thing, except w/ closure commented out (& everything is happy)

    ################################
    # super_ok.py
    class A(object):
      def foo(self):
        return super()
        # comment the closure below
        # & SystemError goes away
        # lambda: self
    A().foo()
    ################################
    >>>
    >>> s = open("super_ok.py").read()
    >>> c = compile(s, "super_ok.py", "exec")
    >>> t = py3to2.codetree(t)
    >>> print( t )
    codetree(
    co_argcount =     0,
    co_cellvars =     (),
    co_code =        
    b'Gd\x00\x00\x84\x00\x00d\x01\x00e\x00\x00\x83\x03\x00Z\x01\x00e\x01\x00\x83\x00\x00j\x02\x00\x83\x00\x00\x01d\x02\x00S',
    co_filename =     'super_closure.py',
    co_firstlineno =  3,
    co_flags =        64,
    co_freevars =     (),
    co_kwonlyargcount =0,
    co_lnotab =       b'\x13\x06',
    co_name =         '<module>',
    co_names =        ('object', 'A', 'foo'),
    co_nlocals =      0,
    co_stacksize =    4,
    co_varnames =     (),
    depth =           0,
    co_consts = (
     codetree(
     co_argcount =     1,
     co_cellvars =     ('__class__',),
     co_code =        
    b'|\x00\x00Ee\x00\x00Z\x01\x00\x87\x00\x00f\x01\x00d\x00\x00\x86\x00\x00Z\x02\x00\x87\x00\x00S',
     co_filename =     'super_closure.py',
     co_firstlineno =  3,
     co_flags =        2,
     co_freevars =     (),
     co_kwonlyargcount =0,
     co_lnotab =       b'\n\x01',
     co_name =         'A',
     co_names =        ('__name__', '__module__', 'foo'),
     co_nlocals =      1,
     co_stacksize =    2,
     co_varnames =     ('__locals__',),
     depth =           1,
     co_consts = (
      codetree(
      co_argcount =     1,
      co_cellvars =     ('self',),
      co_code =        
    b't\x00\x00\x83\x00\x00S\x87\x00\x00f\x01\x00d\x01\x00\x86\x00\x00\x01',
      co_filename =     'super_closure.py',
      co_firstlineno =  4,
      co_flags =        3,
      co_freevars =     ('__class__',),
      co_kwonlyargcount =0,
      co_lnotab =       b'\x00\x01\x07\x03',
      co_name =         'foo',
      co_names =        ('super',),
      co_nlocals =      1,
      co_stacksize =    2,
      co_varnames =     ('self',),
      depth =           2,
      co_consts = (
       None,
       codetree(
       co_argcount =     0,
       co_cellvars =     (),
       co_code =         b'\x88\x00\x00S',
       co_filename =     'super_closure.py',
       co_firstlineno =  8,
       co_flags =        19,
       co_freevars =     ('self',),
       co_kwonlyargcount =0,
       co_lnotab =       b'',
       co_name =         '<lambda>',
       co_names =        (),
       co_nlocals =      0,
       co_stacksize =    1,
       co_varnames =     (),
       depth =           3,
       co_consts = (
        )),
       )),
      )),
     A,
     None,
     ))
    >>>
    >>> print( t.dis() )
      3           0 LOAD_BUILD_CLASS
                  1 LOAD_CONST               0 (<code object A at
    0x2a987af2b0, file "super_closure.py", line 3>)
                  4 MAKE_FUNCTION            0
                  7 LOAD_CONST               1 ('A')
                 10 LOAD_NAME                0 (object)
                 13 CALL_FUNCTION            3
                 16 STORE_NAME               1 (A)

    9 19 LOAD_NAME 1 (A)
    22 CALL_FUNCTION 0
    25 LOAD_ATTR 2 (foo)
    28 CALL_FUNCTION 0
    31 POP_TOP
    32 LOAD_CONST 2 (None)
    35 RETURN_VALUE

      3           0 LOAD_FAST                0 (__locals__)
                  3 STORE_LOCALS
                  4 LOAD_NAME                0 (__name__)
                  7 STORE_NAME               1 (__module__)
    
      4          10 LOAD_CLOSURE             0 (__class__)
                 13 BUILD_TUPLE              1
                 16 LOAD_CONST               0 (\<code object foo at
    

    0x2a987af4b0, file "super_closure.py", line 4>)
    19 MAKE_CLOSURE 0
    22 STORE_NAME 2 (foo)
    25 LOAD_CLOSURE 0 (class)
    28 RETURN_VALUE

          5           0 LOAD_GLOBAL              0 (super)
                      3 CALL_FUNCTION            0
                      6 RETURN_VALUE
    
          8           7 LOAD_CLOSURE             0 (self)
                     10 BUILD_TUPLE              1
                     13 LOAD_CONST               1 (\<code object
    

    <lambda> at 0x2a987af5b0, file "super_closure.py", line 8>)
    16 MAKE_CLOSURE 0
    19 POP_TOP

              8           0 LOAD_DEREF               0 (self)
                          3 RETURN_VALUE
    

    >>

    @kaizhu
    Copy link
    Mannequin Author

    kaizhu mannequin commented Nov 20, 2008

    oops, sorry reprinted the same code ^^;;; ignore previous post, & use
    this: (sorry again for mucking up this page)

    ################################
    # super_ok.py
    class A(object):
      def foo(self):
        return super()
        # comment the closure below
        # & SystemError goes away
        # lambda: self
    A().foo()
    ################################
    >>> s = open("super_ok.py").read(); c = compile(s, "super_ok.py",
    "exec"); t = py3to2.codetree(c)
    >>> print( t )
    codetree(
    co_argcount =     0,
    co_cellvars =     (),
    co_code =        
    b'Gd\x00\x00\x84\x00\x00d\x01\x00e\x00\x00\x83\x03\x00Z\x01\x00e\x01\x00\x83\x00\x00j\x02\x00\x83\x00\x00\x01d\x02\x00S',
    co_filename =     'super_ok.py',
    co_firstlineno =  3,
    co_flags =        64,
    co_freevars =     (),
    co_kwonlyargcount =0,
    co_lnotab =       b'\x13\x06',
    co_name =         '<module>',
    co_names =        ('object', 'A', 'foo'),
    co_nlocals =      0,
    co_stacksize =    4,
    co_varnames =     (),
    depth =           0,
    co_consts = (
     codetree(
     co_argcount =     1,
     co_cellvars =     ('__class__',),
     co_code =        
    b'|\x00\x00Ee\x00\x00Z\x01\x00\x87\x00\x00f\x01\x00d\x00\x00\x86\x00\x00Z\x02\x00\x87\x00\x00S',
     co_filename =     'super_ok.py',
     co_firstlineno =  3,
     co_flags =        2,
     co_freevars =     (),
     co_kwonlyargcount =0,
     co_lnotab =       b'\n\x01',
     co_name =         'A',
     co_names =        ('__name__', '__module__', 'foo'),
     co_nlocals =      1,
     co_stacksize =    2,
     co_varnames =     ('__locals__',),
     depth =           1,
     co_consts = (
      codetree(
      co_argcount =     1,
      co_cellvars =     (),
      co_code =         b't\x00\x00\x83\x00\x00S',
      co_filename =     'super_ok.py',
      co_firstlineno =  4,
      co_flags =        3,
      co_freevars =     ('__class__',),
      co_kwonlyargcount =0,
      co_lnotab =       b'\x00\x01',
      co_name =         'foo',
      co_names =        ('super',),
      co_nlocals =      1,
      co_stacksize =    1,
      co_varnames =     ('self',),
      depth =           2,
      co_consts = (
       None,
       )),
      )),
     A,
     None,
     ))
    >>> print( t.dis() )
      3           0 LOAD_BUILD_CLASS
                  1 LOAD_CONST               0 (<code object A at
    0x2a987afd30, file "super_ok.py", line 3>)
                  4 MAKE_FUNCTION            0
                  7 LOAD_CONST               1 ('A')
                 10 LOAD_NAME                0 (object)
                 13 CALL_FUNCTION            3
                 16 STORE_NAME               1 (A)

    9 19 LOAD_NAME 1 (A)
    22 CALL_FUNCTION 0
    25 LOAD_ATTR 2 (foo)
    28 CALL_FUNCTION 0
    31 POP_TOP
    32 LOAD_CONST 2 (None)
    35 RETURN_VALUE

      3           0 LOAD_FAST                0 (__locals__)
                  3 STORE_LOCALS
                  4 LOAD_NAME                0 (__name__)
                  7 STORE_NAME               1 (__module__)
    
      4          10 LOAD_CLOSURE             0 (__class__)
                 13 BUILD_TUPLE              1
                 16 LOAD_CONST               0 (\<code object foo at
    

    0x2a984c0530, file "super_ok.py", line 4>)
    19 MAKE_CLOSURE 0
    22 STORE_NAME 2 (foo)
    25 LOAD_CLOSURE 0 (class)
    28 RETURN_VALUE

          5           0 LOAD_GLOBAL              0 (super)
                      3 CALL_FUNCTION            0
                      6 RETURN_VALUE
    

    >>

    @amauryfa
    Copy link
    Member

    In a running frame, f->f_localplus is a vector composed of:

    • the values of the local variables
    • the cells containing variables used in a nested closure.
    • the values of free variables defined in a outer scope.

    super() needs to access the free var containing the enclosing class
    object, but forgets to account for the number of cells...

    The attached patch corrects the problem.

    @tiran
    Copy link
    Member

    tiran commented Nov 20, 2008

    Yet another release blocker for Barry. Good work, Amaury.

    @tiran tiran added release-blocker and removed build The build process and cross-build labels Nov 20, 2008
    @brettcannon
    Copy link
    Member

    The patch looks good to me.

    @warsaw
    Copy link
    Member

    warsaw commented Nov 20, 2008

    patch applied; r67299

    @warsaw warsaw closed this as completed Nov 20, 2008
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    interpreter-core (Objects, Python, Grammar, and Parser dirs) release-blocker type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    4 participants