Created
December 9, 2012 15:19
-
-
Save juntalis/4245559 to your computer and use it in GitHub Desktop.
LibJit Cython Bindings WIP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| try: | |
| cimport cython | |
| except: | |
| # To please my IDE, I have to do this. | |
| import Cython.Shadow as cython | |
| #noinspection PyUnresolvedReferences | |
| from jit.internals cimport initinternals, JitType | |
| from libjit cimport * | |
| import weakref | |
| # Not sure this is really going to work, but we'll see.. | |
| cdef object _working_context = None | |
| # Grab our internals module. | |
| cdef object init_internals(): | |
| initinternals() | |
| #noinspection PyUnresolvedReferences | |
| import jit.internals as internals | |
| return internals | |
| cdef object internals = init_internals() | |
| # Declare our default options. | |
| defaults = internals.JitDefaults() | |
| cdef class UserType(JitType): | |
| pass | |
| @cython.final | |
| cdef class Context: | |
| cdef jit_context_t _context | |
| cdef object __weakref__ | |
| cdef bint _build_exceptions | |
| cdef object _on_demand_driver | |
| def __init__(self, start_build=None): | |
| global defaults | |
| self._context = jit_context_create() | |
| self._build_exceptions = defaults.context_exceptions | |
| self._on_demand_driver = None | |
| if start_build is None: | |
| start_build = defaults.context_start_build | |
| if start_build: | |
| self._build_enter() | |
| def __enter__(self): | |
| """ Enter a context to begin building it. (If you're not already doing so. """ | |
| return self._build_enter() | |
| def __exit__(self, exc_type, exc_val, exc_tb): | |
| """ Once exited successfully, a context ends the build phase. """ | |
| if exc_type is None: | |
| self._build_exit() | |
| def __dealloc__(self): | |
| jit_context_destroy(self._context) | |
| def start_build(self): | |
| """ Start the build phase for this context. (Lock it) """ | |
| return self._build_enter() | |
| def stop_build(self): | |
| """ Stop the build phase for this context. (Unlock it) """ | |
| return self._build_exit() | |
| cdef object _build_enter(self): | |
| global _working_context | |
| if self.is_building: | |
| if self._build_exceptions: | |
| raise internals.ContextBuildError('This context is currently being built!') | |
| else: | |
| jit_context_build_start(self._context) | |
| _working_context = weakref.ref(self) | |
| return self | |
| cdef object _build_exit(self): | |
| global _working_context | |
| if not self.is_building: | |
| if self._build_exception: | |
| raise internals.ContextBuildError('This context is not currently being built!') | |
| else: | |
| jit_context_build_end(self._context) | |
| if _working_context is not None and _working_context() is self: | |
| _working_context = None | |
| return self | |
| def _functions(self): | |
| return [ <int>self._context.functions ] | |
| property supports_threads: | |
| """ Whether or not this context supports threads. """ | |
| def __get__(self): | |
| return jit_context_supports_threads(self._context) | |
| property is_building: | |
| """ Whether or not this context is currently being built. """ | |
| def __get__(self): | |
| return jit_context_building(self._context) | |
| property build_exceptions: | |
| """ See defaults.context_exceptions """ | |
| def __get__(self): return self._build_exceptions | |
| def __set__(self, value): | |
| if type(value) not in [ bool, int ]: | |
| raise TypeError('context_exceptions must be a bool value!') | |
| self._build_exceptions = value | |
| property on_demand_driver: | |
| """ Set/Get the on-demand jit driver function for this context. (Not Yet Implemented) """ | |
| def __get__(self): return self._on_demand_driver | |
| def __set__(self, value): | |
| raise NotImplementedError() | |
| #self._on_demand_driver = value | |
| def __del__(self): self._on_demand_driver = None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment