mirror of
https://github.com/python/cpython.git
synced 2026-01-06 23:42:34 +00:00
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.
This commit is contained in:
parent
6a54c676e6
commit
dcaed6b2d9
6 changed files with 112 additions and 12 deletions
|
|
@ -1188,12 +1188,38 @@ def get_long_description(self):
|
|||
def get_keywords(self):
|
||||
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):
|
||||
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):
|
||||
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):
|
||||
return self.download_url or "UNKNOWN"
|
||||
|
||||
|
|
|
|||
|
|
@ -195,6 +195,13 @@ def test_finalize_options(self):
|
|||
self.assertEqual(dist.metadata.platforms, ['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):
|
||||
dist = Distribution()
|
||||
self.assertEqual(dist.command_packages, None)
|
||||
|
|
@ -338,9 +345,46 @@ def test_classifier(self):
|
|||
attrs = {'name': 'Boa', 'version': '3.0',
|
||||
'classifiers': ['Programming Language :: Python :: 3']}
|
||||
dist = Distribution(attrs)
|
||||
self.assertEqual(dist.get_classifiers(),
|
||||
['Programming Language :: Python :: 3'])
|
||||
meta = self.format_metadata(dist)
|
||||
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):
|
||||
attrs = {'name': 'Boa', 'version': '3.0',
|
||||
'download_url': 'http://example.org/boa'}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue