'When an error happens during the print of the stats, C{sys.stdout}
should be restored to its initial value.'

def test_profilePrintStatsError(self):

    class ErroneousProfile(profile.Profile, ):
        def print_stats(self):
            raise RuntimeError('Boom')
    self.patch(profile, 'Profile', ErroneousProfile)
    config = twistd.ServerOptions()
    config['profile'] = self.mktemp()
    config['profiler'] = 'profile'
    profiler = app.AppProfiler(config)
    reactor = DummyReactor()
    oldStdout = sys.stdout
    self.assertRaises(RuntimeError, profiler.run, reactor)
    self.assertIs(sys.stdout, oldStdout)
