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

Skip to content

Commit fc773a2

Browse files
committed
Avoid “error: None” messages from distutils (#4931).
Thanks to Amaury Forgeot d’Arc and Philip J. Eby.
1 parent ed5d95b commit fc773a2

6 files changed

Lines changed: 20 additions & 38 deletions

File tree

Doc/distutils/apiref.rst

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,15 +1171,6 @@ other utility module.
11711171
underscore. No { } or ( ) style quoting is available.
11721172

11731173

1174-
.. function:: grok_environment_error(exc[, prefix='error: '])
1175-
1176-
Generate a useful error message from an :exc:`OSError` exception object.
1177-
Handles Python 1.5.1 and later styles, and does what it can to deal with
1178-
exception objects that don't have a filename (which happens when the error
1179-
is due to a two-file operation, such as :func:`~os.rename` or :func:`~os.link`).
1180-
Returns the error message as a string prefixed with *prefix*.
1181-
1182-
11831174
.. function:: split_quoted(s)
11841175

11851176
Split a string up according to Unix shell-like rules for quotes and backslashes.

Lib/distutils/core.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
from distutils.debug import DEBUG
1313
from distutils.errors import *
14-
from distutils.util import grok_environment_error
1514

1615
# Mainly import these so setup scripts can "from distutils.core import" them.
1716
from distutils.dist import Distribution
@@ -149,13 +148,11 @@ class found in 'cmdclass' is used in place of the default, which is
149148
except KeyboardInterrupt:
150149
raise SystemExit("interrupted")
151150
except (IOError, os.error) as exc:
152-
error = grok_environment_error(exc)
153-
154151
if DEBUG:
155-
sys.stderr.write(error + "\n")
152+
sys.stderr.write("error: %s\n" % (exc,))
156153
raise
157154
else:
158-
raise SystemExit(error)
155+
raise SystemExit("error: %s" % (exc,))
159156

160157
except (DistutilsError,
161158
CCompilerError) as msg:

Lib/distutils/dir_util.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
Utility functions for manipulating directories and directory trees."""
44

5-
import os, sys
5+
import os
66
import errno
77
from distutils.errors import DistutilsFileError, DistutilsInternalError
88
from distutils import log
@@ -182,7 +182,6 @@ def remove_tree(directory, verbose=1, dry_run=0):
182182
Any errors are ignored (apart from being reported to stdout if 'verbose'
183183
is true).
184184
"""
185-
from distutils.util import grok_environment_error
186185
global _path_created
187186

188187
if verbose >= 1:
@@ -199,8 +198,7 @@ def remove_tree(directory, verbose=1, dry_run=0):
199198
if abspath in _path_created:
200199
del _path_created[abspath]
201200
except (IOError, OSError) as exc:
202-
log.warn(grok_environment_error(
203-
exc, "error removing %s: " % directory))
201+
log.warn("error removing %s: %s", directory, exc)
204202

205203
def ensure_relative(path):
206204
"""Take the full path 'path', and make it a relative path.

Lib/distutils/tests/test_util.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
from distutils.errors import DistutilsPlatformError, DistutilsByteCompileError
99
from distutils.util import (get_platform, convert_path, change_root,
1010
check_environ, split_quoted, strtobool,
11-
rfc822_escape, byte_compile)
11+
rfc822_escape, byte_compile,
12+
grok_environment_error)
1213
from distutils import util # used to patch _environ_checked
1314
from distutils.sysconfig import get_config_vars
1415
from distutils import sysconfig
@@ -285,6 +286,13 @@ def test_dont_write_bytecode(self):
285286
finally:
286287
sys.dont_write_bytecode = old_dont_write_bytecode
287288

289+
def test_grok_environment_error(self):
290+
# test obsolete function to ensure backward compat (#4931)
291+
exc = IOError("Unable to find batch file")
292+
msg = grok_environment_error(exc)
293+
self.assertEqual(msg, "error: Unable to find batch file")
294+
295+
288296
def test_suite():
289297
return unittest.makeSuite(UtilTestCase)
290298

Lib/distutils/util.py

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -213,25 +213,10 @@ def _subst (match, local_vars=local_vars):
213213

214214

215215
def grok_environment_error (exc, prefix="error: "):
216-
"""Generate a useful error message from an EnvironmentError (IOError or
217-
OSError) exception object. Handles Python 1.5.1 and 1.5.2 styles, and
218-
does what it can to deal with exception objects that don't have a
219-
filename (which happens when the error is due to a two-file operation,
220-
such as 'rename()' or 'link()'. Returns the error message as a string
221-
prefixed with 'prefix'.
222-
"""
223-
# check for Python 1.5.2-style {IO,OS}Error exception objects
224-
if hasattr(exc, 'filename') and hasattr(exc, 'strerror'):
225-
if exc.filename:
226-
error = prefix + "%s: %s" % (exc.filename, exc.strerror)
227-
else:
228-
# two-argument functions in posix module don't
229-
# include the filename in the exception object!
230-
error = prefix + "%s" % exc.strerror
231-
else:
232-
error = prefix + str(exc.args[-1])
233-
234-
return error
216+
# Function kept for backward compatibility.
217+
# Used to try clever things with EnvironmentErrors,
218+
# but nowadays str(exception) produces good messages.
219+
return prefix + str(exc)
235220

236221

237222
# Needed by 'split_quoted()'

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ Library
1616
- Issue #20875: Prevent possible gzip "'read' is not defined" NameError.
1717
Patch by Claudiu Popa.
1818

19+
- Issue #4931: distutils should not produce unhelpful "error: None" messages
20+
anymore. distutils.util.grok_environment_error is kept but doc-deprecated.
21+
1922
- Issue #20283: RE pattern methods now accept the string keyword parameters
2023
as documented. The pattern and source keyword parameters are left as
2124
deprecated aliases.

0 commit comments

Comments
 (0)