505
506
self.assertRaises(GeneratorExit, shouldThrow)
508
def testErrorsInBool(self):
509
# issue4589: __exit__ return code may raise an exception
510
# when looking at its truth value.
513
def __init__(self, bool_conversion):
515
def __nonzero__(self):
516
return bool_conversion()
517
self.exit_result = Bool()
520
def __exit__(self, a, b, c):
521
return self.exit_result
524
with cm(lambda: True):
525
self.fail("Should NOT see this")
529
with cm(lambda: False):
530
self.fail("Should raise")
531
self.assertRaises(AssertionError, falseAsBool)
534
with cm(lambda: 1//0):
535
self.fail("Should NOT see this")
536
self.assertRaises(ZeroDivisionError, failAsBool)
508
539
class NonLocalFlowControlTestCase(unittest.TestCase):
625
656
self.fail("ZeroDivisionError should have been raised")
659
class NewKeywordsWarningTestCase(unittest.TestCase):
661
def check(self, code, word=None):
663
sys.stderr = stream = StringIO.StringIO()
665
compile(code, "<string>", "exec", 0, True)
669
self.assert_("Warning: %r will become a reserved keyword in Python 2.6" % word
670
in stream.getvalue())
672
self.assertEqual(stream.getvalue(), "")
674
def test_basic(self):
675
self.check("as = 4", "as")
676
self.check("with = 4", "with")
677
self.check("class as: pass", "as")
678
self.check("class with: pass", "with")
679
self.check("obj.as = 4", "as")
680
self.check("with.obj = 4", "with")
681
self.check("def with(): pass", "with")
682
self.check("do(); with = 23", "with")
684
def test_after_import(self):
686
self.check("import sys\nas = 4", "as")
687
self.check("import sys\nwith = 4", "with")
629
691
run_unittest(FailureTestCase, NonexceptionalTestCase,
630
692
NestedNonexceptionalTestCase, ExceptionalTestCase,
631
693
NonLocalFlowControlTestCase,
632
694
AssignmentTargetTestCase,
633
ExitSwallowsExceptionTestCase)
695
ExitSwallowsExceptionTestCase,
696
NewKeywordsWarningTestCase)
636
699
if __name__ == '__main__':