Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add escaping to license field #2640

Merged
merged 1 commit into from May 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/2640.change.rst
@@ -0,0 +1 @@
Fixed handling of multiline license strings. - by :user:`cdce8p`
5 changes: 3 additions & 2 deletions setuptools/dist.py
Expand Up @@ -120,7 +120,7 @@ def read_pkg_file(self, file):
self.author_email = _read_field_from_msg(msg, 'author-email')
self.maintainer_email = None
self.url = _read_field_from_msg(msg, 'home-page')
self.license = _read_field_from_msg(msg, 'license')
self.license = _read_field_unescaped_from_msg(msg, 'license')

if 'download-url' in msg:
self.download_url = _read_field_from_msg(msg, 'download-url')
Expand Down Expand Up @@ -188,7 +188,8 @@ def write_field(key, value):
if attr_val is not None:
write_field(field, attr_val)

write_field('License', self.get_license())
license = rfc822_escape(self.get_license())
write_field('License', license)
if self.download_url:
write_field('Download-URL', self.download_url)
for project_url in self.project_urls.items():
Expand Down
4 changes: 4 additions & 0 deletions setuptools/tests/test_dist.py
Expand Up @@ -116,6 +116,10 @@ def __read_test_cases():
('Metadata Version 2.1: Long Description Content Type', params(
long_description_content_type='text/x-rst; charset=UTF-8',
)),
('License', params(license='MIT', )),
('License multiline', params(
license='This is a long license \nover multiple lines',
)),
pytest.param(
'Metadata Version 2.1: Provides Extra',
params(provides_extras=['foo', 'bar']),
Expand Down
31 changes: 31 additions & 0 deletions setuptools/tests/test_egg_info.py
Expand Up @@ -886,6 +886,37 @@ def test_project_urls(self, tmpdir_cwd, env):
assert expected_line in pkg_info_lines
assert 'Metadata-Version: 1.2' in pkg_info_lines

def test_license(self, tmpdir_cwd, env):
"""Test single line license."""
self._setup_script_with_requires(
"license='MIT',"
)
code, data = environment.run_setup_py(
cmd=['egg_info'],
pypath=os.pathsep.join([env.paths['lib'], str(tmpdir_cwd)]),
data_stream=1,
)
egg_info_dir = os.path.join('.', 'foo.egg-info')
with open(os.path.join(egg_info_dir, 'PKG-INFO')) as pkginfo_file:
pkg_info_lines = pkginfo_file.read().split('\n')
assert 'License: MIT' == pkg_info_lines[7]

def test_license_escape(self, tmpdir_cwd, env):
"""Test license is escaped correctly if longer than one line."""
self._setup_script_with_requires(
"license='This is a long license text \\nover multiple lines',"
)
code, data = environment.run_setup_py(
cmd=['egg_info'],
pypath=os.pathsep.join([env.paths['lib'], str(tmpdir_cwd)]),
data_stream=1,
)
egg_info_dir = os.path.join('.', 'foo.egg-info')
with open(os.path.join(egg_info_dir, 'PKG-INFO')) as pkginfo_file:
pkg_info_lines = pkginfo_file.read().split('\n')
assert 'License: This is a long license text ' == pkg_info_lines[7]
assert ' over multiple lines' == pkg_info_lines[8]

def test_python_requires_egg_info(self, tmpdir_cwd, env):
self._setup_script_with_requires(
"""python_requires='>=2.7.12',""")
Expand Down