Ejemplo n.º 1
0
    def test_type_inference0(self):
        code = '''
        def wc(content):
            d = {}

            for word in content.split():
                d[word] = d.get(word, 0) + 1

            # Use list comprehension
            l = [(freq, word) for word, freq in d.items()]

            return sorted(l)
        '''
        self.run_test(code, "cha-la head cha-la", wc=[str])

        code_bis = code.replace("1", "'1'")


        with self.assertRaises(pythran.types.tog.PythranTypeError):
            pythran.compile_pythrancode("dumbo", dedent(code_bis))

        code_ter = code.replace("0", "None")


        with self.assertRaises(pythran.types.tog.PythranTypeError):
            pythran.compile_pythrancode("dumbo", dedent(code_ter))
Ejemplo n.º 2
0
    def test_complex_import_manipulation2(self):
        code = """
            from bisect import bisect_right
            def unsupported_module():
                return bisect()"""

        with self.assertRaises(pythran.syntax.PythranSyntaxError) as ex:
            pythran.compile_pythrancode("complex_import_manipulation2", dedent(code), pyonly=True)
Ejemplo n.º 3
0
 def test_type_inference9(self):
     code = """
         def invalid_multi_yield(n):
             for i in n:
                 yield [n]
             yield n"""
     with self.assertRaises(pythran.types.tog.PythranTypeError):
         pythran.compile_pythrancode("dumbo", dedent(code))
Ejemplo n.º 4
0
 def test_type_inference8(self):
     code = """
         def invalid_multi_return(n):
             for i in n:
                 return [n]
             return {n}"""
     with self.assertRaises(pythran.types.tog.PythranTypeError):
         pythran.compile_pythrancode("dumbo", dedent(code))
Ejemplo n.º 5
0
 def test_type_inference1(self):
     code = """
         def invalid_augassign(n):
             s = n + "1"
             s += 2
             return s"""
     with self.assertRaises(pythran.types.tog.PythranTypeError):
         pythran.compile_pythrancode("dumbo", dedent(code))
Ejemplo n.º 6
0
 def test_type_inference1(self):
     code = '''
         def invalid_augassign(n):
             s = n + "1"
             s += 2
             return s'''
     with self.assertRaises(pythran.types.tog.PythranTypeError):
         pythran.compile_pythrancode("dumbo", dedent(code))
Ejemplo n.º 7
0
 def test_type_inference8(self):
     code = '''
         def invalid_multi_return(n):
             for i in n:
                 return [n]
             return {n}'''
     with self.assertRaises(pythran.types.tog.PythranTypeError):
         pythran.compile_pythrancode("dumbo", dedent(code))
Ejemplo n.º 8
0
    def test_complex_import_manipulation3(self):
        code = """
            from numpy import random
            def unsupported_module():
                return random.i_do_not_exist()"""

        with self.assertRaises(pythran.syntax.PythranSyntaxError) as ex:
            pythran.compile_pythrancode("complex_import_manipulation3", dedent(code), pyonly=True)
Ejemplo n.º 9
0
 def test_type_inference9(self):
     code = '''
         def invalid_multi_yield(n):
             for i in n:
                 yield [n]
             yield n'''
     with self.assertRaises(pythran.types.tog.PythranTypeError):
         pythran.compile_pythrancode("dumbo", dedent(code))
Ejemplo n.º 10
0
    def test_complex_import_manipulation(self):
        """
        Check correct error is returned for incorrect module manipulation.
        """
        code = """
            import math
            def unsupported_module():
                return math"""

        with self.assertRaises(pythran.syntax.PythranSyntaxError) as ex:
            pythran.compile_pythrancode("flamby", dedent(code))
Ejemplo n.º 11
0
    def test_complex_import_manipulation(self):
        """
        Check correct error is returned for incorrect module manipulation.
        """
        code = """
            import math
            def unsupported_module():
                return math"""

        with self.assertRaises(pythran.syntax.PythranSyntaxError) as ex:
            pythran.compile_pythrancode("flamby", dedent(code))
Ejemplo n.º 12
0
    def test_import_collections(self):
        """
        Check correct error is returned for incorrect module import.

        Check is done for module as .py file.
        """
        code = """
            import collections
            def unsupported_module():
                return collections.Counter()"""

        with self.assertRaises(pythran.syntax.PythranSyntaxError) as ex:
            pythran.compile_pythrancode("flamby", dedent(code), pyonly=True)

        self.assertIn("Module 'collections' not found.", str(ex.exception))
Ejemplo n.º 13
0
    def test_import_collections(self):
        """
        Check correct error is returned for incorrect module import.

        Check is done for module as .py file.
        """
        code = """
            import collections
            def unsupported_module():
                return collections.Counter()"""

        with self.assertRaises(pythran.syntax.PythranSyntaxError) as ex:
            pythran.compile_pythrancode("flamby", dedent(code))

        self.assertIn("Module 'collections' not found.", str(ex.exception))
Ejemplo n.º 14
0
    def pythran(self, line, cell):
        """
        Compile and import everything from a Pythran code cell.

        %%pythran
        #pythran export foo(int)
        def foo(x):
            return x + x
        """
        args = magic_arguments.parse_argstring(self.pythran, line)
        kwargs = {}
        if args.D:
            kwargs['define_macros'] = args.D
        if args.O:
            kwargs.setdefault('extra_compile_args', []).extend(
                '-O' + str(x) for x in args.O)
        if args.march:
            kwargs.setdefault('extra_compile_args', []).extend(
                '-march=' + str(x) for x in args.march)
        if args.fopenmp:
            kwargs.setdefault('extra_compile_args', []).append(
                '-fopenmp')
        m = hashlib.md5()
        m.update(cell)
        module_name = "pythranized_" + m.hexdigest()
        module_path = pythran.compile_pythrancode(module_name, cell, **kwargs)
        module = imp.load_dynamic(module_name, module_path)
        self._import_all(module)
Ejemplo n.º 15
0
    def test_import_collections(self):
        """
        Check correct error is returned for incorrect module import.

        Check is done for module as .py file.
        """
        code = """
            import collections
            def unsupported_module():
                return collections.Counter()"""

        with self.assertRaises(pythran.syntax.PythranSyntaxError) as ex:
            pythran.compile_pythrancode("flamby", dedent(code))

        self.assertEqual(ex.exception.message,
                         "Unpythranizable module: collections")
Ejemplo n.º 16
0
    def pythran(self, line, cell):
        """
        Compile and import everything from a Pythran code cell.

        %%pythran
        #pythran export foo(int)
        def foo(x):
            return x + x
        """
        args = magic_arguments.parse_argstring(self.pythran, line)
        kwargs = {}
        if args.D:
            kwargs['define_macros'] = args.D
        if args.O:
            kwargs.setdefault('extra_compile_args',
                              []).extend('-O' + str(x) for x in args.O)
        if args.march:
            kwargs.setdefault('extra_compile_args',
                              []).extend('-march=' + str(x)
                                         for x in args.march)
        if args.fopenmp:
            kwargs.setdefault('extra_compile_args', []).append('-fopenmp')
            kwargs.setdefault('extra_link_args', []).append('-fopenmp')
        m = hashlib.md5()
        m.update(line.encode('utf-8'))
        m.update(cell.encode('utf-8'))
        module_name = "pythranized_" + m.hexdigest()
        module_path = pythran.compile_pythrancode(module_name, cell, **kwargs)
        module = imp.load_dynamic(module_name, module_path)
        self._import_all(module)
Ejemplo n.º 17
0
    def pythran(self, line, cell):
        """
        Compile and import everything from a Pythran code cell.

        %%pythran
        #pythran export foo(int)
        def foo(x):
            return x + x
        """
        args = magic_arguments.parse_argstring(self.pythran, line)
        kwargs = {}
        if args.D:
            kwargs['define_macros'] = args.D
        for v in "OmWf":
            args_v = getattr(args, v)
            for target in ('extra_compile_args', 'extra_link_args'):
                kwargs.setdefault(target, []).extend('-{}{}'.format(v, x)
                                                     for x in args_v)

        m = hashlib.md5()
        m.update(line.encode('utf-8'))
        m.update(cell.encode('utf-8'))
        module_name = "pythranized_" + m.hexdigest()
        module_path = pythran.compile_pythrancode(module_name, cell, **kwargs)
        module = imp.load_dynamic(module_name, module_path)
        self._import_all(module)
Ejemplo n.º 18
0
    def test_import_collections(self):
        """
        Check correct error is returned for incorrect module import.

        Check is done for module as .py file.
        """
        code = """
            import collections
            def unsupported_module():
                return collections.Counter()"""

        with self.assertRaises(pythran.syntax.PythranSyntaxError) as ex:
            pythran.compile_pythrancode("flamby", dedent(code))

        self.assertEqual(str(ex.exception),
                         "Unpythranizable module: collections")
Ejemplo n.º 19
0
    def jit_pythran(self):
        import pythran
        import imp
        import hashlib
        # self._import_all(module)
        names = []
        funcs = set(vaex.dataset.expression_namespace.keys())
        expression = self.expression
        if expression in self.ds.virtual_columns:
            expression = self.ds.virtual_columns[self.expression]
        all_vars = self.ds.get_column_names(virtual=True, strings=True, hidden=True) + list(self.ds.variables.keys())
        vaex.expresso.validate_expression(expression, all_vars, funcs, names)
        names = list(set(names))
        types = ", ".join(str(self.ds.dtype(name)) + "[]" for name in names)
        argstring = ", ".join(names)
        code = '''
from numpy import *
#pythran export f({2})
def f({0}):
    return {1}'''.format(argstring, expression, types)
        print(code)
        m = hashlib.md5()
        m.update(code.encode('utf-8'))
        module_name = "pythranized_" + m.hexdigest()
        print(m.hexdigest())
        module_path = pythran.compile_pythrancode(module_name, code, extra_compile_args=["-DBOOST_SIMD", "-march=native"])
        module = imp.load_dynamic(module_name, module_path)
        function_name = "f_" + m.hexdigest()
        vaex.dataset.expression_namespace[function_name] = module.f

        return Expression(self.ds, "{0}({1})".format(function_name, argstring))
Ejemplo n.º 20
0
    def pythran(self, line, cell):
        """
        Compile and import everything from a Pythran code cell.

        %%pythran
        #pythran export foo(int)
        def foo(x):
            return x + x
        """
        args = magic_arguments.parse_argstring(self.pythran, line)
        kwargs = {}
        if args.D:
            kwargs['define_macros'] = args.D
        for v in "OmWf":
            args_v = getattr(args, v)
            for target in ('extra_compile_args', 'extra_link_args'):
                kwargs.setdefault(target, []).extend('-{}{}'.format(v, x)
                                                     for x in args_v)

        m = hashlib.md5()
        m.update(line.encode('utf-8'))
        m.update(cell.encode('utf-8'))
        module_name = "pythranized_" + m.hexdigest()
        module_path = pythran.compile_pythrancode(module_name, cell, **kwargs)
        module = imp.load_dynamic(module_name, module_path)
        self._import_all(module)
Ejemplo n.º 21
0
    def run_test_case(self, code, module_name, runas, **interface):
        """
        Test if a function call return value is equal for Pythran and Pythran.

        Args:
           code (str):  python (pythran valid) module to test.
           module_name (str): name of the compiled module
           runas (str): command line to run to check output
           interface (dict): pythran interface for the module to test.
                             Each key is the name of a function to call,
                             the value is a list of the arguments' type.

        Returns: nothing.

        Raises:
           AssertionError by 'unittest' if return value differ.
           SyntaxError if code is not python valid.
           pythran.CompileError if generated code can't be compiled.
           ...possibly others...
        """
        # Extract special keys from interface.
        if runas:
            # runas is a python code string to run the test. By convention
            # the last statement of the sequence is the value to test.
            # We insert ourselves a variable to capture this value:
            # "a=1; b=2; myfun(a+b,a-b)" => "a=1; b=2; RES=myfun(a+b,a-b)"
            runas_commands = runas.split(";")
            begin = ";".join(runas_commands[:-1])
            # this tests the runas initialisation syntax
            exec(code + "\n" + begin, {})
            last = self.TEST_RETURNVAL + '=' + runas_commands[-1]
            runas = begin + "\n" + last

        # We run test for each exported function (not for each possible
        # signature.
        for i, name in enumerate(sorted(interface.keys())):
            # If no module name was provided, create one
            modname = (module_name or ("test_" + name)) + str(i)

            # Compile the code using pythran
            cxx_compiled = compile_pythrancode(
                modname,
                code,
                interface,
                extra_compile_args=self.PYTHRAN_CXX_FLAGS)

            if not runas:
                continue

            python_ref = self.run_python(code, runas)
            pythran_res = self.run_pythran(modname, cxx_compiled, runas)

            print("Python result: ", python_ref)
            print("Pythran result: ", pythran_res)
            self.assertAlmostEqual(python_ref, pythran_res)
Ejemplo n.º 22
0
    def run_test(self, code, *params, **interface):
        """
        Test if a function call return value is equal for Pythran and Pythran.

        Args:
           code (str):  python (pythran valid) module to test.
           params (tuple): arguments to pass to the function to test.
           prelude (fct): function to call between 'code' and the c++
                          generated code
           interface (dict): pythran interface for the module to test.
                             Each key is the name of a function to call,
                             the value is a list of the arguments' type.
                             Special keys are 'prelude' and 'check_exception'.

        Returns: nothing.

        Raises:
           AssertionError by 'unittest' if return value differ.
           SyntaxError if code is not python valid.
           pythran.CompileError if generated code can't be compiled.
           ...possibly others...
        """
        # Extract special keys from interface.
        prelude = interface.pop('prelude', None)
        check_exception = interface.pop('check_exception', False)

        assert len(interface) == 1

        name = interface.keys()[0]
        modname = "test_" + name

        code = dedent(code)

        cxx_compiled = compile_pythrancode(
            modname,
            code,
            interface,
            extra_compile_args=self.PYTHRAN_CXX_FLAGS)

        # FIXME Check should be done on input parameters after function call
        python_ref = self.run_python(code, (name, copy.deepcopy(params)),
                                     prelude, check_exception)
        pythran_res = self.run_pythran(modname, cxx_compiled, (name, params),
                                       prelude, check_exception)

        if check_exception:
            if pythran_res != python_ref:
                raise AssertionError(
                    "expected exception was %s, but received %s" %
                    (python_ref, pythran_res))

        print "Python result: ", python_ref
        print "Pythran result: ", pythran_res
        self.assertAlmostEqual(python_ref, pythran_res)
Ejemplo n.º 23
0
    def run_test_case(self, code, module_name, runas, **interface):
        """
        Test if a function call return value is equal for Pythran and Pythran.

        Args:
           code (str):  python (pythran valid) module to test.
           module_name (str): name of the compiled module
           runas (str): command line to run to check output
           interface (dict): pythran interface for the module to test.
                             Each key is the name of a function to call,
                             the value is a list of the arguments' type.

        Returns: nothing.

        Raises:
           AssertionError by 'unittest' if return value differ.
           SyntaxError if code is not python valid.
           pythran.CompileError if generated code can't be compiled.
           ...possibly others...
        """
        # Extract special keys from interface.
        if runas:
            # runas is a python code string to run the test. By convention
            # the last statement of the sequence is the value to test.
            # We insert ourselves a variable to capture this value:
            # "a=1; b=2; myfun(a+b,a-b)" => "a=1; b=2; RES=myfun(a+b,a-b)"
            runas_commands = runas.split(";")
            begin = ";".join(runas_commands[:-1])
            # this tests the runas initialisation syntax
            exec code + "\n" + begin in {}
            last = self.TEST_RETURNVAL + '=' + runas_commands[-1]
            runas = begin + "\n" + last

        # We run test for each exported function (not for each possible
        # signature.
        for i, name in enumerate(sorted(interface.keys())):
            # If no module name was provided, create one
            modname = (module_name or ("test_" + name)) + str(i)

            # Compile the code using pythran
            cxx_compiled = compile_pythrancode(
                modname, code, None, extra_compile_args=self.PYTHRAN_CXX_FLAGS)

            if not runas:
                continue

            python_ref = self.run_python(code, runas)
            pythran_res = self.run_pythran(modname, cxx_compiled, runas)

            print "Python result: ", python_ref
            print "Pythran result: ", pythran_res
            self.assertAlmostEqual(python_ref, pythran_res)
Ejemplo n.º 24
0
    def pythran(self, line, cell):
        """
        Compile and import everything from a Pythran code cell

        %%pythran
        #pythran export foo(int)
        def foo(x):
            return x + x
        """
        module_name = "pythranized"
        module_path  = pythran.compile_pythrancode(module_name, cell)
        module = imp.load_dynamic(module_name, module_path)
        self._import_all(module)
Ejemplo n.º 25
0
    def jit_pythran(self, verbose=False):
        import logging
        logger = logging.getLogger('pythran')
        log_level = logger.getEffectiveLevel()
        try:
            if not verbose:
                logger.setLevel(logging.ERROR)
            import pythran
            import imp
            import hashlib
            # self._import_all(module)
            names = []
            funcs = set(vaex.dataset.expression_namespace.keys())
            expression = self.expression
            if expression in self.ds.virtual_columns:
                expression = self.ds.virtual_columns[self.expression]
            all_vars = self.ds.get_column_names(
                virtual=True, strings=True, hidden=True) + list(
                    self.ds.variables.keys())
            vaex.expresso.validate_expression(expression, all_vars, funcs,
                                              names)
            names = list(set(names))
            types = ", ".join(
                str(self.ds.dtype(name)) + "[]" for name in names)
            argstring = ", ".join(names)
            code = '''
from numpy import *
#pythran export f({2})
def f({0}):
    return {1}'''.format(argstring, expression, types)
            if verbose:
                print("generated code")
                print(code)
            m = hashlib.md5()
            m.update(code.encode('utf-8'))
            module_name = "pythranized_" + m.hexdigest()
            # print(m.hexdigest())
            module_path = pythran.compile_pythrancode(
                module_name,
                code,
                extra_compile_args=["-DBOOST_SIMD", "-march=native"] +
                [] if verbose else ["-w"])

            module = imp.load_dynamic(module_name, module_path)
            function_name = "f_" + m.hexdigest()
            vaex.dataset.expression_namespace[function_name] = module.f

            return Expression(self.ds,
                              "{0}({1})".format(function_name, argstring))
        finally:
            logger.setLevel(log_level)
Ejemplo n.º 26
0
    def run_test(self, code, *params, **interface):
        """
        Test if a function call return value is equal for Pythran and Pythran.

        Args:
           code (str):  python (pythran valid) module to test.
           params (tuple): arguments to pass to the function to test.
           prelude (fct): function to call between 'code' and the c++
                          generated code
           interface (dict): pythran interface for the module to test.
                             Each key is the name of a function to call,
                             the value is a list of the arguments' type.
                             Special keys are 'prelude' and 'check_exception'.

        Returns: nothing.

        Raises:
           AssertionError by 'unittest' if return value differ.
           SyntaxError if code is not python valid.
           pythran.CompileError if generated code can't be compiled.
           ...possibly others...
        """
        # Extract special keys from interface.
        prelude = interface.pop('prelude', None)
        check_exception = interface.pop('check_exception', False)

        assert len(interface) == 1

        name = interface.keys()[0]
        modname = "test_" + name

        code = dedent(code)

        cxx_compiled = compile_pythrancode(
            modname, code, interface, extra_compile_args=self.PYTHRAN_CXX_FLAGS)

        # FIXME Check should be done on input parameters after function call
        python_ref = self.run_python(code, (name, copy.deepcopy(params)),
                                     prelude, check_exception)
        pythran_res = self.run_pythran(modname, cxx_compiled, (name, params),
                                       prelude, check_exception)

        if check_exception:
            if pythran_res != python_ref:
                raise AssertionError(
                    "expected exception was %s, but received %s" %
                    (python_ref, pythran_res))

        print "Python result: ", python_ref
        print "Pythran result: ", pythran_res
        self.assertAlmostEqual(python_ref, pythran_res)
Ejemplo n.º 27
0
    def test_type_inference0(self):
        code = """
        def wc(content):
            d = {}

            for word in content.split():
                d[word] = d.get(word, 0) + 1

            # Use list comprehension
            l = [(freq, word) for word, freq in d.items()]

            return sorted(l)
        """
        self.run_test(code, "cha-la head cha-la", wc=[str])

        code_bis = code.replace("1", "'1'")

        with self.assertRaises(pythran.types.tog.PythranTypeError):
            pythran.compile_pythrancode("dumbo", dedent(code_bis))

        code_ter = code.replace("0", "None")

        with self.assertRaises(pythran.types.tog.PythranTypeError):
            pythran.compile_pythrancode("dumbo", dedent(code_ter))
Ejemplo n.º 28
0
    def jit_pythran(self, verbose=False):
        import logging
        logger = logging.getLogger('pythran')
        log_level = logger.getEffectiveLevel()
        try:
            if not verbose:
                logger.setLevel(logging.ERROR)
            import pythran
            import imp
            import hashlib
            # self._import_all(module)
            names = []
            funcs = set(expression_namespace.keys())
            expression = self.expression
            if expression in self.ds.virtual_columns:
                expression = self.ds.virtual_columns[self.expression]
            all_vars = self.ds.get_column_names(virtual=True, strings=True, hidden=True) + list(self.ds.variables.keys())
            vaex.expresso.validate_expression(expression, all_vars, funcs, names)
            names = list(set(names))
            types = ", ".join(str(self.ds.dtype(name)) + "[]" for name in names)
            argstring = ", ".join(names)
            code = '''
from numpy import *
#pythran export f({2})
def f({0}):
    return {1}'''.format(argstring, expression, types)
            if verbose:
                print("generated code")
                print(code)
            m = hashlib.md5()
            m.update(code.encode('utf-8'))
            module_name = "pythranized_" + m.hexdigest()
            # print(m.hexdigest())
            module_path = pythran.compile_pythrancode(module_name, code, extra_compile_args=["-DBOOST_SIMD", "-march=native"] + [] if verbose else ["-w"])

            module = imp.load_dynamic(module_name, module_path)
            function_name = "f_" + m.hexdigest()
            expression_namespace[function_name] = module.f

            return Expression(self.ds, "{0}({1})".format(function_name, argstring))
        finally:
                logger.setLevel(log_level)
Ejemplo n.º 29
0
    def pythran(self, line, cell):
        """
        Compile and import everything from a Pythran code cell.

        %%pythran
        #pythran export foo(int)
        def foo(x):
            return x + x
        """
        args = magic_arguments.parse_argstring(self.pythran, line)
        kwargs = {}
        if args.D:
            kwargs['define_macros'] = args.D
        if args.O:
            kwargs.setdefault('extra_compile_args',
                              []).extend('-O' + str(x) for x in args.O)
        if args.march:
            kwargs.setdefault('extra_compile_args',
                              []).extend('-march=' + str(x)
                                         for x in args.march)
        module_name = "pythranized"
        module_path = pythran.compile_pythrancode(module_name, cell, **kwargs)
        module = imp.load_dynamic(module_name, module_path)
        self._import_all(module)
Ejemplo n.º 30
0
 def test_type_inference10(self):
     code = '''
         def valid_augassign(l):
             l *= 0
             return l[1,2]'''
     pythran.compile_pythrancode("dumbo", dedent(code))
Ejemplo n.º 31
0
 def test_type_inference4(self):
     code = """
         def invalid_list(n):
             return [n, len(n)]"""
     with self.assertRaises(pythran.types.tog.PythranTypeError):
         pythran.compile_pythrancode("dumbo", dedent(code))
Ejemplo n.º 32
0
from inspect import getsource
import re, imp

# grab imports
imports = 'import math'
exports = '''
#pythran export growcut_python(float[][][], float[][][], float [][][], int)
'''
modname = 'growcut_pythran'

# grab the source from the original functions
sources = map(getsource,
        (growcut_python.window_floor,
            growcut_python.window_ceil,
            growcut_python.growcut_python,
            )
        )
source = '\n'.join(sources)

# compile to a native module
native = compile_pythrancode(modname,
                             '\n'.join([imports, exports, source]))

# load it
native = imp.load_dynamic(modname, native)

benchmarks = (
        ("growcut_pythran",
            native.growcut_python),
)
Ejemplo n.º 33
0
 def test_invalid_specs2(self):
     code = "#pythran export bar(int)\ndef foo(): pass"
     with self.assertRaises(pythran.syntax.PythranSyntaxError):
         pythran.compile_pythrancode("dumber", code)
Ejemplo n.º 34
0
 def test_invalid_specs0(self):
     code = "#pythran export foo()\ndef foo(n): return n"
     with self.assertRaises(pythran.syntax.PythranSyntaxError):
         pythran.compile_pythrancode("dumber", code)
Ejemplo n.º 35
0
from pythran import compile_pythrancode
from inspect import getsource
import re
import imp

# grab imports
imports = 'import numpy as np'
exports = '#pythran export pairwise_python_nested_for_loops(float[][])'
modname = 'pairwise_pythran'

# grab the source from the original function
source = getsource(pairwise_python.pairwise_python_nested_for_loops)

# a few rewriting rules to prune unsupported features
source = re.sub(r'dtype=data.dtype', 'np.double', source)
source = re.sub(r'#"omp', '"omp', source)

# compile to a native module
native = compile_pythrancode(modname,
                             '\n'.join([imports, exports, source]),
                             cxxflags=['-O2', '-fopenmp']
                             )

# load it
native = imp.load_dynamic(modname, native)

benchmarks = (
    ("pairwise_pythran_nested_for_loops",
     native.pairwise_python_nested_for_loops),
)
Ejemplo n.º 36
0
 def test_type_inference7(self):
     code = """
         def invalid_dict_value(n):
             return {1:n, 2:len(n)}"""
     with self.assertRaises(pythran.types.tog.PythranTypeError):
         pythran.compile_pythrancode("dumbo", dedent(code))
# grab imports
imports = '''
import numpy as np
from math import *
'''

exports = '''
#pythran export arc_distance_python_nested_for_loops(float [][], float [][])
'''

modname = 'arc_distance_pythran'

# grab the source from original functions
funs = (arc_distance_python.arc_distance_python_nested_for_loops,)
sources = map(getsource, funs)
source = '\n'.join(sources)

# patch
source = re.sub(r'\[a_nrows, b_nrows\]', '(a_nrows, b_nrows)', source)

# compile to native module
source = '\n'.join([exports, imports, source])
native = compile_pythrancode(modname, source)

# load
native = imp.load_dynamic(modname, native)

benchmarks = (("arc_distance_pythran_nested_for_loops",
               native.arc_distance_python_nested_for_loops),
              )
Ejemplo n.º 38
0
 def test_type_inference4(self):
     code = '''
         def invalid_list(n):
             return [n, len(n)]'''
     with self.assertRaises(pythran.types.tog.PythranTypeError):
         pythran.compile_pythrancode("dumbo", dedent(code))
Ejemplo n.º 39
0
 def test_type_inference2(self):
     code = '''
         def invalid_ifexp(n):
             return 1 if n else "1"'''
     with self.assertRaises(pythran.types.tog.PythranTypeError):
         pythran.compile_pythrancode("dumbo", dedent(code))
Ejemplo n.º 40
0
 def test_type_inference3(self):
     code = """
         def invalid_unary_op(n):
             return -(n + 'n')"""
     with self.assertRaises(pythran.types.tog.PythranTypeError):
         pythran.compile_pythrancode("dumbo", dedent(code))
Ejemplo n.º 41
0
    def test_module_bad_attribute(self):
        code = 'def module_bad_attribute(): import random as m; return m.real'

        with self.assertRaises(pythran.syntax.PythranSyntaxError):
            pythran.compile_pythrancode("dumbo", code)
Ejemplo n.º 42
0
 def test_invalid_specs3(self):
     code = '#pythran export bar(int, int?, int)\ndef bar(x, y=1, z=1): pass'
     with self.assertRaises(pythran.syntax.PythranSyntaxError):
         pythran.compile_pythrancode("dumber", code)
Ejemplo n.º 43
0
 def test_type_inference2(self):
     code = '''
         def invalid_ifexp(n):
             return 1 if n else "1"'''
     with self.assertRaises(pythran.types.tog.PythranTypeError):
         pythran.compile_pythrancode("dumbo", dedent(code))
Ejemplo n.º 44
0
    def test_undefinied_variable_in_test(self):
        code = 'def undefinied_variable_in_test(x):\n if x: print(A)'

        with self.assertRaises(pythran.syntax.PythranSyntaxError):
            pythran.compile_pythrancode("dumbo", code, pyonly=True)
Ejemplo n.º 45
0
 def test_type_inference3(self):
     code = '''
         def invalid_unary_op(n):
             return -(n + 'n')'''
     with self.assertRaises(pythran.types.tog.PythranTypeError):
         pythran.compile_pythrancode("dumbo", dedent(code))
Ejemplo n.º 46
0
    def test_immutable_default1(self):
        code = 'def immutable_default1(x={1}): pass'

        with self.assertRaises(pythran.syntax.PythranSyntaxError):
            pythran.compile_pythrancode("dumbo", code, pyonly=True)
Ejemplo n.º 47
0
 def test_type_inference7(self):
     code = '''
         def invalid_dict_value(n):
             return {1:n, 2:len(n)}'''
     with self.assertRaises(pythran.types.tog.PythranTypeError):
         pythran.compile_pythrancode("dumbo", dedent(code))
Ejemplo n.º 48
0
 def test_immutable_default4(self):
     code = 'def immutable_default4(x=None): pass'
     pythran.compile_pythrancode("dumbo", code, pyonly=True)
Ejemplo n.º 49
0
    def run_test(self, code, *params, **interface):
        """Test if a function call return value is unchanged when
        executed using python eval or compiled with pythran.

        Args:
           code (str):  python (pythran valid) module to test.
           params (tuple): arguments to pass to the function to test.
           prelude (fct): function to call between 'code' and the c++
                          generated code
           interface (dict): pythran interface for the module to test.
                             Each key is the name of a function to call,
                             the value is a list of the arguments' type.
                             Special keys are 'module_name', 'prelude',
                             'runas', 'check_refcount', 'check_exception'
                             and 'check_output'.

        Returns: nothing.

        Raises:
           AssertionError by 'unittest' if return value differ.
           SyntaxError if code is not python valid.
           pythran.CompileError if generated code can't be compiled.
           ...possibly others...
        """

        # Extract special keys from interface.
        module_name = interface.pop('module_name', None)
        prelude = interface.pop('prelude', None)
        check_output = interface.pop('check_output', True)
        runas = interface.pop('runas', None)
        check_refcount = interface.pop('check_refcount', False)
        check_exception = interface.pop('check_exception', False)
        if runas:
            # runas is a python code string to run the test. By convention
            # the last statement of the sequence is the value to test.
            # We insert ourselves a variable to capture this value:
            # "a=1; b=2; myfun(a+b,a-b)" => "a=1; b=2; RES=myfun(a+b,a-b)"
            runas_commands = runas.split(";")
            begin = ";".join(runas_commands[:-1]+[''])
            exec code+"\n"+begin in {}  # this just tests the syntax of runas
            last = self.TEST_RETURNVAL + '=' + runas_commands[-1]
            runas = begin+"\n"+last

        for name in sorted(interface.keys()):
            if not runas:
                # No runas provided, derive one from interface and params
                attributes = []
                runas = ""
                for p in params:
                    if isinstance(p, str):
                        param = "'{0}'".format(p)
                    elif isinstance(p, ndarray):
                        param = "numpy.{0}".format(
                            repr(p).replace("\n", "")
                                   .replace("dtype=", "dtype=numpy."))
                        runas = "import numpy\n"
                    else:
                         # repr preserve the "L" suffix for long
                        param = repr(p)
                    attributes.append(param.replace("nan", "float('nan')")
                                           .replace("inf", "float('inf')"))
                arglist = ",".join(attributes)
                function_call = "{0}({1})".format(name, arglist)
                runas += self.TEST_RETURNVAL + '=' + function_call

            # Caller may requires some cleaning
            prelude and prelude()

            # Produce the reference, python-way, run in an separated 'env'
            env = {'__builtin__': __import__('__builtin__')}
            refcode = code+"\n"+runas

            # Compare if exception raised in python and in pythran are the same
            python_exception_type = None
            pythran_exception_type = None
            try:
                if check_output:
                    exec refcode in env
                    python_ref = env[self.TEST_RETURNVAL]
                    if check_refcount:
                        python_refcount = sys.getrefcount(python_ref)
            except BaseException as e:
                python_exception_type = type(e)
                if not check_exception:
                    raise

            # If no module name was provided, create one
            modname = module_name or ("test_" + name)

            # Compile the code using pythran
            cxx_compiled = compile_pythrancode(modname, code,
                interface, cxxflags=self.PYTHRAN_CXX_FLAGS)

            try:
                if not check_output:
                    return

                # Caller may requires some cleaning
                prelude and prelude()
                pymod = load_dynamic(modname, cxx_compiled)

                try:
                    # Produce the pythran result, exec in the loaded module ctx
                    exec runas in pymod.__dict__
                except BaseException as e:
                    pythran_exception_type = type(e)
                else:
                    pythran_res = getattr(pymod, self.TEST_RETURNVAL)
                    if check_refcount:
                        pythran_refcount = sys.getrefcount(pythran_res)
                        self.assertEqual(python_refcount, pythran_refcount)
                    # Test Results, assert if mismatch
                    if python_exception_type:
                        raise AssertionError(
                                "expected exception was %s, but nothing happend!" %
                                python_exception_type)
                    self.compare_pythonpythran_results(python_ref, pythran_res)

            finally:
                # Clean temporary DLL
                os.remove(cxx_compiled)

            # Only compare the type of exceptions raised
            if pythran_exception_type != python_exception_type:
                if python_exception_type is None:
                    raise e
                else:
                    raise AssertionError(
                    "expected exception was %s, but received %s" %
                    (python_exception_type, pythran_exception_type))
Ejemplo n.º 50
0
 def test_immutable_default7(self):
     code = 'def g(): pass\ndef immutable_default7(x=g): pass'
     pythran.compile_pythrancode("dumbo", code, pyonly=True)
Ejemplo n.º 51
0
 def test_type_inference11(self):
     code = '''
         def valid_tuple_index(l):
             return (1, 2, 3, 4)[l]'''
     pythran.compile_pythrancode("dumbo", dedent(code))
Ejemplo n.º 52
0
 def test_invalid_specs1(self):
     code = '#pythran export boo(int)\ndef boo(): pass'
     with self.assertRaises(pythran.syntax.PythranSyntaxError):
         pythran.compile_pythrancode("dumber", code)
Ejemplo n.º 53
0
    def test_module_bad_attribute(self):
        code = 'def module_bad_attribute(): import random as m; return m.real'

        with self.assertRaises(pythran.syntax.PythranSyntaxError):
            pythran.compile_pythrancode("dumbo", code)
Ejemplo n.º 54
0
    def run_test(self, code, *params, **interface):
        """
        Test if a function call return value is equal for Pythran and Pythran.

        Args:
           code (str):  python (pythran valid) module to test.
           params (tuple): arguments to pass to the function to test.
           prelude (fct): function to call between 'code' and the c++
                          generated code
           interface (dict): pythran interface for the module to test.
                             Each key is the name of a function to call,
                             the value is a list of the arguments' type.
                             Special keys are 'prelude' and 'check_exception'.

        Returns: nothing.

        Raises:
           AssertionError by 'unittest' if return value differ.
           SyntaxError if code is not python valid.
           pythran.CompileError if generated code can't be compiled.
           ...possibly others...
        """
        # Extract special keys from interface.
        prelude = interface.pop('prelude', None)
        check_exception = interface.pop('check_exception', False)

        assert len(interface) == 1

        name = next(iter(interface.keys()))
        modname = "test_" + name

        code = dedent(code)

        if sys.version_info.major == 3:
            from tempfile import NamedTemporaryFile
            from lib2to3 import main as lib2to3

            tmp_py = NamedTemporaryFile(suffix='.py', delete=False)
            tmp_py.write(code.encode('ascii'))
            tmp_py.close()

            lib2to3.main('lib2to3.fixes', [tmp_py.name, '-w', '-n'])

            code = open(tmp_py.name).read()
            os.remove(tmp_py.name)


        cxx_compiled = compile_pythrancode(
            modname, code, interface, extra_compile_args=self.PYTHRAN_CXX_FLAGS)

        # FIXME Check should be done on input parameters after function call
        python_ref = self.run_python(code, (name, copy.deepcopy(params)),
                                     prelude, check_exception)
        pythran_res = self.run_pythran(modname, cxx_compiled, (name, params),
                                       prelude, check_exception)

        if check_exception:
            if pythran_res != python_ref:
                raise AssertionError(
                    "expected exception was %s, but received %s" %
                    (python_ref, pythran_res))

        print("Python result: ", python_ref)
        print("Pythran result: ", pythran_res)
        self.assertAlmostEqual(python_ref, pythran_res)