Пример #1
0
    def test_class_method_simple(self):
        """
        Make sure the function works on class instance methods.
        """

        from io import StringIO
        dummy = StringIO()

        class TestClass():

            # noinspection PyMethodMayBeStatic
            def testfunc(self, arg):
                foo = float(arg)
                bar = str(foo)
                baz = len(bar)
                print(baz, file=dummy)
                return baz

            def __len__(self):
                return 1

        t = TestClass()
        expected = t.testfunc
        result = _make_constant_globals(expected, self.test_env)
        expected_consts = (None, 'file', float, str, len, print)

        self.check_co_consts(result, expected_consts)

        # make sure it works
        call_result = t.testfunc(12)
        exp = 4
        self.assertEqual(call_result, exp)
        self.assertEqual(str(call_result) + '\n', dummy.getvalue())
Пример #2
0
def time_testfoo2():
    from timeit import Timer
    from pysrc.optimize.optimize_constants import _make_constant_globals
    import builtins
    env = vars(builtins).copy()
    env.update(globals())
    before = Timer(testfoo2).timeit

    after = Timer(_make_constant_globals(testfoo2, env)).timeit

    before_total = after_total = 0

    from itertools import count
    n = 3
    for i in count(1):

        bresult = before(n)
        aresult = after(n)

        before_total += bresult
        after_total += aresult

        print()
        print("Before:", bresult, "After:", aresult)
        print("Before Total", before_total / i, "After Total:", after_total / i)
        print()
Пример #3
0
    def test_simple_buitins2(self):

        def testfunc2(dummy):
            dummy_len = len(dummy)
            print(dummy_len)

        expected = (None, len, print)
        result_func = _make_constant_globals(testfunc2, self.test_env)

        self.assertIsNotNone(result_func, "Forgot to return function!")
        self.assertIsNot(result_func, testfunc2, "Function not modified")
        self.check_co_consts(result_func, expected, 'testfunc2')
Пример #4
0
    def test_make_constants_smooth1(self):
        """
        import the unittest for smooth1. Run them once to verify passing,
        then modify the module namespace with modified function, and run again.

        This is probably not the best way to do this, but oh well.

        @return: None
        @rtype: None
        """

        from pysrc.test.test_smooth import test_smooth

        ref_smooth = test_smooth.smooth1
        new_smooth = _make_constant_globals(ref_smooth, self.test_env)

        if new_smooth is ref_smooth:
            raise self.failureException("Function not modified- unexpected failure")

        test_case = test_smooth.Smooth()

        tests = (
            test_case.test_smooth1_1,
            test_case.test_smooth1_2,
            test_case.test_smooth1_real1
        )

        # Catch and re-raise each exception on failure for
        # accurate error reporting

        # first pass- assert all the other tests actually pass.
        # even though its not our fault, silently ignoring errors
        # could result in false positives.

        for test in tests:
            try:
                test()
            except self.failureException:
                raise self.failureException("Unrelated test unexpectedly failed") from None
            except:
                raise self.failureException("Unknown error from unrelated test") from None

        # Second pass- modify and see if they still pass

        for test in tests:

            test_smooth.smooth1 = new_smooth
            try:
                test()
            except self.failureException:
                # restore ref_smooth in case future tests added to run
                test_smooth.smooth1 = ref_smooth
                raise self.failureException("Mismatch result after modification in test %s" % test.__name__)
Пример #5
0
    def test_simple_store_global(self):
        """
        Test a function with STORE_GLOBAL opcodes
        """

        def test_globals():
            global foo
            print(foo)
            foo = 1

        expected = test_globals
        result = _make_constant_globals(expected, self.test_env)

        self.assertIs(expected, result, "Function incorrectly modified")
Пример #6
0
    def test_simple_builtins1(self):
        """
        @return: None
        @rtype: None
        """

        def testfunc1(dummy):
            print(len(dummy))

        expected = (None, print, len)
        result_func = _make_constant_globals(testfunc1, self.test_env)

        self.assertIsNotNone(result_func, "Forgot to return function!")
        self.assertIsNot(result_func, testfunc1, "Function not modified")
        self.check_co_consts(result_func, expected, "testfunc1")
Пример #7
0
    def test_kw_defaults(self):
        """ There was some bug I found where running a function
        with kw-only defaults (scripts.old_cli.plot) through the
        make_constants function resulted in a "keyword only arg
        requires argument" error upon calling the function without
        explicitly specifying the error.
        """

        src = """
def func(*, kwarg="hello world"):
    print(kwarg)
"""
        ns = {}
        exec(src, ns, ns)

        func = ns['func']
        func()

        after = _make_constant_globals(func, {'print' : print})
        after()