From c36033859ec2f7d9034a93c363ffc858ffbae172 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Thu, 15 Apr 2021 22:32:45 +0200 Subject: [PATCH] Add escaping to license field --- changelog.d/2640.change.rst | 1 + setuptools/dist.py | 5 +++-- setuptools/tests/test_dist.py | 4 ++++ setuptools/tests/test_egg_info.py | 31 +++++++++++++++++++++++++++++++ 4 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 changelog.d/2640.change.rst diff --git a/changelog.d/2640.change.rst b/changelog.d/2640.change.rst new file mode 100644 index 0000000000..dfed671a39 --- /dev/null +++ b/changelog.d/2640.change.rst @@ -0,0 +1 @@ +Fixed handling of multiline license strings. - by :user:`cdce8p` diff --git a/setuptools/dist.py b/setuptools/dist.py index c7af35dc99..4950126379 100644 --- a/setuptools/dist.py +++ b/setuptools/dist.py @@ -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') @@ -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(): diff --git a/setuptools/tests/test_dist.py b/setuptools/tests/test_dist.py index dcec1734a9..e09ed4cb31 100644 --- a/setuptools/tests/test_dist.py +++ b/setuptools/tests/test_dist.py @@ -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']), diff --git a/setuptools/tests/test_egg_info.py b/setuptools/tests/test_egg_info.py index 80d3577424..5283954b28 100644 --- a/setuptools/tests/test_egg_info.py +++ b/setuptools/tests/test_egg_info.py @@ -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',""")