comparison mercurial/pycompat.py @ 35235:5b569d512fbd

fancyopts: use getopt.gnu_getopt() The issue described in the docstring has been fixed since Python 20ab2260dc93, which is in 2.7. https://hg.python.org/cpython/rev/20ab2260dc93 https://bugs.python.org/issue4458 This fixes the handling of '--' value.
author Yuya Nishihara <yuya@tcha.org>
date Fri, 24 Nov 2017 01:09:00 +0900
parents c0a6c19690ff
children e66d6e938d2d
comparison
equal deleted inserted replaced
35234:7ce0ba3a1c32 35235:5b569d512fbd
212 unicode = str 212 unicode = str
213 213
214 def open(name, mode='r', buffering=-1): 214 def open(name, mode='r', buffering=-1):
215 return builtins.open(name, sysstr(mode), buffering) 215 return builtins.open(name, sysstr(mode), buffering)
216 216
217 def getoptb(args, shortlist, namelist): 217 def _getoptbwrapper(orig, args, shortlist, namelist):
218 """ 218 """
219 Takes bytes arguments, converts them to unicode, pass them to 219 Takes bytes arguments, converts them to unicode, pass them to
220 getopt.getopt(), convert the returned values back to bytes and then 220 getopt.getopt(), convert the returned values back to bytes and then
221 return them for Python 3 compatibility as getopt.getopt() don't accepts 221 return them for Python 3 compatibility as getopt.getopt() don't accepts
222 bytes on Python 3. 222 bytes on Python 3.
223 """ 223 """
224 args = [a.decode('latin-1') for a in args] 224 args = [a.decode('latin-1') for a in args]
225 shortlist = shortlist.decode('latin-1') 225 shortlist = shortlist.decode('latin-1')
226 namelist = [a.decode('latin-1') for a in namelist] 226 namelist = [a.decode('latin-1') for a in namelist]
227 opts, args = getopt.getopt(args, shortlist, namelist) 227 opts, args = orig(args, shortlist, namelist)
228 opts = [(a[0].encode('latin-1'), a[1].encode('latin-1')) 228 opts = [(a[0].encode('latin-1'), a[1].encode('latin-1'))
229 for a in opts] 229 for a in opts]
230 args = [a.encode('latin-1') for a in args] 230 args = [a.encode('latin-1') for a in args]
231 return opts, args 231 return opts, args
232 232
289 fsdecode = identity 289 fsdecode = identity
290 290
291 def getdoc(obj): 291 def getdoc(obj):
292 return getattr(obj, '__doc__', None) 292 return getattr(obj, '__doc__', None)
293 293
294 def getoptb(args, shortlist, namelist): 294 def _getoptbwrapper(orig, args, shortlist, namelist):
295 return getopt.getopt(args, shortlist, namelist) 295 return orig(args, shortlist, namelist)
296 296
297 strkwargs = identity 297 strkwargs = identity
298 byteskwargs = identity 298 byteskwargs = identity
299 299
300 oslinesep = os.linesep 300 oslinesep = os.linesep
318 isjython = sysplatform.startswith('java') 318 isjython = sysplatform.startswith('java')
319 319
320 isdarwin = sysplatform == 'darwin' 320 isdarwin = sysplatform == 'darwin'
321 isposix = osname == 'posix' 321 isposix = osname == 'posix'
322 iswindows = osname == 'nt' 322 iswindows = osname == 'nt'
323
324 def getoptb(args, shortlist, namelist):
325 return _getoptbwrapper(getopt.getopt, args, shortlist, namelist)
326
327 def gnugetoptb(args, shortlist, namelist):
328 return _getoptbwrapper(getopt.gnu_getopt, args, shortlist, namelist)