本文整理汇总了Python中lib2to3.pygram.python_grammar_no_print_statement方法的典型用法代码示例。如果您正苦于以下问题:Python pygram.python_grammar_no_print_statement方法的具体用法?Python pygram.python_grammar_no_print_statement怎么用?Python pygram.python_grammar_no_print_statement使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类lib2to3.pygram
的用法示例。
在下文中一共展示了pygram.python_grammar_no_print_statement方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: lib2to3_parse
# 需要导入模块: from lib2to3 import pygram [as 别名]
# 或者: from lib2to3.pygram import python_grammar_no_print_statement [as 别名]
def lib2to3_parse(src_txt):
"""Given a string with source, return the lib2to3 Node."""
grammar = pygram.python_grammar_no_print_statement
drv = driver.Driver(grammar, pytree.convert)
if src_txt[-1] != "\n":
nl = "\r\n" if "\r\n" in src_txt[:1024] else "\n"
src_txt += nl
try:
result = drv.parse_string(src_txt, True)
except ParseError as pe:
lineno, column = pe.context[1]
lines = src_txt.splitlines()
try:
faulty_line = lines[lineno - 1]
except IndexError:
faulty_line = "<line number missing in source>"
raise ValueError(f"Cannot parse: {lineno}:{column}: {faulty_line}") from None
if isinstance(result, Leaf):
result = Node(syms.file_input, [result])
return result
示例2: test_print_function_option
# 需要导入模块: from lib2to3 import pygram [as 别名]
# 或者: from lib2to3.pygram import python_grammar_no_print_statement [as 别名]
def test_print_function_option(self):
rt = self.rt({"print_function" : True})
self.assertIs(rt.grammar, pygram.python_grammar_no_print_statement)
self.assertIs(rt.driver.grammar,
pygram.python_grammar_no_print_statement)
示例3: test_idempotency_print_as_function
# 需要导入模块: from lib2to3 import pygram [as 别名]
# 或者: from lib2to3.pygram import python_grammar_no_print_statement [as 别名]
def test_idempotency_print_as_function(self):
self.refactor.driver.grammar = pygram.python_grammar_no_print_statement
s = """print(1, 1+1, 1+1+1)"""
self.unchanged(s)
s = """print()"""
self.unchanged(s)
s = """print('')"""
self.unchanged(s)
示例4: test_print_function_option
# 需要导入模块: from lib2to3 import pygram [as 别名]
# 或者: from lib2to3.pygram import python_grammar_no_print_statement [as 别名]
def test_print_function_option(self):
rt = self.rt({"print_function" : True})
self.assertTrue(rt.grammar is pygram.python_grammar_no_print_statement)
self.assertTrue(rt.driver.grammar is
pygram.python_grammar_no_print_statement)
示例5: LastLeafNode
# 需要导入模块: from lib2to3 import pygram [as 别名]
# 或者: from lib2to3.pygram import python_grammar_no_print_statement [as 别名]
def LastLeafNode(node):
if isinstance(node, pytree.Leaf):
return node
return LastLeafNode(node.children[-1])
# lib2to3 thoughtfully provides pygram.python_grammar_no_print_statement for
# parsing Python 3 code that wouldn't parse otherwise (when 'print' is used in a
# context where a keyword is disallowed).
# It forgets to do the same for 'exec' though. Luckily, Python is amenable to
# monkey-patching.