newsuper.py (1935B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\packages\future\builtins\newsuper.py 2 from __future__ import absolute_import 3 import sys 4 from types import FunctionType 5 from future.utils import PY3, PY26 6 _builtin_super = super 7 _SENTINEL = object() 8 9 def newsuper(typ = _SENTINEL, type_or_obj = _SENTINEL, framedepth = 1): 10 if typ is _SENTINEL: 11 f = sys._getframe(framedepth) 12 try: 13 type_or_obj = f.f_locals[f.f_code.co_varnames[0]] 14 except (IndexError, KeyError): 15 raise RuntimeError('super() used in a function with no args') 16 17 try: 18 typ = find_owner(type_or_obj, f.f_code) 19 except (AttributeError, RuntimeError, TypeError): 20 try: 21 typ = find_owner(type_or_obj.__class__, f.f_code) 22 except AttributeError: 23 raise RuntimeError('super() used with an old-style class') 24 except TypeError: 25 raise RuntimeError('super() called outside a method') 26 27 if type_or_obj is not _SENTINEL: 28 return _builtin_super(typ, type_or_obj) 29 return _builtin_super(typ) 30 31 32 def find_owner(cls, code): 33 for typ in cls.__mro__: 34 for meth in typ.__dict__.values(): 35 try: 36 while not isinstance(meth, FunctionType): 37 if isinstance(meth, property): 38 meth = meth.fget 39 else: 40 try: 41 meth = meth.__func__ 42 except AttributeError: 43 meth = meth.__get__(cls, typ) 44 45 except (AttributeError, TypeError): 46 continue 47 48 if meth.__code__ is code: 49 return typ 50 51 raise TypeError 52 53 54 def superm(*args, **kwds): 55 f = sys._getframe(1) 56 nm = f.f_code.co_name 57 return getattr(newsuper(framedepth=2), nm)(*args, **kwds) 58 59 60 __all__ = ['newsuper']