示例#1
0
def parse(source, filename='<string>'):
    # NOTE: the raw string should be given to `compile` function
    if isinstance(source, unicode):
        source = fscommands.unicode_to_file_data(source)
    if '\r' in source:
        source = source.replace('\r\n', '\n').replace('\r', '\n')
    if not source.endswith('\n'):
        source += '\n'
    try:
        return compile(source, filename, 'exec', _ast.PyCF_ONLY_AST)
    except (TypeError, ValueError), e:
        error = SyntaxError()
        error.lineno = 1
        error.filename = filename
        error.msg = str(e)
        raise error
示例#2
0
 def _init_source(self, pycore, source_code, resource):
     filename = 'string'
     if resource:
         filename = resource.path
     try:
         if source_code is None:
             source_bytes = resource.read_bytes()
             source_code = fscommands.file_data_to_unicode(source_bytes)
         else:
             if isinstance(source_code, unicode):
                 source_bytes = fscommands.unicode_to_file_data(source_code)
             else:
                 source_bytes = source_code
         ast_node = ast.parse(source_bytes, filename=filename)
     except SyntaxError, e:
         raise exceptions.ModuleSyntaxError(filename, e.lineno, e.msg)
示例#3
0
def parse(source, filename="<string>"):
    # NOTE: the raw string should be given to `compile` function
    if isinstance(source, unicode):
        source = fscommands.unicode_to_file_data(source)
    if b"\r" in source:
        source = source.replace(b"\r\n", b"\n").replace(b"\r", b"\n")
    if not source.endswith(b"\n"):
        source += b"\n"
    try:
        return ast.parse(source, filename="<unknown>")
    except (TypeError, ValueError) as e:
        error = SyntaxError()
        error.lineno = 1
        error.filename = filename
        error.msg = str(e)
        raise error
示例#4
0
 def _init_source(self, pycore, source_code, resource):
     filename = 'string'
     if resource:
         filename = resource.path
     try:
         if source_code is None:
             source_bytes = resource.read_bytes()
             source_code = fscommands.file_data_to_unicode(source_bytes)
         else:
             if isinstance(source_code, unicode):
                 source_bytes = fscommands.unicode_to_file_data(source_code)
             else:
                 source_bytes = source_code
         ast_node = ast.parse(source_bytes, filename=filename)
     except SyntaxError, e:
         raise exceptions.ModuleSyntaxError(filename, e.lineno, e.msg)
示例#5
0
def parse(source, filename="<string>"):
    # NOTE: the raw string should be given to `compile` function
    if isinstance(source, str):
        source = fscommands.unicode_to_file_data(source)
    source = source.decode()
    if "\r" in source:
        source = source.replace("\r\n", "\n").replace("\r", "\n")
    if not source.endswith("\n"):
        source += "\n"
    try:
        return compile(source.encode(), filename, "exec", _ast.PyCF_ONLY_AST)
    except (TypeError, ValueError) as e:
        error = SyntaxError()
        error.lineno = 1
        error.filename = filename
        error.msg = str(e)
        raise error