def fake_object(space, x): if isinstance(x, file): debug_print("fake-wrapping interp file %s" % x) if isinstance(x, type): ft = fake_type(x) return space.gettypeobject(ft.typedef) #debug_print("faking obj %s" % x) ft = fake_type(type(x)) return ft(space, x)
def fake_object(space, x): if isinstance(x, file): debug_print("fake-wrapping interp file %s" % x) if isinstance(x, type): ft = fake_type(x) return space.gettypeobject(ft.typedef) #debug_print("faking obj %s" % x) ft = fake_type(type(x)) return ft(space, x)
def wrap_exception(space): """NOT_RPYTHON""" exc, value, tb = sys.exc_info() if exc is OperationError: raise exc, value, tb # just re-raise it name = exc.__name__ if hasattr(space, 'w_' + name): w_exc = getattr(space, 'w_' + name) w_value = space.call_function(w_exc, *[space.wrap(a) for a in value.args]) for key, value in value.__dict__.items(): if not key.startswith('_'): space.setattr(w_value, space.wrap(key), space.wrap(value)) else: debug_print('likely crashes because of faked exception %s: %s' % ( exc.__name__, value)) w_exc = space.wrap(exc) w_value = space.wrap(value) raise OperationError, OperationError(w_exc, w_value), tb
def wrap_exception(space): """NOT_RPYTHON""" exc, value, tb = sys.exc_info() if exc is OperationError: raise exc, value, tb # just re-raise it name = exc.__name__ if hasattr(space, 'w_' + name): w_exc = getattr(space, 'w_' + name) w_value = space.call_function(w_exc, *[space.wrap(a) for a in value.args]) for key, value in value.__dict__.items(): if not key.startswith('_'): space.setattr(w_value, space.wrap(key), space.wrap(value)) else: debug_print('likely crashes because of faked exception %s: %s' % (exc.__name__, value)) w_exc = space.wrap(exc) w_value = space.wrap(value) raise OperationError, OperationError(w_exc, w_value), tb
def really_build_fake_type(cpy_type): "NOT_RPYTHON (not remotely so!)." debug_print('faking %r'%(cpy_type,)) kw = {} if cpy_type.__name__ == 'SRE_Pattern': import re import __builtin__ p = re.compile("foo") for meth_name in p.__methods__: kw[meth_name] = EvenMoreObscureWrapping(__builtin__.eval("lambda p,*args,**kwds: p.%s(*args,**kwds)" % meth_name)) elif cpy_type.__name__ == 'SRE_Match': import re import __builtin__ m = re.compile("foo").match('foo') for meth_name in m.__methods__: kw[meth_name] = EvenMoreObscureWrapping(__builtin__.eval("lambda m,*args,**kwds: m.%s(*args,**kwds)" % meth_name)) else: for s, v in cpy_type.__dict__.items(): if not (cpy_type is unicode and s in ['__add__', '__contains__']): if s != '__getattribute__' or cpy_type is type(sys) or cpy_type is type(Exception): kw[s] = v kw['__module__'] = cpy_type.__module__ def fake__new__(space, w_type, __args__): args_w, kwds_w = __args__.unpack() args = [space.unwrap(w_arg) for w_arg in args_w] kwds = {} for (key, w_value) in kwds_w.items(): kwds[key] = space.unwrap(w_value) try: r = cpy_type.__new__(*[cpy_type]+args, **kwds) except: wrap_exception(space) raise w_obj = space.allocate_instance(W_Fake, w_type) W_Fake.__init__(w_obj, space, r) return w_obj fake__new__.func_name = "fake__new__" + cpy_type.__name__ kw['__new__'] = gateway.interp2app(fake__new__) if cpy_type.__base__ is not object and not issubclass(cpy_type, Exception): assert cpy_type.__base__ is basestring, cpy_type from pypy.objspace.std.basestringtype import basestring_typedef base = basestring_typedef else: base = None class W_Fake(W_Object): typedef = StdTypeDef( cpy_type.__name__, base, **kw) def __init__(w_self, space, val): w_self.val = val w_self.space = space def getdict(w_self, space): try: d = w_self.val.__dict__ except AttributeError: return W_Object.getdict(w_self, space) return space.wrap(d) def unwrap(w_self, space): return w_self.val if cpy_type is types.FunctionType: def __get__(self, obj, owner): return fake_object(self.space, self.val.__get__(obj, owner)) W_Fake.__name__ = 'W_Fake%s'%(cpy_type.__name__.capitalize()) W_Fake.typedef.fakedcpytype = cpy_type return W_Fake
def really_build_fake_type(cpy_type): "NOT_RPYTHON (not remotely so!)." #assert not issubclass(cpy_type, file), cpy_type debug_print('faking %r' % (cpy_type, )) kw = {} if cpy_type.__name__ == 'SRE_Pattern': import re import __builtin__ p = re.compile("foo") for meth_name in p.__methods__: kw[meth_name] = EvenMoreObscureWrapping( __builtin__.eval("lambda p,*args,**kwds: p.%s(*args,**kwds)" % meth_name)) elif cpy_type.__name__ == 'SRE_Match': import re import __builtin__ m = re.compile("foo").match('foo') for meth_name in m.__methods__: kw[meth_name] = EvenMoreObscureWrapping( __builtin__.eval("lambda m,*args,**kwds: m.%s(*args,**kwds)" % meth_name)) else: for s, v in cpy_type.__dict__.items(): if not (cpy_type is unicode and s in ['__add__', '__contains__']): if s != '__getattribute__' or cpy_type is type( sys) or cpy_type is type(Exception): kw[s] = v kw['__module__'] = cpy_type.__module__ def fake__new__(space, w_type, __args__): args_w, kwds_w = __args__.unpack() args = [space.unwrap(w_arg) for w_arg in args_w] kwds = {} for (key, w_value) in kwds_w.items(): kwds[key] = space.unwrap(w_value) try: r = apply(cpy_type.__new__, [cpy_type] + args, kwds) except: wrap_exception(space) raise w_obj = space.allocate_instance(W_Fake, w_type) W_Fake.__init__(w_obj, space, r) return w_obj kw['__new__'] = gateway.interp2app(fake__new__, unwrap_spec=[ baseobjspace.ObjSpace, baseobjspace.W_Root, argument.Arguments ]) if cpy_type.__base__ is not object: assert cpy_type.__base__ is basestring from pypy.objspace.std.basestringtype import basestring_typedef base = basestring_typedef else: base = None class W_Fake(W_Object): typedef = StdTypeDef(cpy_type.__name__, base, **kw) def __init__(w_self, space, val): w_self.val = val def unwrap(w_self, space): return w_self.val # cannot write to W_Fake.__name__ in Python 2.2! W_Fake = type(W_Object)('W_Fake%s' % (cpy_type.__name__.capitalize()), (W_Object, ), dict(W_Fake.__dict__.items())) W_Fake.typedef.fakedcpytype = cpy_type return W_Fake
def really_build_fake_type(cpy_type): "NOT_RPYTHON (not remotely so!)." #assert not issubclass(cpy_type, file), cpy_type debug_print('faking %r'%(cpy_type,)) kw = {} if cpy_type.__name__ == 'SRE_Pattern': import re import __builtin__ p = re.compile("foo") for meth_name in p.__methods__: kw[meth_name] = EvenMoreObscureWrapping(__builtin__.eval("lambda p,*args,**kwds: p.%s(*args,**kwds)" % meth_name)) elif cpy_type.__name__ == 'SRE_Match': import re import __builtin__ m = re.compile("foo").match('foo') for meth_name in m.__methods__: kw[meth_name] = EvenMoreObscureWrapping(__builtin__.eval("lambda m,*args,**kwds: m.%s(*args,**kwds)" % meth_name)) else: for s, v in cpy_type.__dict__.items(): if not (cpy_type is unicode and s in ['__add__', '__contains__']): if s != '__getattribute__' or cpy_type is type(sys) or cpy_type is type(Exception): kw[s] = v kw['__module__'] = cpy_type.__module__ def fake__new__(space, w_type, __args__): args_w, kwds_w = __args__.unpack() args = [space.unwrap(w_arg) for w_arg in args_w] kwds = {} for (key, w_value) in kwds_w.items(): kwds[key] = space.unwrap(w_value) try: r = apply(cpy_type.__new__, [cpy_type]+args, kwds) except: wrap_exception(space) raise w_obj = space.allocate_instance(W_Fake, w_type) W_Fake.__init__(w_obj, space, r) return w_obj kw['__new__'] = gateway.interp2app(fake__new__, unwrap_spec=[baseobjspace.ObjSpace, baseobjspace.W_Root, argument.Arguments]) if cpy_type.__base__ is not object: assert cpy_type.__base__ is basestring from pypy.objspace.std.basestringtype import basestring_typedef base = basestring_typedef else: base = None class W_Fake(W_Object): typedef = StdTypeDef( cpy_type.__name__, base, **kw) def __init__(w_self, space, val): w_self.val = val def unwrap(w_self, space): return w_self.val # cannot write to W_Fake.__name__ in Python 2.2! W_Fake = type(W_Object)('W_Fake%s'%(cpy_type.__name__.capitalize()), (W_Object,), dict(W_Fake.__dict__.items())) W_Fake.typedef.fakedcpytype = cpy_type return W_Fake
def really_build_fake_type(cpy_type): "NOT_RPYTHON (not remotely so!)." debug_print('faking %r' % (cpy_type, )) kw = {} if cpy_type.__name__ == 'SRE_Pattern': import re import __builtin__ p = re.compile("foo") for meth_name in p.__methods__: kw[meth_name] = EvenMoreObscureWrapping( __builtin__.eval("lambda p,*args,**kwds: p.%s(*args,**kwds)" % meth_name)) elif cpy_type.__name__ == 'SRE_Match': import re import __builtin__ m = re.compile("foo").match('foo') for meth_name in m.__methods__: kw[meth_name] = EvenMoreObscureWrapping( __builtin__.eval("lambda m,*args,**kwds: m.%s(*args,**kwds)" % meth_name)) else: for s, v in cpy_type.__dict__.items(): if not (cpy_type is unicode and s in ['__add__', '__contains__']): if s != '__getattribute__' or cpy_type is type( sys) or cpy_type is type(Exception): kw[s] = v kw['__module__'] = cpy_type.__module__ def fake__new__(space, w_type, __args__): args_w, kwds_w = __args__.unpack() args = [space.unwrap(w_arg) for w_arg in args_w] kwds = {} for (key, w_value) in kwds_w.items(): kwds[key] = space.unwrap(w_value) try: r = cpy_type.__new__(*[cpy_type] + args, **kwds) except: wrap_exception(space) raise w_obj = space.allocate_instance(W_Fake, w_type) W_Fake.__init__(w_obj, space, r) return w_obj fake__new__.func_name = "fake__new__" + cpy_type.__name__ kw['__new__'] = gateway.interp2app(fake__new__) if cpy_type.__base__ is not object and not issubclass(cpy_type, Exception): assert cpy_type.__base__ is basestring, cpy_type from pypy.objspace.std.basestringtype import basestring_typedef base = basestring_typedef else: base = None class W_Fake(W_Object): typedef = StdTypeDef(cpy_type.__name__, base, **kw) def __init__(w_self, space, val): w_self.val = val w_self.space = space def getdict(w_self, space): try: d = w_self.val.__dict__ except AttributeError: return W_Object.getdict(w_self, space) return space.wrap(d) def unwrap(w_self, space): return w_self.val if cpy_type is types.FunctionType: def __get__(self, obj, owner): return fake_object(self.space, self.val.__get__(obj, owner)) W_Fake.__name__ = 'W_Fake%s' % (cpy_type.__name__.capitalize()) W_Fake.typedef.fakedcpytype = cpy_type return W_Fake