[Python-checkins] distutils2: Fix DistributionMetadata.update
tarek.ziade
python-checkins at python.org
Thu Aug 19 08:34:13 CEST 2010
tarek.ziade pushed 80265368bda0 to distutils2:
http://hg.python.org/distutils2/rev/80265368bda0
changeset: 553:80265368bda0
user: ?ric Araujo <merwok at netwok.org>
date: Thu Aug 12 19:46:39 2010 +0200
summary: Fix DistributionMetadata.update
files: src/distutils2/metadata.py, src/distutils2/tests/test_metadata.py
diff --git a/src/distutils2/metadata.py b/src/distutils2/metadata.py
--- a/src/distutils2/metadata.py
+++ b/src/distutils2/metadata.py
@@ -336,34 +336,28 @@
self._write_field(fileobject, field, value)
def update(self, other=None, **kwargs):
- """Set metadata values from the given mapping
+ """Set metadata values from the given iterable `other` and kwargs.
- Convert the keys to Metadata fields. Given keys that don't match a
- metadata argument will not be used.
+ Behavior is like `dict.update`: If `other` has a ``keys`` method,
+ they are looped over and ``self[key]`` is assigned ``other[key]``.
+ Else, ``other`` is an iterable of ``(key, value)`` iterables.
- If overwrite is set to False, just add metadata values that are
- actually not defined.
-
- If there is existing values in conflict with the dictionary ones, the
- new values prevails.
-
- Empty values (e.g. None and []) are not setted this way.
+ Keys that don't match a metadata field or that have an empty value are
+ dropped.
"""
def _set(key, value):
- if value not in ([], None, '') and key in _ATTR2FIELD:
+ if key in _ATTR2FIELD and value:
self.set(self._convert_name(key), value)
if other is None:
pass
- elif hasattr(other, 'iteritems'): # iteritems saves memory and lookups
- for k, v in other.iteritems():
- _set(k, v)
elif hasattr(other, 'keys'):
for k in other.keys():
- _set(k, v)
+ _set(k, other[k])
else:
for k, v in other:
_set(k, v)
+
if kwargs:
self.update(kwargs)
diff --git a/src/distutils2/tests/test_metadata.py b/src/distutils2/tests/test_metadata.py
--- a/src/distutils2/tests/test_metadata.py
+++ b/src/distutils2/tests/test_metadata.py
@@ -152,7 +152,11 @@
self.assertIn('Version', metadata.keys())
self.assertIn('0.5', metadata.values())
self.assertIn(('Version', '0.5'), metadata.items())
- #TODO test update
+
+ metadata.update({'version': '0.6'})
+ self.assertEqual(metadata['Version'], '0.6')
+ metadata.update([('version', '0.7')])
+ self.assertEqual(metadata['Version'], '0.7')
def test_versions(self):
metadata = DistributionMetadata()
--
Repository URL: http://hg.python.org/distutils2
More information about the Python-checkins
mailing list