summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2011-06-02 23:40:24 -0700
committerRaymond Hettinger <python@rcn.com>2011-06-02 23:40:24 -0700
commitb269df3dd7600f3c080956aaaa4961230398b56a (patch)
treeceec22d9716bbedcd7c4e878680aa1d422921ef9
parent686c5829c447c34aa88fbc34d365aeeeb022b66e (diff)
downloadcpython-b269df3dd7600f3c080956aaaa4961230398b56a.tar.gz
Fix named tuples to work with vars().
-rw-r--r--Doc/library/collections.rst2
-rw-r--r--Lib/collections.py2
-rw-r--r--Lib/test/test_collections.py2
-rw-r--r--Misc/NEWS2
4 files changed, 7 insertions, 1 deletions
diff --git a/Doc/library/collections.rst b/Doc/library/collections.rst
index f3b068c9c0..79db55ec40 100644
--- a/Doc/library/collections.rst
+++ b/Doc/library/collections.rst
@@ -623,6 +623,8 @@ they add the ability to access fields by name instead of position index.
'Return a new OrderedDict which maps field names to their values'
return OrderedDict(zip(self._fields, self))
<BLANKLINE>
+ __dict__ = property(_asdict)
+ <BLANKLINE>
def _replace(_self, **kwds):
'Return a new Point object replacing specified fields with new values'
result = _self._make(map(kwds.pop, ('x', 'y'), _self))
diff --git a/Lib/collections.py b/Lib/collections.py
index b904980555..2b6abd879f 100644
--- a/Lib/collections.py
+++ b/Lib/collections.py
@@ -268,6 +268,8 @@ class {typename}(tuple):
'Return a new OrderedDict which maps field names to their values'
return OrderedDict(zip(self._fields, self))
+ __dict__ = property(_asdict)
+
def _replace(_self, **kwds):
'Return a new {typename} object replacing specified fields with new values'
result = _self._make(map(kwds.pop, {field_names!r}, _self))
diff --git a/Lib/test/test_collections.py b/Lib/test/test_collections.py
index 2b7b68be88..ccf93c51c1 100644
--- a/Lib/test/test_collections.py
+++ b/Lib/test/test_collections.py
@@ -180,12 +180,12 @@ class TestNamedTuple(unittest.TestCase):
self.assertRaises(TypeError, eval, 'Point(XXX=1, y=2)', locals()) # wrong keyword argument
self.assertRaises(TypeError, eval, 'Point(x=1)', locals()) # missing keyword argument
self.assertEqual(repr(p), 'Point(x=11, y=22)')
- self.assertNotIn('__dict__', dir(p)) # verify instance has no dict
self.assertNotIn('__weakref__', dir(p))
self.assertEqual(p, Point._make([11, 22])) # test _make classmethod
self.assertEqual(p._fields, ('x', 'y')) # test _fields attribute
self.assertEqual(p._replace(x=1), (1, 22)) # test _replace method
self.assertEqual(p._asdict(), dict(x=11, y=22)) # test _asdict method
+ self.assertEqual(vars(p), p._asdict()) # verify that vars() works
try:
p._replace(x=1, error=2)
diff --git a/Misc/NEWS b/Misc/NEWS
index baf235c23e..16a0f290fb 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -22,6 +22,8 @@ Core and Builtins
Library
-------
+- Named tuples now work correctly with vars().
+
- Issue #12085: Fix an attribute error in subprocess.Popen destructor if the
constructor has failed, e.g. because of an undeclared keyword argument. Patch
written by Oleg Oshmyan.