Exemplo n.º 1
0
 def parse(self, source_desc, scope, pxd, full_module_name):
     if not isinstance(source_desc, FileSourceDescriptor):
         raise RuntimeError("Only file sources for code supported")
     source_filename = source_desc.filename
     scope.cpp = self.cpp
     # Parse the given source file and return a parse tree.
     try:
         f = Utils.open_source_file(source_filename, "rU")
         try:
             import Parsing
             s = PyrexScanner(f,
                              source_desc,
                              source_encoding=f.encoding,
                              scope=scope,
                              context=self)
             tree = Parsing.p_module(s, pxd, full_module_name)
         finally:
             f.close()
     except UnicodeDecodeError, msg:
         #import traceback
         #traceback.print_exc()
         error((
             source_desc, 0, 0
         ), "Decoding error, missing or incorrect coding=<encoding-name> at top of source (%s)"
               % msg)
Exemplo n.º 2
0
def parse_from_strings(name,
                       code,
                       pxds={},
                       level=None,
                       initial_pos=None,
                       context=None,
                       allow_struct_enum_decorator=False):
    """
    Utility method to parse a (unicode) string of code. This is mostly
    used for internal Cython compiler purposes (creating code snippets
    that transforms should emit, as well as unit testing).

    code - a unicode string containing Cython (module-level) code
    name - a descriptive name for the code source (to use in error messages etc.)

    RETURNS

    The tree, i.e. a ModuleNode. The ModuleNode's scope attribute is
    set to the scope used when parsing.
    """
    if context is None:
        context = StringParseContext(name)
    # Since source files carry an encoding, it makes sense in this context
    # to use a unicode string so that code fragments don't have to bother
    # with encoding. This means that test code passed in should not have an
    # encoding header.
    assert isinstance(code, unicode), "unicode code snippets only please"
    encoding = "UTF-8"

    module_name = name
    if initial_pos is None:
        initial_pos = (name, 1, 0)
    code_source = StringSourceDescriptor(name, code)

    scope = context.find_module(module_name, pos=initial_pos, need_pxd=0)

    buf = StringIO(code)

    scanner = PyrexScanner(buf,
                           code_source,
                           source_encoding=encoding,
                           scope=scope,
                           context=context,
                           initial_pos=initial_pos)
    ctx = Parsing.Ctx(allow_struct_enum_decorator=allow_struct_enum_decorator)

    if level is None:
        tree = Parsing.p_module(scanner, 0, module_name, ctx=ctx)
        tree.scope = scope
        tree.is_pxd = False
    else:
        tree = Parsing.p_code(scanner, level=level, ctx=ctx)

    tree.scope = scope
    return tree
Exemplo n.º 3
0
 def parse(self, source_filename, scope, pxd):
     # Parse the given source file and return a parse tree.
     f = open(source_filename, "rU")
     s = PyrexScanner(f, source_filename, scope=scope, context=self)
     try:
         tree = Parsing.p_module(s, pxd)
     finally:
         f.close()
     if Errors.num_errors > 0:
         raise CompileError
     return tree
Exemplo n.º 4
0
    def parse(self, source_desc, scope, pxd, full_module_name):
        if not isinstance(source_desc, FileSourceDescriptor):
            raise RuntimeError("Only file sources for code supported")
        source_filename = source_desc.filename
        scope.cpp = self.cpp
        # Parse the given source file and return a parse tree.
        num_errors = Errors.num_errors
        try:
            f = Utils.open_source_file(source_filename, "rU")
            try:
                import Parsing
                s = PyrexScanner(f,
                                 source_desc,
                                 source_encoding=f.encoding,
                                 scope=scope,
                                 context=self)
                tree = Parsing.p_module(s, pxd, full_module_name)
            finally:
                f.close()
        except UnicodeDecodeError, e:
            #import traceback
            #traceback.print_exc()
            line = 1
            column = 0
            msg = e.args[-1]
            position = e.args[2]
            encoding = e.args[0]

            f = open(source_filename, "rb")
            try:
                byte_data = f.read()
            finally:
                f.close()

            # FIXME: make this at least a little less inefficient
            for idx, c in enumerate(byte_data):
                if c in (ord('\n'), '\n'):
                    line += 1
                    column = 0
                if idx == position:
                    break

                column += 1

            error(
                (source_desc, line, column),
                "Decoding error, missing or incorrect coding=<encoding-name> "
                "at top of source (cannot decode with encoding %r: %s)" %
                (encoding, msg))