Commits

Anonymous committed d69ba24 Merge

Fix TypeError on "setup.py upload --show-response".

  • Participants
  • Parent commits d659e77, bb42e10

Comments (0)

Files changed (5)

Lib/distutils/command/register.py

 
 # created 2002/10/21, Richard Jones
 
-import cgi
 import os, string, getpass
 import io
 import urllib.parse, urllib.request
         '''
         url = self.repository+'?:action=list_classifiers'
         response = urllib.request.urlopen(url)
-        content_type = response.getheader('content-type', 'text/plain')
-        encoding = cgi.parse_header(content_type)[1].get('charset', 'ascii')
-        log.info(response.read().decode(encoding))
+        log.info(self._read_pypi_response(response))
 
     def verify_metadata(self):
         ''' Send the metadata to the package index server to be checked.

Lib/distutils/command/upload.py

             self.announce('Upload failed (%s): %s' % (status, reason),
                           log.ERROR)
         if self.show_response:
-            msg = '\n'.join(('-' * 75, result.read(), '-' * 75))
+            text = self._read_pypi_response(result)
+            msg = '\n'.join(('-' * 75, text, '-' * 75))
             self.announce(msg, log.INFO)

Lib/distutils/config.py

 Provides the PyPIRCCommand class, the base class for the command classes
 that uses .pypirc in the distutils.command package.
 """
+import cgi
 import os
 from configparser import ConfigParser
 
 
         return {}
 
+    def _read_pypi_response(self, response):
+        """Read and decode a PyPI HTTP response."""
+        content_type = response.getheader('content-type', 'text/plain')
+        encoding = cgi.parse_header(content_type)[1].get('charset', 'ascii')
+        return response.read().decode(encoding)
+
     def initialize_options(self):
         """Initialize options."""
         self.repository = None

Lib/distutils/tests/test_upload.py

 from distutils.command import upload as upload_mod
 from distutils.command.upload import upload
 from distutils.core import Distribution
+from distutils.log import INFO
 
 from distutils.tests.test_config import PYPIRC, PyPIRCCommandTestCase
 
             self.req = None
         self.msg = 'OK'
 
+    def getheader(self, name, default=None):
+        return {
+            'content-type': 'text/plain; charset=utf-8',
+            }.get(name.lower(), default)
+
+    def read(self):
+        return b'xyzzy'
+
     def getcode(self):
         return 200
 
         # lets run it
         pkg_dir, dist = self.create_dist(dist_files=dist_files)
         cmd = upload(dist)
+        cmd.show_response = 1
         cmd.ensure_finalized()
         cmd.run()
 
-         # what did we send ?
+        # what did we send ?
         headers = dict(self.last_open.req.headers)
         self.assertEqual(headers['Content-length'], '2087')
         content_type = headers['Content-type']
         self.assertEqual(self.last_open.req.get_full_url(), expected_url)
         self.assertTrue(b'xxx' in self.last_open.req.data)
 
+        # The PyPI response body was echoed
+        results = self.get_logs(INFO)
+        self.assertIn('xyzzy\n', results[-1])
+
+
 def test_suite():
     return unittest.makeSuite(uploadTestCase)
 
 Library
 -------
 
+- Fix TypeError on "setup.py upload --show-response".
+
 - Issue #20045: Fix "setup.py register --list-classifiers".
 
 - Issue #18879: When a method is looked up on a temporary file, avoid closing