]> granicus.if.org Git - python/commitdiff
Fix TypeError on "setup.py upload --show-response".
authorAntoine Pitrou <solipsis@pitrou.net>
Sun, 22 Dec 2013 17:13:51 +0000 (18:13 +0100)
committerAntoine Pitrou <solipsis@pitrou.net>
Sun, 22 Dec 2013 17:13:51 +0000 (18:13 +0100)
Lib/distutils/command/register.py
Lib/distutils/command/upload.py
Lib/distutils/config.py
Lib/distutils/tests/test_upload.py
Misc/NEWS

index 9b39ed36aa4642494e471bc9873664fbd8ea3a84..55656c23538836074a3a31a0a810003cd6f64bed 100644 (file)
@@ -5,7 +5,6 @@ Implements the Distutils 'register' command (register with the repository).
 
 # created 2002/10/21, Richard Jones
 
-import cgi
 import os, string, getpass
 import io
 import urllib.parse, urllib.request
@@ -88,9 +87,7 @@ class register(PyPIRCCommand):
         '''
         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.
index d2bc82cea32ac1cfa2200783392ab404cd8f8a3f..e30c18979114e047a54a1a24deb5c95fe7d5fd14 100644 (file)
@@ -196,5 +196,6 @@ class upload(PyPIRCCommand):
             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)
index a97635f2c0a0249b357f84ec9d482b54c2db8236..7e10fff9d9d32f44d335370350c6a984d96debd8 100644 (file)
@@ -3,6 +3,7 @@
 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
 
@@ -101,6 +102,12 @@ class PyPIRCCommand(Command):
 
         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
index fbf80e3804e14139da36714978a9a6034108c613..8532369aa50a4eee4007373c35fac0578815968b 100644 (file)
@@ -6,6 +6,7 @@ from test.support import run_unittest
 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
 
@@ -48,6 +49,14 @@ class FakeOpen(object):
             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
 
@@ -108,10 +117,11 @@ class uploadTestCase(PyPIRCCommandTestCase):
         # 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')
         self.assertTrue(headers['Content-type'].startswith('multipart/form-data'))
@@ -120,6 +130,11 @@ class uploadTestCase(PyPIRCCommandTestCase):
                          'https://pypi.python.org/pypi')
         self.assertIn(b'xxx', 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)
 
index e8716b2dd78bd3d8505f390fe38ae60f1280d702..8dddb6afe647b84d12ef490b493c20d158744489 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -29,6 +29,8 @@ Core and Builtins
 Library
 -------
 
+- Fix TypeError on "setup.py upload --show-response".
+
 - Issue #12226: HTTPS is now used by default when connecting to PyPI.
 
 - Issue #20045: Fix "setup.py register --list-classifiers".