当前位置: 首页>>代码示例>>Python>>正文


Python parse.ParseError方法代码示例

本文整理汇总了Python中lib2to3.pgen2.parse.ParseError方法的典型用法代码示例。如果您正苦于以下问题:Python parse.ParseError方法的具体用法?Python parse.ParseError怎么用?Python parse.ParseError使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在lib2to3.pgen2.parse的用法示例。


在下文中一共展示了parse.ParseError方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: transform

# 需要导入模块: from lib2to3.pgen2 import parse [as 别名]
# 或者: from lib2to3.pgen2.parse import ParseError [as 别名]
def transform(self, source):
        # This implementation uses lib2to3,
        # you can override and use something else
        # if that's better for you

        # lib2to3 likes a newline at the end
        RTs.setup()
        source += '\n'
        try:
            tree = RTs._rt.refactor_string(source, self.pathname)
        except ParseError as e:
            if e.msg != 'bad input' or e.value != '=':
                raise
            tree = RTs._rtp.refactor_string(source, self.pathname)
        # could optimise a bit for only doing str(tree) if
        # getattr(tree, 'was_changed', False) returns True
        return str(tree)[:-1] # remove added newline 
开发者ID:remg427,项目名称:misp42splunk,代码行数:19,代码来源:__init__.py

示例2: detect_python2

# 需要导入模块: from lib2to3.pgen2 import parse [as 别名]
# 或者: from lib2to3.pgen2.parse import ParseError [as 别名]
def detect_python2(source, pathname):
    """
    Returns a bool indicating whether we think the code is Py2
    """
    RTs.setup_detect_python2()
    try:
        tree = RTs._rt_py2_detect.refactor_string(source, pathname)
    except ParseError as e:
        if e.msg != 'bad input' or e.value != '=':
            raise
        tree = RTs._rtp.refactor_string(source, pathname)

    if source != str(tree)[:-1]:   # remove added newline
        # The above fixers made changes, so we conclude it's Python 2 code
        logger.debug('Detected Python 2 code: {0}'.format(pathname))
        return True
    else:
        logger.debug('Detected Python 3 code: {0}'.format(pathname))
        return False 
开发者ID:alfa-addon,项目名称:addon,代码行数:21,代码来源:__init__.py

示例3: lib2to3_parse

# 需要导入模块: from lib2to3.pgen2 import parse [as 别名]
# 或者: from lib2to3.pgen2.parse import ParseError [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 
开发者ID:ambv,项目名称:retype,代码行数:24,代码来源:__init__.py

示例4: detect_python2

# 需要导入模块: from lib2to3.pgen2 import parse [as 别名]
# 或者: from lib2to3.pgen2.parse import ParseError [as 别名]
def detect_python2(source, pathname):
    """
    Returns a bool indicating whether we think the code is Py2
    """
    RTs.setup_detect_python2()
    try:
        tree = RTs._rt_py2_detect.refactor_string(source, pathname)
    except ParseError as e:
        if e.msg != 'bad input' or e.value != '=':
            raise
        tree = RTs._rtp.refactor_string(source, pathname)

    if source != str(tree)[:-1]:   # remove added newline
        # The above fixers made changes, so we conclude it's Python 2 code
        logger.debug('Detected Python 2 code: {0}'.format(pathname))
        with open('/tmp/original_code.py', 'w') as f:
            f.write('### Original code (detected as py2): %s\n%s' %
                    (pathname, source))
        with open('/tmp/py2_detection_code.py', 'w') as f:
            f.write('### Code after running py3 detection (from %s)\n%s' %
                    (pathname, str(tree)[:-1]))
        return True
    else:
        logger.debug('Detected Python 3 code: {0}'.format(pathname))
        with open('/tmp/original_code.py', 'w') as f:
            f.write('### Original code (detected as py3): %s\n%s' %
                    (pathname, source))
        try:
            os.remove('/tmp/futurize_code.py')
        except OSError:
            pass
        return False 
开发者ID:remg427,项目名称:misp42splunk,代码行数:34,代码来源:__init__.py

示例5: detect_python2

# 需要导入模块: from lib2to3.pgen2 import parse [as 别名]
# 或者: from lib2to3.pgen2.parse import ParseError [as 别名]
def detect_python2(source, pathname):
    """
    Returns a bool indicating whether we think the code is Py2
    """
    RTs.setup_detect_python2()
    try:
        tree = RTs._rt_py2_detect.refactor_string(source, pathname)
    except ParseError as e:
        if e.msg != 'bad input' or e.value != '=':
            raise
        tree = RTs._rtp.refactor_string(source, pathname)

    if source != str(tree)[:-1]:   # remove added newline
        # The above fixers made changes, so we conclude it's Python 2 code
        logger.debug('Detected Python 2 code: {0}'.format(pathname))
        with open('/tmp/original_code.py', 'w') as f:
            f.write('### Original code (detected as py2): %s\n%s' % 
                    (pathname, source))
        with open('/tmp/py2_detection_code.py', 'w') as f:
            f.write('### Code after running py3 detection (from %s)\n%s' % 
                    (pathname, str(tree)[:-1]))
        return True
    else:
        logger.debug('Detected Python 3 code: {0}'.format(pathname))
        with open('/tmp/original_code.py', 'w') as f:
            f.write('### Original code (detected as py3): %s\n%s' % 
                    (pathname, source))
        try:
            os.remove('/tmp/futurize_code.py')
        except OSError:
            pass
        return False 
开发者ID:QData,项目名称:deepWordBug,代码行数:34,代码来源:__init__.py

示例6: convert_with_2to3

# 需要导入模块: from lib2to3.pgen2 import parse [as 别名]
# 或者: from lib2to3.pgen2.parse import ParseError [as 别名]
def convert_with_2to3(filepath: str) -> str:
    from lib2to3.refactor import RefactoringTool, get_fixers_from_package
    from lib2to3.pgen2.parse import ParseError
    fixers = get_fixers_from_package('lib2to3.fixes')
    refactoring_tool = RefactoringTool(fixers)
    source = refactoring_tool._read_python_source(filepath)[0]
    try:
        tree = refactoring_tool.refactor_string(source, 'conf.py')
    except ParseError as err:
        # do not propagate lib2to3 exceptions
        lineno, offset = err.context[1]
        # try to match ParseError details with SyntaxError details
        raise SyntaxError(err.msg, (filepath, lineno, offset, err.value))
    return str(tree) 
开发者ID:sphinx-doc,项目名称:sphinx-intl,代码行数:16,代码来源:pycompat.py

示例7: ParseCodeToTree

# 需要导入模块: from lib2to3.pgen2 import parse [as 别名]
# 或者: from lib2to3.pgen2.parse import ParseError [as 别名]
def ParseCodeToTree(code):
  """Parse the given code to a lib2to3 pytree.

  Arguments:
    code: a string with the code to parse.

  Raises:
    SyntaxError if the code is invalid syntax.
    parse.ParseError if some other parsing failure.

  Returns:
    The root node of the parsed tree.
  """
  # This function is tiny, but the incantation for invoking the parser correctly
  # is sufficiently magical to be worth abstracting away.
  try:
    # Try to parse using a Python 3 grammar, which is more permissive (print and
    # exec are not keywords).
    parser_driver = driver.Driver(_GRAMMAR_FOR_PY3, convert=pytree.convert)
    tree = parser_driver.parse_string(code, debug=False)
  except parse.ParseError:
    # Now try to parse using a Python 2 grammar; If this fails, then
    # there's something else wrong with the code.
    try:
      parser_driver = driver.Driver(_GRAMMAR_FOR_PY2, convert=pytree.convert)
      tree = parser_driver.parse_string(code, debug=False)
    except parse.ParseError:
      # Raise a syntax error if the code is invalid python syntax.
      try:
        ast.parse(code)
      except SyntaxError as e:
        raise e
      else:
        raise
  return _WrapEndMarker(tree) 
开发者ID:google,项目名称:yapf,代码行数:37,代码来源:pytree_utils.py

示例8: literal_destringizer

# 需要导入模块: from lib2to3.pgen2 import parse [as 别名]
# 或者: from lib2to3.pgen2.parse import ParseError [as 别名]
def literal_destringizer(rep):
    """Convert a Python literal to the value it represents.

    Parameters
    ----------
    rep : string
        A Python literal.

    Returns
    -------
    value : object
        The value of the Python literal.

    Raises
    ------
    ValueError
        If ``rep`` is not a Python literal.
    """
    if isinstance(rep, (str, unicode)):
        orig_rep = rep
        try:
            # Python 3.2 does not recognize 'u' prefixes before string literals
            if rtp_fix_unicode:
                rep = str(rtp_fix_unicode.refactor_string(
                    rep + '\n', '<string>'))[:-1]
            return literal_eval(rep)
        except (ParseError, SyntaxError, TokenError):
            raise ValueError('%r is not a valid Python literal' % (orig_rep,))
    else:
        raise ValueError('%r is not a string' % (rep,)) 
开发者ID:SpaceGroupUCL,项目名称:qgisSpaceSyntaxToolkit,代码行数:32,代码来源:gml.py


注:本文中的lib2to3.pgen2.parse.ParseError方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。