<commit_before>""" Base file for all Pythran tests. """

from imp import load_dynamic
from numpy import float32, float64, complex128
try:
    from numpy import float128, complex256
except ImportError:
    float128 = float64
    complex256 = complex128

from numpy import int32, int64, uint32, uint64, number, bool_
from numpy import ndarray, isnan, isinf, isneginf, complex128, complex64
from textwrap import dedent
from threading import Thread
import copy
import math
import glob
import numpy.testing as npt
import os
import numbers
import sys
import unittest
from functools import reduce
from operator import add

import pytest

from pythran import compile_pythrancode, spec_parser, load_specfile, frontend
from pythran.backend import Python
from pythran.middlend import refine
from pythran.passmanager import PassManager
from pythran.toolchain import _parse_optimization
from pythran.spec import Spec


def may_convert_2_to_3(code):
    """Convert code from 2 to 3 if run with Python3, nothing otherwise."""
    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('utf8'))
        tmp_py.close()

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

        with open(tmp_py.name) as fd:
            code = fd.read()
        os.remove(tmp_py.name)
    return code


def harmonize_containers(value):
    if isinstance(value, list):
        def flatten(l, flat):
            for e in l:
                if isinstance(e, list):
                    flatten(e, flat)
                else:
                    flat.append(e)
        flat = []
        flatten(value, flat)

        if not flat:
            return

        if any(not isinstance(v, numbers.Number) for v in flat):
            return


        common_type = type(reduce(add, flat))

        def rec_visit_values(l):
            for i, e in enumerate(l):
                if isinstance(e, list):
                    rec_visit_values(e)
                else:
                    l[i] = common_type(e)

        rec_visit_values(value)


class TestEnv(unittest.TestCase):

    """ Test environment to validate a pythran execution against python. """

    module = pytest.mark.module
    # default options used for the c++ compiler
    PYTHRAN_CXX_FLAGS = ['-O0', '-Wall', '-Wno-unknown-pragmas',
                         '-Wno-mismatched-tags', '-Wno-unused-local-typedefs',
                         '-Wno-unknown-warning-option', '-Werror', '-UNDEBUG'] if sys.platform != "win32" else ['/w']

    TEST_RETURNVAL = "TEST_RETURNVAL"

    def check_type(self, ref, res):
        """ Check if type between reference and result match. """
        print("Type of Pythran res : ", type(res))
        print("Type of Python ref : ", type(ref))

        # cope with subtle situation under Windows where numpy.uint32 have same
        # name but different value
        if type(res).__name__ == type(ref).__name__:
            return

        intn = int64 if sys.maxsize > 2**32 else int32

        if isinstance(ref, ndarray):
            # res can be an ndarray of dim 0 because of isneginf call
            if ref.ndim == 0 and (not isinstance(res, ndarray)
                                  or res.ndim != 0):
                self.check_type(ref.item(0), res)
            else:
                self.assertIsInstance(res, type(ref))
        elif isinstance(ref, (int, intn)):
            self.assertIsInstance(res, (int, intn))
        elif isinstance(ref, (float, float64)):
            self.assertIsInstance(res, (float, float64))
        elif isinstance(ref, (complex, complex128)):
            self.assertIsInstance(res, (complex, complex128))
        elif isinstance(ref, (bool, bool_)):
            self.assertIsInstance(res, (bool, bool_))
        elif sys.version_info[0] == 2 and isinstance(ref, long):
            self.assertIsInstance(res, (int, int64, long))
        elif isinstance(res, int):
            self.assertIsInstance(res, (int, intn))<commit_msg>Fix int/int32/intc testing<commit_after>""" Base file for all Pythran tests. """

from imp import load_dynamic
from numpy import float32, float64, complex128
try:
    from numpy import float128, complex256
except ImportError:
    float128 = float64
    complex256 = complex128

from numpy import intp, intc, number, bool_
from numpy import ndarray, isnan, isinf, isneginf, complex128, complex64
from textwrap import dedent
from threading import Thread
import copy
import math
import glob
import numpy.testing as npt
import os
import numbers
import sys
import unittest
from functools import reduce
from operator import add

import pytest

from pythran import compile_pythrancode, spec_parser, load_specfile, frontend
from pythran.backend import Python
from pythran.middlend import refine
from pythran.passmanager import PassManager
from pythran.toolchain import _parse_optimization
from pythran.spec import Spec


def may_convert_2_to_3(code):
    """Convert code from 2 to 3 if run with Python3, nothing otherwise."""
    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('utf8'))
        tmp_py.close()

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

        with open(tmp_py.name) as fd:
            code = fd.read()
        os.remove(tmp_py.name)
    return code


def harmonize_containers(value):
    if isinstance(value, list):
        def flatten(l, flat):
            for e in l:
                if isinstance(e, list):
                    flatten(e, flat)
                else:
                    flat.append(e)
        flat = []
        flatten(value, flat)

        if not flat:
            return

        if any(not isinstance(v, numbers.Number) for v in flat):
            return


        common_type = type(reduce(add, flat))

        def rec_visit_values(l):
            for i, e in enumerate(l):
                if isinstance(e, list):
                    rec_visit_values(e)
                else:
                    l[i] = common_type(e)

        rec_visit_values(value)


class TestEnv(unittest.TestCase):

    """ Test environment to validate a pythran execution against python. """

    module = pytest.mark.module
    # default options used for the c++ compiler
    PYTHRAN_CXX_FLAGS = ['-O0', '-Wall', '-Wno-unknown-pragmas',
                         '-Wno-mismatched-tags', '-Wno-unused-local-typedefs',
                         '-Wno-unknown-warning-option', '-Werror', '-UNDEBUG'] if sys.platform != "win32" else ['/w']

    TEST_RETURNVAL = "TEST_RETURNVAL"

    def check_type(self, ref, res):
        """ Check if type between reference and result match. """
        print("Type of Pythran res : ", type(res))
        print("Type of Python ref : ", type(ref))

        # cope with subtle situation under Windows where numpy.uint32 have same
        # name but different value
        if type(res).__name__ == type(ref).__name__:
            return

        if isinstance(ref, ndarray):
            # res can be an ndarray of dim 0 because of isneginf call
            if ref.ndim == 0 and (not isinstance(res, ndarray)
                                  or res.ndim != 0):
                self.check_type(ref.item(0), res)
            else:
                self.assertIsInstance(res, type(ref))
        elif isinstance(ref, (float, float64)):
            self.assertIsInstance(res, (float, float64))
        elif isinstance(ref, (complex, complex128)):
            self.assertIsInstance(res, (complex, complex128))
        elif isinstance(ref, (bool, bool_)):
            self.assertIsInstance(res, (bool, bool_))
        elif hasattr(ref, 'dtype'):
            if hasattr(res, 'dtype'):
                self.assertEqual(ref.dtype.itemsize, res.dtype.itemsize)
                self.assertEqual(ref.dtype.type(-1), res.dtype.type(-1))
            else:
                self.assertIsInstance(res, int)
        elif sys.version_info[0] == 2 and isinstance(ref, long):
            self.assertIsInstance(res, (int, intc, intp, long))
{'fraction_non_alphanumeric': 0.09818181818181818, 'fraction_numerical': 0.018303030303030304, 'mean_word_length': 2.3323909531502425, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5799473', 'n_tokens_mistral': 2511, 'n_tokens_neox': 2364, 'n_words': 723}