Mercurial > public > mercurial-scm > hg-stable
comparison mercurial/pycompat.py @ 30681:caf7e1c5efe4
py3: have a bytes version of shlex.split()
shlex.split() only accepts unicodes on Python 3. After this patch we will be
using pycompat.shlexsplit(). This patch also replaces existing occurences of
shlex.split with pycompat.shlexsplit.
author | Pulkit Goyal <7895pulkit@gmail.com> |
---|---|
date | Sun, 25 Dec 2016 03:06:55 +0530 |
parents | 3fcaf0f660ce |
children | 6a70cf94d1b5 |
comparison
equal
deleted
inserted
replaced
30680:c80c16a8a0b0 | 30681:caf7e1c5efe4 |
---|---|
10 | 10 |
11 from __future__ import absolute_import | 11 from __future__ import absolute_import |
12 | 12 |
13 import getopt | 13 import getopt |
14 import os | 14 import os |
15 import shlex | |
15 import sys | 16 import sys |
16 | 17 |
17 ispy3 = (sys.version_info[0] >= 3) | 18 ispy3 = (sys.version_info[0] >= 3) |
18 | 19 |
19 if not ispy3: | 20 if not ispy3: |
120 # again as we need to deal with bytes. | 121 # again as we need to deal with bytes. |
121 def byteskwargs(dic): | 122 def byteskwargs(dic): |
122 dic = dict((k.encode('latin-1'), v) for k, v in dic.iteritems()) | 123 dic = dict((k.encode('latin-1'), v) for k, v in dic.iteritems()) |
123 return dic | 124 return dic |
124 | 125 |
126 # shlex.split() accepts unicodes on Python 3. This function takes bytes | |
127 # argument, convert it into unicodes, pass into shlex.split(), convert the | |
128 # returned value to bytes and return that. | |
129 # TODO: handle shlex.shlex(). | |
130 def shlexsplit(s): | |
131 ret = shlex.split(s.decode('latin-1')) | |
132 return [a.encode('latin-1') for a in ret] | |
133 | |
125 else: | 134 else: |
126 def sysstr(s): | 135 def sysstr(s): |
127 return s | 136 return s |
128 | 137 |
129 # Partial backport from os.py in Python 3, which only accepts bytes. | 138 # Partial backport from os.py in Python 3, which only accepts bytes. |
160 sysargv = sys.argv | 169 sysargv = sys.argv |
161 sysplatform = sys.platform | 170 sysplatform = sys.platform |
162 getcwd = os.getcwd | 171 getcwd = os.getcwd |
163 osgetenv = os.getenv | 172 osgetenv = os.getenv |
164 sysexecutable = sys.executable | 173 sysexecutable = sys.executable |
174 shlexsplit = shlex.split | |
165 | 175 |
166 stringio = io.StringIO | 176 stringio = io.StringIO |
167 empty = _queue.Empty | 177 empty = _queue.Empty |
168 queue = _queue.Queue | 178 queue = _queue.Queue |
169 | 179 |