##// END OF EJS Templates
fancyopts: prevent mutation of the default value in customopts...
Daniel Ploch -
r37110:ef6215df default
parent child Browse files
Show More
@@ -208,11 +208,18 b' class customopt(object):'
208 __metaclass__ = abc.ABCMeta
208 __metaclass__ = abc.ABCMeta
209
209
210 def __init__(self, defaultvalue):
210 def __init__(self, defaultvalue):
211 self.defaultvalue = defaultvalue
211 self._defaultvalue = defaultvalue
212
212
213 def _isboolopt(self):
213 def _isboolopt(self):
214 return False
214 return False
215
215
216 def getdefaultvalue(self):
217 """Returns the default value for this opt.
218
219 Subclasses should override this to return a new value if the value type
220 is mutable."""
221 return self._defaultvalue
222
216 @abc.abstractmethod
223 @abc.abstractmethod
217 def newstate(self, oldstate, newparam, abort):
224 def newstate(self, oldstate, newparam, abort):
218 """Adds newparam to oldstate and returns the new state.
225 """Adds newparam to oldstate and returns the new state.
@@ -221,7 +228,7 b' class customopt(object):'
221
228
222 class _simpleopt(customopt):
229 class _simpleopt(customopt):
223 def _isboolopt(self):
230 def _isboolopt(self):
224 return isinstance(self.defaultvalue, (bool, type(None)))
231 return isinstance(self._defaultvalue, (bool, type(None)))
225
232
226 def newstate(self, oldstate, newparam, abort):
233 def newstate(self, oldstate, newparam, abort):
227 return newparam
234 return newparam
@@ -235,6 +242,9 b' class _callableopt(customopt):'
235 return self.callablefn(newparam)
242 return self.callablefn(newparam)
236
243
237 class _listopt(customopt):
244 class _listopt(customopt):
245 def getdefaultvalue(self):
246 return self._defaultvalue[:]
247
238 def newstate(self, oldstate, newparam, abort):
248 def newstate(self, oldstate, newparam, abort):
239 oldstate.append(newparam)
249 oldstate.append(newparam)
240 return oldstate
250 return oldstate
@@ -313,7 +323,7 b' def fancyopts(args, options, state, gnu='
313 defmap[name] = _defaultopt(default)
323 defmap[name] = _defaultopt(default)
314
324
315 # copy defaults to state
325 # copy defaults to state
316 state[name] = defmap[name].defaultvalue
326 state[name] = defmap[name].getdefaultvalue()
317
327
318 # does it take a parameter?
328 # does it take a parameter?
319 if not defmap[name]._isboolopt():
329 if not defmap[name]._isboolopt():
@@ -87,7 +87,7 b' def optrst(header, options, verbose):'
87 lo = '--' + longopt
87 lo = '--' + longopt
88
88
89 if isinstance(default, fancyopts.customopt):
89 if isinstance(default, fancyopts.customopt):
90 default = default.defaultvalue
90 default = default.getdefaultvalue()
91 if default and not callable(default):
91 if default and not callable(default):
92 # default is of unknown type, and in Python 2 we abused
92 # default is of unknown type, and in Python 2 we abused
93 # the %s-shows-repr property to handle integers etc. To
93 # the %s-shows-repr property to handle integers etc. To
General Comments 0
You need to be logged in to leave comments. Login now