From d8f2d50c204442d0dfafcc29db3177d15abb41df Mon Sep 17 00:00:00 2001 From: Steven Bethard Date: Sat, 26 Mar 2011 19:50:06 +0100 Subject: [PATCH 1/2] Issue #8982: Improve the documentation for the argparse Namespace object. --- Doc/library/argparse.rst | 21 ++++++++++++++++----- Misc/NEWS | 2 ++ 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/Doc/library/argparse.rst b/Doc/library/argparse.rst index 8bd3ca53bb5..102b3e93025 100644 --- a/Doc/library/argparse.rst +++ b/Doc/library/argparse.rst @@ -1314,13 +1314,24 @@ of :data:`sys.argv`. This can be accomplished by passing a list of strings to Namespace(accumulate=, integers=[1, 2, 3, 4]) -Custom namespaces -^^^^^^^^^^^^^^^^^ +The Namespace object +^^^^^^^^^^^^^^^^^^^^ + +By default, :meth:`parse_args` will return a new object of type :class:`Namespace` +where the necessary attributes have been set. This class is deliberately simple, +just an :class:`object` subclass with a readable string representation. If you +prefer to have dict-like view of the attributes, you can use the standard Python +idiom via :func:`vars`:: + + >>> parser = argparse.ArgumentParser() + >>> parser.add_argument('--foo') + >>> args = parser.parse_args(['--foo', 'BAR']) + >>> vars(args) + {'foo': 'BAR'} It may also be useful to have an :class:`ArgumentParser` assign attributes to an -already existing object, rather than the newly-created :class:`Namespace` object -that is normally used. This can be achieved by specifying the ``namespace=`` -keyword argument:: +already existing object, rather than a new :class:`Namespace` object. This can +be achieved by specifying the ``namespace=`` keyword argument:: >>> class C: ... pass diff --git a/Misc/NEWS b/Misc/NEWS index f5ff837d769..5958a325179 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -157,6 +157,8 @@ Library - Issue #9348: Raise an early error if argparse nargs and metavar don't match. +- Issue #8982: Improve the documentation for the argparse Namespace object. + Build ----- From b8a5769a6d35931f4dc5395686f590b5dcb58cdd Mon Sep 17 00:00:00 2001 From: Ross Lagerwall Date: Sat, 26 Mar 2011 21:19:57 +0200 Subject: [PATCH 2/2] Issue #11659: Fix ResourceWarning in test_subprocess introduced by #11459. Patch by Ben Hayden. --- Lib/test/test_subprocess.py | 1 + Misc/ACKS | 1 + Misc/NEWS | 3 +++ 3 files changed, 5 insertions(+) diff --git a/Lib/test/test_subprocess.py b/Lib/test/test_subprocess.py index 410849fc286..837bdb50a45 100644 --- a/Lib/test/test_subprocess.py +++ b/Lib/test/test_subprocess.py @@ -874,6 +874,7 @@ class ProcessTestCase(BaseTestCase): stdout=subprocess.PIPE, bufsize=0) f = p.stdout + self.addCleanup(f.close) try: self.assertEqual(f.read(4), b"appl") self.assertIn(f, select.select([f], [], [], 0.0)[0]) diff --git a/Misc/ACKS b/Misc/ACKS index 99554da92d6..17091ebabf1 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -325,6 +325,7 @@ Jason Harper Larry Hastings Shane Hathaway Rycharde Hawkes +Ben Hayden Jochen Hayek Christian Heimes Thomas Heller diff --git a/Misc/NEWS b/Misc/NEWS index 6825931972b..8e69a841cb1 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -48,6 +48,9 @@ Core and Builtins Library ------- +- Issue #11659: Fix ResourceWarning in test_subprocess introduced by #11459. + Patch by Ben Hayden. + - Issue #5537: Fix time2isoz() and time2netscape() functions of httplib.cookiejar for expiration year greater than 2038 on 32-bit systems.