~pythonregexp2.7/python/issue2636-11

« back to all changes in this revision

Viewing changes to Lib/test/test_exceptions.py

  • Committer: Jeffrey C. "The TimeHorse" Jacobs
  • Date: 2008-09-21 17:53:26 UTC
  • mfrom: (39025.1.14 Regexp-2.7)
  • Revision ID: darklord@timehorse.com-20080921175326-92vaej2hc3yuecxb
Merged in changes from the core Regexp branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
import sys
5
5
import unittest
6
6
import pickle, cPickle
 
7
import warnings
7
8
 
8
 
from test.test_support import (TESTFN, unlink, run_unittest,
9
 
                                catch_warning)
 
9
from test.test_support import TESTFN, unlink, run_unittest, captured_output
10
10
from test.test_pep352 import ignore_message_warning
11
11
 
12
12
# XXX This is not really enough, each *operation* should be tested!
274
274
        except NameError:
275
275
            pass
276
276
 
277
 
        with catch_warning():
 
277
        with warnings.catch_warnings():
278
278
            ignore_message_warning()
279
279
            for exc, args, expected in exceptionList:
280
280
                try:
333
333
                return g()
334
334
            except ValueError:
335
335
                return -1
336
 
        self.assertRaises(RuntimeError, g)
 
336
 
 
337
        # The test prints an unraisable recursion error when
 
338
        # doing "except ValueError", this is because subclass
 
339
        # checking has recursion checking too.
 
340
        with captured_output("stderr"):
 
341
            try:
 
342
                g()
 
343
            except RuntimeError:
 
344
                pass
 
345
            except:
 
346
                self.fail("Should have raised KeyError")
 
347
            else:
 
348
                self.fail("Should have raised KeyError")
337
349
 
338
350
    def testUnicodeStrUsage(self):
339
351
        # Make sure both instances and classes have a str and unicode
342
354
        self.failUnless(unicode(Exception))
343
355
        self.failUnless(str(Exception('a')))
344
356
        self.failUnless(unicode(Exception(u'a')))
 
357
        self.failUnless(unicode(Exception(u'\xe1')))
 
358
 
 
359
    def test_badisinstance(self):
 
360
        # Bug #2542: if issubclass(e, MyException) raises an exception,
 
361
        # it should be ignored
 
362
        class Meta(type):
 
363
            def __subclasscheck__(cls, subclass):
 
364
                raise ValueError()
 
365
 
 
366
        class MyException(Exception):
 
367
            __metaclass__ = Meta
 
368
            pass
 
369
 
 
370
        with captured_output("stderr") as stderr:
 
371
            try:
 
372
                raise KeyError()
 
373
            except MyException, e:
 
374
                self.fail("exception should not be a MyException")
 
375
            except KeyError:
 
376
                pass
 
377
            except:
 
378
                self.fail("Should have raised KeyError")
 
379
            else:
 
380
                self.fail("Should have raised KeyError")
 
381
 
 
382
        with captured_output("stderr") as stderr:
 
383
            def g():
 
384
                try:
 
385
                    return g()
 
386
                except RuntimeError:
 
387
                    return sys.exc_info()
 
388
            e, v, tb = g()
 
389
            self.assert_(e is RuntimeError, e)
 
390
            self.assert_("maximum recursion depth exceeded" in str(v), v)
345
391
 
346
392
 
347
393
def test_main():