Beispiel #1
0
def cythonize_one(pyx_file, c_file, options=None):
    from Cython.Compiler.Main import compile, default_options
    from Cython.Compiler.Errors import CompileError, PyrexError

    if options is None:
        options = CompilationOptions(default_options)
    options.output_file = c_file

    any_failures = 0
    try:
        result = compile([pyx_file], options)
        if result.num_errors > 0:
            any_failures = 1
    except (EnvironmentError, PyrexError) as e:
        sys.stderr.write(str(e) + '\n')
        any_failures = 1
    if any_failures:
        raise CompileError(None, pyx_file)
Beispiel #2
0
            any_failures = 1
    except (EnvironmentError, PyrexError), e:
        sys.stderr.write('%s\n' % e)
        any_failures = 1
        # XXX
        import traceback
        traceback.print_exc()
    except Exception:
        if raise_on_failure:
            raise
        import traceback
        traceback.print_exc()
        any_failures = 1
    if any_failures:
        if raise_on_failure:
            raise CompileError(None, pyx_file)
        elif os.path.exists(c_file):
            os.remove(c_file)
    elif fingerprint:
        f = open(c_file, 'rb')
        try:
            g = gzip_open(fingerprint_file, 'wb')
            try:
                shutil.copyfileobj(f, g)
            finally:
                g.close()
        finally:
            f.close()


def cythonize_one_helper(m):
Beispiel #3
0
    def handle_scope(self, node, scope):
        # For all buffers, insert extra variables in the scope.
        # The variables are also accessible from the buffer_info
        # on the buffer entry
        bufvars = [
            entry for name, entry in scope.entries.iteritems()
            if entry.type.is_buffer
        ]
        if len(bufvars) > 0:
            bufvars.sort(key=lambda entry: entry.name)
            self.buffers_exists = True

        memviewslicevars = [
            entry for name, entry in scope.entries.iteritems()
            if entry.type.is_memoryviewslice
        ]
        if len(memviewslicevars) > 0:
            self.buffers_exists = True

        for (name, entry) in scope.entries.iteritems():
            if name == 'memoryview' and isinstance(
                    entry.utility_code_definition, CythonUtilityCode):
                self.using_memoryview = True
                break

        if isinstance(node, ModuleNode) and len(bufvars) > 0:
            # for now...note that pos is wrong
            raise CompileError(node.pos,
                               "Buffer vars not allowed in module scope")
        for entry in bufvars:
            if entry.type.dtype.is_ptr:
                raise CompileError(
                    node.pos, "Buffers with pointer types not yet supported.")

            name = entry.name
            buftype = entry.type
            if buftype.ndim > Options.buffer_max_dims:
                raise CompileError(
                    node.pos,
                    "Buffer ndims exceeds Options.buffer_max_dims = %d" %
                    Options.buffer_max_dims)
            if buftype.ndim > self.max_ndim:
                self.max_ndim = buftype.ndim

            # Declare auxiliary vars
            def decvar(type, prefix):
                cname = scope.mangle(prefix, name)
                aux_var = scope.declare_var(name=None,
                                            cname=cname,
                                            type=type,
                                            pos=node.pos)
                if entry.is_arg:
                    aux_var.used = True  # otherwise, NameNode will mark whether it is used

                return aux_var

            auxvars = ((PyrexTypes.c_pyx_buffer_nd_type,
                        Naming.pybuffernd_prefix),
                       (PyrexTypes.c_pyx_buffer_type,
                        Naming.pybufferstruct_prefix))
            pybuffernd, rcbuffer = [
                decvar(type, prefix) for (type, prefix) in auxvars
            ]

            entry.buffer_aux = Symtab.BufferAux(pybuffernd, rcbuffer)

        scope.buffer_entries = bufvars
        self.scope = scope
Beispiel #4
0
 def assert_bool(name):
     x = options.get(name)
     if not isinstance(x, bool):
         raise CompileError(globalpos, ERR_BUF_BOOL % name)
Beispiel #5
0
def analyse_buffer_options(globalpos,
                           env,
                           posargs,
                           dictargs,
                           defaults=None,
                           need_complete=True):
    """
    Must be called during type analysis, as analyse is called
    on the dtype argument.

    posargs and dictargs should consist of a list and a dict
    of tuples (value, pos). Defaults should be a dict of values.

    Returns a dict containing all the options a buffer can have and
    its value (with the positions stripped).
    """
    if defaults is None:
        defaults = buffer_defaults

    posargs, dictargs = Interpreter.interpret_compiletime_options(
        posargs, dictargs, type_env=env, type_args=(0, 'dtype'))

    if len(posargs) > buffer_positional_options_count:
        raise CompileError(posargs[-1][1], ERR_BUF_TOO_MANY)

    options = {}
    for name, (value, pos) in dictargs.iteritems():
        if not name in buffer_options:
            raise CompileError(pos, ERR_BUF_OPTION_UNKNOWN % name)
        options[name] = value

    for name, (value, pos) in zip(buffer_options, posargs):
        if not name in buffer_options:
            raise CompileError(pos, ERR_BUF_OPTION_UNKNOWN % name)
        if name in options:
            raise CompileError(pos, ERR_BUF_DUP % name)
        options[name] = value

    # Check that they are all there and copy defaults
    for name in buffer_options:
        if not name in options:
            try:
                options[name] = defaults[name]
            except KeyError:
                if need_complete:
                    raise CompileError(globalpos, ERR_BUF_MISSING % name)

    dtype = options.get("dtype")
    if dtype and dtype.is_extension_type:
        raise CompileError(globalpos, ERR_BUF_DTYPE)

    ndim = options.get("ndim")
    if ndim and (not isinstance(ndim, int) or ndim < 0):
        raise CompileError(globalpos, ERR_BUF_NDIM)

    mode = options.get("mode")
    if mode and not (mode in ('full', 'strided', 'c', 'fortran')):
        raise CompileError(globalpos, ERR_BUF_MODE)

    def assert_bool(name):
        x = options.get(name)
        if not isinstance(x, bool):
            raise CompileError(globalpos, ERR_BUF_BOOL % name)

    assert_bool('negative_indices')
    assert_bool('cast')

    return options
Beispiel #6
0
    def handle_scope(self, node, scope):
        # For all buffers, insert extra variables in the scope.
        # The variables are also accessible from the buffer_info
        # on the buffer entry
        bufvars = [
            entry for name, entry in scope.entries.iteritems()
            if entry.type.is_buffer
        ]
        if len(bufvars) > 0:
            self.buffers_exists = True

        if isinstance(node, ModuleNode) and len(bufvars) > 0:
            # for now...note that pos is wrong
            raise CompileError(node.pos,
                               "Buffer vars not allowed in module scope")
        for entry in bufvars:
            if entry.type.dtype.is_ptr:
                raise CompileError(
                    node.pos, "Buffers with pointer types not yet supported.")

            name = entry.name
            buftype = entry.type
            if buftype.ndim > self.max_ndim:
                self.max_ndim = buftype.ndim

            # Declare auxiliary vars
            cname = scope.mangle(Naming.bufstruct_prefix, name)
            bufinfo = scope.declare_var(name="$%s" % cname,
                                        cname=cname,
                                        type=PyrexTypes.c_py_buffer_type,
                                        pos=node.pos)
            if entry.is_arg:
                bufinfo.used = True  # otherwise, NameNode will mark whether it is used

            def var(prefix, idx, initval):
                cname = scope.mangle(prefix, "%d_%s" % (idx, name))
                result = scope.declare_var("$%s" % cname,
                                           PyrexTypes.c_py_ssize_t_type,
                                           node.pos,
                                           cname=cname,
                                           is_cdef=True)

                result.init = initval
                if entry.is_arg:
                    result.used = True
                return result

            stridevars = [
                var(Naming.bufstride_prefix, i, "0")
                for i in range(entry.type.ndim)
            ]
            shapevars = [
                var(Naming.bufshape_prefix, i, "0")
                for i in range(entry.type.ndim)
            ]
            mode = entry.type.mode
            if mode == 'full':
                suboffsetvars = [
                    var(Naming.bufsuboffset_prefix, i, "-1")
                    for i in range(entry.type.ndim)
                ]
            else:
                suboffsetvars = None

            entry.buffer_aux = Symtab.BufferAux(bufinfo, stridevars, shapevars,
                                                suboffsetvars)

        scope.buffer_entries = bufvars
        self.scope = scope