Example #1
0
def cython_on_demand(unsafe):
    """
    Enables loading .pyx files from .py files (on-demand compilation).

    With `unsafe` deactivates all Cython safety checks and compatibility
    options (do not use without first testing that things work reliably).
    """
    if unsafe:
        _old_get_du_ext = pyximport.pyximport.get_distutils_extension

        @wraps(_old_get_du_ext)
        def _new_get_du_ext(*args, **kwargs):
            extension, setup_args = _old_get_du_ext(*args, **kwargs)
            directives = getattr(extension, 'cython_directives', {})
            directives.update({
                'language_level': 3,
                'boundscheck': False,
                'wraparound': False,
                'initializedcheck': False,
                'cdivision': True,
                'always_allow_keywords': False
            })
            extension.cython_directives = directives
            return extension, setup_args

        pyximport.pyximport.get_distutils_extension = _new_get_du_ext

    pyximport.install(setup_args={'include_dirs': numpy.get_include()})
Example #2
0
def importCython():
    import pyximport.pyximport

    # Hack pyximport to have default options for profiling and embedding
    # signatures in docstrings.
    # Anytime pyximport needs to build a file, it ends up calling
    # pyximport.pyximport.get_distutils_extension.  This function returns an
    # object which has a cython_directives attribute that may be set to a
    # dictionary of compiler directives for cython.
    _old_get_distutils_extension = pyximport.pyximport.get_distutils_extension

    @functools.wraps(_old_get_distutils_extension)
    def _get_distutils_extension_new(*args, **kwargs):
        extension_mod, setup_args = _old_get_distutils_extension(
            *args, **kwargs)

        # Use g++, not gcc
        extension_mod.language = 'c++'

        if not hasattr(extension_mod, 'cython_directives'):
            extension_mod.cython_directives = {}
        extension_mod.cython_directives.setdefault('embedsignature', True)
        extension_mod.cython_directives.setdefault('profile', True)
        return extension_mod, setup_args

    pyximport.pyximport.get_distutils_extension = _get_distutils_extension_new

    # Finally, install pyximport so that each machine has its own build
    # directory (prevents errors with OpenMPI)
    _, pyxImporter = pyximport.install(
        build_dir=os.path.expanduser('~/.pyxbld/{}'.format(
            socket.gethostname())),
        setup_args={'include_dirs': [np.get_include()]})
    _pyx_oFind = pyxImporter.find_module

    def _pyx_nFind(fullname, package_path=None):
        if fullname in ['cuda_ndarray.ProcessLookupErrorda_ndarray']:
            return None
        return _pyx_oFind(fullname, package_path)

    pyxImporter.find_module = _pyx_nFind
Example #3
0
import sys
from restrain_jit.becython.cy_loader import setup_pyx_for_cpp
from pyximport import pyximport
from timeit import timeit

from restrain_jit.becython.cython_vm import Options

setup_pyx_for_cpp()
pyximport.install()
import restrain_jit.becython.cython_rts.hotspot

from restrain_jit.becython.cy_jit_ext_template import mk_module_code
from restrain_jit.becython.cy_jit import JITSystem
jit_sys = JITSystem()

# DEBUG['stack-vm'] = True
# Options['log-phi'] = True

# show generated code for debug
jit_sys.store_base_method_log = True


@jit_sys.jit
def f(seq, init):
    n = len(seq)
    i = 0
    while i < n:
        init = init + seq[i]
        i = i + 1
    return init
Example #4
0
import threading
from pyximport import pyximport
import subprocess
import os.path
import time
import cv2
import numpy as np

pyximport.install(language_level=3,
                  setup_args={"include_dirs": np.get_include()})
import tree_engine as engine
from tree_engine import Grid
from tree_stats import *

start_pos = 0
nutrient_scale = 0
oxygen_scale = 0
energy_scale = 0


class Renderer(threading.Thread):
    def __init__(self, grid: Grid, frames_per_sec: int):
        super(Renderer, self).__init__()
        self.grid = grid
        self.frame_time = (1. / float(frames_per_sec)) * 1000.
        self.step_time = 0
        self.x = -1
        self.y = -1
        self.steps = 0

        self.step_max = 1
Example #5
0
from __future__ import absolute_import, print_function

from pyximport import pyximport
pyximport.install(reload_support=True)

import os
import shutil
import sys
import tempfile
import time
from zipfile import ZipFile

try:
    from __builtin__ import reload
except ImportError:
    from importlib import reload


def make_tempdir():
    tempdir = os.path.join(tempfile.gettempdir(), "pyrex_temp")
    if os.path.exists(tempdir):
        remove_tempdir(tempdir)

    os.mkdir(tempdir)
    return tempdir


def remove_tempdir(tempdir):
    shutil.rmtree(tempdir, 0, on_remove_file_error)

Example #6
0
from __future__ import absolute_import, print_function

from pyximport import pyximport; pyximport.install(reload_support=True)

import os, sys
import time, shutil
import tempfile


def make_tempdir():
    tempdir = os.path.join(tempfile.gettempdir(), "pyrex_temp")
    if os.path.exists(tempdir):
        remove_tempdir(tempdir)

    os.mkdir(tempdir)
    return tempdir


def remove_tempdir(tempdir):
    shutil.rmtree(tempdir, 0, on_remove_file_error)


def on_remove_file_error(func, path, excinfo):
    print("Sorry! Could not remove a temp file:", path)
    print("Extra information.")
    print(func, excinfo)
    print("You may want to delete this yourself when you get a chance.")


def test():
    pyximport._test_files = []