def range(self, parameters, entries_stack=None): if entries_stack is None: entries_stack = set() if self.entry in entries_stack: # We can't keep recursing... return Range() entries_stack.add(self.entry) if isinstance(self.entry, fld.Field): length_range = expression_range(self.entry.length, self.entry, parameters, entries_stack) # If our length is of a variable range, it can be very large. # Attempting to take a power of a large number takes a very long # time, is is quite meaningless; limit it to 64 bits. max_length = min(length_range.max, 64) result = Range(0, pow(2, max_length) - 1) elif isinstance(self.entry, seq.Sequence): result = expression_range(self.entry.value, self.entry, parameters, entries_stack) elif isinstance(self.entry, chc.Choice): ranges = [EntryValueType(child.entry).range(parameters, entries_stack) for child in self.entry.children] result = reduce(Range.union, ranges) else: raise NotImplementedError("Don't know how to query the value range of entry '%s'!'" % self.entry) entries_stack.remove(self.entry) for constraint in self.entry.constraints: result = result.intersect(constraint.range()) return result
def test_intersect_with_none(self): a = Range(None, None) b = Range(0, 10) self.assertEqual(Range(0, 10), a.intersect(b))