def method_reshape(self, s_tuple): if not isinstance(s_tuple, SomeTuple): raise AnnotatorError("reshape expects tuple arg") for s_item in s_tuple.items: if not isinstance(s_item, SomeInteger): raise AnnotatorError("bad shape arg") ndim = len(s_tuple.items) return SomeArray(self.typecode, ndim)
def getitem((s_array, s_index)): ndim = pair(s_array, s_index).get_leftover_dim() if len(s_index.items) > s_array.ndim: raise AnnotatorError("invalid index") if s_array.ndim == 0 and len(s_index.items): raise AnnotatorError( "indexing rank zero array with nonempty tuple") if ndim > 0: return SomeArray(s_array.typecode, ndim) return s_array.get_item_type()
def inplace_add((s_arr, s_other)): if isinstance(s_other, SomeArray): # This can only work if s_other is broadcastable to s_arr if s_other.ndim > s_arr.ndim: raise AnnotatorError("invalid return array shape") elif isinstance(s_other, SomeList): item2 = s_other.listdef.listitem.s_value if not isinstance(item2, SomeFloat): raise AnnotatorError("cannot operate with list of %s" % item2) elif not isinstance(s_other, SomeFloat): raise AnnotatorError("cannot operate with %s" % s_other) return s_arr
def compute_result_annotation(self, s_arg, s_dtype=None): if isinstance(s_arg, SomeTuple): s_tuple = s_arg for s_item in s_tuple.items: if not isinstance(s_item, SomeInteger): raise AnnotatorError("shape arg not understood") ndim = len(s_tuple.items) elif isinstance(s_arg, SomeInteger): ndim = 1 else: raise AnnotatorError("shape arg not understood: %s" % s_arg) typecode = 'd' if isinstance(s_dtype, SomeChar) and s_dtype.is_constant(): typecode = s_dtype.const s_dtype = None return SomeArray(typecode, ndim)
class __extend__(pairtype(SomePBC, SomeObject)): def getitem((pbc, o)): if not pbc.isNone(): raise AnnotatorError("getitem on %r" % pbc) return s_ImpossibleValue def setitem((pbc, o), s_value): if not pbc.isNone(): raise AnnotatorError("setitem on %r" % pbc)
def get_leftover_dim((s_array, s_index)): ndim = s_array.ndim for s_item in s_index.items: if isinstance(s_item, SomeInteger): ndim -= 1 elif isinstance(s_item, SomeSlice): pass else: raise AnnotatorError("cannot index with %s" % s_item) return ndim
class __extend__(pairtype(SomeArray, SomeTuple)): def get_leftover_dim((s_array, s_index)): ndim = s_array.ndim for s_item in s_index.items: if isinstance(s_item, SomeInteger): ndim -= 1 elif isinstance(s_item, SomeSlice): pass else: raise AnnotatorError("cannot index with %s" % s_item) return ndim def setitem((s_array, s_index), s_value): ndim = pair(s_array, s_index).get_leftover_dim() if len(s_index.items) > s_array.ndim: raise AnnotatorError("invalid index") if isinstance(s_value, SomeArray): if s_value.ndim > ndim: raise AnnotatorError("shape mismatch")
def unify_scalar_tp(item1, item2): typecode = None if float in (item1.knowntype, item2.knowntype): typecode = 'd' else: item_knowntype = rarithmetic.compute_restype(item1.knowntype, item2.knowntype) for typecode, s_item in SomeArray.typecode_to_item.items(): if s_item.knowntype == item_knowntype: break if typecode is None: raise AnnotatorError() return typecode
def compute_result_annotation(self, s_list, s_dtype=None): if isinstance(s_list, SomeList): # First guess type from input list listitem = s_list.listdef.listitem key = type(listitem.s_value), listitem.s_value.knowntype typecode = numpy_typedict.get(key, None) ndim = 1 elif isinstance(s_list, SomeArray): typecode = s_list.typecode ndim = s_list.ndim else: raise AnnotatorError("cannot build array from %s" % s_list) # now see if the dtype arg over-rides the typecode if isinstance(s_dtype, SomeChar) and s_dtype.is_constant(): typecode = s_dtype.const s_dtype = None if s_dtype is not None: raise AnnotatorError("dtype is not a valid type specification") if typecode is None or typecode not in valid_typecodes: raise AnnotatorError("List item type not supported") return SomeArray(typecode, ndim)
def add((s_arr, s_other)): item1 = s_arr.get_item_type() ndim = s_arr.ndim if isinstance(s_other, SomeArray): item2 = s_other.get_item_type() ndim = max(ndim, s_other.ndim) elif isinstance(s_other, SomeList): item2 = s_other.listdef.listitem.s_value elif isinstance(s_other, SomeFloat): item2 = s_other else: raise AnnotatorError("cannot operate with %s" % s_other) typecode = unify_scalar_tp(item1, item2) return SomeArray(typecode, ndim)
def ondegenerated(self, what, s_value, where=None, called_from_graph=None): if self.policy.allow_someobjects: return # is the function itself tagged with allow_someobjects? position_key = where or getattr(self.bookkeeper, 'position_key', None) if position_key is not None: graph, block, i = position_key try: if graph.func.allow_someobjects: return except AttributeError: pass msgstr = format_someobject_error(self, position_key, what, s_value, called_from_graph, self.bindings.get(what, "(none)")) raise AnnotatorError(msgstr)
def complete(self): """Process pending blocks until none is left.""" while True: while self.pendingblocks: block, graph = self.pendingblocks.popitem() if annmodel.DEBUG: self.flowin_block = block # we need to keep track of block self.processblock(graph, block) self.policy.no_more_blocks_to_annotate(self) if not self.pendingblocks: break # finished # make sure that the return variables of all graphs is annotated if self.added_blocks is not None: newgraphs = [self.annotated[block] for block in self.added_blocks] newgraphs = dict.fromkeys(newgraphs) got_blocked_blocks = False in newgraphs else: newgraphs = self.translator.graphs #all of them got_blocked_blocks = False in self.annotated.values() if got_blocked_blocks: for graph in self.blocked_graphs.values(): self.blocked_graphs[graph] = True blocked_blocks = [ block for block, done in self.annotated.items() if done is False ] assert len(blocked_blocks) == len(self.blocked_blocks) text = format_blocked_annotation_error(self, self.blocked_blocks) #raise SystemExit() raise AnnotatorError(text) for graph in newgraphs: v = graph.getreturnvar() if v not in self.bindings: self.setbinding(v, annmodel.s_ImpossibleValue) # policy-dependent computation self.bookkeeper.compute_at_fixpoint()
def setattr(pbc, s_attr, s_value): if not pbc.isNone(): raise AnnotatorError("setattr on %r" % pbc)
def add((o, pbc)): if not pbc.isNone(): raise AnnotatorError('add on %r' % pbc) return s_ImpossibleValue
def getitem((pbc, o)): if not pbc.isNone(): raise AnnotatorError("getitem on %r" % pbc) return s_ImpossibleValue
def inplace_add((s_flt, s_arr)): # This involves promoting the type of the lhs raise AnnotatorError()
def __init__(self, typecode, ndim=1): if not typecode in self.typecode_to_item: raise AnnotatorError("bad typecode: %r" % typecode) self.dtype = self.typecode = typecode self.ndim = ndim
def method_astype(self, s_dtype): if isinstance(s_dtype, SomeChar) and s_dtype.is_constant(): typecode = s_dtype.const return SomeArray(typecode, self.ndim) raise AnnotatorError()