From ff5a77eec9d9a9391c5952305adba6ab21e97a5e Mon Sep 17 00:00:00 2001 From: "Mr. Walls" Date: Wed, 11 Sep 2024 15:14:47 -0700 Subject: [PATCH] [TESTS] Testing possible fix for PYL-C0414 (- WIP #80 -) ### ChangeLog: Changes in file tests/__init__.py: Unknown Changes Changes in file tests/context.py: Unknown Changes Changes in file tests/profiling.py: def checkpoint(self, name=''): def do_cprofile(func): def do_time_profile(func, timer_name="time_profile"): Changes in file tests/test_basic.py: Unknown Changes Changes in file tests/test_usage.py: Unknown Changes --- tests/__init__.py | 3 +- tests/context.py | 4 +-- tests/profiling.py | 88 ++++++++++++++++++++++++++++++--------------- tests/test_basic.py | 2 +- tests/test_usage.py | 8 ++--- 5 files changed, 68 insertions(+), 37 deletions(-) diff --git a/tests/__init__.py b/tests/__init__.py index 5b99d4f..33b6638 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -33,6 +33,7 @@ __module__ = """tests""" +"""This is multicast testing module.""" try: import sys @@ -86,7 +87,7 @@ sys.path.insert(0, os.path.abspath(os.path.join(_BASE_NAME, _PARENT_DIR_NAME))) if 'tests' in __file__: sys.path.insert(0, os.path.abspath(os.path.join(_BASE_NAME, _DIR_NAME))) - from tests import profiling as profiling + from tests import profiling as profiling # skipcq: PYL-C0414 from tests import test_basic from tests import test_usage diff --git a/tests/context.py b/tests/context.py index 9967df3..cb33e75 100644 --- a/tests/context.py +++ b/tests/context.py @@ -73,7 +73,7 @@ try: if 'Process' not in sys.modules: - from multiprocessing import Process as Process + from multiprocessing import Process as Process # skipcq: PYL-C0414 else: # pragma: no branch Process = sys.modules["""Process"""] except Exception: # pragma: no branch @@ -100,7 +100,7 @@ try: if 'tests.profiling' not in sys.modules: - import profiling as profiling + import tests.profiling as profiling else: # pragma: no branch profiling = sys.modules["""tests.profiling"""] except Exception: # pragma: no branch diff --git a/tests/profiling.py b/tests/profiling.py index 52a74b1..7ecb6d7 100644 --- a/tests/profiling.py +++ b/tests/profiling.py @@ -29,12 +29,20 @@ # NO ASSOCIATION +__module__ = """tests.profiling""" +"""This is pythonrepo testing module Template.""" + + try: import sys if sys.__name__ is None: # pragma: no branch raise ImportError("[CWE-758] OMG! we could not import sys! ABORT. ABORT.") -except Exception as err: # pragma: no branch - raise ImportError(err) +except Exception as badErr: # pragma: no branch + baton = ImportError(badErr, str("[CWE-758] Test module failed completely.")) + baton.module = __module__ + baton.path = __file__ + baton.__cause__ = badErr + raise baton try: @@ -42,26 +50,50 @@ import os else: # pragma: no branch os = sys.modules["""os"""] -except Exception: # pragma: no branch - raise ImportError("[CWE-758] OS Failed to import.") +except Exception as badErr: # pragma: no branch + baton = ImportError(badErr, str("[CWE-758] Test module failed completely.")) + baton.module = __module__ + baton.path = __file__ + baton.__cause__ = badErr + raise baton + + +try: + if 'functools' not in sys.modules: + import functools + else: # pragma: no branch + functools = sys.modules["""functools"""] +except Exception as badErr: # pragma: no branch + baton = ImportError(badErr, str("[CWE-758] Test module failed completely.")) + baton.module = __module__ + baton.path = __file__ + baton.__cause__ = badErr + raise baton try: import time if time.__name__ is None: # pragma: no branch raise NotImplementedError("[CWE-440] We could not import time. Are we in the speed-force!") -except Exception as err: - raise ImportError(err) - exit(3) +except Exception as badErr: # pragma: no branch + baton = ImportError(badErr, str("[CWE-758] Test module failed completely.")) + baton.module = __module__ + baton.path = __file__ + baton.__cause__ = badErr + raise baton try: - import cProfile - if cProfile.__name__ is None: # pragma: no branch - raise NotImplementedError("[CWE-440] We could not import cProfile. ABORT!") -except Exception as err: # pragma: no branch - raise ImportError(err) - exit(3) + if 'cProfile' not in sys.modules: + import cProfile + else: # pragma: no branch + cProfile = sys.modules["""cProfile"""] +except Exception as badErr: # pragma: no branch + baton = ImportError(badErr, str("[CWE-758] Test module failed completely.")) + baton.module = __module__ + baton.path = __file__ + baton.__cause__ = badErr + raise baton try: @@ -69,20 +101,18 @@ sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), str('..')))) sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), str('.')))) except Exception as ImportErr: # pragma: no branch - print(str('')) - print(str(type(ImportErr))) - print(str(ImportErr)) - print(str((ImportErr.args))) - print(str('')) - ImportErr = None - del ImportErr - raise ImportError(str("[CWE-758] Profile module failed completely.")) -except Exception: # pragma: no branch - raise ImportError("[CWE-440] Failed to import test profiling") + raise ImportError(ImportErr, str("[CWE-758] Profile module failed completely.")) +except Exception as badErr: # pragma: no branch + baton = ImportError(badErr, str("[CWE-758] Test module failed completely.")) + baton.module = __module__ + baton.path = __file__ + baton.__cause__ = badErr + raise baton class timewith(): """Basic timer for do_time_profile.""" + def __init__(self, name=''): self.name = name self.start = time.time() @@ -103,13 +133,12 @@ def checkpoint(self, name=''): def __enter__(self): return self - def __exit__(self, type, value, traceback): + def __exit__(self, type, value, traceback): # skipcq: PYL-W0622 self.checkpoint(str("finished")) - pass def do_time_profile(func, timer_name="time_profile"): - """Runs a function with a timer. + """Run a function with a timer. Time Testing: @@ -136,12 +165,11 @@ def do_time_profile(func, timer_name="time_profile"): work...took ... seconds >>> - """ - import functools + """ @functools.wraps(func) def timer_profile_func(*args, **kwargs): - """Wraps a function in timewith()""" + """Wraps a function in timewith() function.""" theOutput = None with timewith(timer_name) as timer: timer.checkpoint(str("Start Timer")) @@ -183,7 +211,9 @@ def do_cprofile(func): """ + @functools.wraps(func) def profiled_func(*args, **kwargs): + """Wraps a function in profile.enable/disable() functions.""" profile = cProfile.Profile() try: profile.enable() diff --git a/tests/test_basic.py b/tests/test_basic.py index bf4b699..c533b47 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -29,7 +29,7 @@ if context.__name__ is None: raise ImportError("[CWE-758] Failed to import context") else: - from context import unittest as unittest + from context import unittest from context import sys as _sys except Exception: # pragma: no branch raise ImportError("[CWE-758] Failed to import test context") diff --git a/tests/test_usage.py b/tests/test_usage.py index d960d26..dc86f93 100644 --- a/tests/test_usage.py +++ b/tests/test_usage.py @@ -64,10 +64,10 @@ if context.__name__ is None: raise ImportError("[CWE-758] Failed to import context") else: - from context import multicast as multicast - from context import unittest as unittest - from context import subprocess as subprocess - from context import Process as Process + from context import multicast as multicast # skipcq: PYL-C0414 + from context import unittest + from context import subprocess + from context import Process except Exception: raise ImportError("[CWE-758] Failed to import test context")