Skip to content

Commit

Permalink
Replace the __new__ trick with slightly more boiler plate. This sho…
Browse files Browse the repository at this point in the history
…uld make the class more predictable to users.
  • Loading branch information
ionelmc committed Mar 9, 2020
1 parent 24d8d10 commit 32a5665
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions src/tblib/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import re
import sys
from types import CodeType
from types import FrameType
from types import TracebackType

try:
Expand Down Expand Up @@ -42,16 +43,8 @@ class TracebackParseError(Exception):


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 @@ -67,7 +60,7 @@ def __reduce__(self):
return Code, (_AttrDict(self.__dict__),)

# noinspection SpellCheckingInspection
def __tproxy_handler(self, operation, *args, **kwargs):
def __tproxy__(self, operation, *args, **kwargs):
if operation in ('__getattribute__', '__getattr__'):
return getattr(self, args[0])
else:
Expand All @@ -92,9 +85,18 @@ def clear(self):
# in turn is called by unittest.TestCase.assertRaises
pass

# noinspection SpellCheckingInspection
def __tproxy__(self, operation, *args, **kwargs):
if operation in ('__getattribute__', '__getattr__'):
if args[0] == 'f_code':
return tproxy(CodeType, self.f_code.__tproxy__)
else:
return getattr(self, args[0])
else:
return getattr(self, operation)(*args, **kwargs)

class Traceback(object):

class Traceback(object):
tb_next = None

def __init__(self, tb):
Expand All @@ -116,7 +118,7 @@ def __init__(self, tb):

def as_traceback(self):
if tproxy:
return tproxy(TracebackType, self.__tproxy_handler)
return tproxy(TracebackType, self.__tproxy__)
if not tb_set_next:
raise RuntimeError("Unsupported Python interpreter!")

Expand Down Expand Up @@ -169,10 +171,12 @@ def as_traceback(self):
to_traceback = as_traceback

# noinspection SpellCheckingInspection
def __tproxy_handler(self, operation, *args, **kwargs):
def __tproxy__(self, operation, *args, **kwargs):
if operation in ('__getattribute__', '__getattr__'):
if args[0] == 'tb_next':
return self.tb_next and self.tb_next.as_traceback()
elif args[0] == 'tb_frame':
return tproxy(FrameType, self.tb_frame.__tproxy__)
else:
return getattr(self, args[0])
else:
Expand Down

0 comments on commit 32a5665

Please sign in to comment.