Skip to content

Commit

Permalink
Merge broken-failure-1710
Browse files Browse the repository at this point in the history
Author: exarkun
Reviewer: radix
Fixes twisted#1710

Cause Failure to synchronously raise an exception if constructed with no exception state.
Also remove the special handling of this case in the debug mode of Failure.


git-svn-id: svn://svn.twistedmatrix.com/svn/Twisted/trunk@16775 bbbe8e31-12d6-0310-92fd-ac37d47ddeeb
  • Loading branch information
exarkun committed May 12, 2006
1 parent 14de929 commit 801cb12
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
16 changes: 12 additions & 4 deletions twisted/python/failure.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ def format_frames(frames, write, detail="default"):
EXCEPTION_CAUGHT_HERE = "--- <exception caught here> ---"



class NoCurrentExceptionError(Exception):
"""
Raised when trying to create a Failure from the current interpreter
exception state and there is no current exception state.
"""



class Failure:
"""A basic abstraction for an error that has occurred.
Expand Down Expand Up @@ -101,6 +110,8 @@ def __init__(self, exc_value=None, exc_type=None, exc_tb=None):
stackOffset = 0
if exc_value is None:
self.type, self.value, tb = sys.exc_info()
if self.type is None:
raise NoCurrentExceptionError()
stackOffset = 1
elif exc_type is None:
if isinstance(exc_value, Exception):
Expand Down Expand Up @@ -366,10 +377,7 @@ def _debuginit(self, exc_value=None, exc_type=None, exc_tb=None,
Failure__init__=Failure.__init__.im_func):
if (exc_value, exc_type, exc_tb) == (None, None, None):
exc = sys.exc_info()
if exc == (None, None, None):
print "Failure created without exception, debugger will debug stack:"
import pdb; pdb.set_trace()
elif not exc[0] == self.__class__ and DO_POST_MORTEM:
if not exc[0] == self.__class__ and DO_POST_MORTEM:
print "Jumping into debugger for post-mortem of exception '%s':" % exc[1]
import pdb
pdb.post_mortem(exc[2])
Expand Down
4 changes: 2 additions & 2 deletions twisted/spread/pb.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
except ImportError:
import StringIO

import new
import sys
import types
import warnings
Expand Down Expand Up @@ -479,8 +480,7 @@ def printTraceback(self, file=None, elideFrameworkCode=0, detail='default'):
setUnjellyableForClass(CopyableFailure, CopiedFailure)

def failure2Copyable(fail, unsafeTracebacks=0):
f = CopyableFailure()
f.__dict__ = fail.__dict__
f = new.instance(CopyableFailure, fail.__dict__)
f.unsafeTracebacks = unsafeTracebacks
return f

Expand Down
10 changes: 9 additions & 1 deletion twisted/test/test_failure.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def testFailAndTrap(self):
error = f.trap(SystemExit, RuntimeError)
self.assertEquals(error, RuntimeError)
self.assertEquals(f.type, NotImplementedError)

def test_notTrapped(self):
"""Making sure trap doesn't trap what it shouldn't."""
try:
Expand Down Expand Up @@ -117,3 +117,11 @@ def testBrokenStr(self):
f.getTraceback()
except:
self.fail("getTraceback() shouldn't raise an exception")

def testConstructionFails(self):
"""
Creating a Failure with no arguments causes it to try to discover the
current interpreter exception state. If no such state exists, creating
the Failure should raise a synchronous exception.
"""
self.assertRaises(failure.NoCurrentExceptionError, failure.Failure)

0 comments on commit 801cb12

Please sign in to comment.