]> granicus.if.org Git - python/commitdiff
Issue #21776: distutils.upload now correctly handles HTTPError
authorBerker Peksag <berker.peksag@gmail.com>
Thu, 2 Jun 2016 20:45:53 +0000 (13:45 -0700)
committerBerker Peksag <berker.peksag@gmail.com>
Thu, 2 Jun 2016 20:45:53 +0000 (13:45 -0700)
Initial patch by Claudiu Popa.

Lib/distutils/command/upload.py
Lib/distutils/tests/test_upload.py
Misc/NEWS

index 1c4fc48a12935993f747e08cfef583a14085fcb7..0afcbf233e7c96b22c75f52e75146e80c10bd9e1 100644 (file)
@@ -181,21 +181,21 @@ class upload(PyPIRCCommand):
             result = urlopen(request)
             status = result.getcode()
             reason = result.msg
-        except OSError as e:
-            self.announce(str(e), log.ERROR)
-            raise
         except HTTPError as e:
             status = e.code
             reason = e.msg
+        except OSError as e:
+            self.announce(str(e), log.ERROR)
+            raise
 
         if status == 200:
             self.announce('Server response (%s): %s' % (status, reason),
                           log.INFO)
+            if self.show_response:
+                text = self._read_pypi_response(result)
+                msg = '\n'.join(('-' * 75, text, '-' * 75))
+                self.announce(msg, log.INFO)
         else:
             msg = 'Upload failed (%s): %s' % (status, reason)
             self.announce(msg, log.ERROR)
             raise DistutilsError(msg)
-        if self.show_response:
-            text = self._read_pypi_response(result)
-            msg = '\n'.join(('-' * 75, text, '-' * 75))
-            self.announce(msg, log.INFO)
index dccaf77e3e1806304ad9c4afc3f073eb4667c2ee..19193d5b05fb09d020693579ace4d0cf88d44337 100644 (file)
@@ -1,13 +1,16 @@
 """Tests for distutils.command.upload."""
 import os
 import unittest
+import unittest.mock as mock
+from urllib.request import HTTPError
+
 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.errors import DistutilsError
-from distutils.log import INFO
+from distutils.log import ERROR, INFO
 
 from distutils.tests.test_config import PYPIRC, PyPIRCCommandTestCase
 
@@ -144,6 +147,32 @@ class uploadTestCase(PyPIRCCommandTestCase):
         self.next_code = 404
         self.assertRaises(DistutilsError, self.test_upload)
 
+    def test_wrong_exception_order(self):
+        tmp = self.mkdtemp()
+        path = os.path.join(tmp, 'xxx')
+        self.write_file(path)
+        dist_files = [('xxx', '2.6', path)]  # command, pyversion, filename
+        self.write_file(self.rc, PYPIRC_LONG_PASSWORD)
+
+        pkg_dir, dist = self.create_dist(dist_files=dist_files)
+        tests = [
+            (OSError('oserror'), 'oserror', OSError),
+            (HTTPError('url', 400, 'httperror', {}, None),
+             'Upload failed (400): httperror', DistutilsError),
+        ]
+        for exception, expected, raised_exception in tests:
+            with self.subTest(exception=type(exception).__name__):
+                with mock.patch('distutils.command.upload.urlopen',
+                                new=mock.Mock(side_effect=exception)):
+                    with self.assertRaises(raised_exception):
+                        cmd = upload(dist)
+                        cmd.ensure_finalized()
+                        cmd.run()
+                    results = self.get_logs(ERROR)
+                    self.assertIn(expected, results[-1])
+                    self.clear_logs()
+
+
 def test_suite():
     return unittest.makeSuite(uploadTestCase)
 
index e871a3688105a6a95179b7795a44eb7244bf0cdf..6e07c8caea4cf0139768bc42a2136efb78f6a9d3 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -128,6 +128,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #21776: distutils.upload now correctly handles HTTPError.
+  Initial patch by Claudiu Popa.
+
 - Issue #27114: Fix SSLContext._load_windows_store_certs fails with
   PermissionError