def save_modules(): """ Context in which imported modules are saved. Translates exceptions internal to the context into the equivalent exception outside the context. """ saved = sys.modules.copy() try: try: yield saved except: # dump any exception class_, exc, tb = sys.exc_info() saved_cls = pickle.dumps(class_) saved_exc = pickle.dumps(exc) raise finally: sys.modules.update(saved) # remove any modules imported since del_modules = ( mod_name for mod_name in sys.modules if mod_name not in saved # exclude any encodings modules. See #285 and not mod_name.startswith('encodings.') ) _clear_modules(del_modules) except: # reload and re-raise any exception, using restored modules class_, exc, tb = sys.exc_info() new_cls = pickle.loads(saved_cls) new_exc = pickle.loads(saved_exc) compat.reraise(new_cls, new_exc, tb)
def save_modules(): """ Context in which imported modules are saved. Translates exceptions internal to the context into the equivalent exception outside the context. """ saved = sys.modules.copy() try: try: yield saved except: # dump any exception class_, exc, tb = sys.exc_info() saved_cls = pickle.dumps(class_) saved_exc = pickle.dumps(exc) raise finally: sys.modules.update(saved) # remove any modules imported since del_modules = ( mod_name for mod_name in sys.modules if mod_name not in saved # exclude any encodings modules. See #285 and not mod_name.startswith('encodings.')) _clear_modules(del_modules) except: # reload and re-raise any exception, using restored modules class_, exc, tb = sys.exc_info() new_cls = pickle.loads(saved_cls) new_exc = pickle.loads(saved_exc) compat.reraise(new_cls, new_exc, tb)
def resume(self): "restore and re-raise any exception" if '_saved' not in vars(self): return type, exc = map(pickle.loads, self._saved) compat.reraise(type, exc, self._tb)
def prepareTest(self, test): """Prepare and determine test ordering""" all_tests = {} for t in test: for tt in t: if tt.context is None: continue try: if isinstance(tt, ContextSuite): # MethodTestCase all_tests[tt.context.__name__] = self.prepare_suite(tt) setattr(all_tests[tt.context.__name__], 'nosedep_run', True) else: # FunctionTestCase all_tests[tt.test.test.__name__] = tt except AttributeError: # This exception is confusing - reraise the original one reraise(tt.test.exc_class, tt.test.exc_val, tt.test.tb) return self.orderTests(all_tests, test)
def prepare_tests_on_levels(self, test, all_tests): """Find test level of ContextSuite object prepare_tests_on_levels() is a recursive function use to find the parent directory of a test or group of tests. An attempt is made to iterate through the presented level of a ContextSuite class object. If the iteration is successful, the iterated level of ContextSuite is passed to prepare_tests_on_levels() for additional processing. If a TypeError is generated, the exception fails back to the previous prepare_tests_on_levels() call. The TypeError exception processes this ContextSuite level as the parent level of tests. """ for t in test: try: self.prepare_tests_on_levels(t, all_tests) except TypeError: if isinstance(test, ContextSuite): # MethodTestCase all_tests[test.context.__name__] = self.prepare_suite(test) setattr(all_tests[test.context.__name__], 'nosedep_run', True) else: # FunctionTestCase all_tests[test.test.test.__name__] = test break except AttributeError: # This exception is confusing - reraise the original one reraise(test.test.exc_class, test.test.exc_val, test.test.tb) return all_tests