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

Skip to content

Commit 02a1a2b

Browse files
committed
Cleaned up/simplified error-handling:
- DistutilsOptionError is now documented as it's actually used, ie. to indicate bogus option values (usually user options, eg. from the command-line) - added DistutilsSetupError to indicate errors that definitely arise in the setup script - got rid of DistutilsValueError, and changed all usage of it to either DistutilsSetupError or ValueError as appropriate - simplified a bunch of option get/set methods in Command and Distribution classes -- just pass on AttributeError most of the time, rather than turning it into something else
1 parent 4a3dd2d commit 02a1a2b

7 files changed

Lines changed: 52 additions & 79 deletions

File tree

Lib/distutils/cmd.py

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -161,46 +161,36 @@ def announce (self, msg, level=1):
161161

162162
def get_option (self, option):
163163
"""Return the value of a single option for this command. Raise
164-
DistutilsOptionError if 'option' is not known."""
165-
try:
166-
return getattr (self, option)
167-
except AttributeError:
168-
raise DistutilsOptionError, \
169-
"command %s: no such option %s" % \
170-
(self.get_command_name(), option)
164+
AttributeError if 'option' is not known."""
165+
return getattr (self, option)
171166

172167

173168
def get_options (self, *options):
174169
"""Return (as a tuple) the values of several options for this
175-
command. Raise DistutilsOptionError if any of the options in
170+
command. Raise AttributeError if any of the options in
176171
'options' are not known."""
177172

178173
values = []
179-
try:
180-
for opt in options:
181-
values.append (getattr (self, opt))
182-
except AttributeError, name:
183-
raise DistutilsOptionError, \
184-
"command %s: no such option %s" % \
185-
(self.get_command_name(), name)
186-
174+
for opt in options:
175+
values.append (getattr (self, opt))
176+
187177
return tuple (values)
188-
178+
189179

190180
def set_option (self, option, value):
191181
"""Set the value of a single option for this command. Raise
192-
DistutilsOptionError if 'option' is not known."""
182+
AttributeError if 'option' is not known."""
193183

194184
if not hasattr (self, option):
195-
raise DistutilsOptionError, \
185+
raise AttributeError, \
196186
"command '%s': no such option '%s'" % \
197187
(self.get_command_name(), option)
198188
if value is not None:
199189
setattr (self, option, value)
200190

201191
def set_options (self, **optval):
202192
"""Set the values of several options for this command. Raise
203-
DistutilsOptionError if any of the options specified as
193+
AttributeError if any of the options specified as
204194
keyword arguments are not known."""
205195

206196
for k in optval.keys():
@@ -236,14 +226,10 @@ def set_undefined_options (self, src_cmd, *option_pairs):
236226

237227
src_cmd_obj = self.distribution.find_command_obj (src_cmd)
238228
src_cmd_obj.ensure_ready ()
239-
try:
240-
for (src_option, dst_option) in option_pairs:
241-
if getattr (self, dst_option) is None:
242-
self.set_option (dst_option,
243-
src_cmd_obj.get_option (src_option))
244-
except AttributeError, name:
245-
# duh, which command?
246-
raise DistutilsOptionError, "unknown option %s" % name
229+
for (src_option, dst_option) in option_pairs:
230+
if getattr (self, dst_option) is None:
231+
self.set_option (dst_option,
232+
src_cmd_obj.get_option (src_option))
247233

248234

249235
def find_peer (self, command, create=1):

Lib/distutils/command/build_clib.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,33 +115,33 @@ def check_library_list (self, libraries):
115115
"""Ensure that the list of libraries (presumably provided as a
116116
command option 'libraries') is valid, i.e. it is a list of
117117
2-tuples, where the tuples are (library_name, build_info_dict).
118-
Raise DistutilsValueError if the structure is invalid anywhere;
118+
Raise DistutilsSetupError if the structure is invalid anywhere;
119119
just returns otherwise."""
120120

121121
# Yechh, blecch, ackk: this is ripped straight out of build_ext.py,
122122
# with only names changed to protect the innocent!
123123

124124
if type (libraries) is not ListType:
125-
raise DistutilsValueError, \
125+
raise DistutilsSetupError, \
126126
"'libraries' option must be a list of tuples"
127127

128128
for lib in libraries:
129129
if type (lib) is not TupleType and len (lib) != 2:
130-
raise DistutilsValueError, \
130+
raise DistutilsSetupError, \
131131
"each element of 'libraries' must a 2-tuple"
132132

133133
if type (lib[0]) is not StringType:
134-
raise DistutilsValueError, \
134+
raise DistutilsSetupError, \
135135
"first element of each tuple in 'libraries' " + \
136136
"must be a string (the library name)"
137137
if '/' in lib[0] or (os.sep != '/' and os.sep in lib[0]):
138-
raise DistutilsValueError, \
138+
raise DistutilsSetupError, \
139139
("bad library name '%s': " +
140140
"may not contain directory separators") % \
141141
lib[0]
142142

143143
if type (lib[1]) is not DictionaryType:
144-
raise DistutilsValueError, \
144+
raise DistutilsSetupError, \
145145
"second element of each tuple in 'libraries' " + \
146146
"must be a dictionary (build info)"
147147
# for lib
@@ -171,7 +171,7 @@ def build_libraries (self, libraries):
171171
for (lib_name, build_info) in libraries:
172172
sources = build_info.get ('sources')
173173
if sources is None or type (sources) not in (ListType, TupleType):
174-
raise DistutilsValueError, \
174+
raise DistutilsSetupError, \
175175
("in 'libraries' option (library '%s'), " +
176176
"'sources' must be present and must be " +
177177
"a list of source filenames") % lib_name

Lib/distutils/command/build_ext.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -205,26 +205,26 @@ def check_extensions_list (self, extensions):
205205
"""Ensure that the list of extensions (presumably provided as a
206206
command option 'extensions') is valid, i.e. it is a list of
207207
2-tuples, where the tuples are (extension_name, build_info_dict).
208-
Raise DistutilsValueError if the structure is invalid anywhere;
208+
Raise DistutilsSetupError if the structure is invalid anywhere;
209209
just returns otherwise."""
210210

211211
if type (extensions) is not ListType:
212-
raise DistutilsValueError, \
212+
raise DistutilsSetupError, \
213213
"'ext_modules' option must be a list of tuples"
214214

215215
for ext in extensions:
216216
if type (ext) is not TupleType and len (ext) != 2:
217-
raise DistutilsValueError, \
217+
raise DistutilsSetupError, \
218218
"each element of 'ext_modules' option must be a 2-tuple"
219219

220220
if not (type (ext[0]) is StringType and
221221
extension_name_re.match (ext[0])):
222-
raise DistutilsValueError, \
222+
raise DistutilsSetupError, \
223223
"first element of each tuple in 'ext_modules' " + \
224224
"must be the extension name (a string)"
225225

226226
if type (ext[1]) is not DictionaryType:
227-
raise DistutilsValueError, \
227+
raise DistutilsSetupError, \
228228
"second element of each tuple in 'ext_modules' " + \
229229
"must be a dictionary (build info)"
230230

@@ -274,7 +274,7 @@ def build_extensions (self):
274274
for (extension_name, build_info) in self.extensions:
275275
sources = build_info.get ('sources')
276276
if sources is None or type (sources) not in (ListType, TupleType):
277-
raise DistutilsValueError, \
277+
raise DistutilsSetupError, \
278278
("in 'ext_modules' option (extension '%s'), " +
279279
"'sources' must be present and must be " +
280280
"a list of source filenames") % extension_name

Lib/distutils/dist.py

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ def __init__ (self, attrs=None):
152152
if hasattr (self, key):
153153
setattr (self, key, val)
154154
else:
155-
raise DistutilsOptionError, \
155+
raise DistutilsSetupError, \
156156
"invalid distribution option '%s'" % key
157157

158158
# __init__ ()
@@ -447,27 +447,18 @@ def run_commands (self):
447447

448448
def get_option (self, option):
449449
"""Return the value of a distribution option. Raise
450-
DistutilsOptionError if 'option' is not known."""
451-
452-
try:
453-
return getattr (self, opt)
454-
except AttributeError:
455-
raise DistutilsOptionError, \
456-
"unknown distribution option %s" % option
450+
AttributeError if 'option' is not known."""
451+
return getattr (self, opt)
457452

458453

459454
def get_options (self, *options):
460455
"""Return (as a tuple) the values of several distribution
461-
options. Raise DistutilsOptionError if any element of
456+
options. Raise AttributeError if any element of
462457
'options' is not known."""
463458

464459
values = []
465-
try:
466-
for opt in options:
467-
values.append (getattr (self, opt))
468-
except AttributeError, name:
469-
raise DistutilsOptionError, \
470-
"unknown distribution option %s" % name
460+
for opt in options:
461+
values.append (getattr (self, opt))
471462

472463
return tuple (values)
473464

@@ -498,17 +489,12 @@ def run_command (self, command):
498489
def get_command_option (self, command, option):
499490
"""Create a command object for 'command' if necessary, ensure that
500491
its option values are all set to their final values, and return
501-
the value of its 'option' option. Raise DistutilsOptionError if
492+
the value of its 'option' option. Raise AttributeError if
502493
'option' is not known for that 'command'."""
503494

504495
cmd_obj = self.find_command_obj (command)
505496
cmd_obj.ensure_ready ()
506497
return cmd_obj.get_option (option)
507-
try:
508-
return getattr (cmd_obj, option)
509-
except AttributeError:
510-
raise DistutilsOptionError, \
511-
"command %s: no such option %s" % (command, option)
512498

513499

514500
def get_command_options (self, command, *options):
@@ -521,12 +507,8 @@ def get_command_options (self, command, *options):
521507
cmd_obj = self.find_command_obj (command)
522508
cmd_obj.ensure_ready ()
523509
values = []
524-
try:
525-
for opt in options:
526-
values.append (getattr (cmd_obj, option))
527-
except AttributeError, name:
528-
raise DistutilsOptionError, \
529-
"command %s: no such option %s" % (command, name)
510+
for opt in options:
511+
values.append (getattr (cmd_obj, option))
530512

531513
return tuple (values)
532514

Lib/distutils/errors.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,18 @@ class DistutilsArgError (DistutilsError):
4646
class DistutilsFileError (DistutilsError):
4747
pass
4848

49-
# DistutilsOptionError is raised anytime an attempt is made to access
50-
# (get or set) an option that does not exist for a particular command
51-
# (or for the distribution itself).
49+
# DistutilsOptionError is raised for syntactic/semantic errors in
50+
# command options, such as use of mutually conflicting options, or
51+
# inconsistent options, badly-spelled values, etc. No distinction is
52+
# made between option values originating in the setup script, the
53+
# command line, config files, or what-have-you.
5254
class DistutilsOptionError (DistutilsError):
5355
pass
5456

55-
# DistutilsValueError is raised anytime an option value (presumably
56-
# provided by setup.py) is invalid.
57-
class DistutilsValueError (DistutilsError):
57+
# DistutilsSetupError is raised for errors that can be definitely
58+
# blamed on the setup script, such as invalid keyword arguments to
59+
# 'setup()'.
60+
class DistutilsSetupError (DistutilsError):
5861
pass
5962

6063
# DistutilsPlatformError is raised when we find that we don't
@@ -82,7 +85,6 @@ class DistutilsInternalError (DistutilsError):
8285
DistutilsArgError = 'DistutilsArgError'
8386
DistutilsFileError = 'DistutilsFileError'
8487
DistutilsOptionError = 'DistutilsOptionError'
85-
DistutilsValueError = 'DistutilsValueError'
8688
DistutilsPlatformError = 'DistutilsPlatformError'
8789
DistutilsExecError = 'DistutilsExecError'
8890
DistutilsInternalError = 'DistutilsInternalError'

Lib/distutils/msvccompiler.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,9 @@ def link_shared_object (self,
360360
if extra_postargs:
361361
ld_args.extend (extra_postargs)
362362

363+
print "link_shared_object():"
364+
print " output_filename =", output_filename
365+
print " mkpath'ing:", os.path.dirname (output_filename)
363366
self.mkpath (os.path.dirname (output_filename))
364367
self.spawn ([self.link] + ld_args)
365368

Lib/distutils/util.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,16 @@ def native_path (pathname):
4040
using the current directory separator. Needed because filenames in
4141
the setup script are always supplied in Unix style, and have to be
4242
converted to the local convention before we can actually use them in
43-
the filesystem. Raises DistutilsValueError if 'pathname' is
43+
the filesystem. Raises ValueError if 'pathname' is
4444
absolute (starts with '/') or contains local directory separators
4545
(unless the local separator is '/', of course)."""
4646

4747
if pathname[0] == '/':
48-
raise DistutilsValueError, "path '%s' cannot be absolute" % pathname
48+
raise ValueError, "path '%s' cannot be absolute" % pathname
4949
if pathname[-1] == '/':
50-
raise DistutilsValueError, "path '%s' cannot end with '/'" % pathname
50+
raise ValueError, "path '%s' cannot end with '/'" % pathname
5151
if os.sep != '/' and os.sep in pathname:
52-
raise DistutilsValueError, \
52+
raise ValueError, \
5353
"path '%s' cannot contain '%c' character" % \
5454
(pathname, os.sep)
5555

0 commit comments

Comments
 (0)