delegation_dict = {} for key, value in W_BytesObject.typedef.rawdict.iteritems(): if not isinstance(value, interp2app): continue if key in ('__len__', '__add__', '__str__'): continue func = value._code._bltin args = inspect.getargs(func.func_code) if args.varargs or args.keywords: raise TypeError("Varargs and keywords not supported in unwrap_spec") argspec = ', '.join([arg for arg in args.args[1:]]) func_code = py.code.Source(""" def f(self, %(args)s): self.force() return self.w_str.%(func_name)s(%(args)s) """ % {'args': argspec, 'func_name': func.func_name}) d = {} exec func_code.compile() in d f = d['f'] f.func_defaults = func.func_defaults f.__module__ = func.__module__ # necessary for unique identifiers for pickling f.func_name = func.func_name unwrap_spec_ = getattr(func, 'unwrap_spec', None) if unwrap_spec_ is not None: f = unwrap_spec(**unwrap_spec_)(f) setattr(W_StringBufferObject, func.func_name, f) W_StringBufferObject.typedef = W_BytesObject.typedef
def make_perform_trampoline(prefix, exprargs, expr, miniglobals, multimethod, selfindex=0, allow_NotImplemented_results=False): """NOT_RPYTHON""" # mess to figure out how to put a gateway around executing expr argnames = ['_%d'%(i+1) for i in range(multimethod.arity)] explicit_argnames = multimethod.extras.get('argnames', []) argnames[len(argnames)-len(explicit_argnames):] = explicit_argnames solid_arglist = ['w_'+name for name in argnames] wrapper_arglist = solid_arglist[:] if multimethod.extras.get('varargs_w', False): wrapper_arglist.append('args_w') if multimethod.extras.get('keywords', False): raise Exception, "no longer supported, use __args__" if multimethod.extras.get('general__args__', False): wrapper_arglist.append('__args__') wrapper_arglist += multimethod.extras.get('extra_args', ()) miniglobals.update({ 'OperationError': OperationError, 'gettypeerror': gettypeerror}) app_defaults = multimethod.extras.get('defaults', ()) i = len(argnames) - len(app_defaults) wrapper_signature = wrapper_arglist[:] unwrap_spec_kwds = {} for app_default in app_defaults: name = wrapper_signature[i] unwrap_spec_kwds[name] = gateway.WrappedDefault(app_default) i += 1 wrapper_signature.insert(0, wrapper_signature.pop(selfindex)) wrapper_sig = ', '.join(wrapper_signature) src = [] dest = [] for wrapper_arg,expr_arg in zip(['space']+wrapper_arglist, exprargs): if wrapper_arg != expr_arg: src.append(wrapper_arg) dest.append(expr_arg) renaming = ', '.join(dest) +" = "+', '.join(src) if allow_NotImplemented_results and (len(multimethod.specialnames) > 1 or multimethod.name.startswith('inplace_')): # turn FailedToImplement into NotImplemented code = """def %s_perform_call(space, %s): %s try: return %s except FailedToImplement, e: if e.get_w_type(space) is not None: raise OperationError(e.w_type, e.get_w_value(space)) else: return space.w_NotImplemented """ % (prefix, wrapper_sig, renaming, expr) else: # turn FailedToImplement into nice TypeErrors code = """def %s_perform_call(space, %s): %s try: w_res = %s except FailedToImplement, e: if e.get_w_type(space) is not None: raise OperationError(e.w_type, e.get_w_value(space)) else: raise gettypeerror(space, %r, %s) if w_res is None: w_res = space.w_None return w_res """ % (prefix, wrapper_sig, renaming, expr, multimethod.operatorsymbol, ', '.join(solid_arglist)) exec compile2(code, '', 'exec') in miniglobals func = miniglobals["%s_perform_call" % prefix] if unwrap_spec_kwds: func = gateway.unwrap_spec(**unwrap_spec_kwds)(func) return func
continue if key in ('__len__', '__add__', '__str__'): continue func = value._code._bltin args = inspect.getargs(func.func_code) if args.varargs or args.keywords: raise TypeError("Varargs and keywords not supported in unwrap_spec") argspec = ', '.join([arg for arg in args.args[1:]]) func_code = py.code.Source(""" def f(self, %(args)s): self.force() return self.w_str.%(func_name)s(%(args)s) """ % { 'args': argspec, 'func_name': func.func_name }) d = {} exec func_code.compile() in d f = d['f'] f.func_defaults = func.func_defaults f.__module__ = func.__module__ # necessary for unique identifiers for pickling f.func_name = func.func_name unwrap_spec_ = getattr(func, 'unwrap_spec', None) if unwrap_spec_ is not None: f = unwrap_spec(**unwrap_spec_)(f) setattr(W_StringBufferObject, func.func_name, f) W_StringBufferObject.typedef = W_BytesObject.typedef
def make_perform_trampoline(prefix, exprargs, expr, miniglobals, multimethod, selfindex=0, allow_NotImplemented_results=False): """NOT_RPYTHON""" # mess to figure out how to put a gateway around executing expr argnames = ['_%d' % (i + 1) for i in range(multimethod.arity)] explicit_argnames = multimethod.extras.get('argnames', []) argnames[len(argnames) - len(explicit_argnames):] = explicit_argnames solid_arglist = ['w_' + name for name in argnames] wrapper_arglist = solid_arglist[:] if multimethod.extras.get('varargs_w', False): wrapper_arglist.append('args_w') if multimethod.extras.get('keywords', False): raise Exception, "no longer supported, use __args__" if multimethod.extras.get('general__args__', False): wrapper_arglist.append('__args__') wrapper_arglist += multimethod.extras.get('extra_args', ()) miniglobals.update({ 'OperationError': OperationError, 'gettypeerror': gettypeerror }) app_defaults = multimethod.extras.get('defaults', ()) i = len(argnames) - len(app_defaults) wrapper_signature = wrapper_arglist[:] unwrap_spec_kwds = {} for app_default in app_defaults: name = wrapper_signature[i] unwrap_spec_kwds[name] = gateway.WrappedDefault(app_default) i += 1 wrapper_signature.insert(0, wrapper_signature.pop(selfindex)) wrapper_sig = ', '.join(wrapper_signature) src = [] dest = [] for wrapper_arg, expr_arg in zip(['space'] + wrapper_arglist, exprargs): if wrapper_arg != expr_arg: src.append(wrapper_arg) dest.append(expr_arg) renaming = ', '.join(dest) + " = " + ', '.join(src) if allow_NotImplemented_results and ( len(multimethod.specialnames) > 1 or multimethod.name.startswith('inplace_')): # turn FailedToImplement into NotImplemented code = """def %s_perform_call(space, %s): %s try: return %s except FailedToImplement, e: if e.get_w_type(space) is not None: raise OperationError(e.w_type, e.get_w_value(space)) else: return space.w_NotImplemented """ % (prefix, wrapper_sig, renaming, expr) else: # turn FailedToImplement into nice TypeErrors code = """def %s_perform_call(space, %s): %s try: w_res = %s except FailedToImplement, e: if e.get_w_type(space) is not None: raise OperationError(e.w_type, e.get_w_value(space)) else: raise gettypeerror(space, %r, %s) if w_res is None: w_res = space.w_None return w_res """ % (prefix, wrapper_sig, renaming, expr, multimethod.operatorsymbol, ', '.join(solid_arglist)) exec compile2(code, '', 'exec') in miniglobals func = miniglobals["%s_perform_call" % prefix] if unwrap_spec_kwds: func = gateway.unwrap_spec(**unwrap_spec_kwds)(func) return func