def annotationoftype(t, bookkeeper=False): from pypy.rpython import extregistry """The most precise SomeValue instance that contains all objects of type t.""" assert isinstance(t, (type, types.ClassType)) if t is bool: return SomeBool() elif t is int: return SomeInteger() elif t is float: return SomeFloat() elif issubclass(t, str): # py.lib uses annotated str subclasses return SomeString() elif t is unicode: return SomeUnicodeString() elif t is list: return SomeList(MOST_GENERAL_LISTDEF) elif t is dict: return SomeDict(MOST_GENERAL_DICTDEF) # can't do tuple elif t is types.NoneType: return s_None elif bookkeeper and extregistry.is_registered_type(t, bookkeeper.policy): entry = extregistry.lookup_type(t, bookkeeper.policy) return entry.compute_annotation_bk(bookkeeper) elif bookkeeper and t.__module__ != '__builtin__' and t not in bookkeeper.pbctypes: classdef = bookkeeper.getuniqueclassdef(t) return SomeInstance(classdef) else: o = SomeObject() if t != object: o.knowntype = t return o
def getattr(p, s_attr): if s_attr.is_constant() and isinstance(s_attr.const, str): attr = s_attr.const entry = extregistry.lookup_type(p.knowntype) s_value = entry.get_field_annotation(p.knowntype, attr) return s_value else: return SomeObject()
def return_annotation(self): """Returns either 'self' or the annotation of the unwrapped version of this ctype, following the logic used when ctypes operations return a value. """ from pypy.rpython import extregistry assert extregistry.is_registered_type(self.knowntype) entry = extregistry.lookup_type(self.knowntype) # special case for returning primitives or c_char_p return getattr(entry, 's_return_trick', self)
def getattr(p, s_attr): if s_attr.is_constant() and isinstance(s_attr.const, str): # XXX kill with extfunctable.py if p.knowntype in builtin.EXTERNAL_TYPE_ANALYZERS: return SomeObject.getattr(p, s_attr) attr = s_attr.const entry = extregistry.lookup_type(p.knowntype) s_value = entry.get_field_annotation(p.knowntype, attr) return s_value else: return SomeObject()
def rtyper_makerepr(self, rtyper): entry = extregistry.lookup_type(self.knowntype) return entry.get_repr(rtyper, self)
def setattr(p, s_attr, s_value): assert s_attr.is_constant() attr = s_attr.const entry = extregistry.lookup_type(p.knowntype) entry.set_field_annotation(p.knowntype, attr, s_value)
def rtyper_makerepr(self, rtyper): # delegate to the get_repr() of the extregistrered Entry class entry = extregistry.lookup_type(self.knowntype) return entry.get_repr(rtyper, self)
def getcontroller(ctype): """Return the CTypeController instance corresponding to the given ctype.""" entry = extregistry.lookup_type(ctype) return entry.getcontroller()
def simple_call(cto, *args_s): # for variables containing ctypes function pointers entry = extregistry.lookup_type(cto.knowntype) return entry.compute_result_annotation(*args_s)
def compute_annotation(self): return lookup_type(W_Object).compute_annotation_bk(self.bookkeeper)