[New-bugs-announce] [issue32176] Zero argument super is broken in 3.6 for methods with a hacked __class__ cell
Dan Snider
report at bugs.python.org
Thu Nov 30 02:39:35 EST 2017
New submission from Dan Snider <mr.assume.away at gmail.com>:
The following code works in 3.3, 3.4, and 3.5, but in 3.6 it throws RuntimeError: super(): bad __class__ cell.
from types import FunctionType, CodeType
def create_closure(__class__):
return (lambda: __class__).__closure__
def new_code(c_or_f):
'''A new code object with a __class__ cell added to freevars'''
c = c_or_f.__code__ if isinstance(c_or_f, FunctionType) else c_or_f
return CodeType(
c.co_argcount, c.co_kwonlyargcount, c.co_nlocals,
c.co_stacksize, c.co_flags, c.co_code, c.co_consts, c.co_names,
c.co_varnames, c.co_filename, c.co_name, c.co_firstlineno,
c.co_lnotab, c.co_freevars + ('__class__',), c.co_cellvars)
def add_foreign_method(cls, f):
code = new_code(f.__code__)
name = f.__name__
defaults = f.__defaults__
closure = (f.__closure__ or ()) + create_closure(cls)
setattr(cls, name, FunctionType(code, globals(), name, defaults, closure))
class List(list):
def append(self, elem):
super().append(elem)
def extend(self, elems):
super().extend(elems)
def __getitem__(self, i):
print('foreign getitem')
return super().__getitem__(i)
add_foreign_method(List, __getitem__)
self = List([1,2,3])
self[0]
----------
components: Interpreter Core
messages: 307279
nosy: bup
priority: normal
severity: normal
status: open
title: Zero argument super is broken in 3.6 for methods with a hacked __class__ cell
type: behavior
versions: Python 3.6
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue32176>
_______________________________________
More information about the New-bugs-announce
mailing list