Saggi Mizrahi has uploaded a new change for review. Change subject: Clean up findCaller and add tests ......................................................................
Clean up findCaller and add tests Change-Id: I5153028484af6ce2651292865f1e8928e9c61409 Signed-off-by: Saggi Mizrahi <[email protected]> --- M tests/miscTests.py M vdsm/storage/misc.py 2 files changed, 42 insertions(+), 10 deletions(-) git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/67/8567/1 diff --git a/tests/miscTests.py b/tests/miscTests.py index de11e88..6339986 100644 --- a/tests/miscTests.py +++ b/tests/miscTests.py @@ -23,6 +23,7 @@ import time import threading from testrunner import VdsmTestCase as TestCaseBase +import inspect import storage.misc as misc import storage.fileUtils as fileUtils @@ -1030,3 +1031,38 @@ self.fail("Wrong exception was raised") self.fail("Exception was not raised") + + +class FindCallerTests(TestCaseBase): + def _assertFindCaller(self, callback): + frame = inspect.currentframe() + code = frame.f_code + filename = os.path.normcase(code.co_filename) + # Make sure these two lines follow each other + result = (filename, frame.f_lineno + 1, code.co_name) + self.assertEquals(result, callback()) + + def testSkipUp(self): + def _foo(): + return misc.findCaller(1) + + self._assertFindCaller(_foo) + + def testLogSkipName(self): + @misc.logskip("Freeze the Atlantic") + def _foo(): + return misc.findCaller(logSkipName="Freeze the Atlantic") + + self._assertFindCaller(_foo) + + def testMethodIgnore(self): + def _foo(): + return misc.findCaller(ignoreMethodNames=["_foo"]) + + self._assertFindCaller(_foo) + + def testNoSkip(self): + def _foo(): + return misc.findCaller() + + self.assertRaises(AssertionError, self._assertFindCaller, _foo) diff --git a/vdsm/storage/misc.py b/vdsm/storage/misc.py index 2c54ba3..8641312 100644 --- a/vdsm/storage/misc.py +++ b/vdsm/storage/misc.py @@ -53,6 +53,7 @@ import types import weakref import fcntl +import inspect sys.path.append("../") from vdsm import constants @@ -145,12 +146,7 @@ # Ignore file extension can be either py or pyc ignoreSourceFiles = ignoreSourceFiles + [logging._srcfile] ignoreSourceFiles = [os.path.splitext(sf)[0] for sf in ignoreSourceFiles] - frame = None - try: - raise Exception() - except: - # get the caller of my caller - frame = sys.exc_info()[2].tb_frame.f_back.f_back + frame = inspect.currentframe().f_back result = "(unknown file)", 0, "(unknown function)" # pop frames until you find an unfiltered one @@ -208,7 +204,7 @@ (out, err) = p.communicate(data) - if out == None: + if out is None: # Prevent splitlines() from barfing later on out = "" @@ -644,7 +640,7 @@ # Handle reacquiring lock in the same thread if currentThread in self._holdingThreads: - if self._currentState == False and exclusive: + if self._currentState is False and exclusive: raise RuntimeError("Lock promotion is forbidden.") self._holdingThreads[currentThread] += 1 @@ -1015,7 +1011,7 @@ self.__funcParent = None def __call__(self, *args, **kwargs): - if self.__funcParent == None: + if self.__funcParent is None: if (hasattr(self.__func, "func_code") and self.__func.func_code.co_varnames == 'self'): self.__funcParent = args[0].__class__.__name__ @@ -1155,7 +1151,7 @@ continue try: self._log.debug("Calling registered method `%s`", - func.func_name if hasattr(func, "func_name") \ + func.func_name if hasattr(func, "func_name") else str(func)) if self._sync: func(*args, **kwargs) -- To view, visit http://gerrit.ovirt.org/8567 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I5153028484af6ce2651292865f1e8928e9c61409 Gerrit-PatchSet: 1 Gerrit-Project: vdsm Gerrit-Branch: master Gerrit-Owner: Saggi Mizrahi <[email protected]> _______________________________________________ vdsm-patches mailing list [email protected] https://lists.fedorahosted.org/mailman/listinfo/vdsm-patches
