Mercurial > public > mercurial-scm > hg-stable
annotate mercurial/pycompat.py @ 31777:7d2cbe11ae48
pycompat: introduce identity function as a compat stub
I was sometimes too lazy to use 'str' instead of 'lambda a: a'. Let's add
a named function for that purpose.
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Wed, 29 Mar 2017 21:13:55 +0900 |
parents | 55c6788c54e2 |
children | 8181f378b073 |
rev | line source |
---|---|
28818
6041fb8f2da8
pycompat: add empty and queue to handle py3 divergence
timeless <timeless@mozdev.org>
parents:
diff
changeset
|
1 # pycompat.py - portability shim for python 3 |
6041fb8f2da8
pycompat: add empty and queue to handle py3 divergence
timeless <timeless@mozdev.org>
parents:
diff
changeset
|
2 # |
6041fb8f2da8
pycompat: add empty and queue to handle py3 divergence
timeless <timeless@mozdev.org>
parents:
diff
changeset
|
3 # This software may be used and distributed according to the terms of the |
6041fb8f2da8
pycompat: add empty and queue to handle py3 divergence
timeless <timeless@mozdev.org>
parents:
diff
changeset
|
4 # GNU General Public License version 2 or any later version. |
6041fb8f2da8
pycompat: add empty and queue to handle py3 divergence
timeless <timeless@mozdev.org>
parents:
diff
changeset
|
5 |
6041fb8f2da8
pycompat: add empty and queue to handle py3 divergence
timeless <timeless@mozdev.org>
parents:
diff
changeset
|
6 """Mercurial portability shim for python 3. |
6041fb8f2da8
pycompat: add empty and queue to handle py3 divergence
timeless <timeless@mozdev.org>
parents:
diff
changeset
|
7 |
6041fb8f2da8
pycompat: add empty and queue to handle py3 divergence
timeless <timeless@mozdev.org>
parents:
diff
changeset
|
8 This contains aliases to hide python version-specific details from the core. |
6041fb8f2da8
pycompat: add empty and queue to handle py3 divergence
timeless <timeless@mozdev.org>
parents:
diff
changeset
|
9 """ |
6041fb8f2da8
pycompat: add empty and queue to handle py3 divergence
timeless <timeless@mozdev.org>
parents:
diff
changeset
|
10 |
6041fb8f2da8
pycompat: add empty and queue to handle py3 divergence
timeless <timeless@mozdev.org>
parents:
diff
changeset
|
11 from __future__ import absolute_import |
6041fb8f2da8
pycompat: add empty and queue to handle py3 divergence
timeless <timeless@mozdev.org>
parents:
diff
changeset
|
12 |
30583
c6ce11f2ee50
py3: make a bytes version of getopt.getopt()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30509
diff
changeset
|
13 import getopt |
30315
3874ddba1ab4
py3: add a bytes version of os.name
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30313
diff
changeset
|
14 import os |
30681
caf7e1c5efe4
py3: have a bytes version of shlex.split()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30671
diff
changeset
|
15 import shlex |
29584
06587edd1233
pycompat: make pycompat demandimport friendly
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29566
diff
changeset
|
16 import sys |
06587edd1233
pycompat: make pycompat demandimport friendly
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29566
diff
changeset
|
17 |
30031
0f6d6fdd3c2a
pycompat: provide 'ispy3' constant
Yuya Nishihara <yuya@tcha.org>
parents:
29812
diff
changeset
|
18 ispy3 = (sys.version_info[0] >= 3) |
0f6d6fdd3c2a
pycompat: provide 'ispy3' constant
Yuya Nishihara <yuya@tcha.org>
parents:
29812
diff
changeset
|
19 |
0f6d6fdd3c2a
pycompat: provide 'ispy3' constant
Yuya Nishihara <yuya@tcha.org>
parents:
29812
diff
changeset
|
20 if not ispy3: |
29324
b501579147f1
py3: conditionalize cPickle import by adding in util
Pulkit Goyal <7895pulkit@gmail.com>
parents:
28882
diff
changeset
|
21 import cPickle as pickle |
29455
0c741fd6158a
py3: conditionalize httplib import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29433
diff
changeset
|
22 import httplib |
29584
06587edd1233
pycompat: make pycompat demandimport friendly
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29566
diff
changeset
|
23 import Queue as _queue |
29433
33770d2b6cf9
py3: conditionalize SocketServer import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29432
diff
changeset
|
24 import SocketServer as socketserver |
29432
34b914ac573e
py3: conditionalize xmlrpclib import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29431
diff
changeset
|
25 import xmlrpclib |
29584
06587edd1233
pycompat: make pycompat demandimport friendly
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29566
diff
changeset
|
26 else: |
06587edd1233
pycompat: make pycompat demandimport friendly
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29566
diff
changeset
|
27 import http.client as httplib |
06587edd1233
pycompat: make pycompat demandimport friendly
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29566
diff
changeset
|
28 import pickle |
06587edd1233
pycompat: make pycompat demandimport friendly
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29566
diff
changeset
|
29 import queue as _queue |
06587edd1233
pycompat: make pycompat demandimport friendly
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29566
diff
changeset
|
30 import socketserver |
29432
34b914ac573e
py3: conditionalize xmlrpclib import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29431
diff
changeset
|
31 import xmlrpc.client as xmlrpclib |
29431
80880ad3fccd
py3: conditionalize the urlparse import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29414
diff
changeset
|
32 |
31777
7d2cbe11ae48
pycompat: introduce identity function as a compat stub
Yuya Nishihara <yuya@tcha.org>
parents:
31579
diff
changeset
|
33 def identity(a): |
7d2cbe11ae48
pycompat: introduce identity function as a compat stub
Yuya Nishihara <yuya@tcha.org>
parents:
31579
diff
changeset
|
34 return a |
7d2cbe11ae48
pycompat: introduce identity function as a compat stub
Yuya Nishihara <yuya@tcha.org>
parents:
31579
diff
changeset
|
35 |
30031
0f6d6fdd3c2a
pycompat: provide 'ispy3' constant
Yuya Nishihara <yuya@tcha.org>
parents:
29812
diff
changeset
|
36 if ispy3: |
29808
965c91bad9e3
py3: move xrange alias next to import lines
Yuya Nishihara <yuya@tcha.org>
parents:
29790
diff
changeset
|
37 import builtins |
29810
45fa8de47a0f
py3: provide (del|get|has|set)attr wrappers that accepts bytes
Yuya Nishihara <yuya@tcha.org>
parents:
29809
diff
changeset
|
38 import functools |
31381
06440ba06bc0
pycompat: move imports of cStringIO/io to where they are used
Yuya Nishihara <yuya@tcha.org>
parents:
31368
diff
changeset
|
39 import io |
31433
4acc49335a6e
py3: optimize py3 compat.bytechr using Struct.pack
Martin von Zweigbergk <martinvonz@google.com>
parents:
31409
diff
changeset
|
40 import struct |
31381
06440ba06bc0
pycompat: move imports of cStringIO/io to where they are used
Yuya Nishihara <yuya@tcha.org>
parents:
31368
diff
changeset
|
41 |
30119
f4a5e0e86a7e
py3: add an os.fsencode backport to ease path handling
Martijn Pieters <mjpieters@fb.com>
parents:
30086
diff
changeset
|
42 fsencode = os.fsencode |
30313
42af0590f4b9
py3: add os.fsdecode() as pycompat.fsdecode()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30133
diff
changeset
|
43 fsdecode = os.fsdecode |
30315
3874ddba1ab4
py3: add a bytes version of os.name
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30313
diff
changeset
|
44 # A bytes version of os.name. |
3874ddba1ab4
py3: add a bytes version of os.name
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30313
diff
changeset
|
45 osname = os.name.encode('ascii') |
30316
ad40d307a9f0
py3: have pycompat.ospathsep and pycompat.ossep
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30315
diff
changeset
|
46 ospathsep = os.pathsep.encode('ascii') |
ad40d307a9f0
py3: have pycompat.ospathsep and pycompat.ossep
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30315
diff
changeset
|
47 ossep = os.sep.encode('ascii') |
30628
c6026c20a3ce
py3: have a bytes version of os.altsep
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30584
diff
changeset
|
48 osaltsep = os.altsep |
c6026c20a3ce
py3: have a bytes version of os.altsep
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30584
diff
changeset
|
49 if osaltsep: |
c6026c20a3ce
py3: have a bytes version of os.altsep
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30584
diff
changeset
|
50 osaltsep = osaltsep.encode('ascii') |
30509
fc0cfe6c87d7
py3: add os.getcwdb() to have bytes path
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30481
diff
changeset
|
51 # os.getcwd() on Python 3 returns string, but it has os.getcwdb() which |
fc0cfe6c87d7
py3: add os.getcwdb() to have bytes path
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30481
diff
changeset
|
52 # returns bytes. |
fc0cfe6c87d7
py3: add os.getcwdb() to have bytes path
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30481
diff
changeset
|
53 getcwd = os.getcwdb |
30629
a82a6eee2613
py3: have a bytes version of sys.platform
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30628
diff
changeset
|
54 sysplatform = sys.platform.encode('ascii') |
30671
3fcaf0f660ce
py3: have bytes version of sys.executable
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30666
diff
changeset
|
55 sysexecutable = sys.executable |
3fcaf0f660ce
py3: have bytes version of sys.executable
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30666
diff
changeset
|
56 if sysexecutable: |
3fcaf0f660ce
py3: have bytes version of sys.executable
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30666
diff
changeset
|
57 sysexecutable = os.fsencode(sysexecutable) |
31368
73b3bee8febe
pycompat: default to BytesIO instead of StringIO
Pulkit Goyal <7895pulkit@gmail.com>
parents:
31310
diff
changeset
|
58 stringio = io.BytesIO |
31508
a1e40ceee640
pycompat: add maplist alias for old map behavior
Augie Fackler <augie@google.com>
parents:
31448
diff
changeset
|
59 maplist = lambda *args: list(map(*args)) |
30344
19d8e19fde5b
py3: document why os.fsencode() can be used to get back bytes argv
Yuya Nishihara <yuya@tcha.org>
parents:
30340
diff
changeset
|
60 |
30481
277f4fe6d01a
py3: provide bytes stdin/out/err through util module
Yuya Nishihara <yuya@tcha.org>
parents:
30344
diff
changeset
|
61 # TODO: .buffer might not exist if std streams were replaced; we'll need |
277f4fe6d01a
py3: provide bytes stdin/out/err through util module
Yuya Nishihara <yuya@tcha.org>
parents:
30344
diff
changeset
|
62 # a silly wrapper to make a bytes stream backed by a unicode one. |
277f4fe6d01a
py3: provide bytes stdin/out/err through util module
Yuya Nishihara <yuya@tcha.org>
parents:
30344
diff
changeset
|
63 stdin = sys.stdin.buffer |
277f4fe6d01a
py3: provide bytes stdin/out/err through util module
Yuya Nishihara <yuya@tcha.org>
parents:
30344
diff
changeset
|
64 stdout = sys.stdout.buffer |
277f4fe6d01a
py3: provide bytes stdin/out/err through util module
Yuya Nishihara <yuya@tcha.org>
parents:
30344
diff
changeset
|
65 stderr = sys.stderr.buffer |
277f4fe6d01a
py3: provide bytes stdin/out/err through util module
Yuya Nishihara <yuya@tcha.org>
parents:
30344
diff
changeset
|
66 |
30344
19d8e19fde5b
py3: document why os.fsencode() can be used to get back bytes argv
Yuya Nishihara <yuya@tcha.org>
parents:
30340
diff
changeset
|
67 # Since Python 3 converts argv to wchar_t type by Py_DecodeLocale() on Unix, |
19d8e19fde5b
py3: document why os.fsencode() can be used to get back bytes argv
Yuya Nishihara <yuya@tcha.org>
parents:
30340
diff
changeset
|
68 # we can use os.fsencode() to get back bytes argv. |
19d8e19fde5b
py3: document why os.fsencode() can be used to get back bytes argv
Yuya Nishihara <yuya@tcha.org>
parents:
30340
diff
changeset
|
69 # |
19d8e19fde5b
py3: document why os.fsencode() can be used to get back bytes argv
Yuya Nishihara <yuya@tcha.org>
parents:
30340
diff
changeset
|
70 # https://hg.python.org/cpython/file/v3.5.1/Programs/python.c#l55 |
19d8e19fde5b
py3: document why os.fsencode() can be used to get back bytes argv
Yuya Nishihara <yuya@tcha.org>
parents:
30340
diff
changeset
|
71 # |
19d8e19fde5b
py3: document why os.fsencode() can be used to get back bytes argv
Yuya Nishihara <yuya@tcha.org>
parents:
30340
diff
changeset
|
72 # TODO: On Windows, the native argv is wchar_t, so we'll need a different |
19d8e19fde5b
py3: document why os.fsencode() can be used to get back bytes argv
Yuya Nishihara <yuya@tcha.org>
parents:
30340
diff
changeset
|
73 # workaround to simulate the Python 2 (i.e. ANSI Win32 API) behavior. |
30888
86cd1f2cfff5
pycompat: verify sys.argv exists before forwarding it (issue5493)
Augie Fackler <augie@google.com>
parents:
30820
diff
changeset
|
74 if getattr(sys, 'argv', None) is not None: |
86cd1f2cfff5
pycompat: verify sys.argv exists before forwarding it (issue5493)
Augie Fackler <augie@google.com>
parents:
30820
diff
changeset
|
75 sysargv = list(map(os.fsencode, sys.argv)) |
29808
965c91bad9e3
py3: move xrange alias next to import lines
Yuya Nishihara <yuya@tcha.org>
parents:
29790
diff
changeset
|
76 |
31433
4acc49335a6e
py3: optimize py3 compat.bytechr using Struct.pack
Martin von Zweigbergk <martinvonz@google.com>
parents:
31409
diff
changeset
|
77 bytechr = struct.Struct('>B').pack |
31263
64596338ba10
py3: factor out bytechr() function
Yuya Nishihara <yuya@tcha.org>
parents:
31159
diff
changeset
|
78 |
31448
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
79 class bytestr(bytes): |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
80 """A bytes which mostly acts as a Python 2 str |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
81 |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
82 >>> bytestr(), bytestr(bytearray(b'foo')), bytestr(u'ascii'), bytestr(1) |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
83 (b'', b'foo', b'ascii', b'1') |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
84 >>> s = bytestr(b'foo') |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
85 >>> assert s is bytestr(s) |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
86 |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
87 There's no implicit conversion from non-ascii str as its encoding is |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
88 unknown: |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
89 |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
90 >>> bytestr(chr(0x80)) # doctest: +ELLIPSIS |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
91 Traceback (most recent call last): |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
92 ... |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
93 UnicodeEncodeError: ... |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
94 |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
95 Comparison between bytestr and bytes should work: |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
96 |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
97 >>> assert bytestr(b'foo') == b'foo' |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
98 >>> assert b'foo' == bytestr(b'foo') |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
99 >>> assert b'f' in bytestr(b'foo') |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
100 >>> assert bytestr(b'f') in b'foo' |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
101 |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
102 Sliced elements should be bytes, not integer: |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
103 |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
104 >>> s[1], s[:2] |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
105 (b'o', b'fo') |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
106 >>> list(s), list(reversed(s)) |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
107 ([b'f', b'o', b'o'], [b'o', b'o', b'f']) |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
108 |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
109 As bytestr type isn't propagated across operations, you need to cast |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
110 bytes to bytestr explicitly: |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
111 |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
112 >>> s = bytestr(b'foo').upper() |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
113 >>> t = bytestr(s) |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
114 >>> s[0], t[0] |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
115 (70, b'F') |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
116 |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
117 Be careful to not pass a bytestr object to a function which expects |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
118 bytearray-like behavior. |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
119 |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
120 >>> t = bytes(t) # cast to bytes |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
121 >>> assert type(t) is bytes |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
122 """ |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
123 |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
124 def __new__(cls, s=b''): |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
125 if isinstance(s, bytestr): |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
126 return s |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
127 if not isinstance(s, (bytes, bytearray)): |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
128 s = str(s).encode(u'ascii') |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
129 return bytes.__new__(cls, s) |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
130 |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
131 def __getitem__(self, key): |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
132 s = bytes.__getitem__(self, key) |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
133 if not isinstance(s, bytes): |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
134 s = bytechr(s) |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
135 return s |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
136 |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
137 def __iter__(self): |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
138 return iterbytestr(bytes.__iter__(self)) |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
139 |
31391
c9fd842dc886
pycompat: add helper to iterate each char in bytes
Yuya Nishihara <yuya@tcha.org>
parents:
31381
diff
changeset
|
140 def iterbytestr(s): |
c9fd842dc886
pycompat: add helper to iterate each char in bytes
Yuya Nishihara <yuya@tcha.org>
parents:
31381
diff
changeset
|
141 """Iterate bytes as if it were a str object of Python 2""" |
31434
63a39d647888
py3: make py3 compat.iterbytestr simpler and faster
Martin von Zweigbergk <martinvonz@google.com>
parents:
31433
diff
changeset
|
142 return map(bytechr, s) |
31391
c9fd842dc886
pycompat: add helper to iterate each char in bytes
Yuya Nishihara <yuya@tcha.org>
parents:
31381
diff
changeset
|
143 |
30032
2219f4f82ede
pycompat: extract function that converts attribute or encoding name to str
Yuya Nishihara <yuya@tcha.org>
parents:
30031
diff
changeset
|
144 def sysstr(s): |
2219f4f82ede
pycompat: extract function that converts attribute or encoding name to str
Yuya Nishihara <yuya@tcha.org>
parents:
30031
diff
changeset
|
145 """Return a keyword str to be passed to Python functions such as |
2219f4f82ede
pycompat: extract function that converts attribute or encoding name to str
Yuya Nishihara <yuya@tcha.org>
parents:
30031
diff
changeset
|
146 getattr() and str.encode() |
2219f4f82ede
pycompat: extract function that converts attribute or encoding name to str
Yuya Nishihara <yuya@tcha.org>
parents:
30031
diff
changeset
|
147 |
2219f4f82ede
pycompat: extract function that converts attribute or encoding name to str
Yuya Nishihara <yuya@tcha.org>
parents:
30031
diff
changeset
|
148 This never raises UnicodeDecodeError. Non-ascii characters are |
2219f4f82ede
pycompat: extract function that converts attribute or encoding name to str
Yuya Nishihara <yuya@tcha.org>
parents:
30031
diff
changeset
|
149 considered invalid and mapped to arbitrary but unique code points |
2219f4f82ede
pycompat: extract function that converts attribute or encoding name to str
Yuya Nishihara <yuya@tcha.org>
parents:
30031
diff
changeset
|
150 such that 'sysstr(a) != sysstr(b)' for all 'a != b'. |
2219f4f82ede
pycompat: extract function that converts attribute or encoding name to str
Yuya Nishihara <yuya@tcha.org>
parents:
30031
diff
changeset
|
151 """ |
2219f4f82ede
pycompat: extract function that converts attribute or encoding name to str
Yuya Nishihara <yuya@tcha.org>
parents:
30031
diff
changeset
|
152 if isinstance(s, builtins.str): |
2219f4f82ede
pycompat: extract function that converts attribute or encoding name to str
Yuya Nishihara <yuya@tcha.org>
parents:
30031
diff
changeset
|
153 return s |
2219f4f82ede
pycompat: extract function that converts attribute or encoding name to str
Yuya Nishihara <yuya@tcha.org>
parents:
30031
diff
changeset
|
154 return s.decode(u'latin-1') |
2219f4f82ede
pycompat: extract function that converts attribute or encoding name to str
Yuya Nishihara <yuya@tcha.org>
parents:
30031
diff
changeset
|
155 |
29810
45fa8de47a0f
py3: provide (del|get|has|set)attr wrappers that accepts bytes
Yuya Nishihara <yuya@tcha.org>
parents:
29809
diff
changeset
|
156 def _wrapattrfunc(f): |
45fa8de47a0f
py3: provide (del|get|has|set)attr wrappers that accepts bytes
Yuya Nishihara <yuya@tcha.org>
parents:
29809
diff
changeset
|
157 @functools.wraps(f) |
45fa8de47a0f
py3: provide (del|get|has|set)attr wrappers that accepts bytes
Yuya Nishihara <yuya@tcha.org>
parents:
29809
diff
changeset
|
158 def w(object, name, *args): |
30032
2219f4f82ede
pycompat: extract function that converts attribute or encoding name to str
Yuya Nishihara <yuya@tcha.org>
parents:
30031
diff
changeset
|
159 return f(object, sysstr(name), *args) |
29810
45fa8de47a0f
py3: provide (del|get|has|set)attr wrappers that accepts bytes
Yuya Nishihara <yuya@tcha.org>
parents:
29809
diff
changeset
|
160 return w |
45fa8de47a0f
py3: provide (del|get|has|set)attr wrappers that accepts bytes
Yuya Nishihara <yuya@tcha.org>
parents:
29809
diff
changeset
|
161 |
29811
178c89e8519a
py3: import builtin wrappers automagically by code transformer
Yuya Nishihara <yuya@tcha.org>
parents:
29810
diff
changeset
|
162 # these wrappers are automagically imported by hgloader |
29810
45fa8de47a0f
py3: provide (del|get|has|set)attr wrappers that accepts bytes
Yuya Nishihara <yuya@tcha.org>
parents:
29809
diff
changeset
|
163 delattr = _wrapattrfunc(builtins.delattr) |
45fa8de47a0f
py3: provide (del|get|has|set)attr wrappers that accepts bytes
Yuya Nishihara <yuya@tcha.org>
parents:
29809
diff
changeset
|
164 getattr = _wrapattrfunc(builtins.getattr) |
45fa8de47a0f
py3: provide (del|get|has|set)attr wrappers that accepts bytes
Yuya Nishihara <yuya@tcha.org>
parents:
29809
diff
changeset
|
165 hasattr = _wrapattrfunc(builtins.hasattr) |
45fa8de47a0f
py3: provide (del|get|has|set)attr wrappers that accepts bytes
Yuya Nishihara <yuya@tcha.org>
parents:
29809
diff
changeset
|
166 setattr = _wrapattrfunc(builtins.setattr) |
29811
178c89e8519a
py3: import builtin wrappers automagically by code transformer
Yuya Nishihara <yuya@tcha.org>
parents:
29810
diff
changeset
|
167 xrange = builtins.range |
29810
45fa8de47a0f
py3: provide (del|get|has|set)attr wrappers that accepts bytes
Yuya Nishihara <yuya@tcha.org>
parents:
29809
diff
changeset
|
168 |
31159
76a64c1e5439
py3: add pycompat.open and replace open() calls
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30820
diff
changeset
|
169 def open(name, mode='r', buffering=-1): |
76a64c1e5439
py3: add pycompat.open and replace open() calls
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30820
diff
changeset
|
170 return builtins.open(name, sysstr(mode), buffering) |
76a64c1e5439
py3: add pycompat.open and replace open() calls
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30820
diff
changeset
|
171 |
30583
c6ce11f2ee50
py3: make a bytes version of getopt.getopt()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30509
diff
changeset
|
172 # getopt.getopt() on Python 3 deals with unicodes internally so we cannot |
c6ce11f2ee50
py3: make a bytes version of getopt.getopt()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30509
diff
changeset
|
173 # pass bytes there. Passing unicodes will result in unicodes as return |
c6ce11f2ee50
py3: make a bytes version of getopt.getopt()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30509
diff
changeset
|
174 # values which we need to convert again to bytes. |
c6ce11f2ee50
py3: make a bytes version of getopt.getopt()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30509
diff
changeset
|
175 def getoptb(args, shortlist, namelist): |
c6ce11f2ee50
py3: make a bytes version of getopt.getopt()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30509
diff
changeset
|
176 args = [a.decode('latin-1') for a in args] |
c6ce11f2ee50
py3: make a bytes version of getopt.getopt()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30509
diff
changeset
|
177 shortlist = shortlist.decode('latin-1') |
c6ce11f2ee50
py3: make a bytes version of getopt.getopt()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30509
diff
changeset
|
178 namelist = [a.decode('latin-1') for a in namelist] |
c6ce11f2ee50
py3: make a bytes version of getopt.getopt()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30509
diff
changeset
|
179 opts, args = getopt.getopt(args, shortlist, namelist) |
c6ce11f2ee50
py3: make a bytes version of getopt.getopt()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30509
diff
changeset
|
180 opts = [(a[0].encode('latin-1'), a[1].encode('latin-1')) |
c6ce11f2ee50
py3: make a bytes version of getopt.getopt()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30509
diff
changeset
|
181 for a in opts] |
c6ce11f2ee50
py3: make a bytes version of getopt.getopt()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30509
diff
changeset
|
182 args = [a.encode('latin-1') for a in args] |
c6ce11f2ee50
py3: make a bytes version of getopt.getopt()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30509
diff
changeset
|
183 return opts, args |
c6ce11f2ee50
py3: make a bytes version of getopt.getopt()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30509
diff
changeset
|
184 |
30584
fbc3f73dc802
py3: utility functions to convert keys of kwargs to bytes/unicodes
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30583
diff
changeset
|
185 # keys of keyword arguments in Python need to be strings which are unicodes |
fbc3f73dc802
py3: utility functions to convert keys of kwargs to bytes/unicodes
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30583
diff
changeset
|
186 # Python 3. This function takes keyword arguments, convert the keys to str. |
fbc3f73dc802
py3: utility functions to convert keys of kwargs to bytes/unicodes
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30583
diff
changeset
|
187 def strkwargs(dic): |
fbc3f73dc802
py3: utility functions to convert keys of kwargs to bytes/unicodes
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30583
diff
changeset
|
188 dic = dict((k.decode('latin-1'), v) for k, v in dic.iteritems()) |
fbc3f73dc802
py3: utility functions to convert keys of kwargs to bytes/unicodes
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30583
diff
changeset
|
189 return dic |
fbc3f73dc802
py3: utility functions to convert keys of kwargs to bytes/unicodes
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30583
diff
changeset
|
190 |
fbc3f73dc802
py3: utility functions to convert keys of kwargs to bytes/unicodes
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30583
diff
changeset
|
191 # keys of keyword arguments need to be unicode while passing into |
fbc3f73dc802
py3: utility functions to convert keys of kwargs to bytes/unicodes
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30583
diff
changeset
|
192 # a function. This function helps us to convert those keys back to bytes |
fbc3f73dc802
py3: utility functions to convert keys of kwargs to bytes/unicodes
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30583
diff
changeset
|
193 # again as we need to deal with bytes. |
fbc3f73dc802
py3: utility functions to convert keys of kwargs to bytes/unicodes
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30583
diff
changeset
|
194 def byteskwargs(dic): |
fbc3f73dc802
py3: utility functions to convert keys of kwargs to bytes/unicodes
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30583
diff
changeset
|
195 dic = dict((k.encode('latin-1'), v) for k, v in dic.iteritems()) |
fbc3f73dc802
py3: utility functions to convert keys of kwargs to bytes/unicodes
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30583
diff
changeset
|
196 return dic |
fbc3f73dc802
py3: utility functions to convert keys of kwargs to bytes/unicodes
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30583
diff
changeset
|
197 |
30681
caf7e1c5efe4
py3: have a bytes version of shlex.split()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30671
diff
changeset
|
198 # shlex.split() accepts unicodes on Python 3. This function takes bytes |
caf7e1c5efe4
py3: have a bytes version of shlex.split()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30671
diff
changeset
|
199 # argument, convert it into unicodes, pass into shlex.split(), convert the |
caf7e1c5efe4
py3: have a bytes version of shlex.split()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30671
diff
changeset
|
200 # returned value to bytes and return that. |
caf7e1c5efe4
py3: have a bytes version of shlex.split()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30671
diff
changeset
|
201 # TODO: handle shlex.shlex(). |
caf7e1c5efe4
py3: have a bytes version of shlex.split()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30671
diff
changeset
|
202 def shlexsplit(s): |
caf7e1c5efe4
py3: have a bytes version of shlex.split()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30671
diff
changeset
|
203 ret = shlex.split(s.decode('latin-1')) |
caf7e1c5efe4
py3: have a bytes version of shlex.split()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30671
diff
changeset
|
204 return [a.encode('latin-1') for a in ret] |
caf7e1c5efe4
py3: have a bytes version of shlex.split()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30671
diff
changeset
|
205 |
30032
2219f4f82ede
pycompat: extract function that converts attribute or encoding name to str
Yuya Nishihara <yuya@tcha.org>
parents:
30031
diff
changeset
|
206 else: |
31381
06440ba06bc0
pycompat: move imports of cStringIO/io to where they are used
Yuya Nishihara <yuya@tcha.org>
parents:
31368
diff
changeset
|
207 import cStringIO |
06440ba06bc0
pycompat: move imports of cStringIO/io to where they are used
Yuya Nishihara <yuya@tcha.org>
parents:
31368
diff
changeset
|
208 |
31263
64596338ba10
py3: factor out bytechr() function
Yuya Nishihara <yuya@tcha.org>
parents:
31159
diff
changeset
|
209 bytechr = chr |
31448
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
210 bytestr = str |
31391
c9fd842dc886
pycompat: add helper to iterate each char in bytes
Yuya Nishihara <yuya@tcha.org>
parents:
31381
diff
changeset
|
211 iterbytestr = iter |
31777
7d2cbe11ae48
pycompat: introduce identity function as a compat stub
Yuya Nishihara <yuya@tcha.org>
parents:
31579
diff
changeset
|
212 sysstr = identity |
30032
2219f4f82ede
pycompat: extract function that converts attribute or encoding name to str
Yuya Nishihara <yuya@tcha.org>
parents:
30031
diff
changeset
|
213 |
30133
f6dcda7505f9
pycompat: only accept a bytestring filepath in Python 2
Martijn Pieters <mjpieters@fb.com>
parents:
30119
diff
changeset
|
214 # Partial backport from os.py in Python 3, which only accepts bytes. |
f6dcda7505f9
pycompat: only accept a bytestring filepath in Python 2
Martijn Pieters <mjpieters@fb.com>
parents:
30119
diff
changeset
|
215 # In Python 2, our paths should only ever be bytes, a unicode path |
f6dcda7505f9
pycompat: only accept a bytestring filepath in Python 2
Martijn Pieters <mjpieters@fb.com>
parents:
30119
diff
changeset
|
216 # indicates a bug. |
f6dcda7505f9
pycompat: only accept a bytestring filepath in Python 2
Martijn Pieters <mjpieters@fb.com>
parents:
30119
diff
changeset
|
217 def fsencode(filename): |
f6dcda7505f9
pycompat: only accept a bytestring filepath in Python 2
Martijn Pieters <mjpieters@fb.com>
parents:
30119
diff
changeset
|
218 if isinstance(filename, str): |
f6dcda7505f9
pycompat: only accept a bytestring filepath in Python 2
Martijn Pieters <mjpieters@fb.com>
parents:
30119
diff
changeset
|
219 return filename |
30119
f4a5e0e86a7e
py3: add an os.fsencode backport to ease path handling
Martijn Pieters <mjpieters@fb.com>
parents:
30086
diff
changeset
|
220 else: |
30133
f6dcda7505f9
pycompat: only accept a bytestring filepath in Python 2
Martijn Pieters <mjpieters@fb.com>
parents:
30119
diff
changeset
|
221 raise TypeError( |
f6dcda7505f9
pycompat: only accept a bytestring filepath in Python 2
Martijn Pieters <mjpieters@fb.com>
parents:
30119
diff
changeset
|
222 "expect str, not %s" % type(filename).__name__) |
30119
f4a5e0e86a7e
py3: add an os.fsencode backport to ease path handling
Martijn Pieters <mjpieters@fb.com>
parents:
30086
diff
changeset
|
223 |
30313
42af0590f4b9
py3: add os.fsdecode() as pycompat.fsdecode()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30133
diff
changeset
|
224 # In Python 2, fsdecode() has a very chance to receive bytes. So it's |
42af0590f4b9
py3: add os.fsdecode() as pycompat.fsdecode()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30133
diff
changeset
|
225 # better not to touch Python 2 part as it's already working fine. |
31777
7d2cbe11ae48
pycompat: introduce identity function as a compat stub
Yuya Nishihara <yuya@tcha.org>
parents:
31579
diff
changeset
|
226 fsdecode = identity |
30313
42af0590f4b9
py3: add os.fsdecode() as pycompat.fsdecode()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30133
diff
changeset
|
227 |
30583
c6ce11f2ee50
py3: make a bytes version of getopt.getopt()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30509
diff
changeset
|
228 def getoptb(args, shortlist, namelist): |
c6ce11f2ee50
py3: make a bytes version of getopt.getopt()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30509
diff
changeset
|
229 return getopt.getopt(args, shortlist, namelist) |
c6ce11f2ee50
py3: make a bytes version of getopt.getopt()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30509
diff
changeset
|
230 |
31777
7d2cbe11ae48
pycompat: introduce identity function as a compat stub
Yuya Nishihara <yuya@tcha.org>
parents:
31579
diff
changeset
|
231 strkwargs = identity |
7d2cbe11ae48
pycompat: introduce identity function as a compat stub
Yuya Nishihara <yuya@tcha.org>
parents:
31579
diff
changeset
|
232 byteskwargs = identity |
30584
fbc3f73dc802
py3: utility functions to convert keys of kwargs to bytes/unicodes
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30583
diff
changeset
|
233 |
30315
3874ddba1ab4
py3: add a bytes version of os.name
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30313
diff
changeset
|
234 osname = os.name |
30316
ad40d307a9f0
py3: have pycompat.ospathsep and pycompat.ossep
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30315
diff
changeset
|
235 ospathsep = os.pathsep |
ad40d307a9f0
py3: have pycompat.ospathsep and pycompat.ossep
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30315
diff
changeset
|
236 ossep = os.sep |
30628
c6026c20a3ce
py3: have a bytes version of os.altsep
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30584
diff
changeset
|
237 osaltsep = os.altsep |
30481
277f4fe6d01a
py3: provide bytes stdin/out/err through util module
Yuya Nishihara <yuya@tcha.org>
parents:
30344
diff
changeset
|
238 stdin = sys.stdin |
277f4fe6d01a
py3: provide bytes stdin/out/err through util module
Yuya Nishihara <yuya@tcha.org>
parents:
30344
diff
changeset
|
239 stdout = sys.stdout |
277f4fe6d01a
py3: provide bytes stdin/out/err through util module
Yuya Nishihara <yuya@tcha.org>
parents:
30344
diff
changeset
|
240 stderr = sys.stderr |
30888
86cd1f2cfff5
pycompat: verify sys.argv exists before forwarding it (issue5493)
Augie Fackler <augie@google.com>
parents:
30820
diff
changeset
|
241 if getattr(sys, 'argv', None) is not None: |
86cd1f2cfff5
pycompat: verify sys.argv exists before forwarding it (issue5493)
Augie Fackler <augie@google.com>
parents:
30820
diff
changeset
|
242 sysargv = sys.argv |
30629
a82a6eee2613
py3: have a bytes version of sys.platform
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30628
diff
changeset
|
243 sysplatform = sys.platform |
30509
fc0cfe6c87d7
py3: add os.getcwdb() to have bytes path
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30481
diff
changeset
|
244 getcwd = os.getcwd |
30671
3fcaf0f660ce
py3: have bytes version of sys.executable
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30666
diff
changeset
|
245 sysexecutable = sys.executable |
30681
caf7e1c5efe4
py3: have a bytes version of shlex.split()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30671
diff
changeset
|
246 shlexsplit = shlex.split |
31381
06440ba06bc0
pycompat: move imports of cStringIO/io to where they are used
Yuya Nishihara <yuya@tcha.org>
parents:
31368
diff
changeset
|
247 stringio = cStringIO.StringIO |
31508
a1e40ceee640
pycompat: add maplist alias for old map behavior
Augie Fackler <augie@google.com>
parents:
31448
diff
changeset
|
248 maplist = map |
30315
3874ddba1ab4
py3: add a bytes version of os.name
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30313
diff
changeset
|
249 |
28818
6041fb8f2da8
pycompat: add empty and queue to handle py3 divergence
timeless <timeless@mozdev.org>
parents:
diff
changeset
|
250 empty = _queue.Empty |
6041fb8f2da8
pycompat: add empty and queue to handle py3 divergence
timeless <timeless@mozdev.org>
parents:
diff
changeset
|
251 queue = _queue.Queue |
28834
2fac032c1269
pycompat: alias xrange to range in py3
timeless <timeless@mozdev.org>
parents:
28833
diff
changeset
|
252 |
28882
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
253 class _pycompatstub(object): |
29812
c63ab0524db7
pycompat: delay loading modules registered to stub
Yuya Nishihara <yuya@tcha.org>
parents:
29811
diff
changeset
|
254 def __init__(self): |
c63ab0524db7
pycompat: delay loading modules registered to stub
Yuya Nishihara <yuya@tcha.org>
parents:
29811
diff
changeset
|
255 self._aliases = {} |
28882
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
256 |
29812
c63ab0524db7
pycompat: delay loading modules registered to stub
Yuya Nishihara <yuya@tcha.org>
parents:
29811
diff
changeset
|
257 def _registeraliases(self, origin, items): |
c63ab0524db7
pycompat: delay loading modules registered to stub
Yuya Nishihara <yuya@tcha.org>
parents:
29811
diff
changeset
|
258 """Add items that will be populated at the first access""" |
30086
f3a1089654e3
pycompat: when setting attrs, ensure we use sysstr
Augie Fackler <augie@google.com>
parents:
30032
diff
changeset
|
259 items = map(sysstr, items) |
f3a1089654e3
pycompat: when setting attrs, ensure we use sysstr
Augie Fackler <augie@google.com>
parents:
30032
diff
changeset
|
260 self._aliases.update( |
f3a1089654e3
pycompat: when setting attrs, ensure we use sysstr
Augie Fackler <augie@google.com>
parents:
30032
diff
changeset
|
261 (item.replace(sysstr('_'), sysstr('')).lower(), (origin, item)) |
f3a1089654e3
pycompat: when setting attrs, ensure we use sysstr
Augie Fackler <augie@google.com>
parents:
30032
diff
changeset
|
262 for item in items) |
28882
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
263 |
31572
c6df6a23dfe5
pycompat: alias urlreq.unquote to unquote_to_bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
31508
diff
changeset
|
264 def _registeralias(self, origin, attr, name): |
c6df6a23dfe5
pycompat: alias urlreq.unquote to unquote_to_bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
31508
diff
changeset
|
265 """Alias ``origin``.``attr`` as ``name``""" |
c6df6a23dfe5
pycompat: alias urlreq.unquote to unquote_to_bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
31508
diff
changeset
|
266 self._aliases[sysstr(name)] = (origin, sysstr(attr)) |
c6df6a23dfe5
pycompat: alias urlreq.unquote to unquote_to_bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
31508
diff
changeset
|
267 |
29812
c63ab0524db7
pycompat: delay loading modules registered to stub
Yuya Nishihara <yuya@tcha.org>
parents:
29811
diff
changeset
|
268 def __getattr__(self, name): |
28882
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
269 try: |
29812
c63ab0524db7
pycompat: delay loading modules registered to stub
Yuya Nishihara <yuya@tcha.org>
parents:
29811
diff
changeset
|
270 origin, item = self._aliases[name] |
c63ab0524db7
pycompat: delay loading modules registered to stub
Yuya Nishihara <yuya@tcha.org>
parents:
29811
diff
changeset
|
271 except KeyError: |
c63ab0524db7
pycompat: delay loading modules registered to stub
Yuya Nishihara <yuya@tcha.org>
parents:
29811
diff
changeset
|
272 raise AttributeError(name) |
c63ab0524db7
pycompat: delay loading modules registered to stub
Yuya Nishihara <yuya@tcha.org>
parents:
29811
diff
changeset
|
273 self.__dict__[name] = obj = getattr(origin, item) |
c63ab0524db7
pycompat: delay loading modules registered to stub
Yuya Nishihara <yuya@tcha.org>
parents:
29811
diff
changeset
|
274 return obj |
28882
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
275 |
29566
075146e85bb6
py3: conditionalize BaseHTTPServer, SimpleHTTPServer and CGIHTTPServer import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29455
diff
changeset
|
276 httpserver = _pycompatstub() |
28882
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
277 urlreq = _pycompatstub() |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
278 urlerr = _pycompatstub() |
30031
0f6d6fdd3c2a
pycompat: provide 'ispy3' constant
Yuya Nishihara <yuya@tcha.org>
parents:
29812
diff
changeset
|
279 if not ispy3: |
29566
075146e85bb6
py3: conditionalize BaseHTTPServer, SimpleHTTPServer and CGIHTTPServer import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29455
diff
changeset
|
280 import BaseHTTPServer |
075146e85bb6
py3: conditionalize BaseHTTPServer, SimpleHTTPServer and CGIHTTPServer import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29455
diff
changeset
|
281 import CGIHTTPServer |
075146e85bb6
py3: conditionalize BaseHTTPServer, SimpleHTTPServer and CGIHTTPServer import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29455
diff
changeset
|
282 import SimpleHTTPServer |
28882
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
283 import urllib2 |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
284 import urllib |
31575
e68932dfbb55
pycompat: define urlreq.urlparse and urlreq.unparse aliases
Gregory Szorc <gregory.szorc@gmail.com>
parents:
31574
diff
changeset
|
285 import urlparse |
29812
c63ab0524db7
pycompat: delay loading modules registered to stub
Yuya Nishihara <yuya@tcha.org>
parents:
29811
diff
changeset
|
286 urlreq._registeraliases(urllib, ( |
28882
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
287 "addclosehook", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
288 "addinfourl", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
289 "ftpwrapper", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
290 "pathname2url", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
291 "quote", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
292 "splitattr", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
293 "splitpasswd", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
294 "splitport", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
295 "splituser", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
296 "unquote", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
297 "url2pathname", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
298 "urlencode", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
299 )) |
29812
c63ab0524db7
pycompat: delay loading modules registered to stub
Yuya Nishihara <yuya@tcha.org>
parents:
29811
diff
changeset
|
300 urlreq._registeraliases(urllib2, ( |
28882
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
301 "AbstractHTTPHandler", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
302 "BaseHandler", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
303 "build_opener", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
304 "FileHandler", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
305 "FTPHandler", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
306 "HTTPBasicAuthHandler", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
307 "HTTPDigestAuthHandler", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
308 "HTTPHandler", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
309 "HTTPPasswordMgrWithDefaultRealm", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
310 "HTTPSHandler", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
311 "install_opener", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
312 "ProxyHandler", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
313 "Request", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
314 "urlopen", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
315 )) |
31575
e68932dfbb55
pycompat: define urlreq.urlparse and urlreq.unparse aliases
Gregory Szorc <gregory.szorc@gmail.com>
parents:
31574
diff
changeset
|
316 urlreq._registeraliases(urlparse, ( |
e68932dfbb55
pycompat: define urlreq.urlparse and urlreq.unparse aliases
Gregory Szorc <gregory.szorc@gmail.com>
parents:
31574
diff
changeset
|
317 "urlparse", |
e68932dfbb55
pycompat: define urlreq.urlparse and urlreq.unparse aliases
Gregory Szorc <gregory.szorc@gmail.com>
parents:
31574
diff
changeset
|
318 "urlunparse", |
e68932dfbb55
pycompat: define urlreq.urlparse and urlreq.unparse aliases
Gregory Szorc <gregory.szorc@gmail.com>
parents:
31574
diff
changeset
|
319 )) |
29812
c63ab0524db7
pycompat: delay loading modules registered to stub
Yuya Nishihara <yuya@tcha.org>
parents:
29811
diff
changeset
|
320 urlerr._registeraliases(urllib2, ( |
28882
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
321 "HTTPError", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
322 "URLError", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
323 )) |
29812
c63ab0524db7
pycompat: delay loading modules registered to stub
Yuya Nishihara <yuya@tcha.org>
parents:
29811
diff
changeset
|
324 httpserver._registeraliases(BaseHTTPServer, ( |
29566
075146e85bb6
py3: conditionalize BaseHTTPServer, SimpleHTTPServer and CGIHTTPServer import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29455
diff
changeset
|
325 "HTTPServer", |
075146e85bb6
py3: conditionalize BaseHTTPServer, SimpleHTTPServer and CGIHTTPServer import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29455
diff
changeset
|
326 "BaseHTTPRequestHandler", |
075146e85bb6
py3: conditionalize BaseHTTPServer, SimpleHTTPServer and CGIHTTPServer import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29455
diff
changeset
|
327 )) |
29812
c63ab0524db7
pycompat: delay loading modules registered to stub
Yuya Nishihara <yuya@tcha.org>
parents:
29811
diff
changeset
|
328 httpserver._registeraliases(SimpleHTTPServer, ( |
29566
075146e85bb6
py3: conditionalize BaseHTTPServer, SimpleHTTPServer and CGIHTTPServer import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29455
diff
changeset
|
329 "SimpleHTTPRequestHandler", |
075146e85bb6
py3: conditionalize BaseHTTPServer, SimpleHTTPServer and CGIHTTPServer import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29455
diff
changeset
|
330 )) |
29812
c63ab0524db7
pycompat: delay loading modules registered to stub
Yuya Nishihara <yuya@tcha.org>
parents:
29811
diff
changeset
|
331 httpserver._registeraliases(CGIHTTPServer, ( |
29566
075146e85bb6
py3: conditionalize BaseHTTPServer, SimpleHTTPServer and CGIHTTPServer import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29455
diff
changeset
|
332 "CGIHTTPRequestHandler", |
075146e85bb6
py3: conditionalize BaseHTTPServer, SimpleHTTPServer and CGIHTTPServer import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29455
diff
changeset
|
333 )) |
28882
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
334 |
29812
c63ab0524db7
pycompat: delay loading modules registered to stub
Yuya Nishihara <yuya@tcha.org>
parents:
29811
diff
changeset
|
335 else: |
31408
1ed169c5e235
pycompat: alias urllib symbols directly
Gregory Szorc <gregory.szorc@gmail.com>
parents:
31391
diff
changeset
|
336 import urllib.parse |
1ed169c5e235
pycompat: alias urllib symbols directly
Gregory Szorc <gregory.szorc@gmail.com>
parents:
31391
diff
changeset
|
337 urlreq._registeraliases(urllib.parse, ( |
1ed169c5e235
pycompat: alias urllib symbols directly
Gregory Szorc <gregory.szorc@gmail.com>
parents:
31391
diff
changeset
|
338 "splitattr", |
1ed169c5e235
pycompat: alias urllib symbols directly
Gregory Szorc <gregory.szorc@gmail.com>
parents:
31391
diff
changeset
|
339 "splitpasswd", |
1ed169c5e235
pycompat: alias urllib symbols directly
Gregory Szorc <gregory.szorc@gmail.com>
parents:
31391
diff
changeset
|
340 "splitport", |
1ed169c5e235
pycompat: alias urllib symbols directly
Gregory Szorc <gregory.szorc@gmail.com>
parents:
31391
diff
changeset
|
341 "splituser", |
31575
e68932dfbb55
pycompat: define urlreq.urlparse and urlreq.unparse aliases
Gregory Szorc <gregory.szorc@gmail.com>
parents:
31574
diff
changeset
|
342 "urlparse", |
e68932dfbb55
pycompat: define urlreq.urlparse and urlreq.unparse aliases
Gregory Szorc <gregory.szorc@gmail.com>
parents:
31574
diff
changeset
|
343 "urlunparse", |
31408
1ed169c5e235
pycompat: alias urllib symbols directly
Gregory Szorc <gregory.szorc@gmail.com>
parents:
31391
diff
changeset
|
344 )) |
31572
c6df6a23dfe5
pycompat: alias urlreq.unquote to unquote_to_bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
31508
diff
changeset
|
345 urlreq._registeralias(urllib.parse, "unquote_to_bytes", "unquote") |
28882
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
346 import urllib.request |
29812
c63ab0524db7
pycompat: delay loading modules registered to stub
Yuya Nishihara <yuya@tcha.org>
parents:
29811
diff
changeset
|
347 urlreq._registeraliases(urllib.request, ( |
28882
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
348 "AbstractHTTPHandler", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
349 "BaseHandler", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
350 "build_opener", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
351 "FileHandler", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
352 "FTPHandler", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
353 "ftpwrapper", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
354 "HTTPHandler", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
355 "HTTPSHandler", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
356 "install_opener", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
357 "pathname2url", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
358 "HTTPBasicAuthHandler", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
359 "HTTPDigestAuthHandler", |
29414
2646fbba4ca7
pycompat: add HTTPPasswordMgrWithDefaultRealm to Python 3 block
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29405
diff
changeset
|
360 "HTTPPasswordMgrWithDefaultRealm", |
28882
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
361 "ProxyHandler", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
362 "Request", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
363 "url2pathname", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
364 "urlopen", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
365 )) |
31408
1ed169c5e235
pycompat: alias urllib symbols directly
Gregory Szorc <gregory.szorc@gmail.com>
parents:
31391
diff
changeset
|
366 import urllib.response |
1ed169c5e235
pycompat: alias urllib symbols directly
Gregory Szorc <gregory.szorc@gmail.com>
parents:
31391
diff
changeset
|
367 urlreq._registeraliases(urllib.response, ( |
1ed169c5e235
pycompat: alias urllib symbols directly
Gregory Szorc <gregory.szorc@gmail.com>
parents:
31391
diff
changeset
|
368 "addclosehook", |
1ed169c5e235
pycompat: alias urllib symbols directly
Gregory Szorc <gregory.szorc@gmail.com>
parents:
31391
diff
changeset
|
369 "addinfourl", |
1ed169c5e235
pycompat: alias urllib symbols directly
Gregory Szorc <gregory.szorc@gmail.com>
parents:
31391
diff
changeset
|
370 )) |
28882
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
371 import urllib.error |
29812
c63ab0524db7
pycompat: delay loading modules registered to stub
Yuya Nishihara <yuya@tcha.org>
parents:
29811
diff
changeset
|
372 urlerr._registeraliases(urllib.error, ( |
28882
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
373 "HTTPError", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
374 "URLError", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
375 )) |
29566
075146e85bb6
py3: conditionalize BaseHTTPServer, SimpleHTTPServer and CGIHTTPServer import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29455
diff
changeset
|
376 import http.server |
29812
c63ab0524db7
pycompat: delay loading modules registered to stub
Yuya Nishihara <yuya@tcha.org>
parents:
29811
diff
changeset
|
377 httpserver._registeraliases(http.server, ( |
29566
075146e85bb6
py3: conditionalize BaseHTTPServer, SimpleHTTPServer and CGIHTTPServer import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29455
diff
changeset
|
378 "HTTPServer", |
075146e85bb6
py3: conditionalize BaseHTTPServer, SimpleHTTPServer and CGIHTTPServer import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29455
diff
changeset
|
379 "BaseHTTPRequestHandler", |
075146e85bb6
py3: conditionalize BaseHTTPServer, SimpleHTTPServer and CGIHTTPServer import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29455
diff
changeset
|
380 "SimpleHTTPRequestHandler", |
075146e85bb6
py3: conditionalize BaseHTTPServer, SimpleHTTPServer and CGIHTTPServer import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29455
diff
changeset
|
381 "CGIHTTPRequestHandler", |
075146e85bb6
py3: conditionalize BaseHTTPServer, SimpleHTTPServer and CGIHTTPServer import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29455
diff
changeset
|
382 )) |
31409
fb1f70331ee6
pycompat: custom implementation of urllib.parse.quote()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
31408
diff
changeset
|
383 |
fb1f70331ee6
pycompat: custom implementation of urllib.parse.quote()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
31408
diff
changeset
|
384 # urllib.parse.quote() accepts both str and bytes, decodes bytes |
fb1f70331ee6
pycompat: custom implementation of urllib.parse.quote()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
31408
diff
changeset
|
385 # (if necessary), and returns str. This is wonky. We provide a custom |
fb1f70331ee6
pycompat: custom implementation of urllib.parse.quote()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
31408
diff
changeset
|
386 # implementation that only accepts bytes and emits bytes. |
fb1f70331ee6
pycompat: custom implementation of urllib.parse.quote()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
31408
diff
changeset
|
387 def quote(s, safe=r'/'): |
fb1f70331ee6
pycompat: custom implementation of urllib.parse.quote()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
31408
diff
changeset
|
388 s = urllib.parse.quote_from_bytes(s, safe=safe) |
fb1f70331ee6
pycompat: custom implementation of urllib.parse.quote()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
31408
diff
changeset
|
389 return s.encode('ascii', 'strict') |
fb1f70331ee6
pycompat: custom implementation of urllib.parse.quote()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
31408
diff
changeset
|
390 |
fb1f70331ee6
pycompat: custom implementation of urllib.parse.quote()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
31408
diff
changeset
|
391 urlreq.quote = quote |