From a0597a2f0da5a48a96446632ebd2da54e6835c2d Mon Sep 17 00:00:00 2001 From: Thomas Holder Date: Wed, 31 Jan 2024 13:46:26 +0100 Subject: [PATCH] WIP mygetfile --- testing/testing.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/testing/testing.py b/testing/testing.py index eea78ba5c..aa45b88b3 100644 --- a/testing/testing.py +++ b/testing/testing.py @@ -367,6 +367,18 @@ def __exit__(self, exc_type, exc_value, traceback): raise AssertionError(msg) self.test.timings.append((self.msg, delta)) + def getfile(cls) -> str: + try: + return inspect.getfile(cls) + except Exception as ex: + print("inspect.getfile failed:", ex) + return _getfile_via_callable_attr(cls) + + def _getfile_via_callable_attr(cls) -> str: + attr = next(k for (k, v) in cls.__dict__.items() + if not k.startswith("_") and callable(v)) + return getattr(cls, attr).__code__.co_filename + class PyMOLTestCase(PyMOLTestCaseMeta("Base", (unittest.TestCase,), {})): ''' Common PyMOL unit tests should subclass this. @@ -387,7 +399,7 @@ def setUp(self): if cliargs.no_undo: cmd.set('suspend_undo', updates=0) - cwd = os.path.dirname(inspect.getfile(type(self))) + cwd = os.path.dirname(getfile(type(self))) os.chdir(cwd) cmd.feedback('push')