Example #1
0
    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)
Example #2
0
    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)
Example #3
0
    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)
Example #4
0
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)
Example #5
0
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)
Example #6
0
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)
Example #7
0
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)
Example #8
0
 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)
Example #9
0
    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)
Example #10
0
    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)
Example #11
0
 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
Example #12
0
 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
Example #13
0
 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))
Example #14
0
 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)
Example #15
0
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)

Example #16
0
 def annotation_to_cts(self, _tp):
     s_tp = annotation(_tp)
     TP = annotation_to_lltype(s_tp)
     return self.lltype_to_cts(TP)
Example #17
0
 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))
Example #18
0
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
Example #19
0
    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)))
Example #20
0
 def annotation_to_cts(self, _tp):
     s_tp = annotation(_tp)
     TP = annotation_to_lltype(s_tp)
     return self.lltype_to_cts(TP)
Example #21
0
 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)
Example #22
0
 def typeannotation(self, t):
     return signature.annotation(t, self.bookkeeper)
Example #23
0
 def typeannotation(self, t):
     return signature.annotation(t, self.bookkeeper)
Example #24
0
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