Ticket #5795: startdebugmode.patch

File startdebugmode.patch, 7.9 KB (added by Julian Berman, 8 years ago)
  • twisted/python/failure.py

    diff --git a/twisted/python/failure.py b/twisted/python/failure.py
    index ce9b7fb..5c7ffd3 100644
    a b class Failure: 
    354354                        postMortem(exc[2])
    355355                    except bdb.BdbQuit:
    356356                        pass
    357                     except Exception:
    358                         print(
    359                             "Debugging with %r failed. Falling back to pdb." %
    360                             (postMortem,))
    361 
    362                         pdb.post_mortem(exc[2])
    363357
    364358            self._realInit(exc_value, exc_type, exc_tb, captureVars)
    365359
  • twisted/test/test_failure.py

    diff --git a/twisted/test/test_failure.py b/twisted/test/test_failure.py
    index b55507b..731324b 100644
    a b class TestDebugMode(SynchronousTestCase): 
    835835        self.assertEqual(self.debugger.mortems, [])
    836836
    837837
    838     def test_postMortemRaisedException(self):
    839         """
    840         If L{Failure.startDebugMode} is called with a post mortem function that
    841         raised an exception, it falls back to L{pdb.post_mortem}.
    842         """
    843         self.patch(pdb, "post_mortem", self.debugger.post_mortem)
    844         failure.startDebugMode(object())
    845 
    846         try:
    847             raise ZeroDivisionError()
    848         except:
    849             _, _, tb = sys.exc_info()
    850             f = failure.Failure()
    851         self.assertEqual(self.debugger.mortems, [tb])
    852 
    853 
    854838    def test_stopDebugMode(self):
     839        """
     840        If L{Failure.stopDebugMode} is called, an L{Failure} no longer calls
     841        the debugger for an exception.
     842        """
    855843        failure.startDebugMode(self.debugger.post_mortem)
    856844        failure.stopDebugMode()
    857845
  • twisted/topfiles/5795.feature

    diff --git a/twisted/topfiles/5795.feature b/twisted/topfiles/5795.feature
    index 7c99f32..69fce6c 100644
    a b  
    1 failure.startDebugMode now takes a debugger to drop into, and respects --debugger as provided on the command line.
     1failure.startDebugMode now takes a debugger to drop into, which trial --debugger will provide
  • twisted/trial/test/test_runner.py

    diff --git a/twisted/trial/test/test_runner.py b/twisted/trial/test/test_runner.py
    index 1dc5fb7..6f6049f 100644
    a b from twisted.scripts import trial 
    1717from twisted.plugins import twisted_trial
    1818from twisted import plugin
    1919from twisted.internet import defer
     20from twisted.python.test.modules_helpers import TwistedModulesMixin
    2021
    2122
    2223pyunit = __import__('unittest')
    class CapturingDebugger(object): 
    3738
    3839
    3940    def runcall(self, fn, *args, **kwargs):
    40         self.calls.append((args, kwargs))
     41        self.calls.append("runcall")
    4142        fn(*args, **kwargs)
    4243
    4344
    44     @classmethod
    45     def createAndCleanup(cls, test, attr):
    46         """
    47         Create a debugger at the given C{attr} and remove it after the test.
    48 
    49         Exists because command line arguments like C{--debugger} will not have
    50         access to the instance, they need to find a debugger by fully qualified
    51         name on the class.
    52 
    53         @param test: a test case instance where the debugger will live
    54         @param attr: a L{str} which is the name of the attribute to set and
    55             unset after the test has run. The attribute will be set on the
    56             I{class} of the given test case instance.
    57 
    58         """
    59 
    60         debugger = cls()
    61         setattr(test.__class__, attr, debugger)
    62         test.addCleanup(delattr, test.__class__, attr)
    63 
    64 
    6545
    6646class CapturingReporter(object):
    6747    """
    class PyUnitDryRunTest(DryRunMixin, unittest.SynchronousTestCase): 
    308288
    309289
    310290
    311 class TestRunner(unittest.SynchronousTestCase):
     291class TestRunner(TwistedModulesMixin, unittest.SynchronousTestCase):
    312292    def setUp(self):
    313293        self.config = trial.Options()
    314294        # whitebox hack a reporter in, because plugins are CACHED and will
    class TestRunner(unittest.SynchronousTestCase): 
    577557        Trial uses specified debugger if the debugger is available.
    578558        """
    579559
    580         CapturingDebugger.createAndCleanup(self, "capturingDebugger")
    581 
     560        self.replaceSysModules(dict(sys.modules, dbg=CapturingDebugger()))
    582561        self.parseOptions([
    583562            '--reporter', 'capturing',
    584563            '--debugger',
    585             'twisted.trial.test.test_runner.TestRunner.capturingDebugger',
     564            'dbg',
    586565            '--debug',
    587566            'twisted.trial.test.sample',
    588567        ])
    589         my_runner = self.getRunner()
    590         result = self.runSampleSuite(my_runner)
     568        myRunner = self.getRunner()
     569        result = self.runSampleSuite(myRunner)
    591570        self.assertEqual(self.standardReport, result._calls)
    592         self.assertEqual(['runcall'], my_runner.debugger.calls)
     571        self.assertEqual(['runcall'], myRunner.debugger.calls)
    593572
    594573
    595574
  • twisted/trial/test/test_script.py

    diff --git a/twisted/trial/test/test_script.py b/twisted/trial/test/test_script.py
    index a93674e..f427fdf 100644
    a b from twisted.python import failure, util 
    1616from twisted.python.usage import UsageError
    1717from twisted.python.filepath import FilePath
    1818
     19from twisted.python.test.modules_helpers import TwistedModulesMixin
    1920from twisted.trial.test.test_loader import testNames
    2021from twisted.trial.test.test_runner import CapturingDebugger
    2122
    class MakeRunnerTestCase(unittest.TestCase): 
    576577
    577578
    578579
    579 class TestRun(unittest.TestCase):
     580class TestRun(TwistedModulesMixin, unittest.TestCase):
    580581    """
    581582    Tests for the L{run} function.
    582583    """
    class TestRun(unittest.TestCase): 
    596597
    597598        """
    598599
    599         CapturingDebugger.createAndCleanup(self, "capturingDebugger")
     600        debugger = CapturingDebugger()
     601        self.replaceSysModules(dict(sys.modules, capturingDebugger=debugger))
    600602
    601         self.argv.extend(
    602             [
    603                 "--debug",
    604                 "--debugger",
    605                 "twisted.trial.test.test_script.TestRun.capturingDebugger",
    606             ],
    607         )
     603        self.argv.extend(["--debug", "--debugger", "capturingDebugger"])
    608604
    609605        def recordPostMortem(postMortem):
    610606            self.postMortem = postMortem
    class TestRun(unittest.TestCase): 
    615611        except SystemExit:
    616612            pass
    617613
    618         self.assertEqual(self.postMortem, self.capturingDebugger.post_mortem)
     614        self.assertEqual(self.postMortem, debugger.post_mortem)
    619615
    620616
    621617    def test_noPostMortemMethod(self):
    class TestRun(unittest.TestCase): 
    625621
    626622        """
    627623
    628         CapturingDebugger.createAndCleanup(self, "capturingDebugger")
    629         postMortem = CapturingDebugger.post_mortem
    630         del CapturingDebugger.post_mortem
    631         self.addCleanup(setattr, CapturingDebugger, "post_mortem", postMortem)
     624        class DebuggerWithoutPM(object):
     625            def runcall(self, *args, **kwargs):
     626                pass
    632627
    633         self.argv.extend(
    634             [
    635                 "--debug",
    636                 "--debugger",
    637                 "twisted.trial.test.test_script.TestRun.capturingDebugger",
    638             ],
    639         )
     628        self.replaceSysModules(dict(sys.modules, debugger=DebuggerWithoutPM()))
     629
     630        self.argv.extend(["--debug", "--debugger", "debugger"])
    640631
    641632        def recordPostMortem(postMortem):
    642633            self.postMortem = postMortem
    class TestRun(unittest.TestCase): 
    658649
    659650        """
    660651
    661         CapturingDebugger.createAndCleanup(self, "capturingDebugger")
     652        self.replaceSysModules(dict(sys.modules, debugger=CapturingDebugger()))
    662653
    663         self.argv.extend(
    664             [
    665                 "--debug",
    666                 "--debugger",
    667                 "twisted.trial.test.test_script.TestRun.capturingDebugger",
    668                 "--nopm",
    669             ],
    670         )
     654        self.argv.extend(["--debug", "--debugger", "debugger", "--nopm"])
    671655
    672656        def startDebugMode(postMortem):
    673657            self.fail("startDebugMode should not have been called!")