def builtin_range(*args): s_step = immutablevalue(1) if len(args) == 1: s_start = immutablevalue(0) s_stop = args[0] elif len(args) == 2: s_start, s_stop = args elif len(args) == 3: s_start, s_stop = args[:2] s_step = args[2] else: raise AnnotatorError("range() takes 1 to 3 arguments") empty = False # so far if not s_step.is_constant(): step = 0 # this case signals a variable step else: step = s_step.const if step == 0: raise AnnotatorError("range() with step zero") if s_start.is_constant() and s_stop.is_constant(): try: if len(xrange(s_start.const, s_stop.const, step)) == 0: empty = True except TypeError: # if one of the .const is a Symbolic pass if empty: s_item = s_ImpossibleValue else: nonneg = False # so far if step > 0 or s_step.nonneg: nonneg = s_start.nonneg elif step < 0: nonneg = s_stop.nonneg or (s_stop.is_constant() and s_stop.const >= -1) s_item = SomeInteger(nonneg=nonneg) return getbookkeeper().newlist(s_item, range_step=step)
def method_join(self, s_list): if s_None.contains(s_list): return SomeImpossibleValue() s_item = s_list.listdef.read_item() if s_None.contains(s_item): if isinstance(self, SomeUnicodeString): return immutablevalue(u"") return immutablevalue("") no_nul = self.no_nul and s_item.no_nul return self.basestringclass(no_nul=no_nul)
def method_join(self, s_list): if s_None.contains(s_list): return SomeImpossibleValue() s_item = s_list.listdef.read_item() if s_None.contains(s_item): if isinstance(self, SomeUnicodeString): return immutablevalue(u"") return immutablevalue("") no_nul = self.no_nul and s_item.no_nul return self.basestringclass(no_nul=no_nul)
def issubtype(self, s_cls): if hasattr(self, 'is_type_of'): vars = self.is_type_of annotator = getbookkeeper().annotator return builtin.builtin_isinstance(annotator.binding(vars[0]), s_cls, vars) if self.is_constant() and s_cls.is_constant(): return immutablevalue(issubclass(self.const, s_cls.const)) return s_Bool
def issubtype(self, s_cls): if hasattr(self, 'is_type_of'): vars = self.is_type_of annotator = getbookkeeper().annotator return builtin.builtin_isinstance(annotator.binding(vars[0]), s_cls, vars) if self.is_constant() and s_cls.is_constant(): return immutablevalue(issubclass(self.const, s_cls.const)) return s_Bool
def getattr(self, s_attr): # get a SomeBuiltin if the SomeObject has # a corresponding method to handle it if not s_attr.is_constant() or not isinstance(s_attr.const, str): raise AnnotatorError("getattr(%r, %r) has non-constant argument" % (self, s_attr)) attr = s_attr.const s_method = self.find_method(attr) if s_method is not None: return s_method # if the SomeObject is itself a constant, allow reading its attrs if self.is_immutable_constant() and hasattr(self.const, attr): return immutablevalue(getattr(self.const, attr)) raise AnnotatorError("Cannot find attribute %r on %r" % (attr, self))
def getattr(self, s_attr): # get a SomeBuiltin if the SomeObject has # a corresponding method to handle it if not s_attr.is_constant() or not isinstance(s_attr.const, str): raise AnnotatorError("getattr(%r, %r) has non-constant argument" % (self, s_attr)) attr = s_attr.const s_method = self.find_method(attr) if s_method is not None: return s_method # if the SomeObject is itself a constant, allow reading its attrs if self.is_immutable_constant() and hasattr(self.const, attr): return immutablevalue(getattr(self.const, attr)) raise AnnotatorError("Cannot find attribute %r on %r" % (attr, self))
def builtin_range(*args): s_step = immutablevalue(1) if len(args) == 1: s_start = immutablevalue(0) s_stop = args[0] elif len(args) == 2: s_start, s_stop = args elif len(args) == 3: s_start, s_stop = args[:2] s_step = args[2] else: raise AnnotatorError("range() takes 1 to 3 arguments") empty = False # so far if not s_step.is_constant(): step = 0 # this case signals a variable step else: step = s_step.const if step == 0: raise AnnotatorError("range() with step zero") if s_start.is_constant() and s_stop.is_constant(): try: if len(xrange(s_start.const, s_stop.const, step)) == 0: empty = True except TypeError: # if one of the .const is a Symbolic pass if empty: s_item = s_ImpossibleValue else: nonneg = False # so far if step > 0 or s_step.nonneg: nonneg = s_start.nonneg elif step < 0: nonneg = s_stop.nonneg or (s_stop.is_constant() and s_stop.const >= -1) s_item = SomeInteger(nonneg=nonneg) return getbookkeeper().newlist(s_item, range_step=step)
def constpropagate(func, args_s, s_result): """Returns s_result unless all args are constants, in which case the func() is called and a constant result is returned (it must be contained in s_result). """ args = [] for s in args_s: if not s.is_immutable_constant(): return s_result args.append(s.const) try: realresult = func(*args) except (ValueError, OverflowError): # no possible answer for this precise input. Be conservative # and keep the computation non-constant. Example: # unichr(constant-that-doesn't-fit-16-bits) on platforms where # the underlying Python has sys.maxunicode == 0xffff. return s_result s_realresult = immutablevalue(realresult) if not s_result.contains(s_realresult): raise AnnotatorError("%s%r returned %r, which is not contained in %s" % (func, args, realresult, s_result)) return s_realresult
def constpropagate(func, args_s, s_result): """Returns s_result unless all args are constants, in which case the func() is called and a constant result is returned (it must be contained in s_result). """ args = [] for s in args_s: if not s.is_immutable_constant(): return s_result args.append(s.const) try: realresult = func(*args) except (ValueError, OverflowError): # no possible answer for this precise input. Be conservative # and keep the computation non-constant. Example: # unichr(constant-that-doesn't-fit-16-bits) on platforms where # the underlying Python has sys.maxunicode == 0xffff. return s_result s_realresult = immutablevalue(realresult) if not s_result.contains(s_realresult): raise Exception("%s%r returned %r, which is not contained in %s" % (func, args, realresult, s_result)) return s_realresult
def len(self): position = getbookkeeper().position_key if self._is_empty(position): return immutablevalue(0) return SomeObject.len(self)
def len(self): return immutablevalue(1)
def method_endswith(self, frag): if self.is_constant() and frag.is_constant(): return immutablevalue(self.const.endswith(frag.const)) return s_Bool
def len(self): if self._is_empty(): return immutablevalue(0) return SomeObject.len(self)
def default_annotate(annotator, obj1, obj2): s_1, s_2 = annotator.annotation(obj1), annotator.annotation(obj2) if s_1.is_immutable_constant() and s_2.is_immutable_constant(): return immutablevalue(cmp_op.pyfunc(s_1.const, s_2.const)) else: return s_Bool
def len(self): s_item = self.listdef.read_item() if isinstance(s_item, SomeImpossibleValue): return immutablevalue(0) return SomeObject.len(self)
def robjmodel_keepalive_until_here(*args_s): return immutablevalue(None)
def len(self): if self._is_empty(): return immutablevalue(0) return SomeObject.len(self)
def robjmodel_keepalive_until_here(*args_s): return immutablevalue(None)
def len(self): position = getbookkeeper().position_key if self._is_empty(position): return immutablevalue(0) return SomeObject.len(self)
def len(self): position = getbookkeeper().position_key s_item = self.listdef.read_item(position) if isinstance(s_item, SomeImpossibleValue): return immutablevalue(0) return SomeObject.len(self)
def len(self): return immutablevalue(len(self.items))
def cmp((obj1, obj2)): if obj1.is_immutable_constant() and obj2.is_immutable_constant(): return immutablevalue(cmp(obj1.const, obj2.const)) else: return SomeInteger()
def len(self): position = getbookkeeper().position_key s_item = self.listdef.read_item(position) if isinstance(s_item, SomeImpossibleValue): return immutablevalue(0) return SomeObject.len(self)
def cmp((obj1, obj2)): if obj1.is_immutable_constant() and obj2.is_immutable_constant(): return immutablevalue(cmp(obj1.const, obj2.const)) else: return SomeInteger()
def method_endswith(self, frag): if self.is_constant() and frag.is_constant(): return immutablevalue(self.const.endswith(frag.const)) return s_Bool
def len(self): return immutablevalue(len(self.items))
def len(self): return immutablevalue(1)
def len(self): s_item = self.listdef.read_item() if isinstance(s_item, SomeImpossibleValue): return immutablevalue(0) return SomeObject.len(self)
def default_annotate(annotator, obj1, obj2): s_1, s_2 = annotator.annotation(obj1), annotator.annotation(obj2) if s_1.is_immutable_constant() and s_2.is_immutable_constant(): return immutablevalue(cmp_op.pyfunc(s_1.const, s_2.const)) else: return s_Bool
def ge((obj1, obj2)): if obj1.is_immutable_constant() and obj2.is_immutable_constant(): return immutablevalue(obj1.const >= obj2.const) else: return s_Bool