diff options
author | Florent Xicluna <florent.xicluna@gmail.com> | 2010-04-02 18:52:12 +0000 |
---|---|---|
committer | Florent Xicluna <florent.xicluna@gmail.com> | 2010-04-02 18:52:12 +0000 |
commit | 41fe6155394edf0ef65d125b27ceb8d1e37f72d3 (patch) | |
tree | 68af315f367bba93ba4d7d370e755bccd79c047f /Lib | |
parent | 1bfd0cc5d40018a6dd9f279d6d7acb829c76cc8f (diff) | |
download | cpython-git-41fe6155394edf0ef65d125b27ceb8d1e37f72d3.tar.gz |
(partially)
Merged revisions 79534,79537,79539,79558,79606 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r79534 | florent.xicluna | 2010-03-31 23:21:54 +0200 (mer, 31 mar 2010) | 2 lines
Fix test for xml.etree when using a non-ascii path. And use check_warnings instead of catch_warnings.
........
r79537 | florent.xicluna | 2010-03-31 23:40:32 +0200 (mer, 31 mar 2010) | 2 lines
Fix typo
........
r79539 | florent.xicluna | 2010-04-01 00:01:03 +0200 (jeu, 01 avr 2010) | 2 lines
Replace catch_warnings with check_warnings when it makes sense. Use assertRaises context manager to simplify some tests.
........
r79558 | florent.xicluna | 2010-04-01 20:17:09 +0200 (jeu, 01 avr 2010) | 2 lines
#7092: Fix some -3 warnings, and fix Lib/platform.py when the path contains a double-quote.
........
r79606 | florent.xicluna | 2010-04-02 19:26:42 +0200 (ven, 02 avr 2010) | 2 lines
Backport some robotparser test and skip the test if the external resource is not available.
........
Diffstat (limited to 'Lib')
-rwxr-xr-x | Lib/platform.py | 2 | ||||
-rw-r--r-- | Lib/test/test___all__.py | 6 | ||||
-rw-r--r-- | Lib/test/test_argparse.py | 22 | ||||
-rw-r--r-- | Lib/test/test_complex.py | 7 | ||||
-rw-r--r-- | Lib/test/test_contextlib.py | 41 | ||||
-rw-r--r-- | Lib/test/test_descr.py | 1 | ||||
-rw-r--r-- | Lib/test/test_doctest.py | 1 | ||||
-rw-r--r-- | Lib/test/test_global.py | 5 | ||||
-rw-r--r-- | Lib/test/test_hmac.py | 10 | ||||
-rw-r--r-- | Lib/test/test_io.py | 1 | ||||
-rw-r--r-- | Lib/test/test_robotparser.py | 12 | ||||
-rw-r--r-- | Lib/test/test_sundry.py | 4 | ||||
-rw-r--r-- | Lib/test/test_xml_etree.py | 41 |
13 files changed, 59 insertions, 94 deletions
diff --git a/Lib/platform.py b/Lib/platform.py index aa256dca92..1601d590e0 100755 --- a/Lib/platform.py +++ b/Lib/platform.py @@ -942,7 +942,7 @@ def _syscmd_file(target,default=''): if sys.platform in ('dos','win32','win16','os2'): # XXX Others too ? return default - target = _follow_symlinks(target) + target = _follow_symlinks(target).replace('"', '\\"') try: f = os.popen('file "%s" 2> %s' % (target, DEV_NULL)) except (AttributeError,os.error): diff --git a/Lib/test/test___all__.py b/Lib/test/test___all__.py index 200db5cfa2..695669ae22 100644 --- a/Lib/test/test___all__.py +++ b/Lib/test/test___all__.py @@ -2,7 +2,6 @@ import unittest from test import support import os import sys -import warnings class NoAll(RuntimeError): @@ -16,9 +15,8 @@ class AllTest(unittest.TestCase): def check_all(self, modname): names = {} - with warnings.catch_warnings(): - warnings.filterwarnings("ignore", ".* (module|package)", - DeprecationWarning) + with support.check_warnings((".* (module|package)", + DeprecationWarning), quiet=True): try: exec("import %s" % modname, names) except: diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py index a140f1cd9f..85513a5139 100644 --- a/Lib/test/test_argparse.py +++ b/Lib/test/test_argparse.py @@ -7,7 +7,6 @@ import sys import textwrap import tempfile import unittest -import warnings import argparse from io import StringIO @@ -4150,21 +4149,12 @@ class TestImportStar(TestCase): self.assertTrue(hasattr(argparse, name)) def test_main(): - with warnings.catch_warnings(): - # silence warnings about version argument - these are expected - warnings.filterwarnings( - action='ignore', - message='The "version" argument to ArgumentParser is deprecated.', - category=DeprecationWarning) - warnings.filterwarnings( - action='ignore', - message='The format_version method is deprecated', - category=DeprecationWarning) - warnings.filterwarnings( - action='ignore', - message='The print_version method is deprecated', - category=DeprecationWarning) - + # silence warnings about version argument - these are expected + with support.check_warnings( + ('The "version" argument to ArgumentParser is deprecated.', + DeprecationWarning), + ('The (format|print)_version method is deprecated', + DeprecationWarning)): support.run_unittest(__name__) # Remove global references to avoid looking like we have refleaks. RFile.seen = {} diff --git a/Lib/test/test_complex.py b/Lib/test/test_complex.py index a857354954..6441208d80 100644 --- a/Lib/test/test_complex.py +++ b/Lib/test/test_complex.py @@ -1,4 +1,4 @@ -import unittest, os +import unittest from test import support from random import random @@ -395,10 +395,7 @@ class ComplexTest(unittest.TestCase): finally: if (fo is not None) and (not fo.closed): fo.close() - try: - os.remove(support.TESTFN) - except (OSError, IOError): - pass + support.unlink(support.TESTFN) def test_getnewargs(self): self.assertEqual((1+2j).__getnewargs__(), (1.0, 2.0)) diff --git a/Lib/test/test_contextlib.py b/Lib/test/test_contextlib.py index 1d3bd90c58..c733bcafc7 100644 --- a/Lib/test/test_contextlib.py +++ b/Lib/test/test_contextlib.py @@ -1,7 +1,5 @@ """Unit tests for contextlib.py, and other context managers.""" - -import os import sys import tempfile import unittest @@ -9,6 +7,7 @@ import threading from contextlib import * # Tests __all__ from test import support + class ContextManagerTestCase(unittest.TestCase): def test_contextmanager_plain(self): @@ -33,16 +32,12 @@ class ContextManagerTestCase(unittest.TestCase): yield 42 finally: state.append(999) - try: + with self.assertRaises(ZeroDivisionError): with woohoo() as x: self.assertEqual(state, [1]) self.assertEqual(x, 42) state.append(x) raise ZeroDivisionError() - except ZeroDivisionError: - pass - else: - self.fail("Expected ZeroDivisionError") self.assertEqual(state, [1, 42, 999]) def test_contextmanager_no_reraise(self): @@ -130,14 +125,11 @@ class ClosingTestCase(unittest.TestCase): state.append(1) x = C() self.assertEqual(state, []) - try: + with self.assertRaises(ZeroDivisionError): with closing(x) as y: self.assertEqual(x, y) - 1/0 - except ZeroDivisionError: - self.assertEqual(state, [1]) - else: - self.fail("Didn't raise ZeroDivisionError") + 1 / 0 + self.assertEqual(state, [1]) class FileContextTestCase(unittest.TestCase): @@ -150,20 +142,14 @@ class FileContextTestCase(unittest.TestCase): f.write("Booh\n") self.assertTrue(f.closed) f = None - try: + with self.assertRaises(ZeroDivisionError): with open(tfn, "r") as f: self.assertFalse(f.closed) self.assertEqual(f.read(), "Booh\n") - 1/0 - except ZeroDivisionError: - self.assertTrue(f.closed) - else: - self.fail("Didn't raise ZeroDivisionError") + 1 / 0 + self.assertTrue(f.closed) finally: - try: - os.remove(tfn) - except os.error: - pass + support.unlink(tfn) class LockContextTestCase(unittest.TestCase): @@ -172,14 +158,11 @@ class LockContextTestCase(unittest.TestCase): with lock: self.assertTrue(locked()) self.assertFalse(locked()) - try: + with self.assertRaises(ZeroDivisionError): with lock: self.assertTrue(locked()) - 1/0 - except ZeroDivisionError: - self.assertFalse(locked()) - else: - self.fail("Didn't raise ZeroDivisionError") + 1 / 0 + self.assertFalse(locked()) def testWithLock(self): lock = threading.Lock() diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py index 408f72b0a9..5568dd5e27 100644 --- a/Lib/test/test_descr.py +++ b/Lib/test/test_descr.py @@ -2,7 +2,6 @@ import builtins import sys import types import unittest -import warnings from copy import deepcopy from test import support diff --git a/Lib/test/test_doctest.py b/Lib/test/test_doctest.py index 17842c9a08..ff012388bd 100644 --- a/Lib/test/test_doctest.py +++ b/Lib/test/test_doctest.py @@ -4,7 +4,6 @@ Test script for doctest. from test import support import doctest -import warnings # NOTE: There are some additional tests relating to interaction with # zipimport in the test_zipimport_support test module. diff --git a/Lib/test/test_global.py b/Lib/test/test_global.py index a98e339aa2..37ec67255a 100644 --- a/Lib/test/test_global.py +++ b/Lib/test/test_global.py @@ -2,7 +2,6 @@ from test.support import run_unittest, check_syntax_error, check_warnings import unittest - import warnings @@ -54,7 +53,9 @@ x = 2 def test_main(): - run_unittest(GlobalTests) + with warnings.catch_warnings(): + warnings.filterwarnings("error", module="<test string>") + run_unittest(GlobalTests) if __name__ == "__main__": test_main() diff --git a/Lib/test/test_hmac.py b/Lib/test/test_hmac.py index fb78eba552..4de0620e1a 100644 --- a/Lib/test/test_hmac.py +++ b/Lib/test/test_hmac.py @@ -213,19 +213,13 @@ class TestVectorsTestCase(unittest.TestCase): with warnings.catch_warnings(): warnings.simplefilter('error', RuntimeWarning) - try: + with self.assertRaises(RuntimeWarning): hmac.HMAC(b'a', b'b', digestmod=MockCrazyHash) - except RuntimeWarning: - pass - else: self.fail('Expected warning about missing block_size') MockCrazyHash.block_size = 1 - try: + with self.assertRaises(RuntimeWarning): hmac.HMAC(b'a', b'b', digestmod=MockCrazyHash) - except RuntimeWarning: - pass - else: self.fail('Expected warning about small block_size') diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py index bf0d03fa4d..d5a6a4f74c 100644 --- a/Lib/test/test_io.py +++ b/Lib/test/test_io.py @@ -26,7 +26,6 @@ import array import threading import random import unittest -import warnings import weakref import abc from itertools import cycle, count diff --git a/Lib/test/test_robotparser.py b/Lib/test/test_robotparser.py index 86ebe9fbea..4c3b5363fd 100644 --- a/Lib/test/test_robotparser.py +++ b/Lib/test/test_robotparser.py @@ -209,21 +209,19 @@ RobotTest(13, doc, good, bad, agent="googlebot") class NetworkTestCase(unittest.TestCase): def testPasswordProtectedSite(self): - if not support.is_resource_enabled('network'): - return - # whole site is password-protected. + support.requires('network') + # XXX it depends on an external resource which could be unavailable url = 'http://mueblesmoraleda.com' parser = urllib.robotparser.RobotFileParser() parser.set_url(url) try: parser.read() - self.assertEqual(parser.can_fetch("*", url+"/robots.txt"), False) except URLError: - self.skipTest('mueblesmoraleda.com is unavailable') + self.skipTest('%s is unavailable' % url) + self.assertEqual(parser.can_fetch("*", url+"/robots.txt"), False) def testPythonOrg(self): - if not support.is_resource_enabled('network'): - return + support.requires('network') parser = urllib.robotparser.RobotFileParser( "http://www.python.org/robots.txt") parser.read() diff --git a/Lib/test/test_sundry.py b/Lib/test/test_sundry.py index ee387c7454..365b98b7d3 100644 --- a/Lib/test/test_sundry.py +++ b/Lib/test/test_sundry.py @@ -3,12 +3,10 @@ from test import support import sys import unittest -import warnings class TestUntestedModules(unittest.TestCase): def test_at_least_import_untested_modules(self): - with warnings.catch_warnings(): - warnings.simplefilter("ignore") + with support.check_warnings(quiet=True): import bdb import cgitb import code diff --git a/Lib/test/test_xml_etree.py b/Lib/test/test_xml_etree.py index f1990d1e1b..249ac6411b 100644 --- a/Lib/test/test_xml_etree.py +++ b/Lib/test/test_xml_etree.py @@ -12,6 +12,7 @@ # except if the test is specific to the Python implementation. import sys +import cgi from test import support from test.support import findfile @@ -1305,7 +1306,7 @@ XINCLUDE["default.xml"] = """\ <p>Example.</p> <xi:include href="{}"/> </document> -""".format(SIMPLE_XMLFILE) +""".format(cgi.escape(SIMPLE_XMLFILE, True)) def xinclude_loader(href, parse="xml", encoding=None): try: @@ -1808,6 +1809,23 @@ def check_issue6565(): class CleanContext(object): """Provide default namespace mapping and path cache.""" + checkwarnings = None + + def __init__(self, quiet=False): + deprecations = ( + # Search behaviour is broken if search path starts with "/". + ("This search is broken in 1.3 and earlier, and will be fixed " + "in a future version. If you rely on the current behaviour, " + "change it to '.+'", FutureWarning), + # Element.getchildren() and Element.getiterator() are deprecated. + ("This method will be removed in future versions. " + "Use .+ instead.", DeprecationWarning), + ("This method will be removed in future versions. " + "Use .+ instead.", PendingDeprecationWarning), + # XMLParser.doctype() is deprecated. + ("This method of XMLParser is deprecated. Define doctype.. " + "method on the TreeBuilder target.", DeprecationWarning)) + self.checkwarnings = support.check_warnings(*deprecations, quiet=quiet) def __enter__(self): from xml.etree import ElementTree @@ -1817,35 +1835,26 @@ class CleanContext(object): ElementTree._namespace_map = self._nsmap.copy() # Copy the path cache (should be empty) ElementTree.ElementPath._cache = self._path_cache.copy() + self.checkwarnings.__enter__() def __exit__(self, *args): from xml.etree import ElementTree # Restore mapping and path cache ElementTree._namespace_map = self._nsmap ElementTree.ElementPath._cache = self._path_cache + self.checkwarnings.__exit__(*args) def test_main(module_name='xml.etree.ElementTree'): - import warnings from test import test_xml_etree - def ignore(message, category=DeprecationWarning): - warnings.filterwarnings("ignore", message, category) + + use_py_module = (module_name == 'xml.etree.ElementTree') # The same doctests are used for both the Python and the C implementations assert test_xml_etree.ET.__name__ == module_name - with warnings.catch_warnings(), CleanContext(): - # Search behaviour is broken if search path starts with "/". - ignore("This search is broken in 1.3 and earlier, and will be fixed " - "in a future version. If you rely on the current behaviour, " - "change it to '.+'", FutureWarning) - # Element.getchildren() and Element.getiterator() are deprecated. - ignore("This method will be removed in future versions. " - "Use .+ instead.") - # XMLParser.doctype() is deprecated. - ignore("This method of XMLParser is deprecated. " - "Define doctype.. method on the TreeBuilder target.") - + # XXX the C module should give the same warnings as the Python module + with CleanContext(quiet=not use_py_module): support.run_doctest(test_xml_etree, verbosity=True) # The module should not be changed by the tests |