def __init__(self, tags): self.validate = True self.use_fallback = False self.parser_args = None self.db = HachoirParserList.getInstance() self.parsers = set(self.db) parsers = [] for tag in tags: if not self.parsers: break parsers += self._getByTag(tag) if self.fallback is None: self.fallback = len(parsers) == 1 if self.parsers: other = len(parsers) parsers += list(self.parsers) self.other = parsers[other] self.parsers = parsers
def testRandom(seed=0, tests=(1,8)): random.seed(seed) a = array('L') parser_list = HachoirParserList() n = max(tests) * max(parser.getParserTags()["min_size"] for parser in parser_list) k = 8 * a.itemsize for i in xrange((n - 1) // k + 1): a.append(random.getrandbits(k)) a = StringInputStream(a.tostring(), source="<random data>") ok = True for parser in parser_list: size = parser.getParserTags()["min_size"] for test in tests: a._size = a._current_size = size * test try: parser(a, validate=True) error("[%s] Parser didn't reject random data" % parser.__name__) except ValidateError: continue except HACHOIR_ERRORS, err: error(u"[%s] %s" % (parser.__name__, err)) ok = False