def test_proxy_get(self): from __pypy__ import tproxy, get_tproxy_controller l = [1,2,3] def f(name, *args, **kwargs): return getattr(l, name)(*args, **kwargs) lst = tproxy(list, f) assert get_tproxy_controller(lst) is f
def test_proxy_file(self): from __pypy__ import tproxy def f(name, *args, **kwds): pass t = tproxy(file, f)
def as_traceback(self): if tproxy: return tproxy(TracebackType, self.__tproxy_handler) elif tb_set_next: f_code = self.tb_frame.f_code code = compile( '\n' * (self.tb_lineno - 1) + 'raise __traceback_maker', self.tb_frame.f_code.co_filename, 'exec') if PY3: code = CodeType(0, 0, f_code.co_nlocals, f_code.co_stacksize, f_code.co_flags, code.co_code, code.co_consts, code.co_names, code.co_varnames, f_code.co_filename, f_code.co_name, code.co_firstlineno, b"", (), ()) else: code = CodeType(0, f_code.co_nlocals, f_code.co_stacksize, f_code.co_flags, code.co_code, code.co_consts, code.co_names, code.co_varnames, f_code.co_filename.encode(), f_code.co_name.encode(), code.co_firstlineno, b"", (), ()) try: exec(code, self.tb_frame.f_globals, {}) except: tb = sys.exc_info()[2].tb_next tb_set_next(tb, self.tb_next and self.tb_next.as_traceback()) try: return tb finally: del tb else: raise RuntimeError("Cannot re-create traceback !")
def as_traceback(self): if tproxy: return tproxy(TracebackType, self.__tproxy_handler) elif tb_set_next: f_code = self.tb_frame.f_code code = compile('\n' * (self.tb_lineno - 1) + 'raise __traceback_maker', self.tb_frame.f_code.co_filename, 'exec') if PY3: code = CodeType( 0, code.co_kwonlyargcount, code.co_nlocals, code.co_stacksize, code.co_flags, code.co_code, code.co_consts, code.co_names, code.co_varnames, f_code.co_filename, f_code.co_name, code.co_firstlineno, code.co_lnotab, (), () ) else: code = CodeType( 0, code.co_nlocals, code.co_stacksize, code.co_flags, code.co_code, code.co_consts, code.co_names, code.co_varnames, f_code.co_filename.encode(), f_code.co_name.encode(), code.co_firstlineno, code.co_lnotab, (), () ) try: exec(code, self.tb_frame.f_globals, {}) except: tb = sys.exc_info()[2].tb_next tb_set_next(tb, self.tb_next and self.tb_next.as_traceback()) return tb else: raise RuntimeError("Cannot re-create traceback !")
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)
def as_traceback(self): """ Convert to a builtin Traceback object that is usable for raising or rendering a stacktrace. """ if tproxy: return tproxy(TracebackType, self.__tproxy__) if not tb_set_next: raise RuntimeError("Unsupported Python interpreter!") current = self top_tb = None tb = None while current: f_code = current.tb_frame.f_code code = compile( '\n' * (current.tb_lineno - 1) + 'raise __traceback_maker', current.tb_frame.f_code.co_filename, 'exec') if hasattr(code, "replace"): # Python 3.8 and newer code = code.replace(co_argcount=0, co_filename=f_code.co_filename, co_name=f_code.co_name, co_freevars=(), co_cellvars=()) elif PY3: code = CodeType(0, code.co_kwonlyargcount, code.co_nlocals, code.co_stacksize, code.co_flags, code.co_code, code.co_consts, code.co_names, code.co_varnames, f_code.co_filename, f_code.co_name, code.co_firstlineno, code.co_lnotab, (), ()) else: code = CodeType(0, code.co_nlocals, code.co_stacksize, code.co_flags, code.co_code, code.co_consts, code.co_names, code.co_varnames, f_code.co_filename.encode(), f_code.co_name.encode(), code.co_firstlineno, code.co_lnotab, (), ()) # noinspection PyBroadException try: exec(code, dict(current.tb_frame.f_globals), {}) except Exception: next_tb = sys.exc_info()[2].tb_next if top_tb is None: top_tb = next_tb if tb is not None: tb_set_next(tb, next_tb) tb = next_tb del next_tb current = current.tb_next try: return top_tb finally: del top_tb del tb
def test_one(self): x = type(self.wrapped)() from __pypy__ import tproxy def f(name, *args, **kwds): return getattr(x, name)(*args, **kwds) t = tproxy(type(x), f) assert t.__name__ == x.__name__
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: return getattr(self, operation)(*args, **kwargs)
def test_proxy_get(self): from __pypy__ import tproxy, get_tproxy_controller class A(object): pass def f(name, *args, **kwargs): pass lst = tproxy(A, f) assert get_tproxy_controller(lst) is f
def as_traceback(self): if tproxy: return tproxy(TracebackType, self.__tproxy_handler) if not tb_set_next: raise RuntimeError("Cannot re-create traceback !") current = self top_tb = None tb = None while current: f_code = current.tb_frame.f_code code = compile( '\n' * (current.tb_lineno - 1) + 'raise __traceback_maker', current.tb_frame.f_code.co_filename, 'exec') if hasattr(code, "replace"): # Python 3.8 and newer code = code.replace(co_argcount=0, co_filename=f_code.co_filename, co_name=f_code.co_name, co_freevars=(), co_cellvars=()) elif PY3: code = CodeType(0, code.co_kwonlyargcount, code.co_nlocals, code.co_stacksize, code.co_flags, code.co_code, code.co_consts, code.co_names, code.co_varnames, f_code.co_filename, f_code.co_name, code.co_firstlineno, code.co_lnotab, (), ()) else: code = CodeType(0, code.co_nlocals, code.co_stacksize, code.co_flags, code.co_code, code.co_consts, code.co_names, code.co_varnames, f_code.co_filename.encode(), f_code.co_name.encode(), code.co_firstlineno, code.co_lnotab, (), ()) # noinspection PyBroadException try: exec(code, current.tb_frame.f_globals, {}) except: next_tb = sys.exc_info()[2].tb_next if top_tb is None: top_tb = next_tb if tb is not None: tb_set_next(tb, next_tb) tb = next_tb del next_tb current = current.tb_next try: return top_tb finally: del top_tb del tb
def make_proxy(obj, proxy): if tproxy is None: return proxy def operation_handler(operation, *args, **kwargs): if operation in ('__getattribute__', '__getattr__'): return getattr(proxy, args[0]) elif operation == '__setattr__': proxy.__setattr__(*args, **kwargs) else: return getattr(proxy, operation)(*args, **kwargs) return tproxy(type(obj), operation_handler)
def make_frame_proxy(frame): proxy = TracebackFrameProxy(frame) if tproxy is None: return proxy def operation_handler(operation, *args, **kwargs): if operation in ('__getattribute__', '__getattr__'): return getattr(proxy, args[0]) elif operation == '__setattr__': proxy.__setattr__(*args, **kwargs) else: return getattr(proxy, operation)(*args, **kwargs) return tproxy(TracebackType, operation_handler)
def as_traceback(self): if tproxy: return tproxy(TracebackType, self.__tproxy_handler) if not tb_set_next: raise RuntimeError("Unsupported Python interpreter!") current = self top_tb = None tb = None while current: f_code = current.tb_frame.f_code code = compile('\n' * (current.tb_lineno - 1) + 'raise __traceback_maker', current.tb_frame.f_code.co_filename, 'exec') if PY3: code = CodeType( 0, code.co_kwonlyargcount, code.co_nlocals, code.co_stacksize, code.co_flags, code.co_code, code.co_consts, code.co_names, code.co_varnames, f_code.co_filename, f_code.co_name, code.co_firstlineno, code.co_lnotab, (), () ) else: code = CodeType( 0, code.co_nlocals, code.co_stacksize, code.co_flags, code.co_code, code.co_consts, code.co_names, code.co_varnames, f_code.co_filename.encode(), f_code.co_name.encode(), code.co_firstlineno, code.co_lnotab, (), () ) # noinspection PyBroadException try: exec(code, current.tb_frame.f_globals, {}) except Exception: next_tb = sys.exc_info()[2].tb_next if top_tb is None: top_tb = next_tb if tb is not None: tb_set_next(tb, next_tb) tb = next_tb del next_tb current = current.tb_next try: return top_tb finally: del top_tb del tb
def make_proxy(controller, type=_dummy, obj=_dummy): """ return a tranparent proxy controlled by the given 'controller' callable. The proxy will appear as a completely regular instance of the given type but all operations on it are send to the specified controller - which receives on ProxyOperation instance on each such call. A non-specified type will default to type(obj) if obj is specified. """ if type is _dummy: if obj is _dummy: raise TypeError("you must specify a type or an instance obj of it") type = origtype(obj) def perform(opname, *args, **kwargs): operation = ProxyOperation(tp, obj, opname, args, kwargs) return controller(operation) tp = tproxy(type, perform) return tp
def as_traceback(self): if tproxy: return tproxy(TracebackType, self.__tproxy_handler) elif tb_set_next: f_code = self.tb_frame.f_code code = compile( '\n' * (self.tb_lineno - 1) + 'raise __traceback_maker', self.tb_frame.f_code.co_filename, 'exec') if PY3: code = CodeType(0, code.co_kwonlyargcount, code.co_nlocals, code.co_stacksize, code.co_flags, code.co_code, code.co_consts, code.co_names, code.co_varnames, f_code.co_filename, f_code.co_name, code.co_firstlineno, code.co_lnotab, (), ()) else: code = CodeType(0, code.co_nlocals, code.co_stacksize, code.co_flags, code.co_code, code.co_consts, code.co_names, code.co_varnames, f_code.co_filename.encode(), f_code.co_name.encode(), code.co_firstlineno, code.co_lnotab, (), ()) # noinspection PyBroadException try: exec(code, self.tb_frame.f_globals, {}) except: tb = sys.exc_info()[2].tb_next tb_set_next(tb, self.tb_next and self.tb_next.as_traceback()) try: return tb finally: # gevent: don't leak the traceback objects, this # makes our leaktests fail del tb else: raise RuntimeError("Cannot re-create traceback !")