Commits

Tarek Ziadé committed 425899a

now the predicates are used by DistributionMetadata

Comments (0)

Files changed (2)

src/distutils2/metadata.py

 from tokenize import tokenize, NAME, OP, STRING, ENDMARKER
 
 from distutils2.util import rfc822_escape
+from distutils2.version import is_valid_predicate
 
 try:
     # docutils is installed
                'obsoletes': 'Obsoletes',
                }
 
+_PREDICATE_FIELDS = ('Requires-Dist', 'Obsoletes-Dist', 'Provides-Dist')
+
 _LISTFIELDS = ('Platform', 'Classifier', 'Obsoletes',
                'Requires', 'Provides', 'Obsoletes-Dist',
                'Provides-Dist', 'Requires-Dist', 'Requires-Python',
     def set_field(self, name, value):
         """Controls then sets a metadata field"""
         name = self._convert_name(name)
-        if name in ('Requires', 'Obsoletes', 'Provides'):
-            # check the values
-            for version in value:
-                distutils2.versionpredicate.VersionPredicate(version)
+        if name in _PREDICATE_FIELDS and value is not None:
+            for v in value:
+                # check that the values are valid predicates
+                if not is_valid_predicate(v.split(';')[0]):
+                    raise ValueError('"%s" is not a valid predicate' % v)
         if name in _LISTFIELDS + _ELEMENTSFIELD:
             if isinstance(value, str):
                 value = value.split(',')

src/distutils2/version.py

                 return False
         return True
 
+def is_valid_predicate(predicate):
+    try:
+        VersionPredicate(predicate)
+    except ValueError:
+        return False
+    else:
+        return True
+