From 54d412edcc940560a47a581981d0010f9c3504cb Mon Sep 17 00:00:00 2001 From: "Gregory P. Smith" Date: Mon, 14 Mar 2011 14:08:43 -0400 Subject: Add a SubprocessError base class for exceptions in the subprocess module. --- Doc/library/subprocess.rst | 5 +++++ Lib/subprocess.py | 13 +++++++++---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/Doc/library/subprocess.rst b/Doc/library/subprocess.rst index bb6e68b..6ea3c10 100644 --- a/Doc/library/subprocess.rst +++ b/Doc/library/subprocess.rst @@ -387,6 +387,11 @@ All of the functions and methods that accept a *timeout* parameter, such as :func:`call` and :meth:`Popen.communicate` will raise :exc:`TimeoutExpired` if the timeout expires before the process exits. +Exceptions defined in this module all inherit from :ext:`SubprocessError`. + + .. versionadded:: 3.3 + The :exc:`SubprocessError` base class was added. + Security ^^^^^^^^ diff --git a/Lib/subprocess.py b/Lib/subprocess.py index c8af5d1..874aea7 100644 --- a/Lib/subprocess.py +++ b/Lib/subprocess.py @@ -191,8 +191,10 @@ should prepare for OSErrors. A ValueError will be raised if Popen is called with invalid arguments. -check_call() and check_output() will raise CalledProcessError, if the -called process returns a non-zero return code. +Exceptions defined within this module inherit from SubprocessError. +check_call() and check_output() will raise CalledProcessError if the +called process returns a non-zero return code. TimeoutExpired +be raised if a timeout was specified and expired. Security @@ -348,7 +350,10 @@ import builtins import warnings # Exception classes used by this module. -class CalledProcessError(Exception): +class SubprocessError(Exception): pass + + +class CalledProcessError(SubprocessError): """This exception is raised when a process run by check_call() or check_output() returns a non-zero exit status. The exit status will be stored in the returncode attribute; @@ -362,7 +367,7 @@ class CalledProcessError(Exception): return "Command '%s' returned non-zero exit status %d" % (self.cmd, self.returncode) -class TimeoutExpired(Exception): +class TimeoutExpired(SubprocessError): """This exception is raised when the timeout expires while waiting for a child process. """ -- cgit v0.12 From b4039aaf358ec8c2797827175982d7e0ba17f5f2 Mon Sep 17 00:00:00 2001 From: "Gregory P. Smith" Date: Mon, 14 Mar 2011 14:16:20 -0400 Subject: whitespace fix --- Lib/subprocess.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/subprocess.py b/Lib/subprocess.py index 874aea7..69b769b 100644 --- a/Lib/subprocess.py +++ b/Lib/subprocess.py @@ -193,7 +193,7 @@ A ValueError will be raised if Popen is called with invalid arguments. Exceptions defined within this module inherit from SubprocessError. check_call() and check_output() will raise CalledProcessError if the -called process returns a non-zero return code. TimeoutExpired +called process returns a non-zero return code. TimeoutExpired be raised if a timeout was specified and expired. -- cgit v0.12 From 94eceeb89c153d8bf77dc194f8896a66cc25519a Mon Sep 17 00:00:00 2001 From: briancurtin Date: Mon, 14 Mar 2011 15:35:35 -0400 Subject: Fix #11491. When dbm.open was called with a file which already exists and the "flag" argument is "n", dbm.error was being raised. As documented, dbm.open(...,flag='n') will now "Always create a new, empty database, open for reading and writing", regardless of a previous file existing. --- Lib/dbm/__init__.py | 6 +++--- Lib/test/test_dbm.py | 8 ++++++++ Misc/ACKS | 1 + Misc/NEWS | 4 ++++ 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/Lib/dbm/__init__.py b/Lib/dbm/__init__.py index c224847..99c1637 100644 --- a/Lib/dbm/__init__.py +++ b/Lib/dbm/__init__.py @@ -68,10 +68,10 @@ def open(file, flag = 'r', mode = 0o666): if not _defaultmod: raise ImportError("no dbm clone found; tried %s" % _names) - # guess the type of an existing database - result = whichdb(file) + # guess the type of an existing database, if not creating a new one + result = whichdb(file) if 'n' not in flag else None if result is None: - # db doesn't exist + # db doesn't exist or 'n' flag was specified to create a new db if 'c' in flag or 'n' in flag: # file doesn't exist and the new flag was used so use default type mod = _defaultmod diff --git a/Lib/test/test_dbm.py b/Lib/test/test_dbm.py index 818be45..ce48cfd 100644 --- a/Lib/test/test_dbm.py +++ b/Lib/test/test_dbm.py @@ -70,6 +70,14 @@ class AnyDBMTestCase(unittest.TestCase): self.read_helper(f) f.close() + def test_anydbm_creation_n_file_exists_with_invalid_contents(self): + with open(_fname, "w") as w: + pass # create an empty file + + f = dbm.open(_fname, 'n') + self.addCleanup(f.close) + self.assertEqual(len(f), 0) + def test_anydbm_modification(self): self.init_db() f = dbm.open(_fname, 'c') diff --git a/Misc/ACKS b/Misc/ACKS index a18dd18..c95e76d 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -154,6 +154,7 @@ Terrence Cole Benjamin Collar Jeffery Collins Paul Colomiets +Denver Coneybeare Matt Conway David M. Cooke Greg Copeland diff --git a/Misc/NEWS b/Misc/NEWS index 9eb0537..37b2477 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -40,6 +40,10 @@ Core and Builtins Library ------- +- Issue #11491: dbm.error is no longer raised when dbm.open is called with + the "n" as the flag argument and the file exists. The behavior matches + the documentation and general logic. + - Issue #11131: Fix sign of zero in decimal.Decimal plus and minus operations when the rounding mode is ROUND_FLOOR. -- cgit v0.12 From 525c25d42f8740111d14b9f8ca56cc2762175eaf Mon Sep 17 00:00:00 2001 From: briancurtin Date: Mon, 14 Mar 2011 16:03:54 -0400 Subject: Fix #11491. When dbm.open was called with a file which already exists and the "flag" argument is "n", dbm.error was being raised. As documented, dbm.open(...,flag='n') will now "Always create a new, empty database, open for reading and writing", regardless of a previous file existing. --- Lib/dbm/__init__.py | 6 +++--- Lib/test/test_dbm.py | 8 ++++++++ Misc/ACKS | 1 + Misc/NEWS | 4 ++++ 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/Lib/dbm/__init__.py b/Lib/dbm/__init__.py index b2a254a..6e890f3 100644 --- a/Lib/dbm/__init__.py +++ b/Lib/dbm/__init__.py @@ -67,10 +67,10 @@ def open(file, flag = 'r', mode = 0o666): if not _defaultmod: raise ImportError("no dbm clone found; tried %s" % _names) - # guess the type of an existing database - result = whichdb(file) + # guess the type of an existing database, if not creating a new one + result = whichdb(file) if 'n' not in flag else None if result is None: - # db doesn't exist + # db doesn't exist or 'n' flag was specified to create a new db if 'c' in flag or 'n' in flag: # file doesn't exist and the new flag was used so use default type mod = _defaultmod diff --git a/Lib/test/test_dbm.py b/Lib/test/test_dbm.py index 74c9c44..26d4c14 100644 --- a/Lib/test/test_dbm.py +++ b/Lib/test/test_dbm.py @@ -70,6 +70,14 @@ class AnyDBMTestCase(unittest.TestCase): self.read_helper(f) f.close() + def test_anydbm_creation_n_file_exists_with_invalid_contents(self): + with open(_fname, "w") as w: + pass # create an empty file + + f = dbm.open(_fname, 'n') + self.addCleanup(f.close) + self.assertEqual(len(f), 0) + def test_anydbm_modification(self): self.init_db() f = dbm.open(_fname, 'c') diff --git a/Misc/ACKS b/Misc/ACKS index d507ade..a11bcf0 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -169,6 +169,7 @@ Benjamin Collar Jeffery Collins Robert Collins Paul Colomiets +Denver Coneybeare Geremy Condra Juan José Conti Matt Conway diff --git a/Misc/NEWS b/Misc/NEWS index bcda530..f3442a5 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -34,6 +34,10 @@ Core and Builtins Library ------- +- Issue #11491: dbm.error is no longer raised when dbm.open is called with + the "n" as the flag argument and the file exists. The behavior matches + the documentation and general logic. + - Issue #11131: Fix sign of zero in decimal.Decimal plus and minus operations when the rounding mode is ROUND_FLOOR. -- cgit v0.12 From f8b9dfd9a1a3d611d43e3c3ef8031fed96c8dd25 Mon Sep 17 00:00:00 2001 From: R David Murray Date: Mon, 14 Mar 2011 17:10:22 -0400 Subject: #11496: skip history test if clear_history is not available. Patch by Natalia B. Bidart. --- Lib/test/test_readline.py | 4 ++++ Misc/ACKS | 1 + 2 files changed, 5 insertions(+) diff --git a/Lib/test/test_readline.py b/Lib/test/test_readline.py index 5f5a90a..5483dd3 100644 --- a/Lib/test/test_readline.py +++ b/Lib/test/test_readline.py @@ -12,6 +12,10 @@ from test.support import run_unittest, import_module readline = import_module('readline') class TestHistoryManipulation (unittest.TestCase): + + @unittest.skipIf(not hasattr(readline, 'clear_history'), + "The history update test cannot be run because the " + "clear_history method is not available.") def testHistoryUpdates(self): readline.clear_history() diff --git a/Misc/ACKS b/Misc/ACKS index a11bcf0..a1197ad 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -79,6 +79,7 @@ Eric Beser Steven Bethard Stephen Bevan Ron Bickers +Natalia B. Bidart Adrian von Bidder David Binger Dominic Binks -- cgit v0.12 From e72e161851cb5d0bddef3d24e56bb5d2969a28e5 Mon Sep 17 00:00:00 2001 From: Ronald Oussoren Date: Mon, 14 Mar 2011 18:15:25 -0400 Subject: Issue #11500: Fixed a bug in the os x proxy bypass code for fully qualified IP addresses in the proxy exception list Patch by Scott Wilson. --- Lib/test/test_urllib2.py | 35 +++++++++++++++- Lib/urllib/request.py | 104 +++++++++++++++++++++++++---------------------- Misc/NEWS | 3 ++ 3 files changed, 93 insertions(+), 49 deletions(-) diff --git a/Lib/test/test_urllib2.py b/Lib/test/test_urllib2.py index 9320e61..1704683 100644 --- a/Lib/test/test_urllib2.py +++ b/Lib/test/test_urllib2.py @@ -6,7 +6,9 @@ import io import socket import urllib.request -from urllib.request import Request, OpenerDirector +# The proxy bypass method imported below has logic specific to the OSX +# proxy config data structure but is testable on all platforms. +from urllib.request import Request, OpenerDirector, _proxy_bypass_macosx_sysconf # XXX # Request @@ -1030,6 +1032,17 @@ class HandlerTests(unittest.TestCase): self.assertEqual(req.get_host(), "www.python.org") del os.environ['no_proxy'] + def test_proxy_no_proxy_all(self): + os.environ['no_proxy'] = '*' + o = OpenerDirector() + ph = urllib.request.ProxyHandler(dict(http="proxy.example.com")) + o.add_handler(ph) + req = Request("http://www.python.org") + self.assertEqual(req.get_host(), "www.python.org") + r = o.open(req) + self.assertEqual(req.get_host(), "www.python.org") + del os.environ['no_proxy'] + def test_proxy_https(self): o = OpenerDirector() @@ -1070,6 +1083,26 @@ class HandlerTests(unittest.TestCase): self.assertEqual(req.get_host(), "proxy.example.com:3128") self.assertEqual(req.get_header("Proxy-authorization"),"FooBar") + def test_osx_proxy_bypass(self): + bypass = { + 'exclude_simple': False, + 'exceptions': ['foo.bar', '*.bar.com', '127.0.0.1', '10.10', + '10.0/16'] + } + # Check hosts that should trigger the proxy bypass + for host in ('foo.bar', 'www.bar.com', '127.0.0.1', '10.10.0.1', + '10.0.0.1'): + self.assertTrue(_proxy_bypass_macosx_sysconf(host, bypass), + 'expected bypass of %s to be True' % host) + # Check hosts that should not trigger the proxy bypass + for host in ('abc.foo.bar', 'bar.com', '127.0.0.2', '10.11.0.1', 'test'): + self.assertFalse(_proxy_bypass_macosx_sysconf(host, bypass), + 'expected bypass of %s to be False' % host) + + # Check the exclude_simple flag + bypass = {'exclude_simple': True, 'exceptions': []} + self.assertTrue(_proxy_bypass_macosx_sysconf('test', bypass)) + def test_basic_auth(self, quote_char='"'): opener = OpenerDirector() password_manager = MockPasswordManager() diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py index e13381c..087e9a6 100644 --- a/Lib/urllib/request.py +++ b/Lib/urllib/request.py @@ -2175,68 +2175,76 @@ def proxy_bypass_environment(host): return 0 -if sys.platform == 'darwin': - from _scproxy import _get_proxy_settings, _get_proxies - - def proxy_bypass_macosx_sysconf(host): - """ - Return True iff this host shouldn't be accessed using a proxy +# This code tests an OSX specific data structure but is testable on all +# platforms +def _proxy_bypass_macosx_sysconf(host, proxy_settings): + """ + Return True iff this host shouldn't be accessed using a proxy - This function uses the MacOSX framework SystemConfiguration - to fetch the proxy information. - """ - import re - import socket - from fnmatch import fnmatch + This function uses the MacOSX framework SystemConfiguration + to fetch the proxy information. - hostonly, port = splitport(host) + proxy_settings come from _scproxy._get_proxy_settings or get mocked ie: + { 'exclude_simple': bool, + 'exceptions': ['foo.bar', '*.bar.com', '127.0.0.1', '10.1', '10.0/16'] + } + """ + import re + import socket + from fnmatch import fnmatch - def ip2num(ipAddr): - parts = ipAddr.split('.') - parts = list(map(int, parts)) - if len(parts) != 4: - parts = (parts + [0, 0, 0, 0])[:4] - return (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8) | parts[3] + hostonly, port = splitport(host) - proxy_settings = _get_proxy_settings() + def ip2num(ipAddr): + parts = ipAddr.split('.') + parts = list(map(int, parts)) + if len(parts) != 4: + parts = (parts + [0, 0, 0, 0])[:4] + return (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8) | parts[3] - # Check for simple host names: - if '.' not in host: - if proxy_settings['exclude_simple']: - return True + # Check for simple host names: + if '.' not in host: + if proxy_settings['exclude_simple']: + return True - hostIP = None + hostIP = None - for value in proxy_settings.get('exceptions', ()): - # Items in the list are strings like these: *.local, 169.254/16 - if not value: continue + for value in proxy_settings.get('exceptions', ()): + # Items in the list are strings like these: *.local, 169.254/16 + if not value: continue - m = re.match(r"(\d+(?:\.\d+)*)(/\d+)?", value) - if m is not None: - if hostIP is None: - try: - hostIP = socket.gethostbyname(hostonly) - hostIP = ip2num(hostIP) - except socket.error: - continue + m = re.match(r"(\d+(?:\.\d+)*)(/\d+)?", value) + if m is not None: + if hostIP is None: + try: + hostIP = socket.gethostbyname(hostonly) + hostIP = ip2num(hostIP) + except socket.error: + continue + + base = ip2num(m.group(1)) + mask = m.group(2) + if mask is None: + mask = 8 * (m.group(1).count('.') + 1) + else: + mask = int(mask[1:]) + mask = 32 - mask - base = ip2num(m.group(1)) - mask = m.group(2) - if mask is None: - mask = 8 * (m.group(1).count('.') + 1) + if (hostIP >> mask) == (base >> mask): + return True - else: - mask = int(mask[1:]) - mask = 32 - mask + elif fnmatch(host, value): + return True - if (hostIP >> mask) == (base >> mask): - return True + return False - elif fnmatch(host, value): - return True - return False +if sys.platform == 'darwin': + from _scproxy import _get_proxy_settings, _get_proxies + def proxy_bypass_macosx_sysconf(host): + proxy_settings = _get_proxy_settings() + return _proxy_bypass_macosx_sysconf(host, proxy_settings) def getproxies_macosx_sysconf(): """Return a dictionary of scheme -> proxy server URL mappings. diff --git a/Misc/NEWS b/Misc/NEWS index 37b2477..d4ce939 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -197,6 +197,9 @@ Library OSError exception when The OS had been told to ignore SIGCLD in our process or otherwise not wait for exiting child processes. +- Issue #11500: Fixed a bug in the os x proxy bypass code for fully qualified + IP addresses in the proxy exception list. + Extensions ---------- -- cgit v0.12 From cd37dfcfac956d6400659dd2d208bcf81f32aebc Mon Sep 17 00:00:00 2001 From: R David Murray Date: Mon, 14 Mar 2011 18:35:56 -0400 Subject: Harmonize linesep docstrings, and fix the quoting of \r\n --- Lib/email/generator.py | 7 +++++-- Lib/email/header.py | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/Lib/email/generator.py b/Lib/email/generator.py index 531fa9a..f0e7a95 100644 --- a/Lib/email/generator.py +++ b/Lib/email/generator.py @@ -59,7 +59,7 @@ class Generator: self._fp.write(s) def flatten(self, msg, unixfrom=False, linesep='\n'): - """Print the message object tree rooted at msg to the output file + r"""Print the message object tree rooted at msg to the output file specified when the Generator instance was created. unixfrom is a flag that forces the printing of a Unix From_ delimiter @@ -70,7 +70,10 @@ class Generator: Note that for subobjects, no From_ line is printed. linesep specifies the characters used to indicate a new line in - the output. + the output. The default value is the most useful for typical + Python applications, but it can be set to \r\n to produce RFC-compliant + line separators when needed. + """ # We use the _XXX constants for operating on data that comes directly # from the msg, and _encoded_XXX constants for operating on data that diff --git a/Lib/email/header.py b/Lib/email/header.py index 35cdb2b..e171617 100644 --- a/Lib/email/header.py +++ b/Lib/email/header.py @@ -276,7 +276,7 @@ class Header: self._chunks.append((s, charset)) def encode(self, splitchars=';, \t', maxlinelen=None, linesep='\n'): - """Encode a message header into an RFC-compliant format. + r"""Encode a message header into an RFC-compliant format. There are many issues involved in converting a given string for use in an email header. Only certain character sets are readable in most -- cgit v0.12 From d25fd4d7102cfa98dadd6bfeb5753ce419c3fb3b Mon Sep 17 00:00:00 2001 From: Nick Coghlan Date: Tue, 15 Mar 2011 08:54:37 +1000 Subject: Close #11505: Improve string.py coverage --- Lib/test/test_pep292.py | 13 +++++++++++++ Lib/test/test_string.py | 24 ++++++++++++++++++++++++ Misc/ACKS | 1 + Misc/NEWS | 2 ++ 4 files changed, 40 insertions(+) diff --git a/Lib/test/test_pep292.py b/Lib/test/test_pep292.py index a967649..a1e52e9 100644 --- a/Lib/test/test_pep292.py +++ b/Lib/test/test_pep292.py @@ -42,6 +42,19 @@ class TestTemplate(unittest.TestCase): s = Template('$who likes $$') eq(s.substitute(dict(who='tim', what='ham')), 'tim likes $') + def test_invalid(self): + class MyPattern(Template): + pattern = r""" + (?: + (?P) | + (?P%(delim)s) | + @(?P%(id)s) | + @{(?P%(id)s)} + ) + """ + s = MyPattern('$') + self.assertRaises(ValueError, s.substitute, dict()) + def test_percents(self): eq = self.assertEqual s = Template('%(foo)s $foo ${foo}') diff --git a/Lib/test/test_string.py b/Lib/test/test_string.py index b495d69..c4b186a 100644 --- a/Lib/test/test_string.py +++ b/Lib/test/test_string.py @@ -112,6 +112,30 @@ class ModuleTest(unittest.TestCase): self.assertRaises(ValueError, fmt.format, "{0}", 10, 20, i=100) self.assertRaises(ValueError, fmt.format, "{i}", 10, 20, i=100) + def test_vformat_assert(self): + cls = string.Formatter() + kwargs = { + "i": 100 + } + self.assertRaises(ValueError, cls._vformat, + cls.format, "{0}", kwargs, set(), -2) + + def test_convert_field(self): + cls = string.Formatter() + self.assertEqual(cls.format("{0!s}", 'foo'), 'foo') + self.assertRaises(ValueError, cls.format, "{0!h}", 'foo') + + def test_get_field(self): + cls = string.Formatter() + class MyClass: + name = 'lumberjack' + x = MyClass() + self.assertEqual(cls.format("{0.name}", x), 'lumberjack') + + lookup = ["eggs", "and", "spam"] + self.assertEqual(cls.format("{0[2]}", lookup), 'spam') + + def test_main(): support.run_unittest(ModuleTest) diff --git a/Misc/ACKS b/Misc/ACKS index 117cd8d..0c5a26d 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -32,6 +32,7 @@ Oliver Andrich Ross Andrus Jon Anglin Éric Araujo +Alicia Arlen Jason Asbahr David Ascher Chris AtLee diff --git a/Misc/NEWS b/Misc/NEWS index 483e660..56b1f4f 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -197,6 +197,8 @@ Tools/Demos Tests ----- +- Issue #11505: improves test coverage of string.py + - Issue #11490: test_subprocess:test_leaking_fds_on_error no longer gives a false positive if the last directory in the path is inaccessible. -- cgit v0.12 From 1341bb0019868345bab8adff94263c81e1d66eae Mon Sep 17 00:00:00 2001 From: Michael Foord Date: Mon, 14 Mar 2011 19:01:46 -0400 Subject: Closes issue 11407. TestCase.run returns the result object used or created --- Doc/library/unittest.rst | 7 +++++-- Lib/unittest/case.py | 2 +- Lib/unittest/test/test_case.py | 43 ++++++++++++++++++++++++++++++++++++++---- Misc/ACKS | 1 + Misc/NEWS | 3 +++ 5 files changed, 49 insertions(+), 7 deletions(-) diff --git a/Doc/library/unittest.rst b/Doc/library/unittest.rst index a3de29b..a4e2cab 100644 --- a/Doc/library/unittest.rst +++ b/Doc/library/unittest.rst @@ -723,7 +723,7 @@ Test cases Here, we create two instances of :class:`WidgetTestCase`, each of which runs a single test. - .. versionchanged:: + .. versionchanged:: 3.2 `TestCase` can be instantiated successfully without providing a method name. This makes it easier to experiment with `TestCase` from the interactive interpreter. @@ -792,11 +792,14 @@ Test cases Run the test, collecting the result into the test result object passed as *result*. If *result* is omitted or ``None``, a temporary result object is created (by calling the :meth:`defaultTestResult` method) and - used. The result object is not returned to :meth:`run`'s caller. + used. The result object is returned to :meth:`run`'s caller. The same effect may be had by simply calling the :class:`TestCase` instance. + .. versionchanged:: 3.3 + Previous versions of ``run`` did not return the result. Neither did + calling an instance. .. method:: skipTest(reason) diff --git a/Lib/unittest/case.py b/Lib/unittest/case.py index 270e5e8..4e47707 100644 --- a/Lib/unittest/case.py +++ b/Lib/unittest/case.py @@ -469,7 +469,7 @@ class TestCase(object): warnings.warn("TestResult has no addExpectedFailure method, reporting as passes", RuntimeWarning) result.addSuccess(self) - + return result finally: result.stopTest(self) if orig_result is None: diff --git a/Lib/unittest/test/test_case.py b/Lib/unittest/test/test_case.py index 351b6f8..852ac86 100644 --- a/Lib/unittest/test/test_case.py +++ b/Lib/unittest/test/test_case.py @@ -386,27 +386,62 @@ class Test_TestCase(unittest.TestCase, TestEquality, TestHashing): self.assertIsInstance(Foo().id(), str) - # "If result is omitted or None, a temporary result object is created - # and used, but is not made available to the caller. As TestCase owns the + # "If result is omitted or None, a temporary result object is created, + # used, and is made available to the caller. As TestCase owns the # temporary result startTestRun and stopTestRun are called. def test_run__uses_defaultTestResult(self): events = [] + defaultResult = LoggingResult(events) class Foo(unittest.TestCase): def test(self): events.append('test') def defaultTestResult(self): - return LoggingResult(events) + return defaultResult # Make run() find a result object on its own - Foo('test').run() + result = Foo('test').run() + self.assertIs(result, defaultResult) expected = ['startTestRun', 'startTest', 'test', 'addSuccess', 'stopTest', 'stopTestRun'] self.assertEqual(events, expected) + + # "The result object is returned to run's caller" + def test_run__returns_given_result(self): + + class Foo(unittest.TestCase): + def test(self): + pass + + result = unittest.TestResult() + + retval = Foo('test').run(result) + self.assertIs(retval, result) + + + # "The same effect [as method run] may be had by simply calling the + # TestCase instance." + def test_call__invoking_an_instance_delegates_to_run(self): + resultIn = unittest.TestResult() + resultOut = unittest.TestResult() + + class Foo(unittest.TestCase): + def test(self): + pass + + def run(self, result): + self.assertIs(result, resultIn) + return resultOut + + retval = Foo('test')(resultIn) + + self.assertIs(retval, resultOut) + + def testShortDescriptionWithoutDocstring(self): self.assertIsNone(self.shortDescription()) diff --git a/Misc/ACKS b/Misc/ACKS index 117cd8d..a641547 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -351,6 +351,7 @@ Lynda Hardman Derek Harland Jason Harper Brian Harring +Jonathan Hartley Larry Hastings Shane Hathaway Rycharde Hawkes diff --git a/Misc/NEWS b/Misc/NEWS index 2597252..3aecf51 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -68,6 +68,9 @@ Core and Builtins Library ------- +- Issue #11407: `TestCase.run` returns the result object used or created. + Contributed by Janathan Hartley. + - Issue #11500: Fixed a bug in the os x proxy bypass code for fully qualified IP addresses in the proxy exception list. -- cgit v0.12