Skip to content

Commit

Permalink
WIP mygetfile
Browse files Browse the repository at this point in the history
  • Loading branch information
speleo3 committed May 6, 2024
1 parent d1db3d3 commit 59e2882
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion testing/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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')
Expand Down

0 comments on commit 59e2882

Please sign in to comment.