mercurial/pycompat.py
changeset 30578 c6ce11f2ee50
parent 30500 fc0cfe6c87d7
child 30579 fbc3f73dc802
--- a/mercurial/pycompat.py	Tue Dec 06 11:44:49 2016 +0000
+++ b/mercurial/pycompat.py	Tue Dec 06 06:36:36 2016 +0530
@@ -10,6 +10,7 @@
 
 from __future__ import absolute_import
 
+import getopt
 import os
 import sys
 
@@ -87,6 +88,19 @@
     setattr = _wrapattrfunc(builtins.setattr)
     xrange = builtins.range
 
+    # getopt.getopt() on Python 3 deals with unicodes internally so we cannot
+    # pass bytes there. Passing unicodes will result in unicodes as return
+    # values which we need to convert again to bytes.
+    def getoptb(args, shortlist, namelist):
+        args = [a.decode('latin-1') for a in args]
+        shortlist = shortlist.decode('latin-1')
+        namelist = [a.decode('latin-1') for a in namelist]
+        opts, args = getopt.getopt(args, shortlist, namelist)
+        opts = [(a[0].encode('latin-1'), a[1].encode('latin-1'))
+                for a in opts]
+        args = [a.encode('latin-1') for a in args]
+        return opts, args
+
 else:
     def sysstr(s):
         return s
@@ -106,6 +120,9 @@
     def fsdecode(filename):
         return filename
 
+    def getoptb(args, shortlist, namelist):
+        return getopt.getopt(args, shortlist, namelist)
+
     osname = os.name
     ospathsep = os.pathsep
     ossep = os.sep