Show More
@@ -11,21 +11,26 b' This contains aliases to hide python ver' | |||||
11 | from __future__ import absolute_import |
|
11 | from __future__ import absolute_import | |
12 |
|
12 | |||
13 | import builtins |
|
13 | import builtins | |
|
14 | import codecs | |||
14 | import concurrent.futures as futures |
|
15 | import concurrent.futures as futures | |
|
16 | import functools | |||
15 | import getopt |
|
17 | import getopt | |
16 | import http.client as httplib |
|
18 | import http.client as httplib | |
17 | import http.cookiejar as cookielib |
|
19 | import http.cookiejar as cookielib | |
18 | import inspect |
|
20 | import inspect | |
|
21 | import io | |||
19 | import json |
|
22 | import json | |
20 | import os |
|
23 | import os | |
21 | import pickle |
|
24 | import pickle | |
22 | import queue |
|
25 | import queue | |
23 | import shlex |
|
26 | import shlex | |
24 | import socketserver |
|
27 | import socketserver | |
|
28 | import struct | |||
25 | import sys |
|
29 | import sys | |
26 | import tempfile |
|
30 | import tempfile | |
27 | import xmlrpc.client as xmlrpclib |
|
31 | import xmlrpc.client as xmlrpclib | |
28 |
|
32 | |||
|
33 | ||||
29 | ispy3 = sys.version_info[0] >= 3 |
|
34 | ispy3 = sys.version_info[0] >= 3 | |
30 | ispypy = '__pypy__' in sys.builtin_module_names |
|
35 | ispypy = '__pypy__' in sys.builtin_module_names | |
31 | TYPE_CHECKING = False |
|
36 | TYPE_CHECKING = False | |
@@ -82,13 +87,6 b' def rapply(f, xs):' | |||||
82 | return _rapply(f, xs) |
|
87 | return _rapply(f, xs) | |
83 |
|
88 | |||
84 |
|
89 | |||
85 | if ispy3: |
|
|||
86 | import builtins |
|
|||
87 | import codecs |
|
|||
88 | import functools |
|
|||
89 | import io |
|
|||
90 | import struct |
|
|||
91 |
|
||||
92 |
|
|
90 | if os.name == r'nt' and sys.version_info >= (3, 6): | |
93 |
|
|
91 | # MBCS (or ANSI) filesystem encoding must be used as before. | |
94 |
|
|
92 | # Otherwise non-ASCII filenames in existing repositories would be | |
@@ -117,15 +115,19 b' if ispy3:' | |||||
117 |
|
|
115 | # TODO deprecate stringio name, as it is a lie on Python 3. | |
118 |
|
|
116 | stringio = bytesio | |
119 |
|
117 | |||
|
118 | ||||
120 |
|
|
119 | def maplist(*args): | |
121 |
|
|
120 | return list(map(*args)) | |
122 |
|
121 | |||
|
122 | ||||
123 |
|
|
123 | def rangelist(*args): | |
124 |
|
|
124 | return list(range(*args)) | |
125 |
|
125 | |||
|
126 | ||||
126 |
|
|
127 | def ziplist(*args): | |
127 |
|
|
128 | return list(zip(*args)) | |
128 |
|
129 | |||
|
130 | ||||
129 |
|
|
131 | rawinput = input | |
130 |
|
|
132 | getargspec = inspect.getfullargspec | |
131 |
|
133 | |||
@@ -154,6 +156,7 b' if ispy3:' | |||||
154 |
|
|
156 | bytechr = struct.Struct('>B').pack | |
155 |
|
|
157 | byterepr = b'%r'.__mod__ | |
156 |
|
158 | |||
|
159 | ||||
157 |
|
|
160 | class bytestr(bytes): | |
158 |
|
|
161 | """A bytes which mostly acts as a Python 2 str | |
159 |
|
162 | |||
@@ -239,16 +242,19 b' if ispy3:' | |||||
239 |
|
|
242 | def __repr__(self): | |
240 |
|
|
243 | return bytes.__repr__(self)[1:] # drop b'' | |
241 |
|
244 | |||
|
245 | ||||
242 |
|
|
246 | def iterbytestr(s): | |
243 |
|
|
247 | """Iterate bytes as if it were a str object of Python 2""" | |
244 |
|
|
248 | return map(bytechr, s) | |
245 |
|
249 | |||
|
250 | ||||
246 |
|
|
251 | def maybebytestr(s): | |
247 |
|
|
252 | """Promote bytes to bytestr""" | |
248 |
|
|
253 | if isinstance(s, bytes): | |
249 |
|
|
254 | return bytestr(s) | |
250 |
|
|
255 | return s | |
251 |
|
256 | |||
|
257 | ||||
252 |
|
|
258 | def sysbytes(s): | |
253 |
|
|
259 | """Convert an internal str (e.g. keyword, __doc__) back to bytes | |
254 |
|
260 | |||
@@ -259,6 +265,7 b' if ispy3:' | |||||
259 |
|
|
265 | return s | |
260 |
|
|
266 | return s.encode('utf-8') | |
261 |
|
267 | |||
|
268 | ||||
262 |
|
|
269 | def sysstr(s): | |
263 |
|
|
270 | """Return a keyword str to be passed to Python functions such as | |
264 |
|
|
271 | getattr() and str.encode() | |
@@ -271,22 +278,26 b' if ispy3:' | |||||
271 |
|
|
278 | return s | |
272 |
|
|
279 | return s.decode('latin-1') | |
273 |
|
280 | |||
|
281 | ||||
274 |
|
|
282 | def strurl(url): | |
275 |
|
|
283 | """Converts a bytes url back to str""" | |
276 |
|
|
284 | if isinstance(url, bytes): | |
277 |
|
|
285 | return url.decode('ascii') | |
278 |
|
|
286 | return url | |
279 |
|
287 | |||
|
288 | ||||
280 |
|
|
289 | def bytesurl(url): | |
281 |
|
|
290 | """Converts a str url to bytes by encoding in ascii""" | |
282 |
|
|
291 | if isinstance(url, str): | |
283 |
|
|
292 | return url.encode('ascii') | |
284 |
|
|
293 | return url | |
285 |
|
294 | |||
|
295 | ||||
286 |
|
|
296 | def raisewithtb(exc, tb): | |
287 |
|
|
297 | """Raise exception with the given traceback""" | |
288 |
|
|
298 | raise exc.with_traceback(tb) | |
289 |
|
299 | |||
|
300 | ||||
290 |
|
|
301 | def getdoc(obj): | |
291 |
|
|
302 | """Get docstring as bytes; may be None so gettext() won't confuse it | |
292 |
|
|
303 | with _('')""" | |
@@ -295,6 +306,7 b' if ispy3:' | |||||
295 |
|
|
306 | return doc | |
296 |
|
|
307 | return sysbytes(doc) | |
297 |
|
308 | |||
|
309 | ||||
298 |
|
|
310 | def _wrapattrfunc(f): | |
299 |
|
|
311 | @functools.wraps(f) | |
300 |
|
|
312 | def w(object, name, *args): | |
@@ -302,6 +314,7 b' if ispy3:' | |||||
302 |
|
314 | |||
303 |
|
|
315 | return w | |
304 |
|
316 | |||
|
317 | ||||
305 |
|
|
318 | # these wrappers are automagically imported by hgloader | |
306 |
|
|
319 | delattr = _wrapattrfunc(builtins.delattr) | |
307 |
|
|
320 | getattr = _wrapattrfunc(builtins.getattr) | |
@@ -310,11 +323,14 b' if ispy3:' | |||||
310 |
|
|
323 | xrange = builtins.range | |
311 |
|
|
324 | unicode = str | |
312 |
|
325 | |||
|
326 | ||||
313 |
|
|
327 | def open(name, mode=b'r', buffering=-1, encoding=None): | |
314 |
|
|
328 | return builtins.open(name, sysstr(mode), buffering, encoding) | |
315 |
|
329 | |||
|
330 | ||||
316 |
|
|
331 | safehasattr = _wrapattrfunc(builtins.hasattr) | |
317 |
|
332 | |||
|
333 | ||||
318 |
|
|
334 | def _getoptbwrapper(orig, args, shortlist, namelist): | |
319 |
|
|
335 | """ | |
320 |
|
|
336 | Takes bytes arguments, converts them to unicode, pass them to | |
@@ -330,6 +346,7 b' if ispy3:' | |||||
330 |
|
|
346 | args = [a.encode('latin-1') for a in args] | |
331 |
|
|
347 | return opts, args | |
332 |
|
348 | |||
|
349 | ||||
333 |
|
|
350 | def strkwargs(dic): | |
334 |
|
|
351 | """ | |
335 |
|
|
352 | Converts the keys of a python dictonary to str i.e. unicodes so that | |
@@ -339,6 +356,7 b' if ispy3:' | |||||
339 |
|
|
356 | dic = {k.decode('latin-1'): v for k, v in dic.items()} | |
340 |
|
|
357 | return dic | |
341 |
|
358 | |||
|
359 | ||||
342 |
|
|
360 | def byteskwargs(dic): | |
343 |
|
|
361 | """ | |
344 |
|
|
362 | Converts keys of python dictionaries to bytes as they were converted to | |
@@ -347,6 +365,7 b' if ispy3:' | |||||
347 |
|
|
365 | dic = {k.encode('latin-1'): v for k, v in dic.items()} | |
348 |
|
|
366 | return dic | |
349 |
|
367 | |||
|
368 | ||||
350 |
|
|
369 | # TODO: handle shlex.shlex(). | |
351 |
|
|
370 | def shlexsplit(s, comments=False, posix=True): | |
352 |
|
|
371 | """ | |
@@ -357,6 +376,7 b' if ispy3:' | |||||
357 |
|
|
376 | ret = shlex.split(s.decode('latin-1'), comments, posix) | |
358 |
|
|
377 | return [a.encode('latin-1') for a in ret] | |
359 |
|
378 | |||
|
379 | ||||
360 |
|
|
380 | iteritems = lambda x: x.items() | |
361 |
|
|
381 | itervalues = lambda x: x.values() | |
362 |
|
382 | |||
@@ -399,84 +419,8 b' if ispy3:' | |||||
399 |
|
419 | |||
400 |
|
|
420 | return json.loads(s, *args, **kwargs) | |
401 |
|
421 | |||
402 | else: |
|
|||
403 | json_loads = json.loads |
|
|||
404 |
|
422 | |||
405 | else: |
|
423 | else: | |
406 | import cStringIO |
|
|||
407 |
|
||||
408 | xrange = xrange |
|
|||
409 | unicode = unicode |
|
|||
410 | bytechr = chr |
|
|||
411 | byterepr = repr |
|
|||
412 | bytestr = str |
|
|||
413 | iterbytestr = iter |
|
|||
414 | maybebytestr = identity |
|
|||
415 | sysbytes = identity |
|
|||
416 | sysstr = identity |
|
|||
417 | strurl = identity |
|
|||
418 | bytesurl = identity |
|
|||
419 | open = open |
|
|||
420 | delattr = delattr |
|
|||
421 | getattr = getattr |
|
|||
422 | hasattr = hasattr |
|
|||
423 | setattr = setattr |
|
|||
424 |
|
||||
425 | # this can't be parsed on Python 3 |
|
|||
426 | exec(b'def raisewithtb(exc, tb):\n raise exc, None, tb\n') |
|
|||
427 |
|
||||
428 | def fsencode(filename): |
|
|||
429 | """ |
|
|||
430 | Partial backport from os.py in Python 3, which only accepts bytes. |
|
|||
431 | In Python 2, our paths should only ever be bytes, a unicode path |
|
|||
432 | indicates a bug. |
|
|||
433 | """ |
|
|||
434 | if isinstance(filename, str): |
|
|||
435 | return filename |
|
|||
436 | else: |
|
|||
437 | raise TypeError("expect str, not %s" % type(filename).__name__) |
|
|||
438 |
|
||||
439 | # In Python 2, fsdecode() has a very chance to receive bytes. So it's |
|
|||
440 | # better not to touch Python 2 part as it's already working fine. |
|
|||
441 | fsdecode = identity |
|
|||
442 |
|
||||
443 | def getdoc(obj): |
|
|||
444 | return getattr(obj, '__doc__', None) |
|
|||
445 |
|
||||
446 | _notset = object() |
|
|||
447 |
|
||||
448 | def safehasattr(thing, attr): |
|
|||
449 | return getattr(thing, attr, _notset) is not _notset |
|
|||
450 |
|
||||
451 | def _getoptbwrapper(orig, args, shortlist, namelist): |
|
|||
452 | return orig(args, shortlist, namelist) |
|
|||
453 |
|
||||
454 | strkwargs = identity |
|
|||
455 | byteskwargs = identity |
|
|||
456 |
|
||||
457 | oscurdir = os.curdir |
|
|||
458 | oslinesep = os.linesep |
|
|||
459 | osname = os.name |
|
|||
460 | ospathsep = os.pathsep |
|
|||
461 | ospardir = os.pardir |
|
|||
462 | ossep = os.sep |
|
|||
463 | osaltsep = os.altsep |
|
|||
464 | osdevnull = os.devnull |
|
|||
465 | long = long |
|
|||
466 | if getattr(sys, 'argv', None) is not None: |
|
|||
467 | sysargv = sys.argv |
|
|||
468 | sysplatform = sys.platform |
|
|||
469 | sysexecutable = sys.executable |
|
|||
470 | shlexsplit = shlex.split |
|
|||
471 | bytesio = cStringIO.StringIO |
|
|||
472 | stringio = bytesio |
|
|||
473 | maplist = map |
|
|||
474 | rangelist = range |
|
|||
475 | ziplist = zip |
|
|||
476 | rawinput = raw_input |
|
|||
477 | getargspec = inspect.getargspec |
|
|||
478 | iteritems = lambda x: x.iteritems() |
|
|||
479 | itervalues = lambda x: x.itervalues() |
|
|||
480 | json_loads = json.loads |
|
424 | json_loads = json.loads | |
481 |
|
425 | |||
482 | isjython = sysplatform.startswith(b'java') |
|
426 | isjython = sysplatform.startswith(b'java') |
General Comments 0
You need to be logged in to leave comments.
Login now