def _importAndCheckStack(importName): """ Import the given name as a module, then walk the stack to determine whether the failure was the module not existing, or some code in the module (for example a dependent import) failing. This can be helpful to determine whether any actual application code was run. For example, to distiguish administrative error (entering the wrong module name), from programmer error (writing buggy code in a module that fails to import). @raise Exception: if something bad happens. This can be any type of exception, since nobody knows what loading some arbitrary code might do. @raise _NoModuleFound: if no module was found. """ try: try: return __import__(importName) except ImportError: excType, excValue, excTraceback = sys.exc_info() while excTraceback: execName = excTraceback.tb_frame.f_globals["__name__"] if (execName is None or # python 2.4+, post-cleanup execName == importName): # python 2.3, no cleanup reraise(excValue, excTraceback) excTraceback = excTraceback.tb_next raise _NoModuleFound() except: # Necessary for cleaning up modules in 2.3. sys.modules.pop(importName, None) raise
def _importAndCheckStack(importName): """ Import the given name as a module, then walk the stack to determine whether the failure was the module not existing, or some code in the module (for example a dependent import) failing. This can be helpful to determine whether any actual application code was run. For example, to distiguish administrative error (entering the wrong module name), from programmer error (writing buggy code in a module that fails to import). @param importName: The name of the module to import. @type importName: C{str} @raise Exception: if something bad happens. This can be any type of exception, since nobody knows what loading some arbitrary code might do. @raise _NoModuleFound: if no module was found. """ try: return __import__(importName) except ImportError: excType, excValue, excTraceback = sys.exc_info() while excTraceback: execName = excTraceback.tb_frame.f_globals["__name__"] # in Python 2 execName is None when an ImportError is encountered, # where in Python 3 execName is equal to the importName. if execName is None or execName == importName: reraise(excValue, excTraceback) excTraceback = excTraceback.tb_next raise _NoModuleFound()
def _runWithConnection(self, func, *args, **kw): conn = self.connectionFactory(self) try: result = func(conn, *args, **kw) conn.commit() return result except: excType, excValue, excTraceback = sys.exc_info() try: conn.rollback() except: log.err(None, "Rollback failed") compat.reraise(excValue, excTraceback)
def _runWithConnection(self, func, *args, **kw): conn = self.connectionFactory(self) try: result = func(conn, *args, **kw) conn.commit() return result except BaseException: excType, excValue, excTraceback = sys.exc_info() try: conn.rollback() except BaseException: log.err(None, "Rollback failed") compat.reraise(excValue, excTraceback)
def _runInteraction(self, interaction, *args, **kw): conn = self.connectionFactory(self) trans = self.transactionFactory(self, conn) try: result = interaction(trans, *args, **kw) trans.close() conn.commit() return result except BaseException: excType, excValue, excTraceback = sys.exc_info() try: conn.rollback() except BaseException: log.err(None, "Rollback failed") compat.reraise(excValue, excTraceback)
def _runInteraction(self, interaction, *args, **kw): conn = self.connectionFactory(self) trans = self.transactionFactory(self, conn) try: result = interaction(trans, *args, **kw) trans.close() conn.commit() return result except: excType, excValue, excTraceback = sys.exc_info() try: conn.rollback() except: log.err(None, "Rollback failed") compat.reraise(excValue, excTraceback)
def convertError(sourceType, targetType): """ Convert an error into a different error type. @param sourceType: The type of exception that should be caught and converted. @type sourceType: L{Exception} @param targetType: The type of exception to which the original should be converted. @type targetType: L{Exception} """ try: yield None except sourceType: compat.reraise(targetType(), sys.exc_info()[-1])
def test_reraiseWithTraceback(self): """ Calling L{reraise} with an exception instance and a traceback re-raises the exception with the given traceback. """ try: 1 / 0 except: typ, value, tb = sys.exc_info() try: reraise(value, tb) except: typ2, value2, tb2 = sys.exc_info() self.assertEqual(typ2, ZeroDivisionError) self.assertIs(value, value2) self.assertEqual(traceback.format_tb(tb)[-1], traceback.format_tb(tb2)[-1]) else: self.fail("The exception was not raised.")
def test_reraiseWithTraceback(self): """ Calling L{reraise} with an exception instance and a traceback re-raises the exception with the given traceback. """ try: 1/0 except: typ, value, tb = sys.exc_info() try: reraise(value, tb) except: typ2, value2, tb2 = sys.exc_info() self.assertEqual(typ2, ZeroDivisionError) self.assertIs(value, value2) self.assertEqual(traceback.format_tb(tb)[-1], traceback.format_tb(tb2)[-1]) else: self.fail("The exception was not raised.")
def test_reraiseWithNone(self): """ Calling L{reraise} with an exception instance and a traceback of C{None} re-raises it with a new traceback. """ try: 1/0 except: typ, value, tb = sys.exc_info() try: reraise(value, None) except: typ2, value2, tb2 = sys.exc_info() self.assertEqual(typ2, ZeroDivisionError) self.assertTrue(value is value2) self.assertNotEqual(traceback.format_tb(tb)[-1], traceback.format_tb(tb2)[-1]) else: self.fail("The exception was not raised.")
def runWithWarningsSuppressed(suppressedWarnings, f, *a, **kw): """Run the function C{f}, but with some warnings suppressed. @param suppressedWarnings: A list of arguments to pass to filterwarnings. Must be a sequence of 2-tuples (args, kwargs). @param f: A callable, followed by its arguments and keyword arguments """ for args, kwargs in suppressedWarnings: warnings.filterwarnings(*args, **kwargs) addedFilters = warnings.filters[: len(suppressedWarnings)] try: result = f(*a, **kw) except: exc_info = sys.exc_info() _resetWarningFilters(None, addedFilters) reraise(exc_info[1], exc_info[2]) else: if isinstance(result, defer.Deferred): result.addBoth(_resetWarningFilters, addedFilters) else: _resetWarningFilters(None, addedFilters) return result
def runWithWarningsSuppressed(suppressedWarnings, f, *a, **kw): """Run the function C{f}, but with some warnings suppressed. @param suppressedWarnings: A list of arguments to pass to filterwarnings. Must be a sequence of 2-tuples (args, kwargs). @param f: A callable, followed by its arguments and keyword arguments """ for args, kwargs in suppressedWarnings: warnings.filterwarnings(*args, **kwargs) addedFilters = warnings.filters[:len(suppressedWarnings)] try: result = f(*a, **kw) except: exc_info = sys.exc_info() _resetWarningFilters(None, addedFilters) reraise(exc_info[1], exc_info[2]) else: if isinstance(result, defer.Deferred): result.addBoth(_resetWarningFilters, addedFilters) else: _resetWarningFilters(None, addedFilters) return result
def _runInteraction(self, interaction, *args, **kw): """ Interact with the database and return the result. :param interaction: A callable object whose first argument is an L{adbapi.Transaction}. :type interaction: callable :return: a Deferred which will fire the return value of 'interaction(Transaction(...), *args, **kw)', or a Failure. :rtype: twisted.internet.defer.Deferred """ tid = self.threadID() u1db = self._u1dbconnections.get(tid) conn = self.connectionFactory( self, init_u1db=not bool(u1db)) if self.replica_uid is None: replica_uid = conn._u1db._real_replica_uid setProxiedObject(self.replica_uid, replica_uid) if u1db is None: self._u1dbconnections[tid] = conn._u1db else: conn._u1db = u1db trans = self.transactionFactory(self, conn) try: result = interaction(trans, *args, **kw) trans.close() conn.commit() return result except Exception: excType, excValue, excTraceback = sys.exc_info() try: conn.rollback() except Exception: logger.error(None, "Rollback failed") compat.reraise(excValue, excTraceback)
def findObject(name): """Get a fully-named package, module, module-global object or attribute. Forked from twisted.python.reflect.namedAny. Returns a tuple of (bool, obj). If bool is True, the named object exists and is returned as obj. If bool is False, the named object does not exist and the value of obj is unspecified. """ names = name.split('.') topLevelPackage = None moduleNames = names[:] while not topLevelPackage: trialname = '.'.join(moduleNames) if len(trialname) == 0: return (False, None) try: topLevelPackage = __import__(trialname) except ImportError: # if the ImportError happened in the module being imported, # this is a failure that should be handed to our caller. # count stack frames to tell the difference. exc_info = sys.exc_info() if len(traceback.extract_tb(exc_info[2])) > 1: try: # Clean up garbage left in sys.modules. del sys.modules[trialname] except KeyError: # Python 2.4 has fixed this. Yay! pass reraise(exc_info[1], exc_info[2]) moduleNames.pop() obj = topLevelPackage for n in names[1:]: try: obj = getattr(obj, n) except AttributeError: return (False, obj) return (True, obj)
def _runInteraction(self, interaction, *args, **kw): """ Interact with the database and return the result. :param interaction: A callable object whose first argument is an L{adbapi.Transaction}. :type interaction: callable :return: a Deferred which will fire the return value of 'interaction(Transaction(...), *args, **kw)', or a Failure. :rtype: twisted.internet.defer.Deferred """ tid = self.threadID() u1db = self._u1dbconnections.get(tid) conn = self.connectionFactory(self, init_u1db=not bool(u1db)) if self.replica_uid is None: replica_uid = conn._u1db._real_replica_uid setProxiedObject(self.replica_uid, replica_uid) if u1db is None: self._u1dbconnections[tid] = conn._u1db else: conn._u1db = u1db trans = self.transactionFactory(self, conn) try: result = interaction(trans, *args, **kw) trans.close() conn.commit() return result except Exception: excType, excValue, excTraceback = sys.exc_info() try: conn.rollback() except Exception: logger.error(None, "Rollback failed") compat.reraise(excValue, excTraceback)
def startResponse(self, status, headers, excInfo=None): """ The WSGI I{start_response} callable. The given values are saved until they are needed to generate the response. This will be called in a non-I/O thread. """ if self.started and excInfo is not None: reraise(excInfo[1], excInfo[2]) # PEP-3333 mandates that status should be a native string. In practice # this is mandated by Twisted's HTTP implementation too, so we enforce # on both Python 2 and Python 3. if not isinstance(status, str): raise TypeError("status must be str, not %r (%s)" % (status, type(status).__name__)) # PEP-3333 mandates that headers should be a plain list, but in # practice we work with any sequence type and only warn when it's not # a plain list. if isinstance(headers, list): pass # This is okay. elif isinstance(headers, Sequence): warn("headers should be a list, not %r (%s)" % (headers, type(headers).__name__), category=RuntimeWarning) else: raise TypeError("headers must be a list, not %r (%s)" % (headers, type(headers).__name__)) # PEP-3333 mandates that each header should be a (str, str) tuple, but # in practice we work with any sequence type and only warn when it's # not a plain list. for header in headers: if isinstance(header, tuple): pass # This is okay. elif isinstance(header, Sequence): warn("header should be a (str, str) tuple, not %r (%s)" % (header, type(header).__name__), category=RuntimeWarning) else: raise TypeError( "header must be a (str, str) tuple, not %r (%s)" % (header, type(header).__name__)) # However, the sequence MUST contain only 2 elements. if len(header) != 2: raise TypeError("header must be a (str, str) tuple, not %r" % (header, )) # Both elements MUST be native strings. Non-native strings will be # rejected by the underlying HTTP machinery in any case, but we # reject them here in order to provide a more informative error. for elem in header: if not isinstance(elem, str): raise TypeError("header must be (str, str) tuple, not %r" % (header, )) self.status = status self.headers = headers return self.write
def startResponse(self, status, headers, excInfo=None): """ The WSGI I{start_response} callable. The given values are saved until they are needed to generate the response. This will be called in a non-I/O thread. """ if self.started and excInfo is not None: reraise(excInfo[1], excInfo[2]) # PEP-3333 mandates that status should be a native string. In practice # this is mandated by Twisted's HTTP implementation too, so we enforce # on both Python 2 and Python 3. if not isinstance(status, str): raise TypeError( "status must be str, not %r (%s)" % (status, type(status).__name__)) # PEP-3333 mandates that headers should be a plain list, but in # practice we work with any sequence type and only warn when it's not # a plain list. if isinstance(headers, list): pass # This is okay. elif isinstance(headers, Sequence): warn("headers should be a list, not %r (%s)" % ( headers, type(headers).__name__), category=RuntimeWarning) else: raise TypeError( "headers must be a list, not %r (%s)" % (headers, type(headers).__name__)) # PEP-3333 mandates that each header should be a (str, str) tuple, but # in practice we work with any sequence type and only warn when it's # not a plain list. for header in headers: if isinstance(header, tuple): pass # This is okay. elif isinstance(header, Sequence): warn("header should be a (str, str) tuple, not %r (%s)" % ( header, type(header).__name__), category=RuntimeWarning) else: raise TypeError( "header must be a (str, str) tuple, not %r (%s)" % (header, type(header).__name__)) # However, the sequence MUST contain only 2 elements. if len(header) != 2: raise TypeError( "header must be a (str, str) tuple, not %r" % (header, )) # Both elements MUST be native strings. Non-native strings will be # rejected by the underlying HTTP machinery in any case, but we # reject them here in order to provide a more informative error. for elem in header: if not isinstance(elem, str): raise TypeError( "header must be (str, str) tuple, not %r" % (header, )) self.status = status self.headers = headers return self.write