我们从Python开源项目中,提取了以下30个代码示例,用于说明如何使用parser.ParserError()。
def testChunk(t, fileName): global _numFailed print '----', fileName, try: st = parser.suite(t) tup = parser.st2tuple(st) # this discards the first ST; a huge memory savings when running # against a large source file like Tkinter.py. st = None new = parser.tuple2st(tup) except parser.ParserError, err: print print 'parser module raised exception on input file', fileName + ':' traceback.print_exc() _numFailed = _numFailed + 1 else: if tup != parser.st2tuple(new): print print 'parser module failed on input file', fileName _numFailed = _numFailed + 1 else: print 'o.k.'
def determine_all_rates(upload, download): tcp_all_rate = False # serves as flag too udp_all_rate = False # serves as flag too for groups in (upload, download): if not groups: continue for group in groups: # parsed = parse_branch(group) parsed = BranchParser(group, dontcare=True).as_dict() # in this case we don't care for direction if parsed['range'] == 'all' and parsed['protocol'] == 'tcp': if tcp_all_rate: raise ParserError("More than one 'all' range detected for the same protocol (tcp).") tcp_all_rate = parsed['rate'] elif parsed['range'] == 'all' and parsed['protocol'] == 'udp': if udp_all_rate: raise ParserError("More than one 'all' range detected for the same protocol (udp).") udp_all_rate = parsed['rate'] return tcp_all_rate, udp_all_rate
def roundtrip(self, f, s): st1 = f(s) t = st1.totuple() try: st2 = parser.sequence2st(t) except parser.ParserError as why: self.fail("could not roundtrip %r: %s" % (s, why)) self.assertEqual(t, st2.totuple(), "could not re-generate syntax tree")
def check_bad_tree(self, tree, label): try: parser.sequence2st(tree) except parser.ParserError: pass else: self.fail("did not detect invalid tree for %r" % label)
def roundtrip(self, f, s): st1 = f(s) t = st1.totuple() try: st2 = parser.sequence2st(t) except parser.ParserError, why: self.fail("could not roundtrip %r: %s" % (s, why)) self.assertEqual(t, st2.totuple(), "could not re-generate syntax tree")
def test_dict_comprehensions(self): self.check_expr('{x:x for x in seq}') self.check_expr('{x**2:x[3] for x in seq if condition(x)}') self.check_expr('{x:x for x in seq1 for y in seq2 if condition(x, y)}') # # Second, we take *invalid* trees and make sure we get ParserError # rejections for them. #
def pyltc_entry_point(argv=None, target_factory=None): """ Calls the parseargs stuff to make sure all common arguments are handled properly, then executes the default plugin entry point. :param argv: list - the command line arguments as provided by `sys.argv[1:]` :return: None """ TrafficControl.init() try: simnet.plugin_main(argv, target_factory) except ParserError as err: print("ltc.py: error:", err, file=sys.stderr) return 2
def test_extended_unpacking(self): self.check_suite("*a = y") self.check_suite("x, *b, = m") self.check_suite("[*a, *b] = y") self.check_suite("for [*x, b] in x: pass") # # Second, we take *invalid* trees and make sure we get ParserError # rejections for them. #
def test_position(self): # An absolutely minimal test of position information. Better # tests would be a big project. code = "def f(x):\n return x + 1" st1 = parser.suite(code) st2 = st1.totuple(line_info=1, col_info=1) def walk(tree): node_type = tree[0] next = tree[1] if isinstance(next, tuple): for elt in tree[1:]: for x in walk(elt): yield x else: yield tree terminals = list(walk(st2)) self.assertEqual([ (1, 'def', 1, 0), (1, 'f', 1, 4), (7, '(', 1, 5), (1, 'x', 1, 6), (8, ')', 1, 7), (11, ':', 1, 8), (4, '', 1, 9), (5, '', 2, -1), (1, 'return', 2, 4), (1, 'x', 2, 11), (14, '+', 2, 13), (2, '1', 2, 15), (4, '', 2, 16), (6, '', 2, -1), (4, '', 2, -1), (0, '', 2, -1)], terminals) # # Second, we take *invalid* trees and make sure we get ParserError # rejections for them. #
def decode(self, text, location=None): self.doc = pyglet.text.document.FormattedDocument() self.length = 0 self.attributes = {} next_trailing_space = True trailing_newline = True for m in _pattern.finditer(text): group = m.lastgroup trailing_space = True if group == 'text': t = m.group('text') self.append(t) trailing_space = t.endswith(' ') trailing_newline = False elif group == 'nl_soft': if not next_trailing_space: self.append(' ') trailing_newline = False elif group in ('nl_hard1', 'nl_hard2'): self.append('\n') trailing_newline = True elif group == 'nl_para': self.append(m.group('nl_para')[1:]) # ignore the first \n trailing_newline = True elif group == 'attr': try: ast = parser.expr(m.group('attr_val')) if self.safe(ast): val = eval(ast.compile()) else: val = None except (parser.ParserError, SyntaxError): val = None name = m.group('attr_name') if name[0] == '.': if trailing_newline: self.attributes[name[1:]] = val else: self.doc.set_paragraph_style(self.length, self.length, {name[1:]: val}) else: self.attributes[name] = val elif group == 'escape_dec': self.append(chr(int(m.group('escape_dec_val')))) elif group == 'escape_hex': self.append(chr(int(m.group('escape_hex_val'), 16))) elif group == 'escape_lbrace': self.append('{') elif group == 'escape_rbrace': self.append('}') next_trailing_space = trailing_space return self.doc