~ubuntu-branches/ubuntu/oneiric/python2.5/oneiric

« back to all changes in this revision

Viewing changes to Lib/test/test_with.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2008-12-21 08:57:49 UTC
  • mfrom: (1.1.9 upstream)
  • Revision ID: james.westby@ubuntu.com-20081221085749-bijjr25h8na5jdsu
Tags: 2.5.3-0ubuntu1
* New upstream version.
* Regenerate the included documentation.
* Add an option --install-layout=deb, which is ignored for 2.5.

Show diffs side-by-side

added added

removed removed

Lines of Context:
9
9
 
10
10
import sys
11
11
import unittest
 
12
import StringIO
12
13
from collections import deque
13
14
from contextlib import GeneratorContextManager, contextmanager
14
15
from test.test_support import run_unittest
504
505
 
505
506
        self.assertRaises(GeneratorExit, shouldThrow)
506
507
 
 
508
    def testErrorsInBool(self):
 
509
        # issue4589: __exit__ return code may raise an exception
 
510
        # when looking at its truth value.
 
511
 
 
512
        class cm(object):
 
513
            def __init__(self, bool_conversion):
 
514
                class Bool:
 
515
                    def __nonzero__(self):
 
516
                        return bool_conversion()
 
517
                self.exit_result = Bool()
 
518
            def __enter__(self):
 
519
                return 3
 
520
            def __exit__(self, a, b, c):
 
521
                return self.exit_result
 
522
 
 
523
        def trueAsBool():
 
524
            with cm(lambda: True):
 
525
                self.fail("Should NOT see this")
 
526
        trueAsBool()
 
527
 
 
528
        def falseAsBool():
 
529
            with cm(lambda: False):
 
530
                self.fail("Should raise")
 
531
        self.assertRaises(AssertionError, falseAsBool)
 
532
 
 
533
        def failAsBool():
 
534
            with cm(lambda: 1//0):
 
535
                self.fail("Should NOT see this")
 
536
        self.assertRaises(ZeroDivisionError, failAsBool)
 
537
 
507
538
 
508
539
class NonLocalFlowControlTestCase(unittest.TestCase):
509
540
 
625
656
            self.fail("ZeroDivisionError should have been raised")
626
657
 
627
658
 
 
659
class NewKeywordsWarningTestCase(unittest.TestCase):
 
660
 
 
661
    def check(self, code, word=None):
 
662
        save = sys.stderr
 
663
        sys.stderr = stream = StringIO.StringIO()
 
664
        try:
 
665
            compile(code, "<string>", "exec", 0, True)
 
666
        finally:
 
667
            sys.stderr = save
 
668
        if word:
 
669
            self.assert_("Warning: %r will become a reserved keyword in Python 2.6" % word
 
670
                         in stream.getvalue())
 
671
        else:
 
672
            self.assertEqual(stream.getvalue(), "")
 
673
 
 
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")
 
683
 
 
684
    def test_after_import(self):
 
685
        # issue 3936
 
686
        self.check("import sys\nas = 4", "as")
 
687
        self.check("import sys\nwith = 4", "with")
 
688
 
 
689
 
628
690
def test_main():
629
691
    run_unittest(FailureTestCase, NonexceptionalTestCase,
630
692
                 NestedNonexceptionalTestCase, ExceptionalTestCase,
631
693
                 NonLocalFlowControlTestCase,
632
694
                 AssignmentTargetTestCase,
633
 
                 ExitSwallowsExceptionTestCase)
 
695
                 ExitSwallowsExceptionTestCase,
 
696
                 NewKeywordsWarningTestCase)
634
697
 
635
698
 
636
699
if __name__ == '__main__':