Commit c644f6f0 authored by Berker Peksag's avatar Berker Peksag Committed by GitHub

bpo-19610: setup() now raises TypeError for invalid types (GH-4519)

The Distribution class now explicitly raises an
exception when 'classifiers', 'keywords' and
'platforms' fields are not specified as a list.
parent 9d391e77
...@@ -1188,12 +1188,38 @@ class DistributionMetadata: ...@@ -1188,12 +1188,38 @@ class DistributionMetadata:
def get_keywords(self): def get_keywords(self):
return self.keywords or [] return self.keywords or []
def set_keywords(self, value):
# If 'keywords' is a string, it will be converted to a list
# by Distribution.finalize_options(). To maintain backwards
# compatibility, do not raise an exception if 'keywords' is
# a string.
if not isinstance(value, (list, str)):
msg = "'keywords' should be a 'list', not %r"
raise TypeError(msg % type(value).__name__)
self.keywords = value
def get_platforms(self): def get_platforms(self):
return self.platforms or ["UNKNOWN"] return self.platforms or ["UNKNOWN"]
def set_platforms(self, value):
# If 'platforms' is a string, it will be converted to a list
# by Distribution.finalize_options(). To maintain backwards
# compatibility, do not raise an exception if 'platforms' is
# a string.
if not isinstance(value, (list, str)):
msg = "'platforms' should be a 'list', not %r"
raise TypeError(msg % type(value).__name__)
self.platforms = value
def get_classifiers(self): def get_classifiers(self):
return self.classifiers or [] return self.classifiers or []
def set_classifiers(self, value):
if not isinstance(value, list):
msg = "'classifiers' should be a 'list', not %r"
raise TypeError(msg % type(value).__name__)
self.classifiers = value
def get_download_url(self): def get_download_url(self):
return self.download_url or "UNKNOWN" return self.download_url or "UNKNOWN"
......
...@@ -195,6 +195,13 @@ class DistributionTestCase(support.LoggingSilencer, ...@@ -195,6 +195,13 @@ class DistributionTestCase(support.LoggingSilencer,
self.assertEqual(dist.metadata.platforms, ['one', 'two']) self.assertEqual(dist.metadata.platforms, ['one', 'two'])
self.assertEqual(dist.metadata.keywords, ['one', 'two']) self.assertEqual(dist.metadata.keywords, ['one', 'two'])
attrs = {'keywords': 'foo bar',
'platforms': 'foo bar'}
dist = Distribution(attrs=attrs)
dist.finalize_options()
self.assertEqual(dist.metadata.platforms, ['foo bar'])
self.assertEqual(dist.metadata.keywords, ['foo bar'])
def test_get_command_packages(self): def test_get_command_packages(self):
dist = Distribution() dist = Distribution()
self.assertEqual(dist.command_packages, None) self.assertEqual(dist.command_packages, None)
...@@ -338,9 +345,46 @@ class MetadataTestCase(support.TempdirManager, support.EnvironGuard, ...@@ -338,9 +345,46 @@ class MetadataTestCase(support.TempdirManager, support.EnvironGuard,
attrs = {'name': 'Boa', 'version': '3.0', attrs = {'name': 'Boa', 'version': '3.0',
'classifiers': ['Programming Language :: Python :: 3']} 'classifiers': ['Programming Language :: Python :: 3']}
dist = Distribution(attrs) dist = Distribution(attrs)
self.assertEqual(dist.get_classifiers(),
['Programming Language :: Python :: 3'])
meta = self.format_metadata(dist) meta = self.format_metadata(dist)
self.assertIn('Metadata-Version: 1.1', meta) self.assertIn('Metadata-Version: 1.1', meta)
def test_classifier_invalid_type(self):
attrs = {'name': 'Boa', 'version': '3.0',
'classifiers': ('Programming Language :: Python :: 3',)}
msg = "'classifiers' should be a 'list', not 'tuple'"
with self.assertRaises(TypeError, msg=msg):
Distribution(attrs)
def test_keywords(self):
attrs = {'name': 'Monty', 'version': '1.0',
'keywords': ['spam', 'eggs', 'life of brian']}
dist = Distribution(attrs)
self.assertEqual(dist.get_keywords(),
['spam', 'eggs', 'life of brian'])
def test_keywords_invalid_type(self):
attrs = {'name': 'Monty', 'version': '1.0',
'keywords': ('spam', 'eggs', 'life of brian')}
msg = "'keywords' should be a 'list', not 'tuple'"
with self.assertRaises(TypeError, msg=msg):
Distribution(attrs)
def test_platforms(self):
attrs = {'name': 'Monty', 'version': '1.0',
'platforms': ['GNU/Linux', 'Some Evil Platform']}
dist = Distribution(attrs)
self.assertEqual(dist.get_platforms(),
['GNU/Linux', 'Some Evil Platform'])
def test_platforms_invalid_types(self):
attrs = {'name': 'Monty', 'version': '1.0',
'platforms': ('GNU/Linux', 'Some Evil Platform')}
msg = "'platforms' should be a 'list', not 'tuple'"
with self.assertRaises(TypeError, msg=msg):
Distribution(attrs)
def test_download_url(self): def test_download_url(self):
attrs = {'name': 'Boa', 'version': '3.0', attrs = {'name': 'Boa', 'version': '3.0',
'download_url': 'http://example.org/boa'} 'download_url': 'http://example.org/boa'}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment