author | Yuya Nishihara <yuya@tcha.org> |
Sun, 13 Aug 2017 11:10:35 +0900 | |
changeset 33810 | b3a41f4d837b |
parent 33626 | 524b13fc711f |
child 33853 | cfcfbe6c96f8 |
permissions | -rw-r--r-- |
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 |
|
30578
c6ce11f2ee50
py3: make a bytes version of getopt.getopt()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30500
diff
changeset
|
13 |
import getopt |
30302
3874ddba1ab4
py3: add a bytes version of os.name
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30300
diff
changeset
|
14 |
import os |
30678
caf7e1c5efe4
py3: have a bytes version of shlex.split()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30668
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 |
|
30030
0f6d6fdd3c2a
pycompat: provide 'ispy3' constant
Yuya Nishihara <yuya@tcha.org>
parents:
29801
diff
changeset
|
18 |
ispy3 = (sys.version_info[0] >= 3) |
33626
524b13fc711f
util: fix sortdict.update() to call __setitem__() on PyPy (issue5639)
Yuya Nishihara <yuya@tcha.org>
parents:
32865
diff
changeset
|
19 |
ispypy = (r'__pypy__' in sys.builtin_module_names) |
30030
0f6d6fdd3c2a
pycompat: provide 'ispy3' constant
Yuya Nishihara <yuya@tcha.org>
parents:
29801
diff
changeset
|
20 |
|
0f6d6fdd3c2a
pycompat: provide 'ispy3' constant
Yuya Nishihara <yuya@tcha.org>
parents:
29801
diff
changeset
|
21 |
if not ispy3: |
31934
12aca6770046
util: make cookielib module available
Gregory Szorc <gregory.szorc@gmail.com>
parents:
31843
diff
changeset
|
22 |
import cookielib |
29324
b501579147f1
py3: conditionalize cPickle import by adding in util
Pulkit Goyal <7895pulkit@gmail.com>
parents:
28882
diff
changeset
|
23 |
import cPickle as pickle |
29455
0c741fd6158a
py3: conditionalize httplib import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29433
diff
changeset
|
24 |
import httplib |
29584
06587edd1233
pycompat: make pycompat demandimport friendly
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29566
diff
changeset
|
25 |
import Queue as _queue |
29433
33770d2b6cf9
py3: conditionalize SocketServer import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29432
diff
changeset
|
26 |
import SocketServer as socketserver |
29432
34b914ac573e
py3: conditionalize xmlrpclib import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29431
diff
changeset
|
27 |
import xmlrpclib |
29584
06587edd1233
pycompat: make pycompat demandimport friendly
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29566
diff
changeset
|
28 |
else: |
31942
bc0579a25f82
pycompat: import correct cookie module on Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
31934
diff
changeset
|
29 |
import http.cookiejar as cookielib |
29584
06587edd1233
pycompat: make pycompat demandimport friendly
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29566
diff
changeset
|
30 |
import http.client as httplib |
06587edd1233
pycompat: make pycompat demandimport friendly
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29566
diff
changeset
|
31 |
import pickle |
06587edd1233
pycompat: make pycompat demandimport friendly
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29566
diff
changeset
|
32 |
import queue as _queue |
06587edd1233
pycompat: make pycompat demandimport friendly
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29566
diff
changeset
|
33 |
import socketserver |
29432
34b914ac573e
py3: conditionalize xmlrpclib import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29431
diff
changeset
|
34 |
import xmlrpc.client as xmlrpclib |
29431
80880ad3fccd
py3: conditionalize the urlparse import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29414
diff
changeset
|
35 |
|
32865
6e38b4212661
pycompat: move the queue related definitions below queue import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32864
diff
changeset
|
36 |
empty = _queue.Empty |
6e38b4212661
pycompat: move the queue related definitions below queue import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32864
diff
changeset
|
37 |
queue = _queue.Queue |
6e38b4212661
pycompat: move the queue related definitions below queue import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32864
diff
changeset
|
38 |
|
31774
7d2cbe11ae48
pycompat: introduce identity function as a compat stub
Yuya Nishihara <yuya@tcha.org>
parents:
31573
diff
changeset
|
39 |
def identity(a): |
7d2cbe11ae48
pycompat: introduce identity function as a compat stub
Yuya Nishihara <yuya@tcha.org>
parents:
31573
diff
changeset
|
40 |
return a |
7d2cbe11ae48
pycompat: introduce identity function as a compat stub
Yuya Nishihara <yuya@tcha.org>
parents:
31573
diff
changeset
|
41 |
|
30030
0f6d6fdd3c2a
pycompat: provide 'ispy3' constant
Yuya Nishihara <yuya@tcha.org>
parents:
29801
diff
changeset
|
42 |
if ispy3: |
29797
965c91bad9e3
py3: move xrange alias next to import lines
Yuya Nishihara <yuya@tcha.org>
parents:
29779
diff
changeset
|
43 |
import builtins |
29799
45fa8de47a0f
py3: provide (del|get|has|set)attr wrappers that accepts bytes
Yuya Nishihara <yuya@tcha.org>
parents:
29798
diff
changeset
|
44 |
import functools |
31372
06440ba06bc0
pycompat: move imports of cStringIO/io to where they are used
Yuya Nishihara <yuya@tcha.org>
parents:
31359
diff
changeset
|
45 |
import io |
31424
4acc49335a6e
py3: optimize py3 compat.bytechr using Struct.pack
Martin von Zweigbergk <martinvonz@google.com>
parents:
31400
diff
changeset
|
46 |
import struct |
31372
06440ba06bc0
pycompat: move imports of cStringIO/io to where they are used
Yuya Nishihara <yuya@tcha.org>
parents:
31359
diff
changeset
|
47 |
|
30119
f4a5e0e86a7e
py3: add an os.fsencode backport to ease path handling
Martijn Pieters <mjpieters@fb.com>
parents:
30086
diff
changeset
|
48 |
fsencode = os.fsencode |
30300
42af0590f4b9
py3: add os.fsdecode() as pycompat.fsdecode()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30133
diff
changeset
|
49 |
fsdecode = os.fsdecode |
31775
8181f378b073
pycompat: provide bytes os.linesep
Yuya Nishihara <yuya@tcha.org>
parents:
31774
diff
changeset
|
50 |
oslinesep = os.linesep.encode('ascii') |
30302
3874ddba1ab4
py3: add a bytes version of os.name
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30300
diff
changeset
|
51 |
osname = os.name.encode('ascii') |
30303
ad40d307a9f0
py3: have pycompat.ospathsep and pycompat.ossep
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30302
diff
changeset
|
52 |
ospathsep = os.pathsep.encode('ascii') |
ad40d307a9f0
py3: have pycompat.ospathsep and pycompat.ossep
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30302
diff
changeset
|
53 |
ossep = os.sep.encode('ascii') |
30623
c6026c20a3ce
py3: have a bytes version of os.altsep
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30579
diff
changeset
|
54 |
osaltsep = os.altsep |
c6026c20a3ce
py3: have a bytes version of os.altsep
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30579
diff
changeset
|
55 |
if osaltsep: |
c6026c20a3ce
py3: have a bytes version of os.altsep
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30579
diff
changeset
|
56 |
osaltsep = osaltsep.encode('ascii') |
30500
fc0cfe6c87d7
py3: add os.getcwdb() to have bytes path
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30472
diff
changeset
|
57 |
# 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:
30472
diff
changeset
|
58 |
# returns bytes. |
fc0cfe6c87d7
py3: add os.getcwdb() to have bytes path
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30472
diff
changeset
|
59 |
getcwd = os.getcwdb |
30624
a82a6eee2613
py3: have a bytes version of sys.platform
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30623
diff
changeset
|
60 |
sysplatform = sys.platform.encode('ascii') |
30668
3fcaf0f660ce
py3: have bytes version of sys.executable
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30663
diff
changeset
|
61 |
sysexecutable = sys.executable |
3fcaf0f660ce
py3: have bytes version of sys.executable
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30663
diff
changeset
|
62 |
if sysexecutable: |
3fcaf0f660ce
py3: have bytes version of sys.executable
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30663
diff
changeset
|
63 |
sysexecutable = os.fsencode(sysexecutable) |
31359
73b3bee8febe
pycompat: default to BytesIO instead of StringIO
Pulkit Goyal <7895pulkit@gmail.com>
parents:
31301
diff
changeset
|
64 |
stringio = io.BytesIO |
31501
a1e40ceee640
pycompat: add maplist alias for old map behavior
Augie Fackler <augie@google.com>
parents:
31439
diff
changeset
|
65 |
maplist = lambda *args: list(map(*args)) |
30334
19d8e19fde5b
py3: document why os.fsencode() can be used to get back bytes argv
Yuya Nishihara <yuya@tcha.org>
parents:
30330
diff
changeset
|
66 |
|
30472
277f4fe6d01a
py3: provide bytes stdin/out/err through util module
Yuya Nishihara <yuya@tcha.org>
parents:
30334
diff
changeset
|
67 |
# 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:
30334
diff
changeset
|
68 |
# 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:
30334
diff
changeset
|
69 |
stdin = sys.stdin.buffer |
277f4fe6d01a
py3: provide bytes stdin/out/err through util module
Yuya Nishihara <yuya@tcha.org>
parents:
30334
diff
changeset
|
70 |
stdout = sys.stdout.buffer |
277f4fe6d01a
py3: provide bytes stdin/out/err through util module
Yuya Nishihara <yuya@tcha.org>
parents:
30334
diff
changeset
|
71 |
stderr = sys.stderr.buffer |
277f4fe6d01a
py3: provide bytes stdin/out/err through util module
Yuya Nishihara <yuya@tcha.org>
parents:
30334
diff
changeset
|
72 |
|
30334
19d8e19fde5b
py3: document why os.fsencode() can be used to get back bytes argv
Yuya Nishihara <yuya@tcha.org>
parents:
30330
diff
changeset
|
73 |
# 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:
30330
diff
changeset
|
74 |
# 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:
30330
diff
changeset
|
75 |
# |
19d8e19fde5b
py3: document why os.fsencode() can be used to get back bytes argv
Yuya Nishihara <yuya@tcha.org>
parents:
30330
diff
changeset
|
76 |
# 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:
30330
diff
changeset
|
77 |
# |
19d8e19fde5b
py3: document why os.fsencode() can be used to get back bytes argv
Yuya Nishihara <yuya@tcha.org>
parents:
30330
diff
changeset
|
78 |
# 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:
30330
diff
changeset
|
79 |
# workaround to simulate the Python 2 (i.e. ANSI Win32 API) behavior. |
31277
86cd1f2cfff5
pycompat: verify sys.argv exists before forwarding it (issue5493)
Augie Fackler <augie@google.com>
parents:
30820
diff
changeset
|
80 |
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
|
81 |
sysargv = list(map(os.fsencode, sys.argv)) |
29797
965c91bad9e3
py3: move xrange alias next to import lines
Yuya Nishihara <yuya@tcha.org>
parents:
29779
diff
changeset
|
82 |
|
31424
4acc49335a6e
py3: optimize py3 compat.bytechr using Struct.pack
Martin von Zweigbergk <martinvonz@google.com>
parents:
31400
diff
changeset
|
83 |
bytechr = struct.Struct('>B').pack |
31253
64596338ba10
py3: factor out bytechr() function
Yuya Nishihara <yuya@tcha.org>
parents:
31149
diff
changeset
|
84 |
|
31439
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31425
diff
changeset
|
85 |
class bytestr(bytes): |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31425
diff
changeset
|
86 |
"""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:
31425
diff
changeset
|
87 |
|
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31425
diff
changeset
|
88 |
>>> 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:
31425
diff
changeset
|
89 |
(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:
31425
diff
changeset
|
90 |
>>> s = bytestr(b'foo') |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31425
diff
changeset
|
91 |
>>> assert s is bytestr(s) |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31425
diff
changeset
|
92 |
|
32450
548478efc46c
pycompat: try __bytes__() to convert object to bytestr
Yuya Nishihara <yuya@tcha.org>
parents:
32186
diff
changeset
|
93 |
__bytes__() should be called if provided: |
548478efc46c
pycompat: try __bytes__() to convert object to bytestr
Yuya Nishihara <yuya@tcha.org>
parents:
32186
diff
changeset
|
94 |
|
548478efc46c
pycompat: try __bytes__() to convert object to bytestr
Yuya Nishihara <yuya@tcha.org>
parents:
32186
diff
changeset
|
95 |
>>> class bytesable(object): |
548478efc46c
pycompat: try __bytes__() to convert object to bytestr
Yuya Nishihara <yuya@tcha.org>
parents:
32186
diff
changeset
|
96 |
... def __bytes__(self): |
548478efc46c
pycompat: try __bytes__() to convert object to bytestr
Yuya Nishihara <yuya@tcha.org>
parents:
32186
diff
changeset
|
97 |
... return b'bytes' |
548478efc46c
pycompat: try __bytes__() to convert object to bytestr
Yuya Nishihara <yuya@tcha.org>
parents:
32186
diff
changeset
|
98 |
>>> bytestr(bytesable()) |
548478efc46c
pycompat: try __bytes__() to convert object to bytestr
Yuya Nishihara <yuya@tcha.org>
parents:
32186
diff
changeset
|
99 |
b'bytes' |
548478efc46c
pycompat: try __bytes__() to convert object to bytestr
Yuya Nishihara <yuya@tcha.org>
parents:
32186
diff
changeset
|
100 |
|
31439
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31425
diff
changeset
|
101 |
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:
31425
diff
changeset
|
102 |
unknown: |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31425
diff
changeset
|
103 |
|
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31425
diff
changeset
|
104 |
>>> bytestr(chr(0x80)) # doctest: +ELLIPSIS |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31425
diff
changeset
|
105 |
Traceback (most recent call last): |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31425
diff
changeset
|
106 |
... |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31425
diff
changeset
|
107 |
UnicodeEncodeError: ... |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31425
diff
changeset
|
108 |
|
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31425
diff
changeset
|
109 |
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:
31425
diff
changeset
|
110 |
|
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31425
diff
changeset
|
111 |
>>> 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:
31425
diff
changeset
|
112 |
>>> 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:
31425
diff
changeset
|
113 |
>>> 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:
31425
diff
changeset
|
114 |
>>> 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:
31425
diff
changeset
|
115 |
|
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31425
diff
changeset
|
116 |
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:
31425
diff
changeset
|
117 |
|
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31425
diff
changeset
|
118 |
>>> s[1], s[:2] |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31425
diff
changeset
|
119 |
(b'o', b'fo') |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31425
diff
changeset
|
120 |
>>> list(s), list(reversed(s)) |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31425
diff
changeset
|
121 |
([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:
31425
diff
changeset
|
122 |
|
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31425
diff
changeset
|
123 |
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:
31425
diff
changeset
|
124 |
bytes to bytestr explicitly: |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31425
diff
changeset
|
125 |
|
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31425
diff
changeset
|
126 |
>>> s = bytestr(b'foo').upper() |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31425
diff
changeset
|
127 |
>>> t = bytestr(s) |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31425
diff
changeset
|
128 |
>>> s[0], t[0] |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31425
diff
changeset
|
129 |
(70, b'F') |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31425
diff
changeset
|
130 |
|
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31425
diff
changeset
|
131 |
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:
31425
diff
changeset
|
132 |
bytearray-like behavior. |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31425
diff
changeset
|
133 |
|
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31425
diff
changeset
|
134 |
>>> 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:
31425
diff
changeset
|
135 |
>>> assert type(t) is bytes |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31425
diff
changeset
|
136 |
""" |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31425
diff
changeset
|
137 |
|
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31425
diff
changeset
|
138 |
def __new__(cls, s=b''): |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31425
diff
changeset
|
139 |
if isinstance(s, bytestr): |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31425
diff
changeset
|
140 |
return s |
32450
548478efc46c
pycompat: try __bytes__() to convert object to bytestr
Yuya Nishihara <yuya@tcha.org>
parents:
32186
diff
changeset
|
141 |
if (not isinstance(s, (bytes, bytearray)) |
548478efc46c
pycompat: try __bytes__() to convert object to bytestr
Yuya Nishihara <yuya@tcha.org>
parents:
32186
diff
changeset
|
142 |
and not hasattr(s, u'__bytes__')): # hasattr-py3-only |
31439
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31425
diff
changeset
|
143 |
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:
31425
diff
changeset
|
144 |
return bytes.__new__(cls, s) |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31425
diff
changeset
|
145 |
|
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31425
diff
changeset
|
146 |
def __getitem__(self, key): |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31425
diff
changeset
|
147 |
s = bytes.__getitem__(self, key) |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31425
diff
changeset
|
148 |
if not isinstance(s, bytes): |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31425
diff
changeset
|
149 |
s = bytechr(s) |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31425
diff
changeset
|
150 |
return s |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31425
diff
changeset
|
151 |
|
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31425
diff
changeset
|
152 |
def __iter__(self): |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31425
diff
changeset
|
153 |
return iterbytestr(bytes.__iter__(self)) |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31425
diff
changeset
|
154 |
|
31382
c9fd842dc886
pycompat: add helper to iterate each char in bytes
Yuya Nishihara <yuya@tcha.org>
parents:
31372
diff
changeset
|
155 |
def iterbytestr(s): |
c9fd842dc886
pycompat: add helper to iterate each char in bytes
Yuya Nishihara <yuya@tcha.org>
parents:
31372
diff
changeset
|
156 |
"""Iterate bytes as if it were a str object of Python 2""" |
31425
63a39d647888
py3: make py3 compat.iterbytestr simpler and faster
Martin von Zweigbergk <martinvonz@google.com>
parents:
31424
diff
changeset
|
157 |
return map(bytechr, s) |
31382
c9fd842dc886
pycompat: add helper to iterate each char in bytes
Yuya Nishihara <yuya@tcha.org>
parents:
31372
diff
changeset
|
158 |
|
31820
45761ef1bc93
py3: have registrar process docstrings in bytes
Yuya Nishihara <yuya@tcha.org>
parents:
31775
diff
changeset
|
159 |
def sysbytes(s): |
45761ef1bc93
py3: have registrar process docstrings in bytes
Yuya Nishihara <yuya@tcha.org>
parents:
31775
diff
changeset
|
160 |
"""Convert an internal str (e.g. keyword, __doc__) back to bytes |
45761ef1bc93
py3: have registrar process docstrings in bytes
Yuya Nishihara <yuya@tcha.org>
parents:
31775
diff
changeset
|
161 |
|
45761ef1bc93
py3: have registrar process docstrings in bytes
Yuya Nishihara <yuya@tcha.org>
parents:
31775
diff
changeset
|
162 |
This never raises UnicodeEncodeError, but only ASCII characters |
45761ef1bc93
py3: have registrar process docstrings in bytes
Yuya Nishihara <yuya@tcha.org>
parents:
31775
diff
changeset
|
163 |
can be round-trip by sysstr(sysbytes(s)). |
45761ef1bc93
py3: have registrar process docstrings in bytes
Yuya Nishihara <yuya@tcha.org>
parents:
31775
diff
changeset
|
164 |
""" |
45761ef1bc93
py3: have registrar process docstrings in bytes
Yuya Nishihara <yuya@tcha.org>
parents:
31775
diff
changeset
|
165 |
return s.encode(u'utf-8') |
45761ef1bc93
py3: have registrar process docstrings in bytes
Yuya Nishihara <yuya@tcha.org>
parents:
31775
diff
changeset
|
166 |
|
30032
2219f4f82ede
pycompat: extract function that converts attribute or encoding name to str
Yuya Nishihara <yuya@tcha.org>
parents:
30030
diff
changeset
|
167 |
def sysstr(s): |
2219f4f82ede
pycompat: extract function that converts attribute or encoding name to str
Yuya Nishihara <yuya@tcha.org>
parents:
30030
diff
changeset
|
168 |
"""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:
30030
diff
changeset
|
169 |
getattr() and str.encode() |
2219f4f82ede
pycompat: extract function that converts attribute or encoding name to str
Yuya Nishihara <yuya@tcha.org>
parents:
30030
diff
changeset
|
170 |
|
2219f4f82ede
pycompat: extract function that converts attribute or encoding name to str
Yuya Nishihara <yuya@tcha.org>
parents:
30030
diff
changeset
|
171 |
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:
30030
diff
changeset
|
172 |
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:
30030
diff
changeset
|
173 |
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:
30030
diff
changeset
|
174 |
""" |
2219f4f82ede
pycompat: extract function that converts attribute or encoding name to str
Yuya Nishihara <yuya@tcha.org>
parents:
30030
diff
changeset
|
175 |
if isinstance(s, builtins.str): |
2219f4f82ede
pycompat: extract function that converts attribute or encoding name to str
Yuya Nishihara <yuya@tcha.org>
parents:
30030
diff
changeset
|
176 |
return s |
2219f4f82ede
pycompat: extract function that converts attribute or encoding name to str
Yuya Nishihara <yuya@tcha.org>
parents:
30030
diff
changeset
|
177 |
return s.decode(u'latin-1') |
2219f4f82ede
pycompat: extract function that converts attribute or encoding name to str
Yuya Nishihara <yuya@tcha.org>
parents:
30030
diff
changeset
|
178 |
|
32859
a05f3675c46a
py3: add a new strurl() which will convert a bytes url to str
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32615
diff
changeset
|
179 |
def strurl(url): |
a05f3675c46a
py3: add a new strurl() which will convert a bytes url to str
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32615
diff
changeset
|
180 |
"""Converts a bytes url back to str""" |
a05f3675c46a
py3: add a new strurl() which will convert a bytes url to str
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32615
diff
changeset
|
181 |
return url.decode(u'ascii') |
a05f3675c46a
py3: add a new strurl() which will convert a bytes url to str
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32615
diff
changeset
|
182 |
|
32860
f22f39d56bb5
py3: add a new bytesurl() to convert a str url into bytes
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32859
diff
changeset
|
183 |
def bytesurl(url): |
f22f39d56bb5
py3: add a new bytesurl() to convert a str url into bytes
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32859
diff
changeset
|
184 |
"""Converts a str url to bytes by encoding in ascii""" |
f22f39d56bb5
py3: add a new bytesurl() to convert a str url into bytes
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32859
diff
changeset
|
185 |
return url.encode(u'ascii') |
f22f39d56bb5
py3: add a new bytesurl() to convert a str url into bytes
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32859
diff
changeset
|
186 |
|
32186
76f9a0009b4b
pycompat: extract helper to raise exception with traceback
Yuya Nishihara <yuya@tcha.org>
parents:
31942
diff
changeset
|
187 |
def raisewithtb(exc, tb): |
76f9a0009b4b
pycompat: extract helper to raise exception with traceback
Yuya Nishihara <yuya@tcha.org>
parents:
31942
diff
changeset
|
188 |
"""Raise exception with the given traceback""" |
76f9a0009b4b
pycompat: extract helper to raise exception with traceback
Yuya Nishihara <yuya@tcha.org>
parents:
31942
diff
changeset
|
189 |
raise exc.with_traceback(tb) |
76f9a0009b4b
pycompat: extract helper to raise exception with traceback
Yuya Nishihara <yuya@tcha.org>
parents:
31942
diff
changeset
|
190 |
|
32615
c9318beb7c1a
py3: convert __doc__ back to bytes in help.py
Yuya Nishihara <yuya@tcha.org>
parents:
32450
diff
changeset
|
191 |
def getdoc(obj): |
c9318beb7c1a
py3: convert __doc__ back to bytes in help.py
Yuya Nishihara <yuya@tcha.org>
parents:
32450
diff
changeset
|
192 |
"""Get docstring as bytes; may be None so gettext() won't confuse it |
c9318beb7c1a
py3: convert __doc__ back to bytes in help.py
Yuya Nishihara <yuya@tcha.org>
parents:
32450
diff
changeset
|
193 |
with _('')""" |
c9318beb7c1a
py3: convert __doc__ back to bytes in help.py
Yuya Nishihara <yuya@tcha.org>
parents:
32450
diff
changeset
|
194 |
doc = getattr(obj, u'__doc__', None) |
c9318beb7c1a
py3: convert __doc__ back to bytes in help.py
Yuya Nishihara <yuya@tcha.org>
parents:
32450
diff
changeset
|
195 |
if doc is None: |
c9318beb7c1a
py3: convert __doc__ back to bytes in help.py
Yuya Nishihara <yuya@tcha.org>
parents:
32450
diff
changeset
|
196 |
return doc |
c9318beb7c1a
py3: convert __doc__ back to bytes in help.py
Yuya Nishihara <yuya@tcha.org>
parents:
32450
diff
changeset
|
197 |
return sysbytes(doc) |
c9318beb7c1a
py3: convert __doc__ back to bytes in help.py
Yuya Nishihara <yuya@tcha.org>
parents:
32450
diff
changeset
|
198 |
|
29799
45fa8de47a0f
py3: provide (del|get|has|set)attr wrappers that accepts bytes
Yuya Nishihara <yuya@tcha.org>
parents:
29798
diff
changeset
|
199 |
def _wrapattrfunc(f): |
45fa8de47a0f
py3: provide (del|get|has|set)attr wrappers that accepts bytes
Yuya Nishihara <yuya@tcha.org>
parents:
29798
diff
changeset
|
200 |
@functools.wraps(f) |
45fa8de47a0f
py3: provide (del|get|has|set)attr wrappers that accepts bytes
Yuya Nishihara <yuya@tcha.org>
parents:
29798
diff
changeset
|
201 |
def w(object, name, *args): |
30032
2219f4f82ede
pycompat: extract function that converts attribute or encoding name to str
Yuya Nishihara <yuya@tcha.org>
parents:
30030
diff
changeset
|
202 |
return f(object, sysstr(name), *args) |
29799
45fa8de47a0f
py3: provide (del|get|has|set)attr wrappers that accepts bytes
Yuya Nishihara <yuya@tcha.org>
parents:
29798
diff
changeset
|
203 |
return w |
45fa8de47a0f
py3: provide (del|get|has|set)attr wrappers that accepts bytes
Yuya Nishihara <yuya@tcha.org>
parents:
29798
diff
changeset
|
204 |
|
29800
178c89e8519a
py3: import builtin wrappers automagically by code transformer
Yuya Nishihara <yuya@tcha.org>
parents:
29799
diff
changeset
|
205 |
# these wrappers are automagically imported by hgloader |
29799
45fa8de47a0f
py3: provide (del|get|has|set)attr wrappers that accepts bytes
Yuya Nishihara <yuya@tcha.org>
parents:
29798
diff
changeset
|
206 |
delattr = _wrapattrfunc(builtins.delattr) |
45fa8de47a0f
py3: provide (del|get|has|set)attr wrappers that accepts bytes
Yuya Nishihara <yuya@tcha.org>
parents:
29798
diff
changeset
|
207 |
getattr = _wrapattrfunc(builtins.getattr) |
45fa8de47a0f
py3: provide (del|get|has|set)attr wrappers that accepts bytes
Yuya Nishihara <yuya@tcha.org>
parents:
29798
diff
changeset
|
208 |
hasattr = _wrapattrfunc(builtins.hasattr) |
45fa8de47a0f
py3: provide (del|get|has|set)attr wrappers that accepts bytes
Yuya Nishihara <yuya@tcha.org>
parents:
29798
diff
changeset
|
209 |
setattr = _wrapattrfunc(builtins.setattr) |
29800
178c89e8519a
py3: import builtin wrappers automagically by code transformer
Yuya Nishihara <yuya@tcha.org>
parents:
29799
diff
changeset
|
210 |
xrange = builtins.range |
31843
526e4597cca5
py3: add pycompat.unicode and add it to importer
Pulkit Goyal <7895pulkit@gmail.com>
parents:
31842
diff
changeset
|
211 |
unicode = str |
29799
45fa8de47a0f
py3: provide (del|get|has|set)attr wrappers that accepts bytes
Yuya Nishihara <yuya@tcha.org>
parents:
29798
diff
changeset
|
212 |
|
31149
76a64c1e5439
py3: add pycompat.open and replace open() calls
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30820
diff
changeset
|
213 |
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
|
214 |
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
|
215 |
|
30578
c6ce11f2ee50
py3: make a bytes version of getopt.getopt()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30500
diff
changeset
|
216 |
def getoptb(args, shortlist, namelist): |
32864
f57f1f37290d
pycompat: move multiline comments above a function to function doc
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32860
diff
changeset
|
217 |
""" |
f57f1f37290d
pycompat: move multiline comments above a function to function doc
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32860
diff
changeset
|
218 |
Takes bytes arguments, converts them to unicode, pass them to |
f57f1f37290d
pycompat: move multiline comments above a function to function doc
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32860
diff
changeset
|
219 |
getopt.getopt(), convert the returned values back to bytes and then |
f57f1f37290d
pycompat: move multiline comments above a function to function doc
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32860
diff
changeset
|
220 |
return them for Python 3 compatibility as getopt.getopt() don't accepts |
f57f1f37290d
pycompat: move multiline comments above a function to function doc
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32860
diff
changeset
|
221 |
bytes on Python 3. |
f57f1f37290d
pycompat: move multiline comments above a function to function doc
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32860
diff
changeset
|
222 |
""" |
30578
c6ce11f2ee50
py3: make a bytes version of getopt.getopt()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30500
diff
changeset
|
223 |
args = [a.decode('latin-1') for a in args] |
c6ce11f2ee50
py3: make a bytes version of getopt.getopt()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30500
diff
changeset
|
224 |
shortlist = shortlist.decode('latin-1') |
c6ce11f2ee50
py3: make a bytes version of getopt.getopt()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30500
diff
changeset
|
225 |
namelist = [a.decode('latin-1') for a in namelist] |
c6ce11f2ee50
py3: make a bytes version of getopt.getopt()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30500
diff
changeset
|
226 |
opts, args = getopt.getopt(args, shortlist, namelist) |
c6ce11f2ee50
py3: make a bytes version of getopt.getopt()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30500
diff
changeset
|
227 |
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:
30500
diff
changeset
|
228 |
for a in opts] |
c6ce11f2ee50
py3: make a bytes version of getopt.getopt()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30500
diff
changeset
|
229 |
args = [a.encode('latin-1') for a in args] |
c6ce11f2ee50
py3: make a bytes version of getopt.getopt()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30500
diff
changeset
|
230 |
return opts, args |
c6ce11f2ee50
py3: make a bytes version of getopt.getopt()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30500
diff
changeset
|
231 |
|
30579
fbc3f73dc802
py3: utility functions to convert keys of kwargs to bytes/unicodes
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30578
diff
changeset
|
232 |
def strkwargs(dic): |
32864
f57f1f37290d
pycompat: move multiline comments above a function to function doc
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32860
diff
changeset
|
233 |
""" |
f57f1f37290d
pycompat: move multiline comments above a function to function doc
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32860
diff
changeset
|
234 |
Converts the keys of a python dictonary to str i.e. unicodes so that |
f57f1f37290d
pycompat: move multiline comments above a function to function doc
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32860
diff
changeset
|
235 |
they can be passed as keyword arguments as dictonaries with bytes keys |
f57f1f37290d
pycompat: move multiline comments above a function to function doc
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32860
diff
changeset
|
236 |
can't be passed as keyword arguments to functions on Python 3. |
f57f1f37290d
pycompat: move multiline comments above a function to function doc
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32860
diff
changeset
|
237 |
""" |
30579
fbc3f73dc802
py3: utility functions to convert keys of kwargs to bytes/unicodes
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30578
diff
changeset
|
238 |
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:
30578
diff
changeset
|
239 |
return dic |
fbc3f73dc802
py3: utility functions to convert keys of kwargs to bytes/unicodes
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30578
diff
changeset
|
240 |
|
fbc3f73dc802
py3: utility functions to convert keys of kwargs to bytes/unicodes
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30578
diff
changeset
|
241 |
def byteskwargs(dic): |
32864
f57f1f37290d
pycompat: move multiline comments above a function to function doc
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32860
diff
changeset
|
242 |
""" |
f57f1f37290d
pycompat: move multiline comments above a function to function doc
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32860
diff
changeset
|
243 |
Converts keys of python dictonaries to bytes as they were converted to |
f57f1f37290d
pycompat: move multiline comments above a function to function doc
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32860
diff
changeset
|
244 |
str to pass that dictonary as a keyword argument on Python 3. |
f57f1f37290d
pycompat: move multiline comments above a function to function doc
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32860
diff
changeset
|
245 |
""" |
30579
fbc3f73dc802
py3: utility functions to convert keys of kwargs to bytes/unicodes
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30578
diff
changeset
|
246 |
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:
30578
diff
changeset
|
247 |
return dic |
fbc3f73dc802
py3: utility functions to convert keys of kwargs to bytes/unicodes
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30578
diff
changeset
|
248 |
|
30678
caf7e1c5efe4
py3: have a bytes version of shlex.split()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30668
diff
changeset
|
249 |
# TODO: handle shlex.shlex(). |
caf7e1c5efe4
py3: have a bytes version of shlex.split()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30668
diff
changeset
|
250 |
def shlexsplit(s): |
32864
f57f1f37290d
pycompat: move multiline comments above a function to function doc
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32860
diff
changeset
|
251 |
""" |
f57f1f37290d
pycompat: move multiline comments above a function to function doc
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32860
diff
changeset
|
252 |
Takes bytes argument, convert it to str i.e. unicodes, pass that into |
f57f1f37290d
pycompat: move multiline comments above a function to function doc
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32860
diff
changeset
|
253 |
shlex.split(), convert the returned value to bytes and return that for |
f57f1f37290d
pycompat: move multiline comments above a function to function doc
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32860
diff
changeset
|
254 |
Python 3 compatibility as shelx.split() don't accept bytes on Python 3. |
f57f1f37290d
pycompat: move multiline comments above a function to function doc
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32860
diff
changeset
|
255 |
""" |
30678
caf7e1c5efe4
py3: have a bytes version of shlex.split()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30668
diff
changeset
|
256 |
ret = shlex.split(s.decode('latin-1')) |
caf7e1c5efe4
py3: have a bytes version of shlex.split()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30668
diff
changeset
|
257 |
return [a.encode('latin-1') for a in ret] |
caf7e1c5efe4
py3: have a bytes version of shlex.split()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30668
diff
changeset
|
258 |
|
30032
2219f4f82ede
pycompat: extract function that converts attribute or encoding name to str
Yuya Nishihara <yuya@tcha.org>
parents:
30030
diff
changeset
|
259 |
else: |
31372
06440ba06bc0
pycompat: move imports of cStringIO/io to where they are used
Yuya Nishihara <yuya@tcha.org>
parents:
31359
diff
changeset
|
260 |
import cStringIO |
06440ba06bc0
pycompat: move imports of cStringIO/io to where they are used
Yuya Nishihara <yuya@tcha.org>
parents:
31359
diff
changeset
|
261 |
|
31253
64596338ba10
py3: factor out bytechr() function
Yuya Nishihara <yuya@tcha.org>
parents:
31149
diff
changeset
|
262 |
bytechr = chr |
31439
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31425
diff
changeset
|
263 |
bytestr = str |
31382
c9fd842dc886
pycompat: add helper to iterate each char in bytes
Yuya Nishihara <yuya@tcha.org>
parents:
31372
diff
changeset
|
264 |
iterbytestr = iter |
31820
45761ef1bc93
py3: have registrar process docstrings in bytes
Yuya Nishihara <yuya@tcha.org>
parents:
31775
diff
changeset
|
265 |
sysbytes = identity |
31774
7d2cbe11ae48
pycompat: introduce identity function as a compat stub
Yuya Nishihara <yuya@tcha.org>
parents:
31573
diff
changeset
|
266 |
sysstr = identity |
32859
a05f3675c46a
py3: add a new strurl() which will convert a bytes url to str
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32615
diff
changeset
|
267 |
strurl = identity |
32860
f22f39d56bb5
py3: add a new bytesurl() to convert a str url into bytes
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32859
diff
changeset
|
268 |
bytesurl = identity |
30032
2219f4f82ede
pycompat: extract function that converts attribute or encoding name to str
Yuya Nishihara <yuya@tcha.org>
parents:
30030
diff
changeset
|
269 |
|
32186
76f9a0009b4b
pycompat: extract helper to raise exception with traceback
Yuya Nishihara <yuya@tcha.org>
parents:
31942
diff
changeset
|
270 |
# this can't be parsed on Python 3 |
76f9a0009b4b
pycompat: extract helper to raise exception with traceback
Yuya Nishihara <yuya@tcha.org>
parents:
31942
diff
changeset
|
271 |
exec('def raisewithtb(exc, tb):\n' |
76f9a0009b4b
pycompat: extract helper to raise exception with traceback
Yuya Nishihara <yuya@tcha.org>
parents:
31942
diff
changeset
|
272 |
' raise exc, None, tb\n') |
76f9a0009b4b
pycompat: extract helper to raise exception with traceback
Yuya Nishihara <yuya@tcha.org>
parents:
31942
diff
changeset
|
273 |
|
30133
f6dcda7505f9
pycompat: only accept a bytestring filepath in Python 2
Martijn Pieters <mjpieters@fb.com>
parents:
30119
diff
changeset
|
274 |
def fsencode(filename): |
32864
f57f1f37290d
pycompat: move multiline comments above a function to function doc
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32860
diff
changeset
|
275 |
""" |
f57f1f37290d
pycompat: move multiline comments above a function to function doc
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32860
diff
changeset
|
276 |
Partial backport from os.py in Python 3, which only accepts bytes. |
f57f1f37290d
pycompat: move multiline comments above a function to function doc
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32860
diff
changeset
|
277 |
In Python 2, our paths should only ever be bytes, a unicode path |
f57f1f37290d
pycompat: move multiline comments above a function to function doc
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32860
diff
changeset
|
278 |
indicates a bug. |
f57f1f37290d
pycompat: move multiline comments above a function to function doc
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32860
diff
changeset
|
279 |
""" |
30133
f6dcda7505f9
pycompat: only accept a bytestring filepath in Python 2
Martijn Pieters <mjpieters@fb.com>
parents:
30119
diff
changeset
|
280 |
if isinstance(filename, str): |
f6dcda7505f9
pycompat: only accept a bytestring filepath in Python 2
Martijn Pieters <mjpieters@fb.com>
parents:
30119
diff
changeset
|
281 |
return filename |
30119
f4a5e0e86a7e
py3: add an os.fsencode backport to ease path handling
Martijn Pieters <mjpieters@fb.com>
parents:
30086
diff
changeset
|
282 |
else: |
30133
f6dcda7505f9
pycompat: only accept a bytestring filepath in Python 2
Martijn Pieters <mjpieters@fb.com>
parents:
30119
diff
changeset
|
283 |
raise TypeError( |
f6dcda7505f9
pycompat: only accept a bytestring filepath in Python 2
Martijn Pieters <mjpieters@fb.com>
parents:
30119
diff
changeset
|
284 |
"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
|
285 |
|
30300
42af0590f4b9
py3: add os.fsdecode() as pycompat.fsdecode()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30133
diff
changeset
|
286 |
# 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
|
287 |
# better not to touch Python 2 part as it's already working fine. |
31774
7d2cbe11ae48
pycompat: introduce identity function as a compat stub
Yuya Nishihara <yuya@tcha.org>
parents:
31573
diff
changeset
|
288 |
fsdecode = identity |
30300
42af0590f4b9
py3: add os.fsdecode() as pycompat.fsdecode()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30133
diff
changeset
|
289 |
|
32615
c9318beb7c1a
py3: convert __doc__ back to bytes in help.py
Yuya Nishihara <yuya@tcha.org>
parents:
32450
diff
changeset
|
290 |
def getdoc(obj): |
c9318beb7c1a
py3: convert __doc__ back to bytes in help.py
Yuya Nishihara <yuya@tcha.org>
parents:
32450
diff
changeset
|
291 |
return getattr(obj, '__doc__', None) |
c9318beb7c1a
py3: convert __doc__ back to bytes in help.py
Yuya Nishihara <yuya@tcha.org>
parents:
32450
diff
changeset
|
292 |
|
30578
c6ce11f2ee50
py3: make a bytes version of getopt.getopt()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30500
diff
changeset
|
293 |
def getoptb(args, shortlist, namelist): |
c6ce11f2ee50
py3: make a bytes version of getopt.getopt()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30500
diff
changeset
|
294 |
return getopt.getopt(args, shortlist, namelist) |
c6ce11f2ee50
py3: make a bytes version of getopt.getopt()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30500
diff
changeset
|
295 |
|
31774
7d2cbe11ae48
pycompat: introduce identity function as a compat stub
Yuya Nishihara <yuya@tcha.org>
parents:
31573
diff
changeset
|
296 |
strkwargs = identity |
7d2cbe11ae48
pycompat: introduce identity function as a compat stub
Yuya Nishihara <yuya@tcha.org>
parents:
31573
diff
changeset
|
297 |
byteskwargs = identity |
30579
fbc3f73dc802
py3: utility functions to convert keys of kwargs to bytes/unicodes
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30578
diff
changeset
|
298 |
|
31775
8181f378b073
pycompat: provide bytes os.linesep
Yuya Nishihara <yuya@tcha.org>
parents:
31774
diff
changeset
|
299 |
oslinesep = os.linesep |
30302
3874ddba1ab4
py3: add a bytes version of os.name
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30300
diff
changeset
|
300 |
osname = os.name |
30303
ad40d307a9f0
py3: have pycompat.ospathsep and pycompat.ossep
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30302
diff
changeset
|
301 |
ospathsep = os.pathsep |
ad40d307a9f0
py3: have pycompat.ospathsep and pycompat.ossep
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30302
diff
changeset
|
302 |
ossep = os.sep |
30623
c6026c20a3ce
py3: have a bytes version of os.altsep
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30579
diff
changeset
|
303 |
osaltsep = os.altsep |
30472
277f4fe6d01a
py3: provide bytes stdin/out/err through util module
Yuya Nishihara <yuya@tcha.org>
parents:
30334
diff
changeset
|
304 |
stdin = sys.stdin |
277f4fe6d01a
py3: provide bytes stdin/out/err through util module
Yuya Nishihara <yuya@tcha.org>
parents:
30334
diff
changeset
|
305 |
stdout = sys.stdout |
277f4fe6d01a
py3: provide bytes stdin/out/err through util module
Yuya Nishihara <yuya@tcha.org>
parents:
30334
diff
changeset
|
306 |
stderr = sys.stderr |
31277
86cd1f2cfff5
pycompat: verify sys.argv exists before forwarding it (issue5493)
Augie Fackler <augie@google.com>
parents:
30820
diff
changeset
|
307 |
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
|
308 |
sysargv = sys.argv |
30624
a82a6eee2613
py3: have a bytes version of sys.platform
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30623
diff
changeset
|
309 |
sysplatform = sys.platform |
30500
fc0cfe6c87d7
py3: add os.getcwdb() to have bytes path
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30472
diff
changeset
|
310 |
getcwd = os.getcwd |
30668
3fcaf0f660ce
py3: have bytes version of sys.executable
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30663
diff
changeset
|
311 |
sysexecutable = sys.executable |
30678
caf7e1c5efe4
py3: have a bytes version of shlex.split()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30668
diff
changeset
|
312 |
shlexsplit = shlex.split |
31372
06440ba06bc0
pycompat: move imports of cStringIO/io to where they are used
Yuya Nishihara <yuya@tcha.org>
parents:
31359
diff
changeset
|
313 |
stringio = cStringIO.StringIO |
31501
a1e40ceee640
pycompat: add maplist alias for old map behavior
Augie Fackler <augie@google.com>
parents:
31439
diff
changeset
|
314 |
maplist = map |
30302
3874ddba1ab4
py3: add a bytes version of os.name
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30300
diff
changeset
|
315 |
|
28882
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
316 |
class _pycompatstub(object): |
29801
c63ab0524db7
pycompat: delay loading modules registered to stub
Yuya Nishihara <yuya@tcha.org>
parents:
29800
diff
changeset
|
317 |
def __init__(self): |
c63ab0524db7
pycompat: delay loading modules registered to stub
Yuya Nishihara <yuya@tcha.org>
parents:
29800
diff
changeset
|
318 |
self._aliases = {} |
28882
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
319 |
|
29801
c63ab0524db7
pycompat: delay loading modules registered to stub
Yuya Nishihara <yuya@tcha.org>
parents:
29800
diff
changeset
|
320 |
def _registeraliases(self, origin, items): |
c63ab0524db7
pycompat: delay loading modules registered to stub
Yuya Nishihara <yuya@tcha.org>
parents:
29800
diff
changeset
|
321 |
"""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
|
322 |
items = map(sysstr, items) |
f3a1089654e3
pycompat: when setting attrs, ensure we use sysstr
Augie Fackler <augie@google.com>
parents:
30032
diff
changeset
|
323 |
self._aliases.update( |
f3a1089654e3
pycompat: when setting attrs, ensure we use sysstr
Augie Fackler <augie@google.com>
parents:
30032
diff
changeset
|
324 |
(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
|
325 |
for item in items) |
28882
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
326 |
|
31566
c6df6a23dfe5
pycompat: alias urlreq.unquote to unquote_to_bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
31501
diff
changeset
|
327 |
def _registeralias(self, origin, attr, name): |
c6df6a23dfe5
pycompat: alias urlreq.unquote to unquote_to_bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
31501
diff
changeset
|
328 |
"""Alias ``origin``.``attr`` as ``name``""" |
c6df6a23dfe5
pycompat: alias urlreq.unquote to unquote_to_bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
31501
diff
changeset
|
329 |
self._aliases[sysstr(name)] = (origin, sysstr(attr)) |
c6df6a23dfe5
pycompat: alias urlreq.unquote to unquote_to_bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
31501
diff
changeset
|
330 |
|
29801
c63ab0524db7
pycompat: delay loading modules registered to stub
Yuya Nishihara <yuya@tcha.org>
parents:
29800
diff
changeset
|
331 |
def __getattr__(self, name): |
28882
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
332 |
try: |
29801
c63ab0524db7
pycompat: delay loading modules registered to stub
Yuya Nishihara <yuya@tcha.org>
parents:
29800
diff
changeset
|
333 |
origin, item = self._aliases[name] |
c63ab0524db7
pycompat: delay loading modules registered to stub
Yuya Nishihara <yuya@tcha.org>
parents:
29800
diff
changeset
|
334 |
except KeyError: |
c63ab0524db7
pycompat: delay loading modules registered to stub
Yuya Nishihara <yuya@tcha.org>
parents:
29800
diff
changeset
|
335 |
raise AttributeError(name) |
c63ab0524db7
pycompat: delay loading modules registered to stub
Yuya Nishihara <yuya@tcha.org>
parents:
29800
diff
changeset
|
336 |
self.__dict__[name] = obj = getattr(origin, item) |
c63ab0524db7
pycompat: delay loading modules registered to stub
Yuya Nishihara <yuya@tcha.org>
parents:
29800
diff
changeset
|
337 |
return obj |
28882
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
338 |
|
29566
075146e85bb6
py3: conditionalize BaseHTTPServer, SimpleHTTPServer and CGIHTTPServer import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29455
diff
changeset
|
339 |
httpserver = _pycompatstub() |
28882
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
340 |
urlreq = _pycompatstub() |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
341 |
urlerr = _pycompatstub() |
30030
0f6d6fdd3c2a
pycompat: provide 'ispy3' constant
Yuya Nishihara <yuya@tcha.org>
parents:
29801
diff
changeset
|
342 |
if not ispy3: |
29566
075146e85bb6
py3: conditionalize BaseHTTPServer, SimpleHTTPServer and CGIHTTPServer import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29455
diff
changeset
|
343 |
import BaseHTTPServer |
075146e85bb6
py3: conditionalize BaseHTTPServer, SimpleHTTPServer and CGIHTTPServer import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29455
diff
changeset
|
344 |
import CGIHTTPServer |
075146e85bb6
py3: conditionalize BaseHTTPServer, SimpleHTTPServer and CGIHTTPServer import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29455
diff
changeset
|
345 |
import SimpleHTTPServer |
28882
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
346 |
import urllib2 |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
347 |
import urllib |
31569
e68932dfbb55
pycompat: define urlreq.urlparse and urlreq.unparse aliases
Gregory Szorc <gregory.szorc@gmail.com>
parents:
31568
diff
changeset
|
348 |
import urlparse |
29801
c63ab0524db7
pycompat: delay loading modules registered to stub
Yuya Nishihara <yuya@tcha.org>
parents:
29800
diff
changeset
|
349 |
urlreq._registeraliases(urllib, ( |
28882
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
350 |
"addclosehook", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
351 |
"addinfourl", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
352 |
"ftpwrapper", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
353 |
"pathname2url", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
354 |
"quote", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
355 |
"splitattr", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
356 |
"splitpasswd", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
357 |
"splitport", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
358 |
"splituser", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
359 |
"unquote", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
360 |
"url2pathname", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
361 |
"urlencode", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
362 |
)) |
29801
c63ab0524db7
pycompat: delay loading modules registered to stub
Yuya Nishihara <yuya@tcha.org>
parents:
29800
diff
changeset
|
363 |
urlreq._registeraliases(urllib2, ( |
28882
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
364 |
"AbstractHTTPHandler", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
365 |
"BaseHandler", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
366 |
"build_opener", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
367 |
"FileHandler", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
368 |
"FTPHandler", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
369 |
"HTTPBasicAuthHandler", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
370 |
"HTTPDigestAuthHandler", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
371 |
"HTTPHandler", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
372 |
"HTTPPasswordMgrWithDefaultRealm", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
373 |
"HTTPSHandler", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
374 |
"install_opener", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
375 |
"ProxyHandler", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
376 |
"Request", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
377 |
"urlopen", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
378 |
)) |
31569
e68932dfbb55
pycompat: define urlreq.urlparse and urlreq.unparse aliases
Gregory Szorc <gregory.szorc@gmail.com>
parents:
31568
diff
changeset
|
379 |
urlreq._registeraliases(urlparse, ( |
e68932dfbb55
pycompat: define urlreq.urlparse and urlreq.unparse aliases
Gregory Szorc <gregory.szorc@gmail.com>
parents:
31568
diff
changeset
|
380 |
"urlparse", |
e68932dfbb55
pycompat: define urlreq.urlparse and urlreq.unparse aliases
Gregory Szorc <gregory.szorc@gmail.com>
parents:
31568
diff
changeset
|
381 |
"urlunparse", |
e68932dfbb55
pycompat: define urlreq.urlparse and urlreq.unparse aliases
Gregory Szorc <gregory.szorc@gmail.com>
parents:
31568
diff
changeset
|
382 |
)) |
29801
c63ab0524db7
pycompat: delay loading modules registered to stub
Yuya Nishihara <yuya@tcha.org>
parents:
29800
diff
changeset
|
383 |
urlerr._registeraliases(urllib2, ( |
28882
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
384 |
"HTTPError", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
385 |
"URLError", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
386 |
)) |
29801
c63ab0524db7
pycompat: delay loading modules registered to stub
Yuya Nishihara <yuya@tcha.org>
parents:
29800
diff
changeset
|
387 |
httpserver._registeraliases(BaseHTTPServer, ( |
29566
075146e85bb6
py3: conditionalize BaseHTTPServer, SimpleHTTPServer and CGIHTTPServer import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29455
diff
changeset
|
388 |
"HTTPServer", |
075146e85bb6
py3: conditionalize BaseHTTPServer, SimpleHTTPServer and CGIHTTPServer import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29455
diff
changeset
|
389 |
"BaseHTTPRequestHandler", |
075146e85bb6
py3: conditionalize BaseHTTPServer, SimpleHTTPServer and CGIHTTPServer import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29455
diff
changeset
|
390 |
)) |
29801
c63ab0524db7
pycompat: delay loading modules registered to stub
Yuya Nishihara <yuya@tcha.org>
parents:
29800
diff
changeset
|
391 |
httpserver._registeraliases(SimpleHTTPServer, ( |
29566
075146e85bb6
py3: conditionalize BaseHTTPServer, SimpleHTTPServer and CGIHTTPServer import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29455
diff
changeset
|
392 |
"SimpleHTTPRequestHandler", |
075146e85bb6
py3: conditionalize BaseHTTPServer, SimpleHTTPServer and CGIHTTPServer import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29455
diff
changeset
|
393 |
)) |
29801
c63ab0524db7
pycompat: delay loading modules registered to stub
Yuya Nishihara <yuya@tcha.org>
parents:
29800
diff
changeset
|
394 |
httpserver._registeraliases(CGIHTTPServer, ( |
29566
075146e85bb6
py3: conditionalize BaseHTTPServer, SimpleHTTPServer and CGIHTTPServer import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29455
diff
changeset
|
395 |
"CGIHTTPRequestHandler", |
075146e85bb6
py3: conditionalize BaseHTTPServer, SimpleHTTPServer and CGIHTTPServer import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29455
diff
changeset
|
396 |
)) |
28882
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
397 |
|
29801
c63ab0524db7
pycompat: delay loading modules registered to stub
Yuya Nishihara <yuya@tcha.org>
parents:
29800
diff
changeset
|
398 |
else: |
31399
1ed169c5e235
pycompat: alias urllib symbols directly
Gregory Szorc <gregory.szorc@gmail.com>
parents:
31382
diff
changeset
|
399 |
import urllib.parse |
1ed169c5e235
pycompat: alias urllib symbols directly
Gregory Szorc <gregory.szorc@gmail.com>
parents:
31382
diff
changeset
|
400 |
urlreq._registeraliases(urllib.parse, ( |
1ed169c5e235
pycompat: alias urllib symbols directly
Gregory Szorc <gregory.szorc@gmail.com>
parents:
31382
diff
changeset
|
401 |
"splitattr", |
1ed169c5e235
pycompat: alias urllib symbols directly
Gregory Szorc <gregory.szorc@gmail.com>
parents:
31382
diff
changeset
|
402 |
"splitpasswd", |
1ed169c5e235
pycompat: alias urllib symbols directly
Gregory Szorc <gregory.szorc@gmail.com>
parents:
31382
diff
changeset
|
403 |
"splitport", |
1ed169c5e235
pycompat: alias urllib symbols directly
Gregory Szorc <gregory.szorc@gmail.com>
parents:
31382
diff
changeset
|
404 |
"splituser", |
31569
e68932dfbb55
pycompat: define urlreq.urlparse and urlreq.unparse aliases
Gregory Szorc <gregory.szorc@gmail.com>
parents:
31568
diff
changeset
|
405 |
"urlparse", |
e68932dfbb55
pycompat: define urlreq.urlparse and urlreq.unparse aliases
Gregory Szorc <gregory.szorc@gmail.com>
parents:
31568
diff
changeset
|
406 |
"urlunparse", |
31399
1ed169c5e235
pycompat: alias urllib symbols directly
Gregory Szorc <gregory.szorc@gmail.com>
parents:
31382
diff
changeset
|
407 |
)) |
31566
c6df6a23dfe5
pycompat: alias urlreq.unquote to unquote_to_bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
31501
diff
changeset
|
408 |
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
|
409 |
import urllib.request |
29801
c63ab0524db7
pycompat: delay loading modules registered to stub
Yuya Nishihara <yuya@tcha.org>
parents:
29800
diff
changeset
|
410 |
urlreq._registeraliases(urllib.request, ( |
28882
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
411 |
"AbstractHTTPHandler", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
412 |
"BaseHandler", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
413 |
"build_opener", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
414 |
"FileHandler", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
415 |
"FTPHandler", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
416 |
"ftpwrapper", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
417 |
"HTTPHandler", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
418 |
"HTTPSHandler", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
419 |
"install_opener", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
420 |
"pathname2url", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
421 |
"HTTPBasicAuthHandler", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
422 |
"HTTPDigestAuthHandler", |
29414
2646fbba4ca7
pycompat: add HTTPPasswordMgrWithDefaultRealm to Python 3 block
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29405
diff
changeset
|
423 |
"HTTPPasswordMgrWithDefaultRealm", |
28882
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
424 |
"ProxyHandler", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
425 |
"Request", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
426 |
"url2pathname", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
427 |
"urlopen", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
428 |
)) |
31399
1ed169c5e235
pycompat: alias urllib symbols directly
Gregory Szorc <gregory.szorc@gmail.com>
parents:
31382
diff
changeset
|
429 |
import urllib.response |
1ed169c5e235
pycompat: alias urllib symbols directly
Gregory Szorc <gregory.szorc@gmail.com>
parents:
31382
diff
changeset
|
430 |
urlreq._registeraliases(urllib.response, ( |
1ed169c5e235
pycompat: alias urllib symbols directly
Gregory Szorc <gregory.szorc@gmail.com>
parents:
31382
diff
changeset
|
431 |
"addclosehook", |
1ed169c5e235
pycompat: alias urllib symbols directly
Gregory Szorc <gregory.szorc@gmail.com>
parents:
31382
diff
changeset
|
432 |
"addinfourl", |
1ed169c5e235
pycompat: alias urllib symbols directly
Gregory Szorc <gregory.szorc@gmail.com>
parents:
31382
diff
changeset
|
433 |
)) |
28882
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
434 |
import urllib.error |
29801
c63ab0524db7
pycompat: delay loading modules registered to stub
Yuya Nishihara <yuya@tcha.org>
parents:
29800
diff
changeset
|
435 |
urlerr._registeraliases(urllib.error, ( |
28882
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
436 |
"HTTPError", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
437 |
"URLError", |
800ec7c048b0
pycompat: add util.urlerr util.urlreq classes for py3 compat
timeless <timeless@mozdev.org>
parents:
28835
diff
changeset
|
438 |
)) |
29566
075146e85bb6
py3: conditionalize BaseHTTPServer, SimpleHTTPServer and CGIHTTPServer import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29455
diff
changeset
|
439 |
import http.server |
29801
c63ab0524db7
pycompat: delay loading modules registered to stub
Yuya Nishihara <yuya@tcha.org>
parents:
29800
diff
changeset
|
440 |
httpserver._registeraliases(http.server, ( |
29566
075146e85bb6
py3: conditionalize BaseHTTPServer, SimpleHTTPServer and CGIHTTPServer import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29455
diff
changeset
|
441 |
"HTTPServer", |
075146e85bb6
py3: conditionalize BaseHTTPServer, SimpleHTTPServer and CGIHTTPServer import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29455
diff
changeset
|
442 |
"BaseHTTPRequestHandler", |
075146e85bb6
py3: conditionalize BaseHTTPServer, SimpleHTTPServer and CGIHTTPServer import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29455
diff
changeset
|
443 |
"SimpleHTTPRequestHandler", |
075146e85bb6
py3: conditionalize BaseHTTPServer, SimpleHTTPServer and CGIHTTPServer import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29455
diff
changeset
|
444 |
"CGIHTTPRequestHandler", |
075146e85bb6
py3: conditionalize BaseHTTPServer, SimpleHTTPServer and CGIHTTPServer import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29455
diff
changeset
|
445 |
)) |
31400
fb1f70331ee6
pycompat: custom implementation of urllib.parse.quote()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
31399
diff
changeset
|
446 |
|
fb1f70331ee6
pycompat: custom implementation of urllib.parse.quote()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
31399
diff
changeset
|
447 |
# 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:
31399
diff
changeset
|
448 |
# (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:
31399
diff
changeset
|
449 |
# implementation that only accepts bytes and emits bytes. |
fb1f70331ee6
pycompat: custom implementation of urllib.parse.quote()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
31399
diff
changeset
|
450 |
def quote(s, safe=r'/'): |
fb1f70331ee6
pycompat: custom implementation of urllib.parse.quote()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
31399
diff
changeset
|
451 |
s = urllib.parse.quote_from_bytes(s, safe=safe) |
fb1f70331ee6
pycompat: custom implementation of urllib.parse.quote()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
31399
diff
changeset
|
452 |
return s.encode('ascii', 'strict') |
fb1f70331ee6
pycompat: custom implementation of urllib.parse.quote()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
31399
diff
changeset
|
453 |
|
31842
c130d092042a
py3: add a bytes version of urllib.parse.urlencode() to pycompat.py
Pulkit Goyal <7895pulkit@gmail.com>
parents:
31820
diff
changeset
|
454 |
# urllib.parse.urlencode() returns str. We use this function to make |
c130d092042a
py3: add a bytes version of urllib.parse.urlencode() to pycompat.py
Pulkit Goyal <7895pulkit@gmail.com>
parents:
31820
diff
changeset
|
455 |
# sure we return bytes. |
c130d092042a
py3: add a bytes version of urllib.parse.urlencode() to pycompat.py
Pulkit Goyal <7895pulkit@gmail.com>
parents:
31820
diff
changeset
|
456 |
def urlencode(query, doseq=False): |
c130d092042a
py3: add a bytes version of urllib.parse.urlencode() to pycompat.py
Pulkit Goyal <7895pulkit@gmail.com>
parents:
31820
diff
changeset
|
457 |
s = urllib.parse.urlencode(query, doseq=doseq) |
c130d092042a
py3: add a bytes version of urllib.parse.urlencode() to pycompat.py
Pulkit Goyal <7895pulkit@gmail.com>
parents:
31820
diff
changeset
|
458 |
return s.encode('ascii') |
c130d092042a
py3: add a bytes version of urllib.parse.urlencode() to pycompat.py
Pulkit Goyal <7895pulkit@gmail.com>
parents:
31820
diff
changeset
|
459 |
|
31400
fb1f70331ee6
pycompat: custom implementation of urllib.parse.quote()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
31399
diff
changeset
|
460 |
urlreq.quote = quote |
31842
c130d092042a
py3: add a bytes version of urllib.parse.urlencode() to pycompat.py
Pulkit Goyal <7895pulkit@gmail.com>
parents:
31820
diff
changeset
|
461 |
urlreq.urlencode = urlencode |