示例#1
0
文件: setup.py 项目: chadlung/pyrox
def cythonize():
    if not has_cython:
        fail_build('In order to build this project, cython is required.')

    for module in read('tools/cython-modules'):
        for cython_target in module_files(module, 'pyx', 'pyd'):
            compile(cython_target)
示例#2
0
文件: setup.py 项目: mricon/pylognorm
def cythonize():
    if not has_cython:
        fail_build('In order to build this project, cython is required.')

    for module in read('./tools/cython-modules'):
        for cython_target in module_files(module, 'pyx', 'pyd'):
            compile(cython_target)
示例#3
0
 def run(self):
     try:
         from Cython.Compiler.Main import compile
         path = os.path.join(dirname, 'src', 'misaka.pyx')
         print('compiling %s' % path)
         compile(path)
     except ImportError:
         print('Cython is not installed. Please install Cython first.')
示例#4
0
文件: setup.py 项目: mdietz/misaka
 def run(self):
     try:
         from Cython.Compiler.Main import compile
         path = os.path.join(dirname, 'src', 'misaka.pyx')
         print('compiling %s' % path)
         compile(path)
     except ImportError:
         print('Cython is not installed. Please install Cython first.')
示例#5
0
 def run(self):
     try:
         from Cython.Compiler.Main import compile
         for f in ("hoedown.pyx", ):
             path = os.path.join(dirname, 'hoedownpy', f)
             print('compiling %s' % path)
             compile(path)
     except ImportError:
         print('Cython is not installed. Please install Cython first.')
示例#6
0
def run_cython(inc_dirs, extra_opts, out):
    from Cython.Compiler.Main import compile, CompilationOptions
    from Cython.Compiler.Options import directive_defaults
    directive_defaults["boundscheck"] = False
    directive_defaults["wraparound"] = False
    options = dict(include_path=inc_dirs,
                   compiler_directives=directive_defaults,
                   cplus=True)
    if extra_opts is not None:
        options.update(extra_opts)
    options = CompilationOptions(**options)
    compile(out, options=options)
示例#7
0
def run_cython(inc_dirs, extra_opts, out):
    from Cython.Compiler.Main import compile, CompilationOptions
    from Cython.Compiler.Options import directive_defaults
    directive_defaults["boundscheck"] = False
    directive_defaults["wraparound"] = False
    options = dict(include_path=inc_dirs,
                   compiler_directives=directive_defaults,
                   cplus=True)
    if extra_opts is not None:
        options.update(extra_opts)
    options = CompilationOptions(**options)
    compile(out, options=options)
示例#8
0
def build_stamp(pyxes, include_dirs=()):
    """ Cythonize files in `pyxes`, return pyx, C filenames, hashes

    Parameters
    ----------
    pyxes : sequence
        sequence of filenames of files on which to run Cython
    include_dirs : sequence
        Any extra include directories in which to find Cython files.

    Returns
    -------
    pyx_defs : dict
        dict has key, value pairs of <pyx_filename>, <pyx_info>, where
        <pyx_info> is a dict with key, value pairs of "pyx_hash", <pyx file
        SHA1 hash>; "c_filename", <c filemane>; "c_hash", <c file SHA1 hash>.
    """
    pyx_defs = {}
    from Cython.Compiler.Main import compile
    from Cython.Compiler.CmdLine import parse_command_line
    includes = sum([['--include-dir', d] for d in include_dirs], [])
    for source in pyxes:
        base, ext = splitext(source)
        pyx_hash = sha1((open(source, 'rt').read().encode())).hexdigest()
        c_filename = base + '.c'
        options, sources = parse_command_line(['-3'] + includes + [source])
        result = compile(sources, options)
        if result.num_errors > 0:
            raise RuntimeError('Cython failed to compile ' + source)
        c_hash = sha1(open(c_filename, 'rt').read().encode()).hexdigest()
        pyx_defs[source] = dict(pyx_hash=pyx_hash,
                                c_filename=c_filename,
                                c_hash=c_hash)
    return pyx_defs
示例#9
0
def cythonize(source, includes=(),
              output_h=os.curdir):
    name, ext = os.path.splitext(source)
    output_c = name + '.c'
    #
    from Cython.Compiler.Main import \
         CompilationOptions, default_options, \
         compile, \
         PyrexError
    #
    options = CompilationOptions(default_options)
    options.output_file = output_c
    options.include_path = includes
    #
    from Cython.Compiler import Options
    Options.generate_cleanup_code = 3
    #
    any_failures = 0
    try:
        result = compile(source, options)
        if result.num_errors > 0:
            any_failures = 1
    except (EnvironmentError, PyrexError), e:
        sys.stderr.write(str(e) + '\n')
        any_failures = 1
示例#10
0
def cython_step():
    if os.path.exists("pysdif/_pysdif.c") and not is_newer(
            "pysdif/_pysdif.pyx", "pysdif/_pysdif.c"):
        return
    from Cython.Compiler.Main import compile
    compilation_result = compile("pysdif/_pysdif.pyx")
    print("Compiled cython file: ", compilation_result.c_file)
示例#11
0
    def compile_cython_file(self, extension, pyx_source_file):
        c_source_file = replace_extension(pyx_source_file, ".c")

        if not self.pyx_compile:
            print("Cython compilation disabled. Using compiled file:",
                  c_source_file)
            return c_source_file

        try:
            from Cython.Compiler.Main import compile
        except ImportError:
            print("Cython is not installed. Using compiled file:",
                  pyx_source_file)
            return c_source_file

        if not self.should_compile(pyx_source_file, c_source_file):
            print("Generated Cython file is up-to-date.")
            return c_source_file

        print("Compiling Cython file:", pyx_source_file)
        result = compile(pyx_source_file, full_module_name=extension.name)

        if result.c_file:
            c_source_file = result.c_file
            # Py2 distutils can't handle unicode file paths
            if sys.version_info[0] < 3:
                filename_encoding = sys.getfilesystemencoding()
                if filename_encoding is None:
                    filename_encoding = sys.getdefaultencoding()
                c_source_file = c_source_file.encode(filename_encoding)
        else:
            print("Compilation failed:", pyx_source_file)
        return c_source_file
示例#12
0
def cythonize_one(pyx_file, c_file, fingerprint, quiet, options=None):
    from Cython.Compiler.Main import compile, default_options
    from Cython.Compiler.Errors import CompileError, PyrexError

    if fingerprint:
        if not os.path.exists(options.cache):
            try:
                os.mkdir(options.cache)
            except:
                if not os.path.exists(options.cache):
                    raise
        # Cython-generated c files are highly compressible.
        # (E.g. a compression ratio of about 10 for Sage).
        fingerprint_file = os.path.join(
            options.cache, fingerprint + '-' + os.path.basename(c_file) + '.gz')
        if os.path.exists(fingerprint_file):
            if not quiet:
                print("Found compiled %s in cache" % pyx_file)
            os.utime(fingerprint_file, None)
            open(c_file, 'wb').write(gzip.open(fingerprint_file).read())
            return
    if not quiet:
        print("Cythonizing %s" % pyx_file)
    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), e:
        sys.stderr.write('%s\n' % e)
        any_failures = 1
示例#13
0
文件: cythexts.py 项目: MarcCote/dipy
def build_stamp(pyxes, include_dirs=()):
    """ Cythonize files in `pyxes`, return pyx, C filenames, hashes

    Parameters
    ----------
    pyxes : sequence
        sequence of filenames of files on which to run Cython
    include_dirs : sequence
        Any extra include directories in which to find Cython files.

    Returns
    -------
    pyx_defs : dict
        dict has key, value pairs of <pyx_filename>, <pyx_info>, where
        <pyx_info> is a dict with key, value pairs of "pyx_hash", <pyx file SHA1
        hash>; "c_filename", <c filemane>; "c_hash", <c file SHA1 hash>.
    """
    pyx_defs = {}
    from Cython.Compiler.Main import compile
    from Cython.Compiler.CmdLine import parse_command_line
    includes = sum([['--include-dir', d] for d in include_dirs], [])
    for source in pyxes:
        base, ext = splitext(source)
        pyx_hash = sha1(open(source, 'rt').read()).hexdigest()
        c_filename = base + '.c'
        options, sources = parse_command_line(includes + [source])
        result = compile(sources, options)
        if result.num_errors > 0:
            raise RuntimeError('Cython failed to compile ' + source)
        c_hash = sha1(open(c_filename, 'rt').read()).hexdigest()
        pyx_defs[source] = dict(pyx_hash=pyx_hash,
                                c_filename=c_filename,
                                c_hash=c_hash)
    return pyx_defs
示例#14
0
    def compile_cython_file(self, extension, pyx_source_file):
        c_source_file = replace_extension(pyx_source_file, ".c")

        if not self.pyx_compile:
            print("Cython compilation disabled. Using compiled file:",
                c_source_file)
            return c_source_file

        try:
            from Cython.Compiler.Main import compile
        except ImportError:
            print("Cython is not installed. Using compiled file:",
                pyx_source_file)
            return c_source_file

        if not self.should_compile(pyx_source_file, c_source_file):
            print("Generated Cython file is up-to-date.")
            return c_source_file

        print("Compiling Cython file:", pyx_source_file)
        result = compile(pyx_source_file, full_module_name=extension.name)

        if result.c_file:
            c_source_file = result.c_file
            # Py2 distutils can't handle unicode file paths
            if sys.version_info[0] < 3:
                filename_encoding = sys.getfilesystemencoding()
                if filename_encoding is None:
                    filename_encoding = sys.getdefaultencoding()
                c_source_file = c_source_file.encode(filename_encoding)
        else:
            print("Compilation failed:", pyx_source_file)
        return c_source_file
示例#15
0
文件: setup.py 项目: alexlib/openptv
 def cython(self, pyx):
     from Cython.Compiler.CmdLine import parse_command_line
     from Cython.Compiler.Main import compile
     options, sources = parse_command_line(['-2', pyx])
     result = compile(sources, options)
     if result.num_errors > 0:
         print('Errors converting %s to C' % pyx, file=sys.stderr)
         raise Exception('Errors converting %s to C' % pyx)
     self.announce('Converted %s to C' % pyx)
示例#16
0
def cythonize(source,
              includes=(),
              destdir_c=None,
              destdir_h=None,
              wdir=None):
    from Cython.Compiler.Main import \
         CompilationOptions, default_options, \
         compile, \
         PyrexError
    from Cython.Compiler import Options
    cwd = os.getcwd()
    try:
        name, ext = os.path.splitext(source)
        outputs_c = [name+'.c']
        outputs_h = [name+'.h', name+'_api.h']
        # change working directory
        if wdir:
            os.chdir(wdir)
        # run Cython on source
        options = CompilationOptions(default_options)
        options.output_file = outputs_c[0]
        options.include_path = list(includes)
        Options.generate_cleanup_code = 3
        any_failures = 0
        try:
            result = compile(source, options)
            if result.num_errors > 0:
                any_failures = 1
        except (EnvironmentError, PyrexError):
            e = sys.exc_info()[1]
            sys.stderr.write(str(e) + '\n')
            any_failures = 1
        if any_failures:
            for output in outputs_c + outputs_h:
                try:
                    os.remove(output)
                except OSError:
                    pass
            return 1
        # move ouputs
        for destdir, outputs in (
            (destdir_c, outputs_c),
            (destdir_h, outputs_h)):
            if destdir is None: continue
            for output in outputs:
                dest = os.path.join(
                    destdir, os.path.basename(output))
                try:
                    os.remove(dest)
                except OSError:
                    pass
                os.rename(output, dest)
        #
        return 0
    #
    finally:
        os.chdir(cwd)
示例#17
0
def cythonize(source,
              includes=(),
              destdir_c=None,
              destdir_h=None,
              wdir=None):
    from Cython.Compiler.Main import \
         CompilationOptions, default_options, \
         compile, \
         PyrexError
    from Cython.Compiler import Options
    cwd = os.getcwd()
    try:
        name, ext = os.path.splitext(source)
        outputs_c = [name+'.c']
        outputs_h = [name+'.h', name+'_api.h']
        # change working directory
        if wdir:
            os.chdir(wdir)
        # run Cython on source
        options = CompilationOptions(default_options)
        options.output_file = outputs_c[0]
        options.include_path = list(includes)
        Options.generate_cleanup_code = 3
        any_failures = 0
        try:
            result = compile(source, options)
            if result.num_errors > 0:
                any_failures = 1
        except (EnvironmentError, PyrexError):
            e = sys.exc_info()[1]
            sys.stderr.write(str(e) + '\n')
            any_failures = 1
        if any_failures:
            for output in outputs_c + outputs_h:
                try:
                    os.remove(output)
                except OSError:
                    pass
            return 1
        # move ouputs
        for destdir, outputs in (
            (destdir_c, outputs_c),
            (destdir_h, outputs_h)):
            if destdir is None: continue
            for output in outputs:
                dest = os.path.join(
                    destdir, os.path.basename(output))
                try:
                    os.remove(dest)
                except OSError:
                    pass
                os.rename(output, dest)
        #
        return 0
    #
    finally:
        os.chdir(cwd)
示例#18
0
文件: setup.py 项目: alexlib/openptv
 def cython(self, pyx):
     from Cython.Compiler.CmdLine import parse_command_line
     from Cython.Compiler.Main import compile
     options, sources = parse_command_line(['-2', pyx])
     result = compile(sources, options)
     if result.num_errors > 0:
         print('Errors converting %s to C' % pyx, file=sys.stderr)
         raise Exception('Errors converting %s to C' % pyx)
     self.announce('Converted %s to C' % pyx)
示例#19
0
    def build_pyx(self,
                  pyx_source_path,
                  module_name=None,
                  module_dir=None,
                  pyx_kwargs=None,
                  **distutils_kwargs):
        if pyx_kwargs is None:
            pyx_kwargs = {}
        build_cache_root = self.build_cache_root
        if not build_cache_root.isdir():
            build_cache_root.makedirs_p()

        pyx_source_path = path(pyx_source_path).expand()
        if module_name is None:
            module_name = pyx_source_path.namebase
        build_dir = path(
            tempfile.mkdtemp(prefix='temp_%s__' % module_name,
                             dir=build_cache_root))
        try:
            source_file = build_dir.joinpath(module_name + '.pyx')
            pyx_source_path.copy(source_file)

            # If there is a Cython `pxd` header, copy it to the build
            # directory.
            header_file = pyx_source_path.parent.joinpath(
                pyx_source_path.namebase + '.pxd')
            if header_file.isfile():
                header_file.copy(
                    source_file.parent.joinpath(module_name + '.pxd'))
            else:
                print '"%s" is not a file.' % header_file

            compile_result = compile(source_file, default_options,
                                     **pyx_kwargs)
            if module_dir is None:
                module_dir = build_cache_root.joinpath(module_name)
            else:
                module_dir = path(module_dir).expand()
            module_dir.makedirs_p()
            success = self.builder(compile_result.c_file,
                                   build_dir=module_dir,
                                   **distutils_kwargs)
            if not success:
                raise RuntimeError('Error building extension: %s' %
                                   compile_result.c_file)
            else:
                if module_dir not in sys.path:
                    sys.path.insert(0, module_dir)
        except:
            print build_dir
            raise
        else:
            build_dir.rmtree()
        return module_dir, module_name
示例#20
0
def run_cython(inc_dirs, extra_opts, out):
    from Cython.Compiler.Main import compile, CompilationOptions

    # Try to get directive_defaults (API differs from 0.25 on)
    try:
        from Cython.Compiler.Options import directive_defaults
    except ImportError:
        # Cython 0.25
        import Cython.Compiler.Options
        directive_defaults = Cython.Compiler.Options.get_directive_defaults()

    directive_defaults["boundscheck"] = False
    directive_defaults["wraparound"] = False
    options = dict(include_path=inc_dirs,
                   compiler_directives=directive_defaults,
                   cplus=True)
    if extra_opts is not None:
        options.update(extra_opts)
    options = CompilationOptions(**options)
    compile(out, options=options)
示例#21
0
文件: setup.py 项目: dmend/portal
def cythonize():
    if has_cython:
        cmdclass.update({
            'build_ext': build_ext
        })

    for stuple in SOURCES:
        if has_cython:
            build_list = fendswith(stuple[1], ['.pyx', '.pyd'])
            for build_target in build_list:
                compile(build_target)
            ext_modules.append(Extension(
                stuple[0],
                build_list,
                extra_compile_args=COMPILER_ARGS))
        else:
            build_list = fendswith(stuple[1], ['.c'])
            ext_modules.append(Extension(
                stuple[0],
                build_list))
示例#22
0
文件: Main.py 项目: hroest/autowrap
def run_cython(inc_dirs, extra_opts, out):
    from Cython.Compiler.Main import compile, CompilationOptions

    # Try to get directive_defaults (API differs from 0.25 on)
    try:
        from Cython.Compiler.Options import directive_defaults
    except ImportError:
        # Cython 0.25
        import Cython.Compiler.Options
        directive_defaults = Cython.Compiler.Options.get_directive_defaults()


    directive_defaults["boundscheck"] = False
    directive_defaults["wraparound"] = False
    options = dict(include_path=inc_dirs,
                   compiler_directives=directive_defaults,
                   cplus=True)
    if extra_opts is not None:
        options.update(extra_opts)
    options = CompilationOptions(**options)
    compile(out, options=options)
示例#23
0
def cythonize_one(pyx_file,
                  c_file,
                  fingerprint,
                  quiet,
                  options=None,
                  raise_on_failure=True):
    from Cython.Compiler.Main import compile, default_options
    from Cython.Compiler.Errors import CompileError, PyrexError

    if fingerprint:
        if not os.path.exists(options.cache):
            try:
                os.mkdir(options.cache)
            except:
                if not os.path.exists(options.cache):
                    raise
        # Cython-generated c files are highly compressible.
        # (E.g. a compression ratio of about 10 for Sage).
        fingerprint_file = join_path(
            options.cache,
            "%s-%s%s" % (os.path.basename(c_file), fingerprint, gzip_ext))
        if os.path.exists(fingerprint_file):
            if not quiet:
                print("Found compiled %s in cache" % pyx_file)
            os.utime(fingerprint_file, None)
            g = gzip_open(fingerprint_file, 'rb')
            try:
                f = open(c_file, 'wb')
                try:
                    shutil.copyfileobj(g, f)
                finally:
                    f.close()
            finally:
                g.close()
            return
    if not quiet:
        print("Cythonizing %s" % pyx_file)
    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), e:
        sys.stderr.write('%s\n' % e)
        any_failures = 1
        # XXX
        import traceback
        traceback.print_exc()
示例#24
0
def cythonize(source, output=None, includes=(), workdir=None):
    import cyautodoc
    from Cython.Compiler import Options
    from Cython.Compiler.Main import (
        CompilationOptions,
        default_options,
        compile,
        PyrexError,
    )
    cwd = os.getcwd()
    try:
        # compute output filenames
        if output is None:
            name, _ = os.path.splitext(source)
            output = name + '.c'
        else:
            name, _ = os.path.splitext(output)
        outputs_c = [output]
        outputs_h = [name + '.h', name + '_api.h']
        # run Cython on source
        options = CompilationOptions(default_options)
        lang_level = Options.directive_types.get('language_level', int)
        options.language_level = lang_level(3)
        options.output_file = output
        options.include_path = list(includes)
        options.working_path = workdir or ""
        Options.generate_cleanup_code = 3
        any_failures = 0
        try:
            if options.working_path:
                os.chdir(options.working_path)
            result = compile(source, 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:
            for out in outputs_c + outputs_h:
                try:
                    os.remove(out)
                except OSError:
                    pass
            return 1
        return 0
    #
    finally:
        os.chdir(cwd)
示例#25
0
    def build_pyx(self, pyx_source_path, module_name=None, module_dir=None,
                  pyx_kwargs=None, **distutils_kwargs):
        if pyx_kwargs is None:
            pyx_kwargs = {}
        build_cache_root = self.build_cache_root
        if not build_cache_root.isdir():
            build_cache_root.makedirs_p()

        pyx_source_path = path(pyx_source_path).expand()
        if module_name is None:
            module_name = pyx_source_path.namebase
        build_dir = path(tempfile.mkdtemp(prefix='temp_%s__' % module_name,
                                          dir=build_cache_root))
        try:
            source_file = build_dir.joinpath(module_name + '.pyx')
            pyx_source_path.copy(source_file)

            # If there is a Cython `pxd` header, copy it to the build
            # directory.
            header_file = pyx_source_path.parent.joinpath(
                pyx_source_path.namebase + '.pxd')
            if header_file.isfile():
                header_file.copy(source_file.parent.joinpath(module_name +
                                                             '.pxd'))
            else:
                print '"%s" is not a file.' % header_file

            compile_result = compile(source_file, default_options,
                                     **pyx_kwargs)
            if module_dir is None:
                module_dir = build_cache_root.joinpath(module_name)
            else:
                module_dir = path(module_dir).expand()
            module_dir.makedirs_p()
            success = self.builder(compile_result.c_file, build_dir=module_dir,
                                   **distutils_kwargs)
            if not success:
                raise RuntimeError('Error building extension: %s' %
                                   compile_result.c_file)
            else:
                if module_dir not in sys.path:
                    sys.path.insert(0, module_dir)
        except:
            print build_dir
            raise
        else:
            build_dir.rmtree()
        return module_dir, module_name
示例#26
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), e:
        sys.stderr.write(str(e) + '\n')
        any_failures = 1
示例#27
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), e:
        sys.stderr.write(str(e) + '\n')
        any_failures = 1
示例#28
0
文件: setup.py 项目: minrk/h5py
        def cythonize(api):

            outpath = localpath('api%d' % api)
            self.checkdir(outpath)

            pxi_str = \
"""# This file is automatically generated.  Do not edit.

DEF H5PY_VERSION = "%(VERSION)s"

DEF H5PY_API = %(API_MAX)d     # Highest API level (i.e. 18 or 16)
DEF H5PY_16API = %(API_16)d    # 1.6.X API available (always true, for now)
DEF H5PY_18API = %(API_18)d    # 1.8.X API available
"""
            pxi_str %= {
                "VERSION": VERSION,
                "API_MAX": api,
                "API_16": True,
                "API_18": api == 18
            }

            f = open(op.join(outpath, 'config.pxi'), 'w')
            f.write(pxi_str)
            f.close()

            self.debug_print("  Cython: %s" % Version.version)
            self.debug_print("  API level: %d" % api)

            for module in MODULES:

                pyx_path = localpath('h5py', module + '.pyx')
                c_path = localpath(outpath, module + '.c')

                if self.force or \
                not op.exists(c_path) or \
                os.stat(pyx_path).st_mtime > os.stat(c_path).st_mtime:

                    self.debug_print("Cythoning %s" % pyx_path)
                    result = compile(
                        pyx_path,
                        verbose=False,
                        compiler_directives={'profile': self.profile},
                        include_path=[outpath],
                        output_file=c_path)
                    if result.num_errors != 0:
                        fatal("Cython error; aborting.")
示例#29
0
def cythonize_one(pyx_file, c_file, fingerprint, quiet, options=None, raise_on_failure=True):
    from Cython.Compiler.Main import compile, default_options
    from Cython.Compiler.Errors import CompileError, PyrexError

    if fingerprint:
        if not os.path.exists(options.cache):
            try:
                os.mkdir(options.cache)
            except:
                if not os.path.exists(options.cache):
                    raise
        # Cython-generated c files are highly compressible.
        # (E.g. a compression ratio of about 10 for Sage).
        fingerprint_file = join_path(
            options.cache, "%s-%s%s" % (os.path.basename(c_file), fingerprint, gzip_ext))
        if os.path.exists(fingerprint_file):
            if not quiet:
                print("Found compiled %s in cache" % pyx_file)
            os.utime(fingerprint_file, None)
            g = gzip_open(fingerprint_file, 'rb')
            try:
                f = open(c_file, 'wb')
                try:
                    shutil.copyfileobj(g, f)
                finally:
                    f.close()
            finally:
                g.close()
            return
    if not quiet:
        print("Cythonizing %s" % pyx_file)
    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), e:
        sys.stderr.write('%s\n' % e)
        any_failures = 1
        # XXX
        import traceback
        traceback.print_exc()
示例#30
0
文件: setup.py 项目: HKCaesar/PySAR-1
def cythonize():
    try:
        from Cython.Compiler.Main import CompilationOptions, default_options, \
           compile, PyrexError
        from Cython.Compiler import Options
        import subprocess

        for code in cythoncodes:
            source = code[0] + '.pyx'
            options = CompilationOptions(default_options)
            options.output_file = code[0] + '.c'
            options.include_path = code[1]
            Options.generate_cleanup_code = 3
            any_failures = False
            try:
                result = compile(source, options)
                if result.num_errors > 0:
                    any_failures = True
                if not any_failures:
                    callist = [
                        cythcompiler, '-shared', '-fwrapv', '-Wall',
                        '-fno-strict-aliasing'
                    ]
                    for x in CFLAGS:
                        callist.append(x)
                    for x in code[1]:
                        callist.append('-L' + x)
                    callist.append('-o')
                    callist.append('_' + code[0] + '.so')
                    callist.append(code[0] + '.c')
                    subprocess.call(callist)
            except (EnvironmentError, PyrexError):
                e = sys.exc_info()[1]
                sys.stderr.write(str(e) + '\n')
                any_failures = True
            if any_failures:
                try:
                    os.remove(code[0] + '.c')
                except OSError:
                    pass
    except:
        raise ValueError
示例#31
0
文件: setup.py 项目: qsnake/h5py
        def cythonize(api):

            outpath = localpath('api%d' % api)
            self.checkdir(outpath)

            pxi_str = \
"""# This file is automatically generated.  Do not edit.

DEF H5PY_VERSION = "%(VERSION)s"

DEF H5PY_API = %(API_MAX)d     # Highest API level (i.e. 18 or 16)
DEF H5PY_16API = %(API_16)d    # 1.6.X API available (always true, for now)
DEF H5PY_18API = %(API_18)d    # 1.8.X API available
"""
            pxi_str %= {"VERSION": VERSION, "API_MAX": api,
                        "API_16": True, "API_18": api == 18}

            f = open(op.join(outpath, 'config.pxi'),'w')
            f.write(pxi_str)
            f.close()

            self.debug_print("  Cython: %s" % Version.version)
            self.debug_print("  API level: %d" % api)

            for module in MODULES:

                pyx_path = localpath('h5py', module+'.pyx')
                c_path = localpath(outpath, module+'.c')

                if self.force or \
                not op.exists(c_path) or \
                os.stat(pyx_path).st_mtime > os.stat(c_path).st_mtime:

                    self.debug_print("Cythoning %s" % pyx_path)
                    result = compile(pyx_path, verbose=False,
                                     compiler_directives = {'profile': self.profile},
                                     include_path=[outpath], output_file=c_path)
                    if result.num_errors != 0:
                        fatal("Cython error; aborting.")
示例#32
0
文件: setup.py 项目: EJFielding/PySAR
def cythonize():
   try:
      from Cython.Compiler.Main import CompilationOptions, default_options, \
         compile, PyrexError
      from Cython.Compiler import Options
      import subprocess 

      for code in cythoncodes:
         source = code[0] + '.pyx'
         options = CompilationOptions(default_options)
         options.output_file = code[0] + '.c'
         options.include_path = code[1]
         Options.generate_cleanup_code = 3
         any_failures = False
         try:
            result = compile(source, options)
            if result.num_errors > 0: 
               any_failures = True
            if not any_failures:
               callist = [cythcompiler,'-shared','-fwrapv','-Wall','-fno-strict-aliasing']
               for x in CFLAGS:
                  callist.append(x)
               for x in code[1]:
                  callist.append('-L' + x)
               callist.append('-o')
               callist.append('_' + code[0] + '.so')
               callist.append(code[0] + '.c')
               subprocess.call(callist)
         except (EnvironmentError, PyrexError):
            e = sys.exc_info()[1]
            sys.stderr.write(str(e) + '\n')
            any_failures = True
         if any_failures:
            try:  os.remove(code[0] + '.c')
            except OSError: pass
   except:
      raise ValueError
示例#33
0
def cythonize(source, includes=(), output_h=os.curdir):
    name, ext = os.path.splitext(source)
    output_c = name + '.c'
    #
    from Cython.Compiler.Main import \
         CompilationOptions, default_options, \
         compile, \
         PyrexError
    #
    options = CompilationOptions(default_options)
    options.output_file = output_c
    options.include_path = includes
    #
    from Cython.Compiler import Options
    Options.generate_cleanup_code = 3
    #
    any_failures = 0
    try:
        result = compile(source, options)
        if result.num_errors > 0:
            any_failures = 1
    except (EnvironmentError, PyrexError), e:
        sys.stderr.write(str(e) + '\n')
        any_failures = 1
 def run(self):
     from Cython.Compiler.Main import compile
     CommandUtils.write_definitions_pyx()
     compile('pynodegl.pyx', output_file='pynodegl.c')
示例#35
0
def godopy_cython():
    source, outfile_path = sys.argv[1:]
    pyinit_src_symbol = 'PyInit_' + os.path.basename(outfile_path[:-4])
    pyinit_dst_symbol = 'PyInit_' + strip_internal_path(outfile_path).replace(
        os.sep, '__')[:-4]

    tempfile = outfile_path.replace('.cpp', '.temp.cpp')
    tempheaderfile = outfile_path.replace('.cpp', '.temp.h')

    header_path = outfile_path.replace('.cpp', '.hpp')

    from Cython.Compiler import Options
    from Cython.Compiler.Main import compile, default_options, CompilationOptions

    directives = {'c_string_encoding': 'utf-8'}

    options = CompilationOptions(default_options,
                                 compiler_directives=directives)

    Options.fast_fail = True
    options.output_file = tempfile
    options.cplus = 1
    options.language_level = 3

    result = compile(source, options)
    if result.num_errors > 0:
        raise SystemExit('Cython compilation finished with errors')

    def clean_line(line):
        if pyinit_src_symbol in line:
            line = line.replace(pyinit_src_symbol, pyinit_dst_symbol)

        # Undefined by #define NO_IMPORT_ARRAY
        if '_import_array()' in line:
            line = line.replace('_import_array()', '0')

        # Fix variable declarations with GDCALLINGCONV, GDCALLINGCONV is valid only for functions
        if line.lstrip().startswith('GDCALLINGCONV_'):
            line = re.sub(r'^(\s+)(GDCALLINGCONV_VOID_PTR)(\s\w+;)$',
                          r'\1void *\3', line)
            line = re.sub(r'^(\s+)(GDCALLINGCONV_VOID)(\s\w+;)$', r'\1void\3',
                          line)
            line = re.sub(r'^(\s+)(GDCALLINGCONV_BOOL)(\s\w+;)$', r'\1bool\3',
                          line)

        # Remove "tp_print" deprecation warnings
        if line.strip(
        ) == '#if PY_VERSION_HEX >= 0x030800b4 && PY_VERSION_HEX < 0x03090000':
            line = line.replace(
                'PY_VERSION_HEX >= 0x030800b4 && PY_VERSION_HEX < 0x03090000',
                'PY_VERSION_HEX < 0x030800b1')

        # FIXME: undeclared identifier, looks like a bug in Cython
        if line.strip() == '__Pyx_CppExn2PyErr();':
            line = ''

        return line

    with open(outfile_path, 'w', encoding='utf-8') as outfile:
        with open(tempfile, 'r', encoding='utf-8') as fp:
            for line in fp:
                outfile.write(clean_line(line))

    os.unlink(tempfile)

    if os.path.exists(tempheaderfile):
        with open(header_path, 'w', encoding='utf-8') as outheaderfile:
            with open(tempheaderfile, 'r', encoding='utf-8') as fp:
                for line in fp:
                    outheaderfile.write(clean_line(line))

        os.unlink(tempheaderfile)
示例#36
0
        continue
    for filename in fnfilter(filenames, '*.pyx'):
        pyx_files.append(join(root, filename))

# check for cython
try:
    have_cython = True
    from Cython.Distutils import build_ext
except:
    have_cython = False

# create .c for every module
if 'sdist' in argv and have_cython:
    from Cython.Compiler.Main import compile
    print 'Generating C files...',
    compile(pyx_files)
    print 'Done !'

# add cython core extension modules if cython is available
if have_cython:
    cmdclass['build_ext'] = build_ext
else:
    pyx_files = ['%s.c' % x[:-4] for x in pyx_files]

if True:
    libraries = []
    include_dirs = []
    extra_link_args = []
    if platform == 'win32':
        libraries.append('opengl32')
    elif platform == 'darwin':
示例#37
0
文件: build.py 项目: 101man/sympy
    "sympy.polys.euclidtools",
    "sympy.polys.factortools",
    "sympy.polys.galoistools",
    "sympy.polys.monomialtools",
    "sympy.polys.orthopolys",
    "sympy.polys.specialpolys",
    "sympy.polys.sqfreetools",
]

extensions = []

for module in compiled_modules:
    source_file = os.path.join(source_root, *module.split('.')) + ".py"

    print("Compiling module %s ..." % module)
    result = compile(source_file)

    if result.c_file is None:
        raise RuntimeError("failed to compile %s" % module)

    extensions.append(
        Extension(module, sources=[str(result.c_file)],
            extra_compile_args=['-O2', '-Wall'],
        )
    )

setup(
    name        = "SymPy",
    packages    = [
        "sympy",
        "sympy.polys",
示例#38
0
#check for cython
try:
    have_cython = True
    from Cython.Distutils import build_ext
except:
    have_cython = False

# create .c for every module in c_ext
if 'sdist' in sys.argv and have_cython:
    from glob import glob
    from Cython.Compiler.Main import compile
    print 'Generating C files...',
    files = glob(
        os.path.join(os.path.dirname(__file__), 'pymt', 'c_ext', '*.pyx'))
    compile(files)
    print 'Done !'

#add cython core extension modules if cython is available
if have_cython:
    cmdclass['build_ext'] = build_ext
    libraries = []
    include_dirs = []
    extra_link_args = []
    if sys.platform == 'win32':
        libraries.append('opengl32')
    elif sys.platform == 'darwin':
        # On OSX, gl.h is not in GL/gl.h but OpenGL/gl.h. Cython has no
        # such thing as #ifdef, hence we just copy the file here.
        source = '/System/Library/Frameworks/OpenGL.framework/Versions/A/Headers/gl.h'
        incl = 'build/include/'
示例#39
0
文件: setup.py 项目: bhy/cython-haoyu
def compile_cython_modules(profile=False):
    source_root = os.path.abspath(os.path.dirname(__file__))
    compiled_modules = ["Cython.Plex.Scanners",
                        "Cython.Compiler.Scanning",
                        "Cython.Compiler.Parsing",
                        "Cython.Compiler.Visitor",
                        "Cython.Runtime.refnanny"]
    extensions = []

    if sys.version_info[0] >= 3:
        from Cython.Distutils import build_ext as build_ext_orig
        for module in compiled_modules:
            source_file = os.path.join(source_root, *module.split('.'))
            if os.path.exists(source_file + ".py"):
                pyx_source_file = source_file + ".py"
            else:
                pyx_source_file = source_file + ".pyx"
            extensions.append(
                Extension(module, sources = [pyx_source_file])
                )

        class build_ext(build_ext_orig):
            def build_extensions(self):
                # add path where 2to3 installed the transformed sources
                # and make sure Python (re-)imports them from there
                already_imported = [ module for module in sys.modules
                                     if module == 'Cython' or module.startswith('Cython.') ]
                for module in already_imported:
                    del sys.modules[module]
                sys.path.insert(0, os.path.join(source_root, self.build_lib))

                if profile:
                    from Cython.Compiler.Options import directive_defaults
                    directive_defaults['profile'] = True
                    print("Enabled profiling for the Cython binary modules")
                build_ext_orig.build_extensions(self)

        setup_args['ext_modules'] = extensions
        add_command_class("build_ext", build_ext)

    else: # Python 2.x
        from distutils.command.build_ext import build_ext as build_ext_orig
        try:
            class build_ext(build_ext_orig):
                def build_extension(self, ext, *args, **kargs):
                    try:
                        build_ext_orig.build_extension(self, ext, *args, **kargs)
                    except StandardError:
                        print("Compilation of '%s' failed" % ext.sources[0])
            from Cython.Compiler.Main import compile
            from Cython import Utils
            if profile:
                from Cython.Compiler.Options import directive_defaults
                directive_defaults['profile'] = True
                print("Enabled profiling for the Cython binary modules")
            source_root = os.path.dirname(__file__)
            for module in compiled_modules:
                source_file = os.path.join(source_root, *module.split('.'))
                if os.path.exists(source_file + ".py"):
                    pyx_source_file = source_file + ".py"
                else:
                    pyx_source_file = source_file + ".pyx"
                c_source_file = source_file + ".c"
                if not os.path.exists(c_source_file) or \
                   Utils.file_newer_than(pyx_source_file,
                                         Utils.modification_time(c_source_file)):
                    print("Compiling module %s ..." % module)
                    result = compile(pyx_source_file)
                    c_source_file = result.c_file
                if c_source_file:
                    # Py2 distutils can't handle unicode file paths
                    if isinstance(c_source_file, unicode):
                        filename_encoding = sys.getfilesystemencoding()
                        if filename_encoding is None:
                            filename_encoding = sys.getdefaultencoding()
                        c_source_file = c_source_file.encode(filename_encoding)
                    extensions.append(
                        Extension(module, sources = [c_source_file])
                        )
                else:
                    print("Compilation failed")
            if extensions:
                setup_args['ext_modules'] = extensions
                add_command_class("build_ext", build_ext)
        except Exception:
            print('''
ERROR: %s

Extension module compilation failed, looks like Cython cannot run
properly on this system.  To work around this, pass the option
"--no-cython-compile".  This will install a pure Python version of
Cython without compiling its own sources.
''' % sys.exc_info()[1])
            raise
示例#40
0
        except ImportError:
            raise Exception("PICO requires %s. " % name + skipmsg)

    checklib('numpy', 'NumPy', '1.6.1')
    checklib('scipy', 'SciPy', '0.10.1')

from numpy.distutils.core import setup
from numpy.distutils.misc_util import Configuration

# By default don't try to use Cython to compile the pyx file,
# just use the file distributed with pypico.
build_cython = ('--build_cython' in sys.argv)
if build_cython:
    sys.argv.remove('--build_cython')
    from Cython.Compiler.Main import compile
    compile('pypico/pico.pyx')

config = Configuration(
    'pypico',
    name='pypico',
    version='3.3.0',
    author='Marius Millea',
    author_email='*****@*****.**',
    packages=['pypico'],
    url='https://sites.google.com/a/ucdavis.edu/pico',
    license='LICENSE.txt',
    description=
    'Quickly compute the CMB powerspectra and matter transfer functions.',
    long_description=open('README.rst').read())

# Compile libpico.a
示例#41
0
def compile_cython_modules(profile=False,
                           compile_more=False,
                           cython_with_refnanny=False):
    source_root = os.path.abspath(os.path.dirname(__file__))
    compiled_modules = [
        "Cython.Plex.Scanners",
        "Cython.Plex.Actions",
        "Cython.Compiler.Lexicon",
        "Cython.Compiler.Scanning",
        "Cython.Compiler.Parsing",
        "Cython.Compiler.Visitor",
        "Cython.Compiler.FlowControl",
        "Cython.Compiler.Code",
        "Cython.Runtime.refnanny",
    ]
    if compile_more:
        compiled_modules.extend([
            "Cython.Compiler.ParseTreeTransforms",
            "Cython.Compiler.Nodes",
            "Cython.Compiler.ExprNodes",
            "Cython.Compiler.ModuleNode",
            "Cython.Compiler.Optimize",
        ])

    defines = []
    if cython_with_refnanny:
        defines.append(('CYTHON_REFNANNY', '1'))

    extensions = []
    if sys.version_info[0] >= 3:
        from Cython.Distutils import build_ext as build_ext_orig
        for module in compiled_modules:
            source_file = os.path.join(source_root, *module.split('.'))
            if os.path.exists(source_file + ".py"):
                pyx_source_file = source_file + ".py"
            else:
                pyx_source_file = source_file + ".pyx"
            dep_files = []
            if os.path.exists(source_file + '.pxd'):
                dep_files.append(source_file + '.pxd')
            if '.refnanny' in module:
                defines_for_module = []
            else:
                defines_for_module = defines
            extensions.append(
                Extension(module,
                          sources=[pyx_source_file],
                          define_macros=defines_for_module,
                          depends=dep_files))

        class build_ext(build_ext_orig):
            # we must keep the original modules alive to make sure
            # their code keeps working when we remove them from
            # sys.modules
            dead_modules = []

            def build_extensions(self):
                # add path where 2to3 installed the transformed sources
                # and make sure Python (re-)imports them from there
                already_imported = [
                    module for module in sys.modules
                    if module == 'Cython' or module.startswith('Cython.')
                ]
                keep_alive = self.dead_modules.append
                for module in already_imported:
                    keep_alive(sys.modules[module])
                    del sys.modules[module]
                sys.path.insert(0, os.path.join(source_root, self.build_lib))

                if profile:
                    from Cython.Compiler.Options import directive_defaults
                    directive_defaults['profile'] = True
                    print("Enabled profiling for the Cython binary modules")
                build_ext_orig.build_extensions(self)

        setup_args['ext_modules'] = extensions
        add_command_class("build_ext", build_ext)

    else:  # Python 2.x
        from distutils.command.build_ext import build_ext as build_ext_orig
        try:

            class build_ext(build_ext_orig):
                def build_extension(self, ext, *args, **kargs):
                    try:
                        build_ext_orig.build_extension(self, ext, *args,
                                                       **kargs)
                    except StandardError:
                        print("Compilation of '%s' failed" % ext.sources[0])

            from Cython.Compiler.Main import compile
            from Cython import Utils
            if profile:
                from Cython.Compiler.Options import directive_defaults
                directive_defaults['profile'] = True
                print("Enabled profiling for the Cython binary modules")
            source_root = os.path.dirname(__file__)
            for module in compiled_modules:
                source_file = os.path.join(source_root, *module.split('.'))
                if os.path.exists(source_file + ".py"):
                    pyx_source_file = source_file + ".py"
                else:
                    pyx_source_file = source_file + ".pyx"
                c_source_file = source_file + ".c"
                source_is_newer = False
                if not os.path.exists(c_source_file):
                    source_is_newer = True
                else:
                    c_last_modified = Utils.modification_time(c_source_file)
                    if Utils.file_newer_than(pyx_source_file, c_last_modified):
                        source_is_newer = True
                    else:
                        pxd_source_file = source_file + ".pxd"
                        if os.path.exists(
                                pxd_source_file) and Utils.file_newer_than(
                                    pxd_source_file, c_last_modified):
                            source_is_newer = True
                if source_is_newer:
                    print("Compiling module %s ..." % module)
                    result = compile(pyx_source_file)
                    c_source_file = result.c_file
                if c_source_file:
                    # Py2 distutils can't handle unicode file paths
                    if isinstance(c_source_file, unicode):
                        filename_encoding = sys.getfilesystemencoding()
                        if filename_encoding is None:
                            filename_encoding = sys.getdefaultencoding()
                        c_source_file = c_source_file.encode(filename_encoding)
                    if '.refnanny' in module:
                        defines_for_module = []
                    else:
                        defines_for_module = defines
                    extensions.append(
                        Extension(module,
                                  sources=[c_source_file],
                                  define_macros=defines_for_module))
                else:
                    print("Compilation failed")
            if extensions:
                setup_args['ext_modules'] = extensions
                add_command_class("build_ext", build_ext)
        except Exception:
            print('''
ERROR: %s

Extension module compilation failed, looks like Cython cannot run
properly on this system.  To work around this, pass the option
"--no-cython-compile".  This will install a pure Python version of
Cython without compiling its own sources.
''' % sys.exc_info()[1])
            raise
示例#42
0
def compile_cython_modules(profile=False, compile_more=False, cython_with_refnanny=False):
    source_root = os.path.abspath(os.path.dirname(__file__))
    compiled_modules = [
        "Cython.Plex.Scanners",
        "Cython.Plex.Actions",
        "Cython.Compiler.Lexicon",
        "Cython.Compiler.Scanning",
        "Cython.Compiler.Parsing",
        "Cython.Compiler.Visitor",
        "Cython.Compiler.FlowControl",
        "Cython.Compiler.Code",
        "Cython.Runtime.refnanny",
        # "Cython.Compiler.FusedNode",
    ]
    if compile_more:
        compiled_modules.extend(
            [
                "Cython.Build.Dependencies",
                "Cython.Compiler.ParseTreeTransforms",
                "Cython.Compiler.Nodes",
                "Cython.Compiler.ExprNodes",
                "Cython.Compiler.ModuleNode",
                "Cython.Compiler.Optimize",
            ]
        )

    defines = []
    if cython_with_refnanny:
        defines.append(("CYTHON_REFNANNY", "1"))

    extensions = []
    if sys.version_info[0] >= 3:
        from Cython.Distutils import build_ext as build_ext_orig

        for module in compiled_modules:
            source_file = os.path.join(source_root, *module.split("."))
            if os.path.exists(source_file + ".py"):
                pyx_source_file = source_file + ".py"
            else:
                pyx_source_file = source_file + ".pyx"
            dep_files = []
            if os.path.exists(source_file + ".pxd"):
                dep_files.append(source_file + ".pxd")
            if ".refnanny" in module:
                defines_for_module = []
            else:
                defines_for_module = defines
            extensions.append(
                Extension(module, sources=[pyx_source_file], define_macros=defines_for_module, depends=dep_files)
            )

        class build_ext(build_ext_orig):
            # we must keep the original modules alive to make sure
            # their code keeps working when we remove them from
            # sys.modules
            dead_modules = []

            def build_extensions(self):
                # add path where 2to3 installed the transformed sources
                # and make sure Python (re-)imports them from there
                already_imported = [
                    module for module in sys.modules if module == "Cython" or module.startswith("Cython.")
                ]
                keep_alive = self.dead_modules.append
                for module in already_imported:
                    keep_alive(sys.modules[module])
                    del sys.modules[module]
                sys.path.insert(0, os.path.join(source_root, self.build_lib))

                if profile:
                    from Cython.Compiler.Options import directive_defaults

                    directive_defaults["profile"] = True
                    print("Enabled profiling for the Cython binary modules")
                build_ext_orig.build_extensions(self)

        setup_args["ext_modules"] = extensions
        add_command_class("build_ext", build_ext)

    else:  # Python 2.x
        from distutils.command.build_ext import build_ext as build_ext_orig

        try:

            class build_ext(build_ext_orig):
                def build_extension(self, ext, *args, **kargs):
                    try:
                        build_ext_orig.build_extension(self, ext, *args, **kargs)
                    except StandardError:
                        print("Compilation of '%s' failed" % ext.sources[0])

            from Cython.Compiler.Main import compile
            from Cython import Utils

            if profile:
                from Cython.Compiler.Options import directive_defaults

                directive_defaults["profile"] = True
                print("Enabled profiling for the Cython binary modules")
            source_root = os.path.dirname(__file__)
            for module in compiled_modules:
                source_file = os.path.join(source_root, *module.split("."))
                if os.path.exists(source_file + ".py"):
                    pyx_source_file = source_file + ".py"
                else:
                    pyx_source_file = source_file + ".pyx"
                c_source_file = source_file + ".c"
                source_is_newer = False
                if not os.path.exists(c_source_file):
                    source_is_newer = True
                else:
                    c_last_modified = Utils.modification_time(c_source_file)
                    if Utils.file_newer_than(pyx_source_file, c_last_modified):
                        source_is_newer = True
                    else:
                        pxd_source_file = source_file + ".pxd"
                        if os.path.exists(pxd_source_file) and Utils.file_newer_than(pxd_source_file, c_last_modified):
                            source_is_newer = True
                if source_is_newer:
                    print("Compiling module %s ..." % module)
                    result = compile(pyx_source_file)
                    c_source_file = result.c_file
                if c_source_file:
                    # Py2 distutils can't handle unicode file paths
                    if isinstance(c_source_file, unicode):
                        filename_encoding = sys.getfilesystemencoding()
                        if filename_encoding is None:
                            filename_encoding = sys.getdefaultencoding()
                        c_source_file = c_source_file.encode(filename_encoding)
                    if ".refnanny" in module:
                        defines_for_module = []
                    else:
                        defines_for_module = defines
                    extensions.append(Extension(module, sources=[c_source_file], define_macros=defines_for_module))
                else:
                    print("Compilation failed")
            if extensions:
                setup_args["ext_modules"] = extensions
                add_command_class("build_ext", build_ext)
        except Exception:
            print(
                """
ERROR: %s

Extension module compilation failed, looks like Cython cannot run
properly on this system.  To work around this, pass the option
"--no-cython-compile".  This will install a pure Python version of
Cython without compiling its own sources.
"""
                % sys.exc_info()[1]
            )
            raise
# Py2/3 fix
try:
    import cPickle
except ImportError:
    import _pickle as cPickle

persisted_data_path = "include_dir.bin"
try:
    autowrap_include_dirs = cPickle.load(open(persisted_data_path, "rb"))
except IOError:
    print("The file include_dir.bin does not yet exist, please run setup.py first to create it.")

from Cython.Compiler.Main import compile, CompilationOptions
import Cython
print("Will try to compile with Cython version", Cython.__version__)

# Prepare options
print ("include:", autowrap_include_dirs)
options = dict(include_path=autowrap_include_dirs,
               #output_dir=".",
               #gdb_debug=True,
               cplus=True)

# Do Cython compile
import sys
out  = sys.argv[1]
options = CompilationOptions(**options)
compile(out, options=options)

示例#44
0
# files need to be made (not recommended!).
# 
# == taken from autowrap/Main.py run(), lower part
from __future__ import print_function

infile  = "pyopenms/pyopenms.pyx"

import cPickle
persisted_data_path = "include_dir.bin"
autowrap_include_dirs = cPickle.load(open(persisted_data_path, "rb"))
# autowrap_include_dirs = ['/usr/local/lib/python2.7/dist-packages/autowrap/data_files/boost', '/usr/local/lib/python2.7/dist-packages/autowrap/data_files', '/path/to/pyOpenMS/pxds', 'from Map cimport Map as _Map']


from Cython.Compiler.Main import compile, CompilationOptions
from Cython.Compiler.Options import directive_defaults
directive_defaults["boundscheck"] = False
directive_defaults["wraparound"] = False

options = dict(include_path=autowrap_include_dirs,
               compiler_directives=directive_defaults,
               #output_dir=".",
               #gdb_debug=True,
               cplus=True)

print("Compiling with Cython the file", infile)
print("Using include_path", autowrap_include_dirs)
options = CompilationOptions(**options)
compile(infile, options=options)
print("Success!")

示例#45
0
文件: setup.py 项目: c-oreills/pywt
from distutils.core import setup
from distutils.extension import Extension

import util.templating

if os.path.exists('MANIFEST'):
    os.remove('MANIFEST')

try:
    from Cython.Distutils import build_ext as build_ext_orig 
except ImportError:
    print "A recent version of Cython is required to build PyWavelets. Get Cython from http://www.cython.org/!"
    sys.exit(1)
else:
    from Cython.Compiler.Main import compile
    compile('src/_pywt.pyx')

# tune the C compiler settings
extra_compile_args = ['-fwrapv', '-O2', '-Wall', '-fno-strict-aliasing', '-finline-limit=1',] # '-msse2'] #, '-ftree-vectorize', '-ftree-vectorizer-verbose=7']
#extra_compile_args += ['-march=pentium4',  '-mtune=pentium4']
#extra_compile_args += ['-Wno-long-long', '-Wno-uninitialized', '-Wno-unused']

macros = [('PY_EXTENSION', None),
          #('OPT_UNROLL2', None), # enable some manual unroll loop optimizations
          #('OPT_UNROLL4', None)  # enable more manual unroll loop optimizations
         ]

source_ext = '.pyx'
setup_args = {}
if 'setuptools' in sys.modules:
    setup_args['zip_safe'] = False
示例#46
0
    "sympy.polys.euclidtools",
    "sympy.polys.factortools",
    "sympy.polys.galoistools",
    "sympy.polys.monomialtools",
    "sympy.polys.orthopolys",
    "sympy.polys.specialpolys",
    "sympy.polys.sqfreetools",
]

extensions = []

for module in compiled_modules:
    source_file = os.path.join(source_root, *module.split('.')) + ".py"

    print("Compiling module %s ..." % module)
    result = compile(source_file)

    if result.c_file is None:
        raise RuntimeError("failed to compile %s" % module)

    extensions.append(
        Extension(
            module,
            sources=[str(result.c_file)],
            extra_compile_args=['-O2', '-Wall'],
        ))

setup(name="SymPy",
      packages=[
          "sympy",
          "sympy.polys",
示例#47
0
try:
    import cPickle
except ImportError:
    import _pickle as cPickle

persisted_data_path = "include_dir.bin"
try:
    autowrap_include_dirs = cPickle.load(open(persisted_data_path, "rb"))
except IOError:
    print(
        "The file include_dir.bin does not yet exist, please run setup.py first to create it."
    )

from Cython.Compiler.Main import compile, CompilationOptions
import Cython
print("Will try to compile with Cython version", Cython.__version__)

# Prepare options
print("include:", autowrap_include_dirs)
options = dict(
    include_path=autowrap_include_dirs,
    #output_dir=".",
    #gdb_debug=True,
    cplus=True)

# Do Cython compile
import sys
out = sys.argv[1]
options = CompilationOptions(**options)
compile(out, options=options)
示例#48
0
)

#check for cython
try:
    have_cython = True
    from Cython.Distutils import build_ext
except:
    have_cython = False

# create .c for every module in c_ext
if 'sdist' in sys.argv and have_cython:
    from glob import glob
    from Cython.Compiler.Main import compile
    print 'Generating C files...',
    files = glob(os.path.join(os.path.dirname(__file__), 'pymt', 'c_ext', '*.pyx'))
    compile(files)
    print 'Done !'

#add cython core extension modules if cython is available
if have_cython:
    cmdclass['build_ext'] = build_ext
    libraries = []
    include_dirs = []
    extra_link_args = []
    if sys.platform == 'win32':
        libraries.append('opengl32')
    elif sys.platform == 'darwin':
        # On OSX, gl.h is not in GL/gl.h but OpenGL/gl.h. Cython has no
        # such thing as #ifdef, hence we just copy the file here.
        source = '/System/Library/Frameworks/OpenGL.framework/Versions/A/Headers/gl.h'
        incl = 'build/include/'
示例#49
0
文件: setup.py 项目: fajran/kivy
        continue
    for filename in fnfilter(filenames, '*.pyx'):
        pyx_files.append(join(root, filename))

# check for cython
try:
    have_cython = True
    from Cython.Distutils import build_ext
except:
    have_cython = False

# create .c for every module
if 'sdist' in argv and have_cython:
    from Cython.Compiler.Main import compile
    print 'Generating C files...',
    compile(pyx_files)
    print 'Done !'

# add cython core extension modules if cython is available
if have_cython:
    cmdclass['build_ext'] = build_ext
else:
    pyx_files = ['%s.c' % x[:-4] for x in pyx_files]

if True:
    libraries = []
    include_dirs = []
    extra_link_args = []
    if platform == 'win32':
        libraries.append('opengl32')
    elif platform == 'darwin':
示例#50
0
文件: setup.py 项目: hainm/cython
def compile_cython_modules(profile=False, compile_more=False, cython_with_refnanny=False):
    source_root = os.path.abspath(os.path.dirname(__file__))
    compiled_modules = ["Cython.Plex.Scanners",
                        "Cython.Plex.Actions",
                        "Cython.Compiler.Lexicon",
                        "Cython.Compiler.Scanning",
                        "Cython.Compiler.Parsing",
                        "Cython.Compiler.Visitor",
                        "Cython.Compiler.FlowControl",
                        "Cython.Compiler.Code",
                        "Cython.Runtime.refnanny",
                        # "Cython.Compiler.FusedNode",
                        "Cython.Tempita._tempita",
                        ]
    if compile_more:
        compiled_modules.extend([
            "Cython.Build.Dependencies",
            "Cython.Compiler.ParseTreeTransforms",
            "Cython.Compiler.Nodes",
            "Cython.Compiler.ExprNodes",
            "Cython.Compiler.ModuleNode",
            "Cython.Compiler.Optimize",
            ])

    from distutils.spawn import find_executable
    from distutils.sysconfig import get_python_inc
    pgen = find_executable(
        'pgen', os.pathsep.join([os.environ['PATH'], os.path.join(get_python_inc(), '..', 'Parser')]))
    if not pgen:
        print ("Unable to find pgen, not compiling formal grammar.")
    else:
        parser_dir = os.path.join(os.path.dirname(__file__), 'Cython', 'Parser')
        grammar = os.path.join(parser_dir, 'Grammar')
        subprocess.check_call([
            pgen,
            os.path.join(grammar),
            os.path.join(parser_dir, 'graminit.h'),
            os.path.join(parser_dir, 'graminit.c'),
            ])
        cst_pyx = os.path.join(parser_dir, 'ConcreteSyntaxTree.pyx')
        if os.stat(grammar)[stat.ST_MTIME] > os.stat(cst_pyx)[stat.ST_MTIME]:
            mtime = os.stat(grammar)[stat.ST_MTIME]
            os.utime(cst_pyx, (mtime, mtime))
        compiled_modules.extend([
                "Cython.Parser.ConcreteSyntaxTree",
            ])

    defines = []
    if cython_with_refnanny:
        defines.append(('CYTHON_REFNANNY', '1'))

    extensions = []
    if sys.version_info[0] >= 3:
        from Cython.Distutils import build_ext as build_ext_orig
        for module in compiled_modules:
            source_file = os.path.join(source_root, *module.split('.'))
            if os.path.exists(source_file + ".py"):
                pyx_source_file = source_file + ".py"
            else:
                pyx_source_file = source_file + ".pyx"
            dep_files = []
            if os.path.exists(source_file + '.pxd'):
                dep_files.append(source_file + '.pxd')
            if '.refnanny' in module:
                defines_for_module = []
            else:
                defines_for_module = defines
            extensions.append(
                Extension(module, sources = [pyx_source_file],
                          define_macros = defines_for_module,
                          depends = dep_files)
                )

        class build_ext(build_ext_orig):
            # we must keep the original modules alive to make sure
            # their code keeps working when we remove them from
            # sys.modules
            dead_modules = []

            def build_extensions(self):
                # add path where 2to3 installed the transformed sources
                # and make sure Python (re-)imports them from there
                already_imported = [ module for module in sys.modules
                                     if module == 'Cython' or module.startswith('Cython.') ]
                keep_alive = self.dead_modules.append
                for module in already_imported:
                    keep_alive(sys.modules[module])
                    del sys.modules[module]
                sys.path.insert(0, os.path.join(source_root, self.build_lib))

                if profile:
                    from Cython.Compiler.Options import directive_defaults
                    directive_defaults['profile'] = True
                    print("Enabled profiling for the Cython binary modules")
                build_ext_orig.build_extensions(self)

        setup_args['ext_modules'] = extensions
        add_command_class("build_ext", build_ext)

    else: # Python 2.x
        from distutils.command.build_ext import build_ext as build_ext_orig
        try:
            class build_ext(build_ext_orig):
                def build_extension(self, ext, *args, **kargs):
                    try:
                        build_ext_orig.build_extension(self, ext, *args, **kargs)
                    except StandardError:
                        print("Compilation of '%s' failed" % ext.sources[0])
            from Cython.Compiler.Main import compile
            from Cython import Utils
            if profile:
                from Cython.Compiler.Options import directive_defaults
                directive_defaults['profile'] = True
                print("Enabled profiling for the Cython binary modules")
            source_root = os.path.dirname(__file__)
            for module in compiled_modules:
                source_file = os.path.join(source_root, *module.split('.'))
                if os.path.exists(source_file + ".py"):
                    pyx_source_file = source_file + ".py"
                else:
                    pyx_source_file = source_file + ".pyx"
                c_source_file = source_file + ".c"
                source_is_newer = False
                if not os.path.exists(c_source_file):
                    source_is_newer = True
                else:
                    c_last_modified = Utils.modification_time(c_source_file)
                    if Utils.file_newer_than(pyx_source_file, c_last_modified):
                        source_is_newer = True
                    else:
                        pxd_source_file = source_file + ".pxd"
                        if os.path.exists(pxd_source_file) and Utils.file_newer_than(pxd_source_file, c_last_modified):
                            source_is_newer = True
                if source_is_newer:
                    print("Compiling module %s ..." % module)
                    result = compile(pyx_source_file)
                    c_source_file = result.c_file
                if c_source_file:
                    # Py2 distutils can't handle unicode file paths
                    if isinstance(c_source_file, unicode):
                        filename_encoding = sys.getfilesystemencoding()
                        if filename_encoding is None:
                            filename_encoding = sys.getdefaultencoding()
                        c_source_file = c_source_file.encode(filename_encoding)
                    if '.refnanny' in module:
                        defines_for_module = []
                    else:
                        defines_for_module = defines
                    extensions.append(
                        Extension(module, sources = [c_source_file],
                                  define_macros = defines_for_module)
                        )
                else:
                    print("Compilation failed")
            if extensions:
                setup_args['ext_modules'] = extensions
                add_command_class("build_ext", build_ext)
        except Exception:
            print('''
ERROR: %s

Extension module compilation failed, looks like Cython cannot run
properly on this system.  To work around this, pass the option
"--no-cython-compile".  This will install a pure Python version of
Cython without compiling its own sources.
''' % sys.exc_info()[1])
            raise
示例#51
0
文件: setup.py 项目: marius311/pypico
          raise Exception("PICO requires %s. "%name+skipmsg)
    checklib('numpy','NumPy','1.6.1')
    checklib('scipy','SciPy','0.10.1')


from numpy.distutils.core import setup
from numpy.distutils.misc_util import Configuration


# By default don't try to use Cython to compile the pyx file,
# just use the file distributed with pypico. 
build_cython=('--build_cython' in sys.argv)
if build_cython:
    sys.argv.remove('--build_cython')
    from Cython.Compiler.Main import compile
    compile('pypico/pico.pyx')



config = Configuration('pypico',
    name='pypico',
    version='4.0.0',
    author='Marius Millea',
    author_email='*****@*****.**',
    packages=['pypico'],
    url='https://github.com/marius311/pypico',
    license='LICENSE.txt',
    description='Quickly compute the CMB powerspectra and matter transfer functions.',
    long_description=open('README.md').read()
)
示例#52
0
CYTHON_OUT = "svdlib/_svdlib.c"
CYTHON_SRC = "svdlib/_svdlib.pyx"

### Update the Cython file, if necessary.
def get_modification_time(filename):
    return os.stat(filename)[ST_MTIME]


if not os.path.exists(CYTHON_OUT) or get_modification_time(CYTHON_SRC) > get_modification_time(CYTHON_OUT):
    try:
        # Try building the Cython file
        print "Building Cython source"
        from Cython.Compiler.Main import compile

        res = compile(CYTHON_SRC)
        if res.num_errors > 0:
            print >> sys.stderr, "Error building the Cython file."
            sys.exit(1)
    except ImportError:
        print >> sys.stderr, "Warning: Skipped building the Cython file."
        print >> sys.stderr, " The svdlib source file is more recent than the Cython output file, but"
        print >> sys.stderr, " you seem to lack Cython, so skipping rebuilding it."


svdlibc = Extension(
    name="csc.divisi._svdlib",
    sources=[CYTHON_OUT, "svdlib/svdwrapper.c", "svdlib/las2.c", "svdlib/svdlib.c", "svdlib/svdutil.c"],
    include_dirs=[numpy.get_include(), "svdlib"],
    extra_compile_args=["-g"],
    extra_link_args=["-g"],
示例#53
0
CYTHON_SRC = 'svdlib/_svdlib.pyx'


### Update the Cython file, if necessary.
def get_modification_time(filename):
    return os.stat(filename)[ST_MTIME]


try:
    if not os.path.exists(CYTHON_OUT) or get_modification_time(
            CYTHON_SRC) > get_modification_time(CYTHON_OUT):
        try:
            # Try building the Cython file
            print 'Building Cython source'
            from Cython.Compiler.Main import compile
            res = compile(CYTHON_SRC)
            if res.num_errors > 0:
                print >> sys.stderr, "Error building the Cython file."
                sys.exit(1)
        except ImportError:
            print >> sys.stderr, 'Warning: Skipped building the Cython file.'
            print >> sys.stderr, ' The svdlib source file is more recent than the Cython output file, but'
            print >> sys.stderr, ' you seem to lack Cython, so skipping rebuilding it.'
            raw_input('Press Enter to acknowledge. ')
except OSError:
    print >> sys.stderr, 'Warning: Skipped building the Cython file.'

svdlibc = Extension(
    name='divisi2._svdlib',
    sources=[
        CYTHON_OUT,
示例#54
0
# It can be run in the build process of pyopenms when manual changes to the pyx
# files need to be made (not recommended!).
#
# == taken from autowrap/Main.py run(), lower part
from __future__ import print_function

infile = "pyopenms/pyopenms.pyx"

import cPickle
persisted_data_path = "include_dir.bin"
autowrap_include_dirs = cPickle.load(open(persisted_data_path, "rb"))
# autowrap_include_dirs = ['/usr/local/lib/python2.7/dist-packages/autowrap/data_files/boost', '/usr/local/lib/python2.7/dist-packages/autowrap/data_files', '/path/to/pyOpenMS/pxds', 'from Map cimport Map as _Map']

from Cython.Compiler.Main import compile, CompilationOptions
from Cython.Compiler.Options import directive_defaults
directive_defaults["boundscheck"] = False
directive_defaults["wraparound"] = False

options = dict(
    include_path=autowrap_include_dirs,
    compiler_directives=directive_defaults,
    #output_dir=".",
    #gdb_debug=True,
    cplus=True)

print("Compiling with Cython the file", infile)
print("Using include_path", autowrap_include_dirs)
options = CompilationOptions(**options)
compile(infile, options=options)
print("Success!")