From 41fe6155394edf0ef65d125b27ceb8d1e37f72d3 Mon Sep 17 00:00:00 2001 From: Florent Xicluna Date: Fri, 2 Apr 2010 18:52:12 +0000 Subject: (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. ........ --- Doc/whatsnew/2.7.rst | 2 +- Lib/platform.py | 2 +- Lib/test/test___all__.py | 6 ++---- Lib/test/test_argparse.py | 22 ++++++---------------- Lib/test/test_complex.py | 7 ++----- Lib/test/test_contextlib.py | 41 ++++++++++++----------------------------- Lib/test/test_descr.py | 1 - Lib/test/test_doctest.py | 1 - Lib/test/test_global.py | 5 +++-- Lib/test/test_hmac.py | 10 ++-------- Lib/test/test_io.py | 1 - Lib/test/test_robotparser.py | 12 +++++------- Lib/test/test_sundry.py | 4 +--- Lib/test/test_xml_etree.py | 41 +++++++++++++++++++++++++---------------- 14 files changed, 60 insertions(+), 95 deletions(-) diff --git a/Doc/whatsnew/2.7.rst b/Doc/whatsnew/2.7.rst index e6aa3c5..406588d 100644 --- a/Doc/whatsnew/2.7.rst +++ b/Doc/whatsnew/2.7.rst @@ -836,7 +836,7 @@ changes, or look through the Subversion logs for all the details. The :mod:`site` module now reports exceptions occurring when the :mod:`sitecustomize` module is imported, and will no longer - catch and swallow the :exc:`KeyboardError` exception. (Fixed by + catch and swallow the :exc:`KeyboardInterrupt` exception. (Fixed by Victor Stinner; :issue:`3137`.) * The :mod:`socket` module's :class:`SSL` objects now support the diff --git a/Lib/platform.py b/Lib/platform.py index aa256dc..1601d59 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 200db5c..695669a 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 a140f1c..85513a5 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 a857354..6441208 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 1d3bd90..c733bca 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 408f72b..5568dd5 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 17842c9..ff01238 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 a98e339..37ec672 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="") + run_unittest(GlobalTests) if __name__ == "__main__": test_main() diff --git a/Lib/test/test_hmac.py b/Lib/test/test_hmac.py index fb78eba..4de0620 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 bf0d03f..d5a6a4f 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 86ebe9f..4c3b536 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 ee387c7..365b98b 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 f1990d1..249ac64 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"] = """\

Example.

-""".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 -- cgit v0.12