def setup(self): _signs = self._methods = {} self._fields = {} for i, val in self._class_._methods.iteritems(): retval = annotation(val.retval._type) values = [annotation(arg._type) for arg in val.args] s_args = [j for j in values] _signs[i] = MethodDesc(tuple(s_args), retval) next = annmodel.SomeBuiltin( Analyzer(i, val, retval, s_args), s_self=annmodel.SomeExternalInstance(self._class_), methodname=i ) next.const = True self._fields[i] = next for i, val in self._class_._fields.iteritems(): self._fields[i] = annotation(val)
class FunEntry(ExtFuncEntry): _about_ = function safe_not_sandboxed = sandboxsafe if args is None: def normalize_args(self, *args_s): return args_s # accept any argument unmodified elif callable(args): # custom annotation normalizer (see e.g. os.utime()) normalize_args = staticmethod(args) else: # use common case behavior signature_args = args signature_result = annotation(result, None) name = export_name if llimpl: lltypeimpl = staticmethod(llimpl) if ooimpl: ootypeimpl = staticmethod(ooimpl) if llfakeimpl: lltypefakeimpl = staticmethod(llfakeimpl) if oofakeimpl: ootypefakeimpl = staticmethod(oofakeimpl) if annotation_hook: ann_hook = staticmethod(annotation_hook)
def setup(self): _signs = self._methods = {} self._fields = {} for i, val in self._class_._methods.iteritems(): retval = annotation(val.retval._type) values = [annotation(arg._type) for arg in val.args] s_args = [j for j in values] _signs[i] = MethodDesc(tuple(s_args), retval) next = annmodel.SomeBuiltin(Analyzer(i, val, retval, s_args), s_self=annmodel.SomeExternalInstance( self._class_), methodname=i) next.const = True self._fields[i] = next for i, val in self._class_._fields.iteritems(): self._fields[i] = annotation(val)
def get_loader(type): s_obj = annotation(type, None) try: # look for a marshaller in the 'loaders' list return find_loader(s_obj) except CannotUnmarshall: # ask the annotation to produce an appropriate loader pair(_tag, s_obj).install_unmarshaller() return find_loader(s_obj)
def get_marshaller(type): """Return a marshaller function. The marshaller takes two arguments: a buffer and an object of type 'type'. The buffer is list of characters that gets extended with new data when the marshaller is called. """ s_obj = annotation(type, None) try: # look for a marshaller in the 'dumpers' list return find_dumper(s_obj) except CannotMarshal: # ask the annotation to produce an appropriate dumper pair(_tag, s_obj).install_marshaller() return find_dumper(s_obj)
def rtype_setattr(self, hop): if self.lowleveltype is ootype.Void: return vlist = [hop.inputarg(self, arg=0), hop.inputarg(ootype.Void, arg=1)] field_name = hop.args_s[1].const obj = self.ext_desc._class_._fields[field_name] bookkeeper = hop.rtyper.annotator.bookkeeper # XXX WARNING XXX # annotation() here should not be called, but we somehow # have overwritten _fields. This will do no harm, but may hide some # errors r = hop.rtyper.getrepr(annotation(obj, bookkeeper)) r.setup() v = hop.inputarg(r, arg=2) vlist.append(v) return hop.genop('oosetfield', vlist)
def install_marshaller((tag, s_list)): def dump_list_or_none(buf, x): if x is None: dump_none(buf, x) else: buf.append(TYPE_LIST) w_long(buf, len(x)) for item in x: itemdumper(buf, item) itemdumper = get_marshaller(s_list.listdef.listitem.s_value) if s_list.listdef.listitem.dont_change_any_more: s_general_list = s_list else: s_item = get_dumper_annotation(itemdumper) s_general_list = annotation([s_item]) add_dumper(s_general_list, dump_list_or_none)
def normalize_args(self, *args_s): args = self.signature_args signature_args = [annotation(arg, None) for arg in args] assert len(args_s) == len(signature_args),\ "Argument number mismatch" for i, expected in enumerate(signature_args): arg = unionof(args_s[i], expected) if not expected.contains(arg): name = getattr(self, 'name', None) if not name: try: name = self.instance.__name__ except AttributeError: name = '?' raise Exception("In call to external function %r:\n" "arg %d must be %s,\n" " got %s" % ( name, i+1, expected, args_s[i])) return signature_args
def compute_result_annotation(self): from pypy.annotation import model as annmodel return annmodel.SomeGenericCallable([annotation(i, self.bookkeeper) for i in self.instance.args], annotation(self.instance.result, self.bookkeeper))
def compute_annotation(self): bookkeeper = getbookkeeper() args_s = [annotation(i._type) for i in self.instance.args] s_result = annotation(self.instance.result._type) return annmodel.SomeGenericCallable(args=args_s, result=s_result)
def load_int(loader): if readchr(loader) != TYPE_INT: raise ValueError("expected an int") return readlong(loader) add_loader(annmodel.SomeInteger(), load_int) def dump_longlong(buf, x): buf.append(TYPE_INT64) w_long(buf, intmask(x)) w_long(buf, intmask(x >> 32)) add_dumper(annotation(r_longlong), dump_longlong) r_32bits_mask = r_longlong(0xFFFFFFFF) def load_longlong(loader): if readchr(loader) != TYPE_INT64: raise ValueError("expected a longlong") x = r_longlong(readlong(loader)) & r_32bits_mask x |= (r_longlong(readlong(loader)) << 32) return x add_loader(annotation(r_longlong), load_longlong)
def annotation_to_cts(self, _tp): s_tp = annotation(_tp) TP = annotation_to_lltype(s_tp) return self.lltype_to_cts(TP)
def test_genericcallable(): py.test.skip("this two annotations should be equal - fix!") from pypy.rpython.extfunc import genericcallable s1 = annotation([genericcallable([str], int)]) s2 = annotation([genericcallable([str], int)]) assert s1 == s2
if x < 0: raise ValueError("expected a non-negative int") return x add_loader(annmodel.SomeInteger(nonneg=True), load_int_nonneg) def load_int(loader): if readchr(loader) != TYPE_INT: raise ValueError("expected an int") return readlong(loader) add_loader(annmodel.SomeInteger(), load_int) def dump_longlong(buf, x): buf.append(TYPE_INT64) w_long(buf, intmask(x)) w_long(buf, intmask(x>>32)) add_dumper(annotation(r_longlong), dump_longlong) r_32bits_mask = r_longlong(0xFFFFFFFF) def load_longlong(loader): if readchr(loader) != TYPE_INT64: raise ValueError("expected a longlong") x = r_longlong(readlong(loader)) & r_32bits_mask x |= (r_longlong(readlong(loader)) << 32) return x add_loader(annotation(r_longlong), load_longlong) def dump_float(buf, x): buf.append(TYPE_FLOAT) s = formatd("%.17g", x) buf.append(chr(len(s)))
def typeannotation(self, t): return signature.annotation(t, self.bookkeeper)