Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2e4d3b1

Browse files
committed
Issue #21722: The distutils "upload" command now exits with a non-zero return code when uploading fails.
Patch by Martin Dengler.
1 parent 845fd9a commit 2e4d3b1

4 files changed

Lines changed: 24 additions & 11 deletions

File tree

Lib/distutils/command/upload.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22
33
Implements the Distutils 'upload' subcommand (upload package to PyPI)."""
44

5-
from distutils.errors import *
6-
from distutils.core import PyPIRCCommand
7-
from distutils.spawn import spawn
8-
from distutils import log
95
import sys
106
import os, io
117
import socket
128
import platform
139
from base64 import standard_b64encode
1410
from urllib.request import urlopen, Request, HTTPError
1511
from urllib.parse import urlparse
12+
from distutils.errors import DistutilsError, DistutilsOptionError
13+
from distutils.core import PyPIRCCommand
14+
from distutils.spawn import spawn
15+
from distutils import log
1616

1717
# this keeps compatibility for 2.3 and 2.4
1818
if sys.version < "2.5":
@@ -184,7 +184,7 @@ def upload_file(self, command, pyversion, filename):
184184
reason = result.msg
185185
except OSError as e:
186186
self.announce(str(e), log.ERROR)
187-
return
187+
raise
188188
except HTTPError as e:
189189
status = e.code
190190
reason = e.msg
@@ -193,8 +193,9 @@ def upload_file(self, command, pyversion, filename):
193193
self.announce('Server response (%s): %s' % (status, reason),
194194
log.INFO)
195195
else:
196-
self.announce('Upload failed (%s): %s' % (status, reason),
197-
log.ERROR)
196+
msg = 'Upload failed (%s): %s' % (status, reason)
197+
self.announce(msg, log.ERROR)
198+
raise DistutilsError(msg)
198199
if self.show_response:
199200
text = self._read_pypi_response(result)
200201
msg = '\n'.join(('-' * 75, text, '-' * 75))

Lib/distutils/tests/test_upload.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from distutils.command import upload as upload_mod
77
from distutils.command.upload import upload
88
from distutils.core import Distribution
9+
from distutils.errors import DistutilsError
910
from distutils.log import INFO
1011

1112
from distutils.tests.test_config import PYPIRC, PyPIRCCommandTestCase
@@ -41,13 +42,14 @@
4142

4243
class FakeOpen(object):
4344

44-
def __init__(self, url):
45+
def __init__(self, url, msg=None, code=None):
4546
self.url = url
4647
if not isinstance(url, str):
4748
self.req = url
4849
else:
4950
self.req = None
50-
self.msg = 'OK'
51+
self.msg = msg or 'OK'
52+
self.code = code or 200
5153

5254
def getheader(self, name, default=None):
5355
return {
@@ -58,7 +60,7 @@ def read(self):
5860
return b'xyzzy'
5961

6062
def getcode(self):
61-
return 200
63+
return self.code
6264

6365

6466
class uploadTestCase(PyPIRCCommandTestCase):
@@ -68,13 +70,15 @@ def setUp(self):
6870
self.old_open = upload_mod.urlopen
6971
upload_mod.urlopen = self._urlopen
7072
self.last_open = None
73+
self.next_msg = None
74+
self.next_code = None
7175

7276
def tearDown(self):
7377
upload_mod.urlopen = self.old_open
7478
super(uploadTestCase, self).tearDown()
7579

7680
def _urlopen(self, url):
77-
self.last_open = FakeOpen(url)
81+
self.last_open = FakeOpen(url, msg=self.next_msg, code=self.next_code)
7882
return self.last_open
7983

8084
def test_finalize_options(self):
@@ -135,6 +139,10 @@ def test_upload(self):
135139
results = self.get_logs(INFO)
136140
self.assertIn('xyzzy\n', results[-1])
137141

142+
def test_upload_fails(self):
143+
self.next_msg = "Not Found"
144+
self.next_code = 404
145+
self.assertRaises(DistutilsError, self.test_upload)
138146

139147
def test_suite():
140148
return unittest.makeSuite(uploadTestCase)

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ Vincent Delft
308308
Arnaud Delobelle
309309
Konrad Delong
310310
Erik Demaine
311+
Martin Dengler
311312
John Dennis
312313
L. Peter Deutsch
313314
Roger Dev

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ Core and Builtins
2727
Library
2828
-------
2929

30+
- Issue #21722: The distutils "upload" command now exits with a non-zero
31+
return code when uploading fails. Patch by Martin Dengler.
32+
3033
- Issue #21723: asyncio.Queue: support any type of number (ex: float) for the
3134
maximum size. Patch written by Vajrasky Kok.
3235

0 commit comments

Comments
 (0)