Kaydet (Commit) 1f517e1d authored tarafından Ezio Melotti's avatar Ezio Melotti

#7092: Silence py3k warnings in test_exceptions and test_pep352. Patch by Florent Xicluna.

üst 8d3f130d
...@@ -7,7 +7,7 @@ import pickle, cPickle ...@@ -7,7 +7,7 @@ import pickle, cPickle
import warnings import warnings
from test.test_support import TESTFN, unlink, run_unittest, captured_output from test.test_support import TESTFN, unlink, run_unittest, captured_output
from test.test_pep352 import ignore_message_warning from test.test_pep352 import ignore_deprecation_warnings
# XXX This is not really enough, each *operation* should be tested! # XXX This is not really enough, each *operation* should be tested!
...@@ -17,6 +17,7 @@ class ExceptionTests(unittest.TestCase): ...@@ -17,6 +17,7 @@ class ExceptionTests(unittest.TestCase):
# Reloading the built-in exceptions module failed prior to Py2.2, while it # Reloading the built-in exceptions module failed prior to Py2.2, while it
# should act the same as reloading built-in sys. # should act the same as reloading built-in sys.
try: try:
from imp import reload
import exceptions import exceptions
reload(exceptions) reload(exceptions)
except ImportError, e: except ImportError, e:
...@@ -108,11 +109,11 @@ class ExceptionTests(unittest.TestCase): ...@@ -108,11 +109,11 @@ class ExceptionTests(unittest.TestCase):
self.assertRaises(ValueError, chr, 10000) self.assertRaises(ValueError, chr, 10000)
self.raise_catch(ZeroDivisionError, "ZeroDivisionError") self.raise_catch(ZeroDivisionError, "ZeroDivisionError")
try: x = 1/0 try: x = 1 // 0
except ZeroDivisionError: pass except ZeroDivisionError: pass
self.raise_catch(Exception, "Exception") self.raise_catch(Exception, "Exception")
try: x = 1/0 try: x = 1 // 0
except Exception, e: pass except Exception, e: pass
def testSyntaxErrorMessage(self): def testSyntaxErrorMessage(self):
...@@ -197,6 +198,7 @@ class ExceptionTests(unittest.TestCase): ...@@ -197,6 +198,7 @@ class ExceptionTests(unittest.TestCase):
self.assertEqual(WindowsError(1001, "message").errno, 22) self.assertEqual(WindowsError(1001, "message").errno, 22)
self.assertEqual(WindowsError(1001, "message").winerror, 1001) self.assertEqual(WindowsError(1001, "message").winerror, 1001)
@ignore_deprecation_warnings
def testAttributes(self): def testAttributes(self):
# test that exception attributes are happy # test that exception attributes are happy
...@@ -274,34 +276,32 @@ class ExceptionTests(unittest.TestCase): ...@@ -274,34 +276,32 @@ class ExceptionTests(unittest.TestCase):
except NameError: except NameError:
pass pass
with warnings.catch_warnings(): for exc, args, expected in exceptionList:
ignore_message_warning() try:
for exc, args, expected in exceptionList: raise exc(*args)
try: except BaseException, e:
raise exc(*args) if type(e) is not exc:
except BaseException, e: raise
if type(e) is not exc: # Verify module name
raise self.assertEquals(type(e).__module__, 'exceptions')
# Verify module name # Verify no ref leaks in Exc_str()
self.assertEquals(type(e).__module__, 'exceptions') s = str(e)
# Verify no ref leaks in Exc_str() for checkArgName in expected:
s = str(e) self.assertEquals(repr(getattr(e, checkArgName)),
for checkArgName in expected: repr(expected[checkArgName]),
self.assertEquals(repr(getattr(e, checkArgName)), 'exception "%s", attribute "%s"' %
repr(expected[checkArgName]), (repr(e), checkArgName))
'exception "%s", attribute "%s"' %
(repr(e), checkArgName)) # test for pickling support
for p in pickle, cPickle:
# test for pickling support for protocol in range(p.HIGHEST_PROTOCOL + 1):
for p in pickle, cPickle: new = p.loads(p.dumps(e, protocol))
for protocol in range(p.HIGHEST_PROTOCOL + 1): for checkArgName in expected:
new = p.loads(p.dumps(e, protocol)) got = repr(getattr(new, checkArgName))
for checkArgName in expected: want = repr(expected[checkArgName])
got = repr(getattr(new, checkArgName)) self.assertEquals(got, want,
want = repr(expected[checkArgName]) 'pickled "%r", attribute "%s"' %
self.assertEquals(got, want, (e, checkArgName))
'pickled "%r", attribute "%s"' %
(e, checkArgName))
def testDeprecatedMessageAttribute(self): def testDeprecatedMessageAttribute(self):
...@@ -331,6 +331,7 @@ class ExceptionTests(unittest.TestCase): ...@@ -331,6 +331,7 @@ class ExceptionTests(unittest.TestCase):
with self.assertRaises(AttributeError): with self.assertRaises(AttributeError):
exc.message exc.message
@ignore_deprecation_warnings
def testPickleMessageAttribute(self): def testPickleMessageAttribute(self):
# Pickling with message attribute must work, as well. # Pickling with message attribute must work, as well.
e = Exception("foo") e = Exception("foo")
...@@ -338,18 +339,18 @@ class ExceptionTests(unittest.TestCase): ...@@ -338,18 +339,18 @@ class ExceptionTests(unittest.TestCase):
f.message = "bar" f.message = "bar"
for p in pickle, cPickle: for p in pickle, cPickle:
ep = p.loads(p.dumps(e)) ep = p.loads(p.dumps(e))
with warnings.catch_warnings(): self.assertEqual(ep.message, "foo")
ignore_message_warning()
self.assertEqual(ep.message, "foo")
fp = p.loads(p.dumps(f)) fp = p.loads(p.dumps(f))
self.assertEqual(fp.message, "bar") self.assertEqual(fp.message, "bar")
@ignore_deprecation_warnings
def testSlicing(self): def testSlicing(self):
# Test that you can slice an exception directly instead of requiring # Test that you can slice an exception directly instead of requiring
# going through the 'args' attribute. # going through the 'args' attribute.
args = (1, 2, 3) args = (1, 2, 3)
exc = BaseException(*args) exc = BaseException(*args)
self.assertEqual(exc[:], args) self.assertEqual(exc[:], args)
self.assertEqual(exc.args[:], args)
def testKeywordArgs(self): def testKeywordArgs(self):
# test that builtin exception don't take keyword args, # test that builtin exception don't take keyword args,
......
...@@ -4,14 +4,27 @@ import exceptions ...@@ -4,14 +4,27 @@ import exceptions
import warnings import warnings
from test.test_support import run_unittest from test.test_support import run_unittest
import os import os
import sys
from platform import system as platform_system from platform import system as platform_system
def ignore_message_warning(): DEPRECATION_WARNINGS = ["BaseException.message has been deprecated"]
"""Ignore the DeprecationWarning for BaseException.message."""
warnings.resetwarnings()
warnings.filterwarnings("ignore", "BaseException.message",
DeprecationWarning)
if sys.py3kwarning:
DEPRECATION_WARNINGS.extend(
["exceptions must derive from BaseException",
"catching classes that don't inherit from BaseException is not allowed",
"__get(item|slice)__ not supported for exception classes"])
# Silence Py3k and other deprecation warnings
def ignore_deprecation_warnings(func):
"""Ignore the known DeprecationWarnings."""
def wrapper(*args, **kw):
with warnings.catch_warnings():
warnings.resetwarnings()
for text in DEPRECATION_WARNINGS:
warnings.filterwarnings("ignore", text, DeprecationWarning)
return func(*args, **kw)
return wrapper
class ExceptionClassTests(unittest.TestCase): class ExceptionClassTests(unittest.TestCase):
...@@ -21,13 +34,11 @@ class ExceptionClassTests(unittest.TestCase): ...@@ -21,13 +34,11 @@ class ExceptionClassTests(unittest.TestCase):
def test_builtins_new_style(self): def test_builtins_new_style(self):
self.assertTrue(issubclass(Exception, object)) self.assertTrue(issubclass(Exception, object))
@ignore_deprecation_warnings
def verify_instance_interface(self, ins): def verify_instance_interface(self, ins):
with warnings.catch_warnings(): for attr in ("args", "message", "__str__", "__repr__", "__getitem__"):
ignore_message_warning() self.assertTrue(hasattr(ins, attr),
for attr in ("args", "message", "__str__", "__repr__", "%s missing %s attribute" %
"__getitem__"):
self.assertTrue(hasattr(ins, attr),
"%s missing %s attribute" %
(ins.__class__.__name__, attr)) (ins.__class__.__name__, attr))
def test_inheritance(self): def test_inheritance(self):
...@@ -91,43 +102,39 @@ class ExceptionClassTests(unittest.TestCase): ...@@ -91,43 +102,39 @@ class ExceptionClassTests(unittest.TestCase):
self.assertEqual(given, expected, "%s: %s != %s" % (test_name, self.assertEqual(given, expected, "%s: %s != %s" % (test_name,
given, expected)) given, expected))
@ignore_deprecation_warnings
def test_interface_single_arg(self): def test_interface_single_arg(self):
# Make sure interface works properly when given a single argument # Make sure interface works properly when given a single argument
arg = "spam" arg = "spam"
exc = Exception(arg) exc = Exception(arg)
with warnings.catch_warnings(): results = ([len(exc.args), 1], [exc.args[0], arg], [exc.message, arg],
ignore_message_warning() [str(exc), str(arg)], [unicode(exc), unicode(arg)],
results = ([len(exc.args), 1], [exc.args[0], arg], [repr(exc), exc.__class__.__name__ + repr(exc.args)],
[exc.message, arg], [exc[0], arg])
[str(exc), str(arg)], [unicode(exc), unicode(arg)], self.interface_test_driver(results)
[repr(exc), exc.__class__.__name__ + repr(exc.args)], [exc[0],
arg])
self.interface_test_driver(results)
@ignore_deprecation_warnings
def test_interface_multi_arg(self): def test_interface_multi_arg(self):
# Make sure interface correct when multiple arguments given # Make sure interface correct when multiple arguments given
arg_count = 3 arg_count = 3
args = tuple(range(arg_count)) args = tuple(range(arg_count))
exc = Exception(*args) exc = Exception(*args)
with warnings.catch_warnings(): results = ([len(exc.args), arg_count], [exc.args, args],
ignore_message_warning() [exc.message, ''], [str(exc), str(args)],
results = ([len(exc.args), arg_count], [exc.args, args], [unicode(exc), unicode(args)],
[exc.message, ''], [str(exc), str(args)], [repr(exc), exc.__class__.__name__ + repr(exc.args)],
[unicode(exc), unicode(args)], [exc[-1], args[-1]])
[repr(exc), exc.__class__.__name__ + repr(exc.args)], self.interface_test_driver(results)
[exc[-1], args[-1]])
self.interface_test_driver(results)
@ignore_deprecation_warnings
def test_interface_no_arg(self): def test_interface_no_arg(self):
# Make sure that with no args that interface is correct # Make sure that with no args that interface is correct
exc = Exception() exc = Exception()
with warnings.catch_warnings(): results = ([len(exc.args), 0], [exc.args, tuple()],
ignore_message_warning() [exc.message, ''],
results = ([len(exc.args), 0], [exc.args, tuple()], [str(exc), ''], [unicode(exc), u''],
[exc.message, ''], [repr(exc), exc.__class__.__name__ + '()'], [True, True])
[str(exc), ''], [unicode(exc), u''], self.interface_test_driver(results)
[repr(exc), exc.__class__.__name__ + '()'], [True, True])
self.interface_test_driver(results)
def test_message_deprecation(self): def test_message_deprecation(self):
...@@ -179,6 +186,7 @@ class UsageTests(unittest.TestCase): ...@@ -179,6 +186,7 @@ class UsageTests(unittest.TestCase):
self.fail("TypeError expected when catching %s as specified in a " self.fail("TypeError expected when catching %s as specified in a "
"tuple" % type(object_)) "tuple" % type(object_))
@ignore_deprecation_warnings
def test_raise_classic(self): def test_raise_classic(self):
# Raising a classic class is okay (for now). # Raising a classic class is okay (for now).
class ClassicClass: class ClassicClass:
...@@ -194,7 +202,7 @@ class UsageTests(unittest.TestCase): ...@@ -194,7 +202,7 @@ class UsageTests(unittest.TestCase):
except ClassicClass: except ClassicClass:
pass pass
except: except:
self.fail("unable to raise class class instance") self.fail("unable to raise classic class instance")
def test_raise_new_style_non_exception(self): def test_raise_new_style_non_exception(self):
# You cannot raise a new-style class that does not inherit from # You cannot raise a new-style class that does not inherit from
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment