def test_on_trait_lambda_failure(self): # Lambda function are converted like builtins when cythonized which # causes the following code to fail code = """ from traits.api import HasTraits, Str, Int, Property def Alias(name): return Property( lambda obj: getattr(obj, name), lambda obj, value: setattr(obj, name, value) ) class Test(HasTraits): name = Str funky_name = Alias('name') return Test() """ try: cython.inline(code, get_type=_always_object_type, force=True, locals={}, globals={}) except: # We suppose we have an exception. Because of the usage of the # skipIf decorator on the test, we can't use an expectedFailure # decorator as they don't play well together. pass else: self.fail( 'Unexpected results. Cython was not managing lambda as regular' ' functions. Behaviour changed ...' )
def gilsleep(t): """gil-holding sleep with cython.inline""" code = "\n".join(["from posix cimport unistd", "unistd.sleep(t)"]) while True: inline(code, quiet=True, t=t) print(time.time()) sys.stdout.flush() # this is important
def gilsleep(t): """gil-holding sleep with cython.inline""" code = '\n'.join([ 'from posix cimport unistd', 'unistd.sleep(t)', ]) while True: inline(code, quiet=True, t=t) print time.time() sys.stdout.flush() # this is important
def has_no_compiler(): if no_cython: return True # Easy way to check if we have access to a compiler code = "return 1+1" try: cython.inline(code) return False except: return True
def _test_cython(cls): if not hasattr(cls, '_use_cython'): cls._use_cython = False if cython is None: return try: cython.inline('x = 1', force=True, quiet=True) cls._use_cython = True except cython.Compiler.Errors.CompileError: pass
def test_on_trait_aliasing_different_scope(self): code = """ from traits.api import HasTraits, Str, Int, Property def _get_value(self, name): return getattr(self, 'name') def _set_value(self, name, value): return setattr(self, 'name', value) class Test(HasTraits): name = Str funky_name = Property(_get_value, _set_value) return Test() """ obj = cython.inline(code, get_type=_always_object_type) self.assertEquals(obj.funky_name, obj.name) # Assert dependency works obj.name = 'Bob' self.assertEquals(obj.funky_name, obj.name)
def generate_marshall_method(schema, context=missing, threshold=100): # type: (Schema, JitContext, int) -> Union[SerializeProxy, Callable, None] """Generates a function to marshall objects for a given schema. :param schema: The Schema to generate a marshall method for. :param threshold: The number of calls of the same type to observe before specializing the marshal method for that type. :param use_cython: Whether or not to attempt to use cython when Jitting. :return: A Callable that can be used to marshall objects for the schema """ if is_overridden(schema.get_attribute, Schema.get_attribute): # Bail if get_attribute is overridden return None if context is missing: context = JitContext() context.namespace['dict_class'] = lambda: schema.dict_class() # pylint: disable=unnecessary-lambda jit_options = getattr(schema.opts, 'jit_options', {}) context.schema_stack.add(schema.__class__) result = generate_marshall_method_bodies(schema, context) result += '\n\n' + '\n\n'.join(context.additional_method_bodies) context.additional_method_bodies = [] context.schema_stack.remove(schema.__class__) namespace = context.namespace for key, value in iteritems(schema.fields): namespace[field_symbol_name(key) + '__serialize'] = value._serialize if value.default is not missing: namespace[field_symbol_name(key) + '__default'] = value.default if context.use_cython and CYTHON_AVAILABLE: namespace = cython.inline(result, **namespace) else: exec_(result, namespace) proxy = None # type: Optional[SerializeProxy] marshall_method = None # type: Union[SerializeProxy, Callable, None] if jit_options.get('expected_marshal_type') in EXPECTED_TYPE_TO_CLASS: marshall_method = namespace[EXPECTED_TYPE_TO_CLASS[ jit_options['expected_marshal_type']].__name__] else: marshall_method = SerializeProxy( namespace[DictSerializer.__name__], namespace[HybridSerializer.__name__], namespace[InstanceSerializer.__name__], threshold=threshold) proxy = marshall_method def marshall(obj, many=False): if many: return [marshall_method(x) for x in obj] return marshall_method(obj) if proxy: marshall.proxy = proxy # type: ignore return marshall
def typed(obj): """Compiles a function or class with cython. Use annotations for static type declarations. Example: import statically @statically.typed def add_two(x: int) -> int: two: int = 2 return x + two """ if not _can_cython_inline(): return obj elif has_async_gen_fun and inspect.isasyncgenfunction(obj): raise TypeError("Async generator funcions are not supported.") source = _get_source_code(obj) frame = inspect.currentframe().f_back if inspect.isclass(obj): locals_ = frame.f_locals else: locals_ = _get_outer_variables(obj) with warnings.catch_warnings(): warnings.simplefilter("ignore") compiled = cython.inline(source, locals=locals_, globals=frame.f_globals, quiet=True) return compiled[obj.__name__]
def test_on_trait_on_trait_change_decorator(self): code = """ from traits.api import HasTraits, Str, Int, on_trait_change class Test(HasTraits): name = Str value = Int @on_trait_change('name') def _update_value(self): self.value += 1 return Test() """ obj = cython.inline( code, get_type=_always_object_type, force=True, locals={}, globals={}, ) with self.assertTraitChanges(obj, "value", count=1): obj.name = "changing_name" self.assertEqual(obj.value, 1)
def test_on_trait_aliasing(self): code = """ from traits.api import HasTraits, Str, Int, Property def Alias(name): def _get_value(self): return getattr(self, name) def _set_value(self, value): return setattr(self, name, value) return Property(_get_value, _set_value) class Test(HasTraits): name = Str funky_name = Alias('name') return Test() """ obj = cython.inline(code, get_type=_always_object_type, force=True, locals={}, globals={}) self.assertEquals(obj.funky_name, obj.name) # Assert dependency works obj.name = 'Bob' self.assertEquals(obj.funky_name, obj.name)
def test_on_trait_properties_with_standard_getter(self): code = """ from traits.api import HasTraits, Str, Int, Property class Test(HasTraits): name = Str def _get_name_length(self): return len(self.name) name_len = Property(_get_name_length) return Test() """ obj = cython.inline( code, get_type=_always_object_type, force=True, locals={}, globals={}, ) self.assertEqual(obj.name_len, len(obj.name)) # Assert dependency works obj.name = "Bob" self.assertEqual(obj.name_len, len(obj.name))
def cythonize_func_inline_method(self, func, function_lines, def_line_index, hash_only=False, *args, **kwargs): """ Compiles the function if it hasn't been compile before, and stores the function for reuse """ if hash_only: cython_def_lines = self.format_function_code_to_cython( func, function_lines, def_line_index, hash_only=True, *args, **kwargs ) function_hash_name = self.hash_func(cython_def_lines) return function_hash_name else: cython_function_code, cython_def_lines = self.format_function_code_to_cython( func, function_lines, def_line_index, *args, **kwargs ) cythonized_func = cython_inline(cython_function_code, cython_compiler_directives={ "infer_types": self.infer_types, }) cythonized_func = cythonized_func.get(func.__name__, None) if cythonized_func is None: python_func = "".join(function_lines) cythonized_func = cython.inline(python_func).get(func.__name__, None) if cythonized_func is None: cythonized_func = func function_hash_name = self.hash_func(cython_def_lines) return function_hash_name, cythonized_func
def test_on_trait_properties(self): code = """ from traits.api import HasTraits, Str, Int, Property, cached_property class Test(HasTraits): name = Str name_len = Property(depends_on='name') @cached_property def _get_name_len(self): return len(self.name) return Test() """ obj = cython.inline( code, get_type=_always_object_type, force=True, locals={}, globals={}, ) self.assertEqual(obj.name_len, len(obj.name)) # Assert dependency works obj.name = "Bob" self.assertEqual(obj.name_len, len(obj.name))
def test_on_trait_aliasing(self): code = """ from traits.api import HasTraits, Str, Int, Property def Alias(name): def _get_value(self): return getattr(self, name) def _set_value(self, value): return setattr(self, name, value) return Property(_get_value, _set_value) class Test(HasTraits): name = Str funky_name = Alias('name') return Test() """ obj = cython.inline( code, get_type=_always_object_type, force=True, locals={}, globals={}, ) self.assertEqual(obj.funky_name, obj.name) # Assert dependency works obj.name = "Bob" self.assertEqual(obj.funky_name, obj.name)
def test_on_trait_aliasing_different_scope(self): code = """ from traits.api import HasTraits, Str, Int, Property def _get_value(self, name): return getattr(self, 'name') def _set_value(self, name, value): return setattr(self, 'name', value) class Test(HasTraits): name = Str funky_name = Property(_get_value, _set_value) return Test() """ obj = cython.inline(code, get_type=_always_object_type) self.assertEqual(obj.funky_name, obj.name) # Assert dependency works obj.name = "Bob" self.assertEqual(obj.funky_name, obj.name)
def has_no_compiler(): if no_cython: return True code = "return 1+1" try: result = cython.inline(code) return False except: return True
def test_on_trait_lambda_failure(self): # Lambda function are converted like builtins when cythonized which # causes the following code to fail code = """ from traits.api import HasTraits, Str, Int, Property def Alias(name): return Property( lambda obj: getattr(obj, name), lambda obj, value: setattr(obj, name, value) ) class Test(HasTraits): name = Str funky_name = Alias('name') return Test() """ try: cython.inline( code, get_type=_always_object_type, force=True, locals={}, globals={}, ) except: # We suppose we have an exception. Because of the usage of the # skipIf decorator on the test, we can't use an expectedFailure # decorator as they don't play well together. pass else: self.fail( "Unexpected results. Cython was not managing lambda as regular" " functions. Behaviour changed ...")
def test_basic_events(self): code = """ from traits.api import HasTraits, Str class Test(HasTraits): name = Str return Test() """ obj = cython.inline(code) with self.assertTraitChanges(obj, "name", count=1): obj.name = "changing_name"
def test_basic_events(self): code = """ from traits.api import HasTraits, Str class Test(HasTraits): name = Str return Test() """ obj = cython.inline(code) with self.assertTraitChanges(obj, 'name', count=1): obj.name = 'changing_name'
def test_simple_default_methods(self): code = """ from traits.api import HasTraits, Str class Test(HasTraits): name = Str def _name_default(self): return 'Joe' return Test() """ obj = cython.inline(code) self.assertEqual(obj.name, "Joe")
def test_simple_default_methods(self): code = """ from traits.api import HasTraits, Str class Test(HasTraits): name = Str def _name_default(self): return 'Joe' return Test() """ obj = cython.inline(code) self.assertEquals(obj.name, 'Joe')
def test_on_trait_static_handlers(self): code = """ from traits.api import HasTraits, Str, Int class Test(HasTraits): name = Str value = Int def _name_changed(self): self.value += 1 return Test() """ obj = cython.inline(code, get_type=_always_object_type, force=True) with self.assertTraitChanges(obj, "value", count=1): obj.name = "changing_name" self.assertEqual(obj.value, 1)
def test_on_trait_static_handlers(self): code = """ from traits.api import HasTraits, Str, Int class Test(HasTraits): name = Str value = Int def _name_changed(self): self.value += 1 return Test() """ obj = cython.inline(code, get_type=_always_object_type, force=True) with self.assertTraitChanges(obj, 'value', count=1): obj.name = 'changing_name' self.assertEquals(obj.value, 1)
def test_on_trait_change_decorator(self): code = """ from traits.api import Int, HasTraits, Str, on_trait_change class Test(HasTraits): name = Str value = Int @on_trait_change('name') def _update_a_value(self): self.value += 1 return Test() """ obj = cython.inline(code) with self.assertTraitChanges(obj, 'value', count=1): obj.name = 'changing_name' self.assertEquals(obj.value, 1)
def timefunc_cython_inline(): cython.inline(code, locals=ns)
def _create_cnamedtuple_class(classname, code, cython_options): code = code + "\nGenericClass = " + classname + "\n" ret = cython.inline(code, **cython_options) return ret["GenericClass"]
#!/usr/bin/env python import os import sys try: from setuptools import setup except ImportError: from distutils.core import setup try: # check we can compile on this machine import cython cython.inline("return 1;") from Cython.Build import cythonize ext_modules = cythonize("param/*.py", exclude=['param/ipython.py']) except: ext_modules = [] setup_args = {} setup_args.update( dict(name='param', version="1.5.1", description='Declarative Python programming using Parameters.', long_description=open('README.rst').read() if os.path.isfile('README.rst') else 'Consult README.rst', author="IOAM", author_email="*****@*****.**", ext_modules=ext_modules, maintainer="IOAM",
#!/usr/bin/env python import os import sys try: from setuptools import setup except ImportError: from distutils.core import setup try: # check we can compile on this machine import cython; cython.inline("return 1;") from Cython.Build import cythonize ext_modules = cythonize("param/*.py", exclude=['param/ipython.py']) except: ext_modules = [] setup_args = {} setup_args.update(dict( name='param', version="1.5.0", description='Declarative Python programming using Parameters.', long_description=open('README.rst').read() if os.path.isfile('README.rst') else 'Consult README.rst', author= "IOAM", author_email= "*****@*****.**", ext_modules=ext_modules, maintainer="IOAM", maintainer_email="*****@*****.**", platforms=['Windows', 'Mac OS X', 'Linux'],
from brian2 import * from brian2.memory.dynamicarray import * from cython import inline from brian2.codegen.runtime.cython_rt.modified_inline import modified_cython_inline as inline x = DynamicArray1D(10) code = ''' print 'hi' for i in xrange(len(x)): print x[i] ''' ns = {'x': x} a, b = inline(code, locals=ns, globals={}) print dir(a) print b a.__invoke(*b)
packages=find_packages(exclude=('*.tests.*', '*.tests')), include_package_data=True, scripts=['scripts/pyou', ], install_requires=requirements, extras_require={'full': full_requirements} ) if build_cmd != 'clean' and not PYPY: # skip cython in pypy try: from Cython.Build import cythonize from Cython.Distutils import build_ext import cython # detect if cython works if sys.platform == 'win32': cython.inline('return a + b', a=1, b=1) extension_kw = dict() if 'MSC' in sys.version: extension_kw['extra_compile_args'] = ['/Ot'] extensions = [ Extension('odps.types_c', ['odps/src/types_c.pyx'], **extension_kw), Extension('odps.crc32c_c', ['odps/src/crc32c/*.pyx'], **extension_kw), Extension('odps.tunnel.pb.encoder_c', ['odps/tunnel/pb/encoder_c.pyx'], **extension_kw), Extension('odps.tunnel.pb.decoder_c', ['odps/tunnel/pb/decoder_c.pyx'], **extension_kw), Extension('odps.tunnel.pb.util_c', ['odps/tunnel/pb/util_c.pyx'], **extension_kw), Extension('odps.tunnel.checksum_c', ['odps/tunnel/checksum_c.pyx'], **extension_kw), Extension('odps.tunnel.tabletunnel.writer_c', ['odps/tunnel/tabletunnel/writer_c.pyx'], **extension_kw), Extension('odps.tunnel.tabletunnel.reader_c', ['odps/tunnel/tabletunnel/reader_c.pyx'], **extension_kw), ]
if CHECK_BOUNDS: add("while p > cells.__len__(): cells.append(0)") elif c == BWD: if OPTIMIZATION and prog_lines[-1][:len(indent)+5]==indent+"p -= ": n = int(prog_lines[-1][len(indent)+5:]) + 1 prog_lines[-1] = indent+"p -= %d"%n else: add("p -= 1") if CHECK_BOUNDS: add("if p < 0: sys.exit(1)") elif c == INC: if OPTIMIZATION and prog_lines[-1][:len(indent)+12]==indent+"cells[p] += ": n = int(prog_lines[-1][len(indent)+12:]) + 1 prog_lines[-1] = indent+"cells[p] += %d"%n else: add("cells[p] += 1") elif c == DEC: if OPTIMIZATION and prog_lines[-1][:len(indent)+12]==indent+"cells[p] -= ": n = int(prog_lines[-1][len(indent)+12:]) + 1 prog_lines[-1] = indent+"cells[p] -= %d"%n else: add("cells[p] -= 1") elif c == START: add("while cells[p]:") indent += " " elif c == END: indent = indent[:-2] import cython cython.inline("\n".join(prog_lines))
#!/usr/bin/env python import os import sys try: from setuptools import setup except ImportError: from distutils.core import setup try: # check we can compile on this machine import cython; cython.inline("return 1;") from Cython.Build import cythonize ext_modules = cythonize("param/*.py", exclude=['param/ipython.py']) except: ext_modules = [] setup_args = {} setup_args.update(dict( name='param', version="1.5.1", description='Declarative Python programming using Parameters.', long_description=open('README.rst').read() if os.path.isfile('README.rst') else 'Consult README.rst', author= "IOAM", author_email= "*****@*****.**", ext_modules=ext_modules, maintainer="IOAM", maintainer_email="*****@*****.**", platforms=['Windows', 'Mac OS X', 'Linux'],
include_package_data=True, scripts=[ 'scripts/pyou', ], install_requires=requirements, extras_require={'full': full_requirements}) if build_cmd != 'clean' and not PYPY: # skip cython in pypy try: from Cython.Build import cythonize from Cython.Distutils import build_ext import cython # detect if cython works if sys.platform == 'win32': cython.inline('return a + b', a=1, b=1) extensions = [ Extension('odps.types_c', ['odps/src/types_c.pyx']), Extension('odps.crc32c_c', ['odps/src/crc32c/*.pyx']), Extension('odps.tunnel.pb.encoder_c', ['odps/tunnel/pb/encoder_c.pyx']), Extension('odps.tunnel.pb.decoder_c', ['odps/tunnel/pb/decoder_c.pyx']), Extension('odps.tunnel.pb.util_c', ['odps/tunnel/pb/util_c.pyx']), Extension('odps.tunnel.checksum_c', ['odps/tunnel/checksum_c.pyx']), Extension('odps.tunnel.tabletunnel.writer_c', ['odps/tunnel/tabletunnel/writer_c.pyx']), Extension('odps.tunnel.tabletunnel.reader_c', ['odps/tunnel/tabletunnel/reader_c.pyx']),
def rhs(t, y, p): # note that the evaluated code sets ydot as a side effect cython.inline(code_eqs, quiet=True) return ydot
def jacobian(t, y, p): cython.inline(jac_eqs, quiet=True) return jac