mirror of
https://github.com/python/cpython.git
synced 2025-11-25 21:11:09 +00:00
Make test.test_support.catch_warnings more robust as discussed on python-dev. Also add explicit tests for itto test_warnings.
This commit is contained in:
parent
3d0b9f095a
commit
38469e2719
6 changed files with 127 additions and 73 deletions
|
|
@ -26,41 +26,30 @@ class TestPy3KWarnings(unittest.TestCase):
|
|||
with catch_warning() as w:
|
||||
safe_exec("True = False")
|
||||
self.assertWarning(None, w, expected)
|
||||
with catch_warning() as w:
|
||||
safe_exec("False = True")
|
||||
self.assertWarning(None, w, expected)
|
||||
with catch_warning() as w:
|
||||
try:
|
||||
safe_exec("obj.False = True")
|
||||
except NameError: pass
|
||||
self.assertWarning(None, w, expected)
|
||||
with catch_warning() as w:
|
||||
try:
|
||||
safe_exec("obj.True = False")
|
||||
except NameError: pass
|
||||
self.assertWarning(None, w, expected)
|
||||
with catch_warning() as w:
|
||||
safe_exec("def False(): pass")
|
||||
self.assertWarning(None, w, expected)
|
||||
with catch_warning() as w:
|
||||
safe_exec("def True(): pass")
|
||||
self.assertWarning(None, w, expected)
|
||||
with catch_warning() as w:
|
||||
safe_exec("class False: pass")
|
||||
self.assertWarning(None, w, expected)
|
||||
with catch_warning() as w:
|
||||
safe_exec("class True: pass")
|
||||
self.assertWarning(None, w, expected)
|
||||
with catch_warning() as w:
|
||||
safe_exec("def f(True=43): pass")
|
||||
self.assertWarning(None, w, expected)
|
||||
with catch_warning() as w:
|
||||
safe_exec("def f(False=None): pass")
|
||||
self.assertWarning(None, w, expected)
|
||||
with catch_warning() as w:
|
||||
safe_exec("f(False=True)")
|
||||
self.assertWarning(None, w, expected)
|
||||
with catch_warning() as w:
|
||||
safe_exec("f(True=1)")
|
||||
self.assertWarning(None, w, expected)
|
||||
|
||||
|
|
@ -69,25 +58,20 @@ class TestPy3KWarnings(unittest.TestCase):
|
|||
expected = 'type inequality comparisons not supported in 3.x'
|
||||
with catch_warning() as w:
|
||||
self.assertWarning(int < str, w, expected)
|
||||
with catch_warning() as w:
|
||||
self.assertWarning(type < object, w, expected)
|
||||
|
||||
def test_object_inequality_comparisons(self):
|
||||
expected = 'comparing unequal types not supported in 3.x'
|
||||
with catch_warning() as w:
|
||||
self.assertWarning(str < [], w, expected)
|
||||
with catch_warning() as w:
|
||||
self.assertWarning(object() < (1, 2), w, expected)
|
||||
|
||||
def test_dict_inequality_comparisons(self):
|
||||
expected = 'dict inequality comparisons not supported in 3.x'
|
||||
with catch_warning() as w:
|
||||
self.assertWarning({} < {2:3}, w, expected)
|
||||
with catch_warning() as w:
|
||||
self.assertWarning({} <= {}, w, expected)
|
||||
with catch_warning() as w:
|
||||
self.assertWarning({} > {2:3}, w, expected)
|
||||
with catch_warning() as w:
|
||||
self.assertWarning({2:3} >= {}, w, expected)
|
||||
|
||||
def test_cell_inequality_comparisons(self):
|
||||
|
|
@ -100,7 +84,6 @@ class TestPy3KWarnings(unittest.TestCase):
|
|||
cell1, = f(1).func_closure
|
||||
with catch_warning() as w:
|
||||
self.assertWarning(cell0 == cell1, w, expected)
|
||||
with catch_warning() as w:
|
||||
self.assertWarning(cell0 < cell1, w, expected)
|
||||
|
||||
def test_code_inequality_comparisons(self):
|
||||
|
|
@ -111,11 +94,8 @@ class TestPy3KWarnings(unittest.TestCase):
|
|||
pass
|
||||
with catch_warning() as w:
|
||||
self.assertWarning(f.func_code < g.func_code, w, expected)
|
||||
with catch_warning() as w:
|
||||
self.assertWarning(f.func_code <= g.func_code, w, expected)
|
||||
with catch_warning() as w:
|
||||
self.assertWarning(f.func_code >= g.func_code, w, expected)
|
||||
with catch_warning() as w:
|
||||
self.assertWarning(f.func_code > g.func_code, w, expected)
|
||||
|
||||
def test_builtin_function_or_method_comparisons(self):
|
||||
|
|
@ -125,11 +105,8 @@ class TestPy3KWarnings(unittest.TestCase):
|
|||
meth = {}.get
|
||||
with catch_warning() as w:
|
||||
self.assertWarning(func < meth, w, expected)
|
||||
with catch_warning() as w:
|
||||
self.assertWarning(func > meth, w, expected)
|
||||
with catch_warning() as w:
|
||||
self.assertWarning(meth <= func, w, expected)
|
||||
with catch_warning() as w:
|
||||
self.assertWarning(meth >= func, w, expected)
|
||||
|
||||
def assertWarning(self, _, warning, expected_message):
|
||||
|
|
@ -142,11 +119,8 @@ class TestPy3KWarnings(unittest.TestCase):
|
|||
|
||||
with catch_warning() as w:
|
||||
self.assertWarning(lst.sort(cmp=cmp), w, expected)
|
||||
with catch_warning() as w:
|
||||
self.assertWarning(sorted(lst, cmp=cmp), w, expected)
|
||||
with catch_warning() as w:
|
||||
self.assertWarning(lst.sort(cmp), w, expected)
|
||||
with catch_warning() as w:
|
||||
self.assertWarning(sorted(lst, cmp), w, expected)
|
||||
|
||||
def test_sys_exc_clear(self):
|
||||
|
|
@ -229,7 +203,7 @@ class TestStdlibRemovals(unittest.TestCase):
|
|||
"""Make sure the specified module, when imported, raises a
|
||||
DeprecationWarning and specifies itself in the message."""
|
||||
with CleanImport(module_name):
|
||||
with catch_warning(record=False) as w:
|
||||
with catch_warning(record=False):
|
||||
warnings.filterwarnings("error", ".+ removed",
|
||||
DeprecationWarning)
|
||||
try:
|
||||
|
|
@ -290,7 +264,7 @@ class TestStdlibRemovals(unittest.TestCase):
|
|||
|
||||
|
||||
def test_main():
|
||||
with catch_warning(record=True):
|
||||
with catch_warning():
|
||||
warnings.simplefilter("always")
|
||||
run_unittest(TestPy3KWarnings,
|
||||
TestStdlibRemovals)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue