我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用__pypy__.tproxy()。
def _init(): global _installed global tb_set_next if _installed: return _installed = True import platform try: if platform.python_implementation() == 'CPython': tb_set_next = _init_ugly_crap() except Exception as exc: sys.stderr.write("Failed to initialize cpython support: {!r}".format(exc)) try: from __pypy__ import tproxy except ImportError: tproxy = None if not tb_set_next and not tproxy: raise ImportError("Cannot use tblib. Runtime not supported.") _import_dump_load() install()
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 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 standard_exc_info(self): """Standard python exc_info for re-raising""" tb = self.frames[0] # the frame will be an actual traceback (or transparent proxy) if # we are on pypy or a python implementation with support for tproxy if type(tb) is not TracebackType: tb = tb.tb return self.exc_type, self.exc_value, tb
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, (), () ) # 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 !") # noinspection SpellCheckingInspection