Skip to content

Commit fddf121

Browse files
committed
Issue python#21776: Merge from 3.5
2 parents 0d20189 + 6a8e626 commit fddf121

3 files changed

Lines changed: 40 additions & 8 deletions

File tree

‎Lib/distutils/command/upload.py‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -181,21 +181,21 @@ def upload_file(self, command, pyversion, filename):
181181
result = urlopen(request)
182182
status = result.getcode()
183183
reason = result.msg
184-
except OSError as e:
185-
self.announce(str(e), log.ERROR)
186-
raise
187184
except HTTPError as e:
188185
status = e.code
189186
reason = e.msg
187+
except OSError as e:
188+
self.announce(str(e), log.ERROR)
189+
raise
190190

191191
if status == 200:
192192
self.announce('Server response (%s): %s' % (status, reason),
193193
log.INFO)
194+
if self.show_response:
195+
text = self._read_pypi_response(result)
196+
msg = '\n'.join(('-' * 75, text, '-' * 75))
197+
self.announce(msg, log.INFO)
194198
else:
195199
msg = 'Upload failed (%s): %s' % (status, reason)
196200
self.announce(msg, log.ERROR)
197201
raise DistutilsError(msg)
198-
if self.show_response:
199-
text = self._read_pypi_response(result)
200-
msg = '\n'.join(('-' * 75, text, '-' * 75))
201-
self.announce(msg, log.INFO)

‎Lib/distutils/tests/test_upload.py‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
"""Tests for distutils.command.upload."""
22
import os
33
import unittest
4+
import unittest.mock as mock
5+
from urllib.request import HTTPError
6+
47
from test.support import run_unittest
58

69
from distutils.command import upload as upload_mod
710
from distutils.command.upload import upload
811
from distutils.core import Distribution
912
from distutils.errors import DistutilsError
10-
from distutils.log import INFO
13+
from distutils.log import ERROR, INFO
1114

1215
from distutils.tests.test_config import PYPIRC, PyPIRCCommandTestCase
1316

@@ -144,6 +147,32 @@ def test_upload_fails(self):
144147
self.next_code = 404
145148
self.assertRaises(DistutilsError, self.test_upload)
146149

150+
def test_wrong_exception_order(self):
151+
tmp = self.mkdtemp()
152+
path = os.path.join(tmp, 'xxx')
153+
self.write_file(path)
154+
dist_files = [('xxx', '2.6', path)] # command, pyversion, filename
155+
self.write_file(self.rc, PYPIRC_LONG_PASSWORD)
156+
157+
pkg_dir, dist = self.create_dist(dist_files=dist_files)
158+
tests = [
159+
(OSError('oserror'), 'oserror', OSError),
160+
(HTTPError('url', 400, 'httperror', {}, None),
161+
'Upload failed (400): httperror', DistutilsError),
162+
]
163+
for exception, expected, raised_exception in tests:
164+
with self.subTest(exception=type(exception).__name__):
165+
with mock.patch('distutils.command.upload.urlopen',
166+
new=mock.Mock(side_effect=exception)):
167+
with self.assertRaises(raised_exception):
168+
cmd = upload(dist)
169+
cmd.ensure_finalized()
170+
cmd.run()
171+
results = self.get_logs(ERROR)
172+
self.assertIn(expected, results[-1])
173+
self.clear_logs()
174+
175+
147176
def test_suite():
148177
return unittest.makeSuite(uploadTestCase)
149178

‎Misc/NEWS‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ Core and Builtins
2222
Library
2323
-------
2424

25+
- Issue #21776: distutils.upload now correctly handles HTTPError.
26+
Initial patch by Claudiu Popa.
27+
2528
- Issue #26526: Replace custom parse tree validation in the parser
2629
module with a simple DFA validator.
2730

0 commit comments

Comments
 (0)