Skip to content

Commit

Permalink
Add tproxy
Browse files Browse the repository at this point in the history
  • Loading branch information
purplesyringa committed Mar 8, 2020
1 parent 43ed15d commit 24d8d10
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/tblib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@

class _AttrDict(dict):
__slots__ = ()
__getattr__ = dict.__getitem__

def __getattr__(self, name):
try:
return self[name]
except KeyError:
raise AttributeError(name)


# noinspection PyPep8Naming
Expand All @@ -40,6 +45,13 @@ class Code(object):

co_code = None

def __new__(cls, *args, **kwargs):
code = super(Code, cls).__new__(cls)
if tproxy:
code.__init__(*args, **kwargs)
return tproxy(CodeType, code.__tproxy_handler)
return code

def __init__(self, code):
self.co_filename = code.co_filename
self.co_name = code.co_name
Expand All @@ -51,6 +63,16 @@ def __init__(self, code):
self.co_flags = 64
self.co_firstlineno = 0

def __reduce__(self):
return Code, (_AttrDict(self.__dict__),)

# noinspection SpellCheckingInspection
def __tproxy_handler(self, operation, *args, **kwargs):
if operation in ('__getattribute__', '__getattr__'):
return getattr(self, args[0])
else:
return getattr(self, operation)(*args, **kwargs)


class Frame(object):
def __init__(self, frame):
Expand Down

0 comments on commit 24d8d10

Please sign in to comment.