Mercurial > hg > mercurial-source
annotate mercurial/pycompat.py @ 43892:649d3ac37a12
py3: define and use pycompat.iteritems() for hgext/
.iteritems() -> .items() is the last source transform being performed.
But it is also the most widely used.
This commit adds a pycompat.iteritems symbol and imports it in place
of .iteritems() for usage in hgext/. I chose to stop at just hgext/
because the patch will be large and it is an easy boundary to stop at
since we can disable source transformation on a per-package basis.
There are places where the type does implement items() and we could
call items() directly. However, this would require critical thought
and I thought it would be easier to just blindly change the code. We
know which call sites need to be audited in the future because they
have "pycompat.iteritems."
With this change, we no longer perform source transformation on
hgext!
Differential Revision: https://phab.mercurial-scm.org/D7014
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Sun, 06 Oct 2019 19:25:18 -0400 |
parents | 74802979dd9d |
children | d783f945a701 |
rev | line source |
---|---|
29568
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 |
31357
c6ce11f2ee50
py3: make a bytes version of getopt.getopt()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
31279
diff
changeset
|
13 import getopt |
36964
646002338365
py3: introduce and use pycompat.getargspec
Augie Fackler <augie@google.com>
parents:
36831
diff
changeset
|
14 import inspect |
31081
3874ddba1ab4
py3: add a bytes version of os.name
Pulkit Goyal <7895pulkit@gmail.com>
parents:
31079
diff
changeset
|
15 import os |
31457
caf7e1c5efe4
py3: have a bytes version of shlex.split()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
31447
diff
changeset
|
16 import shlex |
30345
06587edd1233
pycompat: make pycompat demandimport friendly
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30327
diff
changeset
|
17 import sys |
38950
aac4be30e250
py3: wrap tempfile.mkstemp() to use bytes path
Yuya Nishihara <yuya@tcha.org>
parents:
38630
diff
changeset
|
18 import tempfile |
30345
06587edd1233
pycompat: make pycompat demandimport friendly
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30327
diff
changeset
|
19 |
43863
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43806
diff
changeset
|
20 ispy3 = sys.version_info[0] >= 3 |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43806
diff
changeset
|
21 ispypy = r'__pypy__' in sys.builtin_module_names |
30810
0f6d6fdd3c2a
pycompat: provide 'ispy3' constant
Yuya Nishihara <yuya@tcha.org>
parents:
30571
diff
changeset
|
22 |
0f6d6fdd3c2a
pycompat: provide 'ispy3' constant
Yuya Nishihara <yuya@tcha.org>
parents:
30571
diff
changeset
|
23 if not ispy3: |
32713
12aca6770046
util: make cookielib module available
Gregory Szorc <gregory.szorc@gmail.com>
parents:
32622
diff
changeset
|
24 import cookielib |
30074
b501579147f1
py3: conditionalize cPickle import by adding in util
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29632
diff
changeset
|
25 import cPickle as pickle |
30216
0c741fd6158a
py3: conditionalize httplib import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30194
diff
changeset
|
26 import httplib |
38630
8fb9985382be
pycompat: export queue module instead of symbols in module (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
38455
diff
changeset
|
27 import Queue as queue |
30194
33770d2b6cf9
py3: conditionalize SocketServer import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30193
diff
changeset
|
28 import SocketServer as socketserver |
30193
34b914ac573e
py3: conditionalize xmlrpclib import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30192
diff
changeset
|
29 import xmlrpclib |
38414
8da30ceae88f
pycompat: export a handle on concurrent.futures
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37885
diff
changeset
|
30 |
8da30ceae88f
pycompat: export a handle on concurrent.futures
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37885
diff
changeset
|
31 from .thirdparty.concurrent import futures |
38455
1cb54e6193a6
py3: paper over differences in future exception handling
Augie Fackler <augie@google.com>
parents:
38414
diff
changeset
|
32 |
1cb54e6193a6
py3: paper over differences in future exception handling
Augie Fackler <augie@google.com>
parents:
38414
diff
changeset
|
33 def future_set_exception_info(f, exc_info): |
1cb54e6193a6
py3: paper over differences in future exception handling
Augie Fackler <augie@google.com>
parents:
38414
diff
changeset
|
34 f.set_exception_info(*exc_info) |
43863
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43806
diff
changeset
|
35 |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43806
diff
changeset
|
36 |
30345
06587edd1233
pycompat: make pycompat demandimport friendly
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30327
diff
changeset
|
37 else: |
38414
8da30ceae88f
pycompat: export a handle on concurrent.futures
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37885
diff
changeset
|
38 import concurrent.futures as futures |
32721
bc0579a25f82
pycompat: import correct cookie module on Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
32713
diff
changeset
|
39 import http.cookiejar as cookielib |
30345
06587edd1233
pycompat: make pycompat demandimport friendly
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30327
diff
changeset
|
40 import http.client as httplib |
06587edd1233
pycompat: make pycompat demandimport friendly
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30327
diff
changeset
|
41 import pickle |
38630
8fb9985382be
pycompat: export queue module instead of symbols in module (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
38455
diff
changeset
|
42 import queue as queue |
30345
06587edd1233
pycompat: make pycompat demandimport friendly
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30327
diff
changeset
|
43 import socketserver |
30193
34b914ac573e
py3: conditionalize xmlrpclib import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30192
diff
changeset
|
44 import xmlrpc.client as xmlrpclib |
30192
80880ad3fccd
py3: conditionalize the urlparse import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30175
diff
changeset
|
45 |
38455
1cb54e6193a6
py3: paper over differences in future exception handling
Augie Fackler <augie@google.com>
parents:
38414
diff
changeset
|
46 def future_set_exception_info(f, exc_info): |
1cb54e6193a6
py3: paper over differences in future exception handling
Augie Fackler <augie@google.com>
parents:
38414
diff
changeset
|
47 f.set_exception(exc_info[0]) |
1cb54e6193a6
py3: paper over differences in future exception handling
Augie Fackler <augie@google.com>
parents:
38414
diff
changeset
|
48 |
43863
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43806
diff
changeset
|
49 |
32553
7d2cbe11ae48
pycompat: introduce identity function as a compat stub
Yuya Nishihara <yuya@tcha.org>
parents:
32352
diff
changeset
|
50 def identity(a): |
7d2cbe11ae48
pycompat: introduce identity function as a compat stub
Yuya Nishihara <yuya@tcha.org>
parents:
32352
diff
changeset
|
51 return a |
7d2cbe11ae48
pycompat: introduce identity function as a compat stub
Yuya Nishihara <yuya@tcha.org>
parents:
32352
diff
changeset
|
52 |
43863
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43806
diff
changeset
|
53 |
39361
152f4822d210
pycompat: move rapply() from util
Yuya Nishihara <yuya@tcha.org>
parents:
39118
diff
changeset
|
54 def _rapply(f, xs): |
152f4822d210
pycompat: move rapply() from util
Yuya Nishihara <yuya@tcha.org>
parents:
39118
diff
changeset
|
55 if xs is None: |
152f4822d210
pycompat: move rapply() from util
Yuya Nishihara <yuya@tcha.org>
parents:
39118
diff
changeset
|
56 # assume None means non-value of optional data |
152f4822d210
pycompat: move rapply() from util
Yuya Nishihara <yuya@tcha.org>
parents:
39118
diff
changeset
|
57 return xs |
152f4822d210
pycompat: move rapply() from util
Yuya Nishihara <yuya@tcha.org>
parents:
39118
diff
changeset
|
58 if isinstance(xs, (list, set, tuple)): |
152f4822d210
pycompat: move rapply() from util
Yuya Nishihara <yuya@tcha.org>
parents:
39118
diff
changeset
|
59 return type(xs)(_rapply(f, x) for x in xs) |
152f4822d210
pycompat: move rapply() from util
Yuya Nishihara <yuya@tcha.org>
parents:
39118
diff
changeset
|
60 if isinstance(xs, dict): |
152f4822d210
pycompat: move rapply() from util
Yuya Nishihara <yuya@tcha.org>
parents:
39118
diff
changeset
|
61 return type(xs)((_rapply(f, k), _rapply(f, v)) for k, v in xs.items()) |
152f4822d210
pycompat: move rapply() from util
Yuya Nishihara <yuya@tcha.org>
parents:
39118
diff
changeset
|
62 return f(xs) |
152f4822d210
pycompat: move rapply() from util
Yuya Nishihara <yuya@tcha.org>
parents:
39118
diff
changeset
|
63 |
43863
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43806
diff
changeset
|
64 |
39361
152f4822d210
pycompat: move rapply() from util
Yuya Nishihara <yuya@tcha.org>
parents:
39118
diff
changeset
|
65 def rapply(f, xs): |
152f4822d210
pycompat: move rapply() from util
Yuya Nishihara <yuya@tcha.org>
parents:
39118
diff
changeset
|
66 """Apply function recursively to every item preserving the data structure |
152f4822d210
pycompat: move rapply() from util
Yuya Nishihara <yuya@tcha.org>
parents:
39118
diff
changeset
|
67 |
152f4822d210
pycompat: move rapply() from util
Yuya Nishihara <yuya@tcha.org>
parents:
39118
diff
changeset
|
68 >>> def f(x): |
152f4822d210
pycompat: move rapply() from util
Yuya Nishihara <yuya@tcha.org>
parents:
39118
diff
changeset
|
69 ... return 'f(%s)' % x |
152f4822d210
pycompat: move rapply() from util
Yuya Nishihara <yuya@tcha.org>
parents:
39118
diff
changeset
|
70 >>> rapply(f, None) is None |
152f4822d210
pycompat: move rapply() from util
Yuya Nishihara <yuya@tcha.org>
parents:
39118
diff
changeset
|
71 True |
152f4822d210
pycompat: move rapply() from util
Yuya Nishihara <yuya@tcha.org>
parents:
39118
diff
changeset
|
72 >>> rapply(f, 'a') |
152f4822d210
pycompat: move rapply() from util
Yuya Nishihara <yuya@tcha.org>
parents:
39118
diff
changeset
|
73 'f(a)' |
152f4822d210
pycompat: move rapply() from util
Yuya Nishihara <yuya@tcha.org>
parents:
39118
diff
changeset
|
74 >>> rapply(f, {'a'}) == {'f(a)'} |
152f4822d210
pycompat: move rapply() from util
Yuya Nishihara <yuya@tcha.org>
parents:
39118
diff
changeset
|
75 True |
152f4822d210
pycompat: move rapply() from util
Yuya Nishihara <yuya@tcha.org>
parents:
39118
diff
changeset
|
76 >>> rapply(f, ['a', 'b', None, {'c': 'd'}, []]) |
152f4822d210
pycompat: move rapply() from util
Yuya Nishihara <yuya@tcha.org>
parents:
39118
diff
changeset
|
77 ['f(a)', 'f(b)', None, {'f(c)': 'f(d)'}, []] |
152f4822d210
pycompat: move rapply() from util
Yuya Nishihara <yuya@tcha.org>
parents:
39118
diff
changeset
|
78 |
152f4822d210
pycompat: move rapply() from util
Yuya Nishihara <yuya@tcha.org>
parents:
39118
diff
changeset
|
79 >>> xs = [object()] |
152f4822d210
pycompat: move rapply() from util
Yuya Nishihara <yuya@tcha.org>
parents:
39118
diff
changeset
|
80 >>> rapply(identity, xs) is xs |
152f4822d210
pycompat: move rapply() from util
Yuya Nishihara <yuya@tcha.org>
parents:
39118
diff
changeset
|
81 True |
152f4822d210
pycompat: move rapply() from util
Yuya Nishihara <yuya@tcha.org>
parents:
39118
diff
changeset
|
82 """ |
152f4822d210
pycompat: move rapply() from util
Yuya Nishihara <yuya@tcha.org>
parents:
39118
diff
changeset
|
83 if f is identity: |
152f4822d210
pycompat: move rapply() from util
Yuya Nishihara <yuya@tcha.org>
parents:
39118
diff
changeset
|
84 # fast path mainly for py2 |
152f4822d210
pycompat: move rapply() from util
Yuya Nishihara <yuya@tcha.org>
parents:
39118
diff
changeset
|
85 return xs |
152f4822d210
pycompat: move rapply() from util
Yuya Nishihara <yuya@tcha.org>
parents:
39118
diff
changeset
|
86 return _rapply(f, xs) |
152f4822d210
pycompat: move rapply() from util
Yuya Nishihara <yuya@tcha.org>
parents:
39118
diff
changeset
|
87 |
43863
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43806
diff
changeset
|
88 |
30810
0f6d6fdd3c2a
pycompat: provide 'ispy3' constant
Yuya Nishihara <yuya@tcha.org>
parents:
30571
diff
changeset
|
89 if ispy3: |
30567
965c91bad9e3
py3: move xrange alias next to import lines
Yuya Nishihara <yuya@tcha.org>
parents:
30540
diff
changeset
|
90 import builtins |
30569
45fa8de47a0f
py3: provide (del|get|has|set)attr wrappers that accepts bytes
Yuya Nishihara <yuya@tcha.org>
parents:
30568
diff
changeset
|
91 import functools |
32151
06440ba06bc0
pycompat: move imports of cStringIO/io to where they are used
Yuya Nishihara <yuya@tcha.org>
parents:
32138
diff
changeset
|
92 import io |
32203
4acc49335a6e
py3: optimize py3 compat.bytechr using Struct.pack
Martin von Zweigbergk <martinvonz@google.com>
parents:
32179
diff
changeset
|
93 import struct |
32151
06440ba06bc0
pycompat: move imports of cStringIO/io to where they are used
Yuya Nishihara <yuya@tcha.org>
parents:
32138
diff
changeset
|
94 |
30898
f4a5e0e86a7e
py3: add an os.fsencode backport to ease path handling
Martijn Pieters <mjpieters@fb.com>
parents:
30865
diff
changeset
|
95 fsencode = os.fsencode |
31079
42af0590f4b9
py3: add os.fsdecode() as pycompat.fsdecode()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30912
diff
changeset
|
96 fsdecode = os.fsdecode |
37434
6585ac350fd9
py3: make os.curdir a bytes
Yuya Nishihara <yuya@tcha.org>
parents:
37433
diff
changeset
|
97 oscurdir = os.curdir.encode('ascii') |
32554
8181f378b073
pycompat: provide bytes os.linesep
Yuya Nishihara <yuya@tcha.org>
parents:
32553
diff
changeset
|
98 oslinesep = os.linesep.encode('ascii') |
31081
3874ddba1ab4
py3: add a bytes version of os.name
Pulkit Goyal <7895pulkit@gmail.com>
parents:
31079
diff
changeset
|
99 osname = os.name.encode('ascii') |
31082
ad40d307a9f0
py3: have pycompat.ospathsep and pycompat.ossep
Pulkit Goyal <7895pulkit@gmail.com>
parents:
31081
diff
changeset
|
100 ospathsep = os.pathsep.encode('ascii') |
37433
052351e3e1cd
py3: make os.pardir a bytes
Yuya Nishihara <yuya@tcha.org>
parents:
37430
diff
changeset
|
101 ospardir = os.pardir.encode('ascii') |
31082
ad40d307a9f0
py3: have pycompat.ospathsep and pycompat.ossep
Pulkit Goyal <7895pulkit@gmail.com>
parents:
31081
diff
changeset
|
102 ossep = os.sep.encode('ascii') |
31402
c6026c20a3ce
py3: have a bytes version of os.altsep
Pulkit Goyal <7895pulkit@gmail.com>
parents:
31358
diff
changeset
|
103 osaltsep = os.altsep |
c6026c20a3ce
py3: have a bytes version of os.altsep
Pulkit Goyal <7895pulkit@gmail.com>
parents:
31358
diff
changeset
|
104 if osaltsep: |
c6026c20a3ce
py3: have a bytes version of os.altsep
Pulkit Goyal <7895pulkit@gmail.com>
parents:
31358
diff
changeset
|
105 osaltsep = osaltsep.encode('ascii') |
40604
24e493ec2229
py3: rename pycompat.getcwd() to encoding.getcwd() (API)
Matt Harbison <matt_harbison@yahoo.com>
parents:
40428
diff
changeset
|
106 |
31403
a82a6eee2613
py3: have a bytes version of sys.platform
Pulkit Goyal <7895pulkit@gmail.com>
parents:
31402
diff
changeset
|
107 sysplatform = sys.platform.encode('ascii') |
31447
3fcaf0f660ce
py3: have bytes version of sys.executable
Pulkit Goyal <7895pulkit@gmail.com>
parents:
31442
diff
changeset
|
108 sysexecutable = sys.executable |
3fcaf0f660ce
py3: have bytes version of sys.executable
Pulkit Goyal <7895pulkit@gmail.com>
parents:
31442
diff
changeset
|
109 if sysexecutable: |
3fcaf0f660ce
py3: have bytes version of sys.executable
Pulkit Goyal <7895pulkit@gmail.com>
parents:
31442
diff
changeset
|
110 sysexecutable = os.fsencode(sysexecutable) |
37744
644a02f6b34f
util: prefer "bytesio" to "stringio"
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37720
diff
changeset
|
111 bytesio = io.BytesIO |
644a02f6b34f
util: prefer "bytesio" to "stringio"
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37720
diff
changeset
|
112 # TODO deprecate stringio name, as it is a lie on Python 3. |
644a02f6b34f
util: prefer "bytesio" to "stringio"
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37720
diff
changeset
|
113 stringio = bytesio |
37720
dbae581010ea
pycompat: name maplist() and ziplist() for better traceback message
Yuya Nishihara <yuya@tcha.org>
parents:
37434
diff
changeset
|
114 |
dbae581010ea
pycompat: name maplist() and ziplist() for better traceback message
Yuya Nishihara <yuya@tcha.org>
parents:
37434
diff
changeset
|
115 def maplist(*args): |
dbae581010ea
pycompat: name maplist() and ziplist() for better traceback message
Yuya Nishihara <yuya@tcha.org>
parents:
37434
diff
changeset
|
116 return list(map(*args)) |
dbae581010ea
pycompat: name maplist() and ziplist() for better traceback message
Yuya Nishihara <yuya@tcha.org>
parents:
37434
diff
changeset
|
117 |
37850
434e520adb8c
annotate: do not construct attr.s object per line while computing history
Yuya Nishihara <yuya@tcha.org>
parents:
37744
diff
changeset
|
118 def rangelist(*args): |
434e520adb8c
annotate: do not construct attr.s object per line while computing history
Yuya Nishihara <yuya@tcha.org>
parents:
37744
diff
changeset
|
119 return list(range(*args)) |
434e520adb8c
annotate: do not construct attr.s object per line while computing history
Yuya Nishihara <yuya@tcha.org>
parents:
37744
diff
changeset
|
120 |
37720
dbae581010ea
pycompat: name maplist() and ziplist() for better traceback message
Yuya Nishihara <yuya@tcha.org>
parents:
37434
diff
changeset
|
121 def ziplist(*args): |
dbae581010ea
pycompat: name maplist() and ziplist() for better traceback message
Yuya Nishihara <yuya@tcha.org>
parents:
37434
diff
changeset
|
122 return list(zip(*args)) |
dbae581010ea
pycompat: name maplist() and ziplist() for better traceback message
Yuya Nishihara <yuya@tcha.org>
parents:
37434
diff
changeset
|
123 |
34636
cfcfbe6c96f8
py3: select input or raw_input by pycompat
Yuya Nishihara <yuya@tcha.org>
parents:
34409
diff
changeset
|
124 rawinput = input |
36964
646002338365
py3: introduce and use pycompat.getargspec
Augie Fackler <augie@google.com>
parents:
36831
diff
changeset
|
125 getargspec = inspect.getfullargspec |
31113
19d8e19fde5b
py3: document why os.fsencode() can be used to get back bytes argv
Yuya Nishihara <yuya@tcha.org>
parents:
31109
diff
changeset
|
126 |
40242
8d858fbf2759
cbor: teach the encoder to handle python `long` type for Windows
Matt Harbison <matt_harbison@yahoo.com>
parents:
40088
diff
changeset
|
127 long = int |
8d858fbf2759
cbor: teach the encoder to handle python `long` type for Windows
Matt Harbison <matt_harbison@yahoo.com>
parents:
40088
diff
changeset
|
128 |
31251
277f4fe6d01a
py3: provide bytes stdin/out/err through util module
Yuya Nishihara <yuya@tcha.org>
parents:
31113
diff
changeset
|
129 # 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:
31113
diff
changeset
|
130 # 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:
31113
diff
changeset
|
131 stdin = sys.stdin.buffer |
277f4fe6d01a
py3: provide bytes stdin/out/err through util module
Yuya Nishihara <yuya@tcha.org>
parents:
31113
diff
changeset
|
132 stdout = sys.stdout.buffer |
277f4fe6d01a
py3: provide bytes stdin/out/err through util module
Yuya Nishihara <yuya@tcha.org>
parents:
31113
diff
changeset
|
133 stderr = sys.stderr.buffer |
277f4fe6d01a
py3: provide bytes stdin/out/err through util module
Yuya Nishihara <yuya@tcha.org>
parents:
31113
diff
changeset
|
134 |
31113
19d8e19fde5b
py3: document why os.fsencode() can be used to get back bytes argv
Yuya Nishihara <yuya@tcha.org>
parents:
31109
diff
changeset
|
135 # 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:
31109
diff
changeset
|
136 # 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:
31109
diff
changeset
|
137 # |
19d8e19fde5b
py3: document why os.fsencode() can be used to get back bytes argv
Yuya Nishihara <yuya@tcha.org>
parents:
31109
diff
changeset
|
138 # 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:
31109
diff
changeset
|
139 # |
19d8e19fde5b
py3: document why os.fsencode() can be used to get back bytes argv
Yuya Nishihara <yuya@tcha.org>
parents:
31109
diff
changeset
|
140 # 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:
31109
diff
changeset
|
141 # workaround to simulate the Python 2 (i.e. ANSI Win32 API) behavior. |
32056
86cd1f2cfff5
pycompat: verify sys.argv exists before forwarding it (issue5493)
Augie Fackler <augie@google.com>
parents:
31599
diff
changeset
|
142 if getattr(sys, 'argv', None) is not None: |
86cd1f2cfff5
pycompat: verify sys.argv exists before forwarding it (issue5493)
Augie Fackler <augie@google.com>
parents:
31599
diff
changeset
|
143 sysargv = list(map(os.fsencode, sys.argv)) |
30567
965c91bad9e3
py3: move xrange alias next to import lines
Yuya Nishihara <yuya@tcha.org>
parents:
30540
diff
changeset
|
144 |
40428
a407f9009392
py3: byteify strings in pycompat
Matt Harbison <matt_harbison@yahoo.com>
parents:
40242
diff
changeset
|
145 bytechr = struct.Struct(r'>B').pack |
37051
b44fac3a49fb
py3: factor out byterepr() which returns an asciified value on py3
Yuya Nishihara <yuya@tcha.org>
parents:
36964
diff
changeset
|
146 byterepr = b'%r'.__mod__ |
32032
64596338ba10
py3: factor out bytechr() function
Yuya Nishihara <yuya@tcha.org>
parents:
31928
diff
changeset
|
147 |
32218
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
32204
diff
changeset
|
148 class bytestr(bytes): |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
32204
diff
changeset
|
149 """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:
32204
diff
changeset
|
150 |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
32204
diff
changeset
|
151 >>> bytestr(), bytestr(bytearray(b'foo')), bytestr(u'ascii'), bytestr(1) |
36689
1a31111e6239
py3: always drop b'' prefix from repr() of bytestr
Yuya Nishihara <yuya@tcha.org>
parents:
36188
diff
changeset
|
152 ('', 'foo', 'ascii', '1') |
32218
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
32204
diff
changeset
|
153 >>> s = bytestr(b'foo') |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
32204
diff
changeset
|
154 >>> assert s is bytestr(s) |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
32204
diff
changeset
|
155 |
33233
548478efc46c
pycompat: try __bytes__() to convert object to bytestr
Yuya Nishihara <yuya@tcha.org>
parents:
32969
diff
changeset
|
156 __bytes__() should be called if provided: |
548478efc46c
pycompat: try __bytes__() to convert object to bytestr
Yuya Nishihara <yuya@tcha.org>
parents:
32969
diff
changeset
|
157 |
548478efc46c
pycompat: try __bytes__() to convert object to bytestr
Yuya Nishihara <yuya@tcha.org>
parents:
32969
diff
changeset
|
158 >>> class bytesable(object): |
548478efc46c
pycompat: try __bytes__() to convert object to bytestr
Yuya Nishihara <yuya@tcha.org>
parents:
32969
diff
changeset
|
159 ... def __bytes__(self): |
548478efc46c
pycompat: try __bytes__() to convert object to bytestr
Yuya Nishihara <yuya@tcha.org>
parents:
32969
diff
changeset
|
160 ... return b'bytes' |
548478efc46c
pycompat: try __bytes__() to convert object to bytestr
Yuya Nishihara <yuya@tcha.org>
parents:
32969
diff
changeset
|
161 >>> bytestr(bytesable()) |
36689
1a31111e6239
py3: always drop b'' prefix from repr() of bytestr
Yuya Nishihara <yuya@tcha.org>
parents:
36188
diff
changeset
|
162 'bytes' |
33233
548478efc46c
pycompat: try __bytes__() to convert object to bytestr
Yuya Nishihara <yuya@tcha.org>
parents:
32969
diff
changeset
|
163 |
32218
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
32204
diff
changeset
|
164 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:
32204
diff
changeset
|
165 unknown: |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
32204
diff
changeset
|
166 |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
32204
diff
changeset
|
167 >>> bytestr(chr(0x80)) # doctest: +ELLIPSIS |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
32204
diff
changeset
|
168 Traceback (most recent call last): |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
32204
diff
changeset
|
169 ... |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
32204
diff
changeset
|
170 UnicodeEncodeError: ... |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
32204
diff
changeset
|
171 |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
32204
diff
changeset
|
172 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:
32204
diff
changeset
|
173 |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
32204
diff
changeset
|
174 >>> 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:
32204
diff
changeset
|
175 >>> 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:
32204
diff
changeset
|
176 >>> 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:
32204
diff
changeset
|
177 >>> 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:
32204
diff
changeset
|
178 |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
32204
diff
changeset
|
179 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:
32204
diff
changeset
|
180 |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
32204
diff
changeset
|
181 >>> s[1], s[:2] |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
32204
diff
changeset
|
182 (b'o', b'fo') |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
32204
diff
changeset
|
183 >>> list(s), list(reversed(s)) |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
32204
diff
changeset
|
184 ([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:
32204
diff
changeset
|
185 |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
32204
diff
changeset
|
186 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:
32204
diff
changeset
|
187 bytes to bytestr explicitly: |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
32204
diff
changeset
|
188 |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
32204
diff
changeset
|
189 >>> s = bytestr(b'foo').upper() |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
32204
diff
changeset
|
190 >>> t = bytestr(s) |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
32204
diff
changeset
|
191 >>> s[0], t[0] |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
32204
diff
changeset
|
192 (70, b'F') |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
32204
diff
changeset
|
193 |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
32204
diff
changeset
|
194 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:
32204
diff
changeset
|
195 bytearray-like behavior. |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
32204
diff
changeset
|
196 |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
32204
diff
changeset
|
197 >>> 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:
32204
diff
changeset
|
198 >>> assert type(t) is bytes |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
32204
diff
changeset
|
199 """ |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
32204
diff
changeset
|
200 |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
32204
diff
changeset
|
201 def __new__(cls, s=b''): |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
32204
diff
changeset
|
202 if isinstance(s, bytestr): |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
32204
diff
changeset
|
203 return s |
43863
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43806
diff
changeset
|
204 if not isinstance( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43806
diff
changeset
|
205 s, (bytes, bytearray) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43806
diff
changeset
|
206 ) and not hasattr( # hasattr-py3-only |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43806
diff
changeset
|
207 s, u'__bytes__' |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43806
diff
changeset
|
208 ): |
43878
127cc1f72e70
py3: stop normalizing .encode()/.decode() arguments to unicode
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43877
diff
changeset
|
209 s = str(s).encode('ascii') |
32218
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
32204
diff
changeset
|
210 return bytes.__new__(cls, s) |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
32204
diff
changeset
|
211 |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
32204
diff
changeset
|
212 def __getitem__(self, key): |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
32204
diff
changeset
|
213 s = bytes.__getitem__(self, key) |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
32204
diff
changeset
|
214 if not isinstance(s, bytes): |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
32204
diff
changeset
|
215 s = bytechr(s) |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
32204
diff
changeset
|
216 return s |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
32204
diff
changeset
|
217 |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
32204
diff
changeset
|
218 def __iter__(self): |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
32204
diff
changeset
|
219 return iterbytestr(bytes.__iter__(self)) |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
32204
diff
changeset
|
220 |
36689
1a31111e6239
py3: always drop b'' prefix from repr() of bytestr
Yuya Nishihara <yuya@tcha.org>
parents:
36188
diff
changeset
|
221 def __repr__(self): |
1a31111e6239
py3: always drop b'' prefix from repr() of bytestr
Yuya Nishihara <yuya@tcha.org>
parents:
36188
diff
changeset
|
222 return bytes.__repr__(self)[1:] # drop b'' |
1a31111e6239
py3: always drop b'' prefix from repr() of bytestr
Yuya Nishihara <yuya@tcha.org>
parents:
36188
diff
changeset
|
223 |
32161
c9fd842dc886
pycompat: add helper to iterate each char in bytes
Yuya Nishihara <yuya@tcha.org>
parents:
32151
diff
changeset
|
224 def iterbytestr(s): |
c9fd842dc886
pycompat: add helper to iterate each char in bytes
Yuya Nishihara <yuya@tcha.org>
parents:
32151
diff
changeset
|
225 """Iterate bytes as if it were a str object of Python 2""" |
32204
63a39d647888
py3: make py3 compat.iterbytestr simpler and faster
Martin von Zweigbergk <martinvonz@google.com>
parents:
32203
diff
changeset
|
226 return map(bytechr, s) |
32161
c9fd842dc886
pycompat: add helper to iterate each char in bytes
Yuya Nishihara <yuya@tcha.org>
parents:
32151
diff
changeset
|
227 |
36690
fc44c2657dc5
py3: drop b'' from repr() of smartset
Yuya Nishihara <yuya@tcha.org>
parents:
36689
diff
changeset
|
228 def maybebytestr(s): |
fc44c2657dc5
py3: drop b'' from repr() of smartset
Yuya Nishihara <yuya@tcha.org>
parents:
36689
diff
changeset
|
229 """Promote bytes to bytestr""" |
fc44c2657dc5
py3: drop b'' from repr() of smartset
Yuya Nishihara <yuya@tcha.org>
parents:
36689
diff
changeset
|
230 if isinstance(s, bytes): |
fc44c2657dc5
py3: drop b'' from repr() of smartset
Yuya Nishihara <yuya@tcha.org>
parents:
36689
diff
changeset
|
231 return bytestr(s) |
fc44c2657dc5
py3: drop b'' from repr() of smartset
Yuya Nishihara <yuya@tcha.org>
parents:
36689
diff
changeset
|
232 return s |
fc44c2657dc5
py3: drop b'' from repr() of smartset
Yuya Nishihara <yuya@tcha.org>
parents:
36689
diff
changeset
|
233 |
32599
45761ef1bc93
py3: have registrar process docstrings in bytes
Yuya Nishihara <yuya@tcha.org>
parents:
32554
diff
changeset
|
234 def sysbytes(s): |
45761ef1bc93
py3: have registrar process docstrings in bytes
Yuya Nishihara <yuya@tcha.org>
parents:
32554
diff
changeset
|
235 """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:
32554
diff
changeset
|
236 |
45761ef1bc93
py3: have registrar process docstrings in bytes
Yuya Nishihara <yuya@tcha.org>
parents:
32554
diff
changeset
|
237 This never raises UnicodeEncodeError, but only ASCII characters |
45761ef1bc93
py3: have registrar process docstrings in bytes
Yuya Nishihara <yuya@tcha.org>
parents:
32554
diff
changeset
|
238 can be round-trip by sysstr(sysbytes(s)). |
45761ef1bc93
py3: have registrar process docstrings in bytes
Yuya Nishihara <yuya@tcha.org>
parents:
32554
diff
changeset
|
239 """ |
43878
127cc1f72e70
py3: stop normalizing .encode()/.decode() arguments to unicode
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43877
diff
changeset
|
240 return s.encode('utf-8') |
32599
45761ef1bc93
py3: have registrar process docstrings in bytes
Yuya Nishihara <yuya@tcha.org>
parents:
32554
diff
changeset
|
241 |
30811
2219f4f82ede
pycompat: extract function that converts attribute or encoding name to str
Yuya Nishihara <yuya@tcha.org>
parents:
30810
diff
changeset
|
242 def sysstr(s): |
2219f4f82ede
pycompat: extract function that converts attribute or encoding name to str
Yuya Nishihara <yuya@tcha.org>
parents:
30810
diff
changeset
|
243 """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:
30810
diff
changeset
|
244 getattr() and str.encode() |
2219f4f82ede
pycompat: extract function that converts attribute or encoding name to str
Yuya Nishihara <yuya@tcha.org>
parents:
30810
diff
changeset
|
245 |
2219f4f82ede
pycompat: extract function that converts attribute or encoding name to str
Yuya Nishihara <yuya@tcha.org>
parents:
30810
diff
changeset
|
246 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:
30810
diff
changeset
|
247 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:
30810
diff
changeset
|
248 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:
30810
diff
changeset
|
249 """ |
2219f4f82ede
pycompat: extract function that converts attribute or encoding name to str
Yuya Nishihara <yuya@tcha.org>
parents:
30810
diff
changeset
|
250 if isinstance(s, builtins.str): |
2219f4f82ede
pycompat: extract function that converts attribute or encoding name to str
Yuya Nishihara <yuya@tcha.org>
parents:
30810
diff
changeset
|
251 return s |
43878
127cc1f72e70
py3: stop normalizing .encode()/.decode() arguments to unicode
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43877
diff
changeset
|
252 return s.decode('latin-1') |
30811
2219f4f82ede
pycompat: extract function that converts attribute or encoding name to str
Yuya Nishihara <yuya@tcha.org>
parents:
30810
diff
changeset
|
253 |
33642
a05f3675c46a
py3: add a new strurl() which will convert a bytes url to str
Pulkit Goyal <7895pulkit@gmail.com>
parents:
33398
diff
changeset
|
254 def strurl(url): |
a05f3675c46a
py3: add a new strurl() which will convert a bytes url to str
Pulkit Goyal <7895pulkit@gmail.com>
parents:
33398
diff
changeset
|
255 """Converts a bytes url back to str""" |
37430
e2b87e19c6ef
pycompat: prevent encoding or decoding values if not required
Pulkit Goyal <7895pulkit@gmail.com>
parents:
37342
diff
changeset
|
256 if isinstance(url, bytes): |
43878
127cc1f72e70
py3: stop normalizing .encode()/.decode() arguments to unicode
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43877
diff
changeset
|
257 return url.decode('ascii') |
37430
e2b87e19c6ef
pycompat: prevent encoding or decoding values if not required
Pulkit Goyal <7895pulkit@gmail.com>
parents:
37342
diff
changeset
|
258 return url |
33642
a05f3675c46a
py3: add a new strurl() which will convert a bytes url to str
Pulkit Goyal <7895pulkit@gmail.com>
parents:
33398
diff
changeset
|
259 |
33643
f22f39d56bb5
py3: add a new bytesurl() to convert a str url into bytes
Pulkit Goyal <7895pulkit@gmail.com>
parents:
33642
diff
changeset
|
260 def bytesurl(url): |
f22f39d56bb5
py3: add a new bytesurl() to convert a str url into bytes
Pulkit Goyal <7895pulkit@gmail.com>
parents:
33642
diff
changeset
|
261 """Converts a str url to bytes by encoding in ascii""" |
37430
e2b87e19c6ef
pycompat: prevent encoding or decoding values if not required
Pulkit Goyal <7895pulkit@gmail.com>
parents:
37342
diff
changeset
|
262 if isinstance(url, str): |
43878
127cc1f72e70
py3: stop normalizing .encode()/.decode() arguments to unicode
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43877
diff
changeset
|
263 return url.encode('ascii') |
37430
e2b87e19c6ef
pycompat: prevent encoding or decoding values if not required
Pulkit Goyal <7895pulkit@gmail.com>
parents:
37342
diff
changeset
|
264 return url |
33643
f22f39d56bb5
py3: add a new bytesurl() to convert a str url into bytes
Pulkit Goyal <7895pulkit@gmail.com>
parents:
33642
diff
changeset
|
265 |
32969
76f9a0009b4b
pycompat: extract helper to raise exception with traceback
Yuya Nishihara <yuya@tcha.org>
parents:
32721
diff
changeset
|
266 def raisewithtb(exc, tb): |
76f9a0009b4b
pycompat: extract helper to raise exception with traceback
Yuya Nishihara <yuya@tcha.org>
parents:
32721
diff
changeset
|
267 """Raise exception with the given traceback""" |
76f9a0009b4b
pycompat: extract helper to raise exception with traceback
Yuya Nishihara <yuya@tcha.org>
parents:
32721
diff
changeset
|
268 raise exc.with_traceback(tb) |
76f9a0009b4b
pycompat: extract helper to raise exception with traceback
Yuya Nishihara <yuya@tcha.org>
parents:
32721
diff
changeset
|
269 |
33398
c9318beb7c1a
py3: convert __doc__ back to bytes in help.py
Yuya Nishihara <yuya@tcha.org>
parents:
33233
diff
changeset
|
270 def getdoc(obj): |
c9318beb7c1a
py3: convert __doc__ back to bytes in help.py
Yuya Nishihara <yuya@tcha.org>
parents:
33233
diff
changeset
|
271 """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:
33233
diff
changeset
|
272 with _('')""" |
43890
c95b2f40db7c
py3: stop normalizing 2nd argument of *attr() to unicode
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43883
diff
changeset
|
273 doc = getattr(obj, '__doc__', None) |
33398
c9318beb7c1a
py3: convert __doc__ back to bytes in help.py
Yuya Nishihara <yuya@tcha.org>
parents:
33233
diff
changeset
|
274 if doc is None: |
c9318beb7c1a
py3: convert __doc__ back to bytes in help.py
Yuya Nishihara <yuya@tcha.org>
parents:
33233
diff
changeset
|
275 return doc |
c9318beb7c1a
py3: convert __doc__ back to bytes in help.py
Yuya Nishihara <yuya@tcha.org>
parents:
33233
diff
changeset
|
276 return sysbytes(doc) |
c9318beb7c1a
py3: convert __doc__ back to bytes in help.py
Yuya Nishihara <yuya@tcha.org>
parents:
33233
diff
changeset
|
277 |
30569
45fa8de47a0f
py3: provide (del|get|has|set)attr wrappers that accepts bytes
Yuya Nishihara <yuya@tcha.org>
parents:
30568
diff
changeset
|
278 def _wrapattrfunc(f): |
45fa8de47a0f
py3: provide (del|get|has|set)attr wrappers that accepts bytes
Yuya Nishihara <yuya@tcha.org>
parents:
30568
diff
changeset
|
279 @functools.wraps(f) |
45fa8de47a0f
py3: provide (del|get|has|set)attr wrappers that accepts bytes
Yuya Nishihara <yuya@tcha.org>
parents:
30568
diff
changeset
|
280 def w(object, name, *args): |
30811
2219f4f82ede
pycompat: extract function that converts attribute or encoding name to str
Yuya Nishihara <yuya@tcha.org>
parents:
30810
diff
changeset
|
281 return f(object, sysstr(name), *args) |
43863
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43806
diff
changeset
|
282 |
30569
45fa8de47a0f
py3: provide (del|get|has|set)attr wrappers that accepts bytes
Yuya Nishihara <yuya@tcha.org>
parents:
30568
diff
changeset
|
283 return w |
45fa8de47a0f
py3: provide (del|get|has|set)attr wrappers that accepts bytes
Yuya Nishihara <yuya@tcha.org>
parents:
30568
diff
changeset
|
284 |
30570
178c89e8519a
py3: import builtin wrappers automagically by code transformer
Yuya Nishihara <yuya@tcha.org>
parents:
30569
diff
changeset
|
285 # these wrappers are automagically imported by hgloader |
30569
45fa8de47a0f
py3: provide (del|get|has|set)attr wrappers that accepts bytes
Yuya Nishihara <yuya@tcha.org>
parents:
30568
diff
changeset
|
286 delattr = _wrapattrfunc(builtins.delattr) |
45fa8de47a0f
py3: provide (del|get|has|set)attr wrappers that accepts bytes
Yuya Nishihara <yuya@tcha.org>
parents:
30568
diff
changeset
|
287 getattr = _wrapattrfunc(builtins.getattr) |
45fa8de47a0f
py3: provide (del|get|has|set)attr wrappers that accepts bytes
Yuya Nishihara <yuya@tcha.org>
parents:
30568
diff
changeset
|
288 hasattr = _wrapattrfunc(builtins.hasattr) |
45fa8de47a0f
py3: provide (del|get|has|set)attr wrappers that accepts bytes
Yuya Nishihara <yuya@tcha.org>
parents:
30568
diff
changeset
|
289 setattr = _wrapattrfunc(builtins.setattr) |
30570
178c89e8519a
py3: import builtin wrappers automagically by code transformer
Yuya Nishihara <yuya@tcha.org>
parents:
30569
diff
changeset
|
290 xrange = builtins.range |
32622
526e4597cca5
py3: add pycompat.unicode and add it to importer
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32621
diff
changeset
|
291 unicode = str |
30569
45fa8de47a0f
py3: provide (del|get|has|set)attr wrappers that accepts bytes
Yuya Nishihara <yuya@tcha.org>
parents:
30568
diff
changeset
|
292 |
40428
a407f9009392
py3: byteify strings in pycompat
Matt Harbison <matt_harbison@yahoo.com>
parents:
40242
diff
changeset
|
293 def open(name, mode=b'r', buffering=-1, encoding=None): |
37342
63fe5ca93b13
pycompat: add support for encoding argument to our wrapper
Augie Fackler <augie@google.com>
parents:
37120
diff
changeset
|
294 return builtins.open(name, sysstr(mode), buffering, encoding) |
31928
76a64c1e5439
py3: add pycompat.open and replace open() calls
Pulkit Goyal <7895pulkit@gmail.com>
parents:
31599
diff
changeset
|
295 |
37885
6ca5f825a0ca
util: make safehasattr() a pycompat function
Yuya Nishihara <yuya@tcha.org>
parents:
37850
diff
changeset
|
296 safehasattr = _wrapattrfunc(builtins.hasattr) |
6ca5f825a0ca
util: make safehasattr() a pycompat function
Yuya Nishihara <yuya@tcha.org>
parents:
37850
diff
changeset
|
297 |
36009
5b569d512fbd
fancyopts: use getopt.gnu_getopt()
Yuya Nishihara <yuya@tcha.org>
parents:
35427
diff
changeset
|
298 def _getoptbwrapper(orig, args, shortlist, namelist): |
33647
f57f1f37290d
pycompat: move multiline comments above a function to function doc
Pulkit Goyal <7895pulkit@gmail.com>
parents:
33643
diff
changeset
|
299 """ |
f57f1f37290d
pycompat: move multiline comments above a function to function doc
Pulkit Goyal <7895pulkit@gmail.com>
parents:
33643
diff
changeset
|
300 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:
33643
diff
changeset
|
301 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:
33643
diff
changeset
|
302 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:
33643
diff
changeset
|
303 bytes on Python 3. |
f57f1f37290d
pycompat: move multiline comments above a function to function doc
Pulkit Goyal <7895pulkit@gmail.com>
parents:
33643
diff
changeset
|
304 """ |
31357
c6ce11f2ee50
py3: make a bytes version of getopt.getopt()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
31279
diff
changeset
|
305 args = [a.decode('latin-1') for a in args] |
c6ce11f2ee50
py3: make a bytes version of getopt.getopt()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
31279
diff
changeset
|
306 shortlist = shortlist.decode('latin-1') |
c6ce11f2ee50
py3: make a bytes version of getopt.getopt()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
31279
diff
changeset
|
307 namelist = [a.decode('latin-1') for a in namelist] |
36009
5b569d512fbd
fancyopts: use getopt.gnu_getopt()
Yuya Nishihara <yuya@tcha.org>
parents:
35427
diff
changeset
|
308 opts, args = orig(args, shortlist, namelist) |
43863
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43806
diff
changeset
|
309 opts = [(a[0].encode('latin-1'), a[1].encode('latin-1')) for a in opts] |
31357
c6ce11f2ee50
py3: make a bytes version of getopt.getopt()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
31279
diff
changeset
|
310 args = [a.encode('latin-1') for a in args] |
c6ce11f2ee50
py3: make a bytes version of getopt.getopt()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
31279
diff
changeset
|
311 return opts, args |
c6ce11f2ee50
py3: make a bytes version of getopt.getopt()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
31279
diff
changeset
|
312 |
31358
fbc3f73dc802
py3: utility functions to convert keys of kwargs to bytes/unicodes
Pulkit Goyal <7895pulkit@gmail.com>
parents:
31357
diff
changeset
|
313 def strkwargs(dic): |
33647
f57f1f37290d
pycompat: move multiline comments above a function to function doc
Pulkit Goyal <7895pulkit@gmail.com>
parents:
33643
diff
changeset
|
314 """ |
f57f1f37290d
pycompat: move multiline comments above a function to function doc
Pulkit Goyal <7895pulkit@gmail.com>
parents:
33643
diff
changeset
|
315 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:
33643
diff
changeset
|
316 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:
33643
diff
changeset
|
317 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:
33643
diff
changeset
|
318 """ |
31358
fbc3f73dc802
py3: utility functions to convert keys of kwargs to bytes/unicodes
Pulkit Goyal <7895pulkit@gmail.com>
parents:
31357
diff
changeset
|
319 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:
31357
diff
changeset
|
320 return dic |
fbc3f73dc802
py3: utility functions to convert keys of kwargs to bytes/unicodes
Pulkit Goyal <7895pulkit@gmail.com>
parents:
31357
diff
changeset
|
321 |
fbc3f73dc802
py3: utility functions to convert keys of kwargs to bytes/unicodes
Pulkit Goyal <7895pulkit@gmail.com>
parents:
31357
diff
changeset
|
322 def byteskwargs(dic): |
33647
f57f1f37290d
pycompat: move multiline comments above a function to function doc
Pulkit Goyal <7895pulkit@gmail.com>
parents:
33643
diff
changeset
|
323 """ |
f57f1f37290d
pycompat: move multiline comments above a function to function doc
Pulkit Goyal <7895pulkit@gmail.com>
parents:
33643
diff
changeset
|
324 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:
33643
diff
changeset
|
325 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:
33643
diff
changeset
|
326 """ |
31358
fbc3f73dc802
py3: utility functions to convert keys of kwargs to bytes/unicodes
Pulkit Goyal <7895pulkit@gmail.com>
parents:
31357
diff
changeset
|
327 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:
31357
diff
changeset
|
328 return dic |
fbc3f73dc802
py3: utility functions to convert keys of kwargs to bytes/unicodes
Pulkit Goyal <7895pulkit@gmail.com>
parents:
31357
diff
changeset
|
329 |
31457
caf7e1c5efe4
py3: have a bytes version of shlex.split()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
31447
diff
changeset
|
330 # TODO: handle shlex.shlex(). |
37120
4cd2d1cc2a31
pycompat: correct the shlex.split() proxy method signature in py3
Matt Harbison <matt_harbison@yahoo.com>
parents:
37051
diff
changeset
|
331 def shlexsplit(s, comments=False, posix=True): |
33647
f57f1f37290d
pycompat: move multiline comments above a function to function doc
Pulkit Goyal <7895pulkit@gmail.com>
parents:
33643
diff
changeset
|
332 """ |
f57f1f37290d
pycompat: move multiline comments above a function to function doc
Pulkit Goyal <7895pulkit@gmail.com>
parents:
33643
diff
changeset
|
333 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:
33643
diff
changeset
|
334 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:
33643
diff
changeset
|
335 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:
33643
diff
changeset
|
336 """ |
37120
4cd2d1cc2a31
pycompat: correct the shlex.split() proxy method signature in py3
Matt Harbison <matt_harbison@yahoo.com>
parents:
37051
diff
changeset
|
337 ret = shlex.split(s.decode('latin-1'), comments, posix) |
31457
caf7e1c5efe4
py3: have a bytes version of shlex.split()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
31447
diff
changeset
|
338 return [a.encode('latin-1') for a in ret] |
caf7e1c5efe4
py3: have a bytes version of shlex.split()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
31447
diff
changeset
|
339 |
43892
649d3ac37a12
py3: define and use pycompat.iteritems() for hgext/
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43891
diff
changeset
|
340 iteritems = lambda x: x.items() |
43891
74802979dd9d
py3: define and use pycompat.itervalues()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43890
diff
changeset
|
341 itervalues = lambda x: x.values() |
43806
2cc453284d5c
patchbomb: protect email addresses from shell
Floris Bruynooghe <flub@google.com>
parents:
41313
diff
changeset
|
342 |
30811
2219f4f82ede
pycompat: extract function that converts attribute or encoding name to str
Yuya Nishihara <yuya@tcha.org>
parents:
30810
diff
changeset
|
343 else: |
32151
06440ba06bc0
pycompat: move imports of cStringIO/io to where they are used
Yuya Nishihara <yuya@tcha.org>
parents:
32138
diff
changeset
|
344 import cStringIO |
06440ba06bc0
pycompat: move imports of cStringIO/io to where they are used
Yuya Nishihara <yuya@tcha.org>
parents:
32138
diff
changeset
|
345 |
39568
7eba8f83129b
pycompat: add xrange alias for Python 2
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39361
diff
changeset
|
346 xrange = xrange |
39098
79dd61a4554f
py3: replace `unicode` with pycompat.unicode
Pulkit Goyal <7895pulkit@gmail.com>
parents:
38952
diff
changeset
|
347 unicode = unicode |
32032
64596338ba10
py3: factor out bytechr() function
Yuya Nishihara <yuya@tcha.org>
parents:
31928
diff
changeset
|
348 bytechr = chr |
37051
b44fac3a49fb
py3: factor out byterepr() which returns an asciified value on py3
Yuya Nishihara <yuya@tcha.org>
parents:
36964
diff
changeset
|
349 byterepr = repr |
32218
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
32204
diff
changeset
|
350 bytestr = str |
32161
c9fd842dc886
pycompat: add helper to iterate each char in bytes
Yuya Nishihara <yuya@tcha.org>
parents:
32151
diff
changeset
|
351 iterbytestr = iter |
36690
fc44c2657dc5
py3: drop b'' from repr() of smartset
Yuya Nishihara <yuya@tcha.org>
parents:
36689
diff
changeset
|
352 maybebytestr = identity |
32599
45761ef1bc93
py3: have registrar process docstrings in bytes
Yuya Nishihara <yuya@tcha.org>
parents:
32554
diff
changeset
|
353 sysbytes = identity |
32553
7d2cbe11ae48
pycompat: introduce identity function as a compat stub
Yuya Nishihara <yuya@tcha.org>
parents:
32352
diff
changeset
|
354 sysstr = identity |
33642
a05f3675c46a
py3: add a new strurl() which will convert a bytes url to str
Pulkit Goyal <7895pulkit@gmail.com>
parents:
33398
diff
changeset
|
355 strurl = identity |
33643
f22f39d56bb5
py3: add a new bytesurl() to convert a str url into bytes
Pulkit Goyal <7895pulkit@gmail.com>
parents:
33642
diff
changeset
|
356 bytesurl = identity |
43872
eef9a2d67051
py3: manually import pycompat.open into files that need it
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43864
diff
changeset
|
357 open = open |
43877
1f339b503a40
py3: manually import pycompat.delattr where it is needed
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43876
diff
changeset
|
358 delattr = delattr |
43876
c59eb1560c44
py3: manually import getattr where it is needed
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43875
diff
changeset
|
359 getattr = getattr |
43875
0d612db7047c
py3: stop injecting pycompat.hasattr into modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43874
diff
changeset
|
360 hasattr = hasattr |
43874
66f2cc210a29
py3: manually import pycompat.setattr where it is needed
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43872
diff
changeset
|
361 setattr = setattr |
30811
2219f4f82ede
pycompat: extract function that converts attribute or encoding name to str
Yuya Nishihara <yuya@tcha.org>
parents:
30810
diff
changeset
|
362 |
32969
76f9a0009b4b
pycompat: extract helper to raise exception with traceback
Yuya Nishihara <yuya@tcha.org>
parents:
32721
diff
changeset
|
363 # this can't be parsed on Python 3 |
43864
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43863
diff
changeset
|
364 exec(b'def raisewithtb(exc, tb):\n' b' raise exc, None, tb\n') |
32969
76f9a0009b4b
pycompat: extract helper to raise exception with traceback
Yuya Nishihara <yuya@tcha.org>
parents:
32721
diff
changeset
|
365 |
30912
f6dcda7505f9
pycompat: only accept a bytestring filepath in Python 2
Martijn Pieters <mjpieters@fb.com>
parents:
30898
diff
changeset
|
366 def fsencode(filename): |
33647
f57f1f37290d
pycompat: move multiline comments above a function to function doc
Pulkit Goyal <7895pulkit@gmail.com>
parents:
33643
diff
changeset
|
367 """ |
f57f1f37290d
pycompat: move multiline comments above a function to function doc
Pulkit Goyal <7895pulkit@gmail.com>
parents:
33643
diff
changeset
|
368 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:
33643
diff
changeset
|
369 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:
33643
diff
changeset
|
370 indicates a bug. |
f57f1f37290d
pycompat: move multiline comments above a function to function doc
Pulkit Goyal <7895pulkit@gmail.com>
parents:
33643
diff
changeset
|
371 """ |
30912
f6dcda7505f9
pycompat: only accept a bytestring filepath in Python 2
Martijn Pieters <mjpieters@fb.com>
parents:
30898
diff
changeset
|
372 if isinstance(filename, str): |
f6dcda7505f9
pycompat: only accept a bytestring filepath in Python 2
Martijn Pieters <mjpieters@fb.com>
parents:
30898
diff
changeset
|
373 return filename |
30898
f4a5e0e86a7e
py3: add an os.fsencode backport to ease path handling
Martijn Pieters <mjpieters@fb.com>
parents:
30865
diff
changeset
|
374 else: |
43863
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43806
diff
changeset
|
375 raise TypeError(r"expect str, not %s" % type(filename).__name__) |
30898
f4a5e0e86a7e
py3: add an os.fsencode backport to ease path handling
Martijn Pieters <mjpieters@fb.com>
parents:
30865
diff
changeset
|
376 |
31079
42af0590f4b9
py3: add os.fsdecode() as pycompat.fsdecode()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30912
diff
changeset
|
377 # 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:
30912
diff
changeset
|
378 # better not to touch Python 2 part as it's already working fine. |
32553
7d2cbe11ae48
pycompat: introduce identity function as a compat stub
Yuya Nishihara <yuya@tcha.org>
parents:
32352
diff
changeset
|
379 fsdecode = identity |
31079
42af0590f4b9
py3: add os.fsdecode() as pycompat.fsdecode()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30912
diff
changeset
|
380 |
33398
c9318beb7c1a
py3: convert __doc__ back to bytes in help.py
Yuya Nishihara <yuya@tcha.org>
parents:
33233
diff
changeset
|
381 def getdoc(obj): |
c9318beb7c1a
py3: convert __doc__ back to bytes in help.py
Yuya Nishihara <yuya@tcha.org>
parents:
33233
diff
changeset
|
382 return getattr(obj, '__doc__', None) |
c9318beb7c1a
py3: convert __doc__ back to bytes in help.py
Yuya Nishihara <yuya@tcha.org>
parents:
33233
diff
changeset
|
383 |
37885
6ca5f825a0ca
util: make safehasattr() a pycompat function
Yuya Nishihara <yuya@tcha.org>
parents:
37850
diff
changeset
|
384 _notset = object() |
6ca5f825a0ca
util: make safehasattr() a pycompat function
Yuya Nishihara <yuya@tcha.org>
parents:
37850
diff
changeset
|
385 |
6ca5f825a0ca
util: make safehasattr() a pycompat function
Yuya Nishihara <yuya@tcha.org>
parents:
37850
diff
changeset
|
386 def safehasattr(thing, attr): |
6ca5f825a0ca
util: make safehasattr() a pycompat function
Yuya Nishihara <yuya@tcha.org>
parents:
37850
diff
changeset
|
387 return getattr(thing, attr, _notset) is not _notset |
6ca5f825a0ca
util: make safehasattr() a pycompat function
Yuya Nishihara <yuya@tcha.org>
parents:
37850
diff
changeset
|
388 |
36009
5b569d512fbd
fancyopts: use getopt.gnu_getopt()
Yuya Nishihara <yuya@tcha.org>
parents:
35427
diff
changeset
|
389 def _getoptbwrapper(orig, args, shortlist, namelist): |
5b569d512fbd
fancyopts: use getopt.gnu_getopt()
Yuya Nishihara <yuya@tcha.org>
parents:
35427
diff
changeset
|
390 return orig(args, shortlist, namelist) |
31357
c6ce11f2ee50
py3: make a bytes version of getopt.getopt()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
31279
diff
changeset
|
391 |
32553
7d2cbe11ae48
pycompat: introduce identity function as a compat stub
Yuya Nishihara <yuya@tcha.org>
parents:
32352
diff
changeset
|
392 strkwargs = identity |
7d2cbe11ae48
pycompat: introduce identity function as a compat stub
Yuya Nishihara <yuya@tcha.org>
parents:
32352
diff
changeset
|
393 byteskwargs = identity |
31358
fbc3f73dc802
py3: utility functions to convert keys of kwargs to bytes/unicodes
Pulkit Goyal <7895pulkit@gmail.com>
parents:
31357
diff
changeset
|
394 |
37434
6585ac350fd9
py3: make os.curdir a bytes
Yuya Nishihara <yuya@tcha.org>
parents:
37433
diff
changeset
|
395 oscurdir = os.curdir |
32554
8181f378b073
pycompat: provide bytes os.linesep
Yuya Nishihara <yuya@tcha.org>
parents:
32553
diff
changeset
|
396 oslinesep = os.linesep |
31081
3874ddba1ab4
py3: add a bytes version of os.name
Pulkit Goyal <7895pulkit@gmail.com>
parents:
31079
diff
changeset
|
397 osname = os.name |
31082
ad40d307a9f0
py3: have pycompat.ospathsep and pycompat.ossep
Pulkit Goyal <7895pulkit@gmail.com>
parents:
31081
diff
changeset
|
398 ospathsep = os.pathsep |
37433
052351e3e1cd
py3: make os.pardir a bytes
Yuya Nishihara <yuya@tcha.org>
parents:
37430
diff
changeset
|
399 ospardir = os.pardir |
31082
ad40d307a9f0
py3: have pycompat.ospathsep and pycompat.ossep
Pulkit Goyal <7895pulkit@gmail.com>
parents:
31081
diff
changeset
|
400 ossep = os.sep |
31402
c6026c20a3ce
py3: have a bytes version of os.altsep
Pulkit Goyal <7895pulkit@gmail.com>
parents:
31358
diff
changeset
|
401 osaltsep = os.altsep |
40242
8d858fbf2759
cbor: teach the encoder to handle python `long` type for Windows
Matt Harbison <matt_harbison@yahoo.com>
parents:
40088
diff
changeset
|
402 long = long |
31251
277f4fe6d01a
py3: provide bytes stdin/out/err through util module
Yuya Nishihara <yuya@tcha.org>
parents:
31113
diff
changeset
|
403 stdin = sys.stdin |
277f4fe6d01a
py3: provide bytes stdin/out/err through util module
Yuya Nishihara <yuya@tcha.org>
parents:
31113
diff
changeset
|
404 stdout = sys.stdout |
277f4fe6d01a
py3: provide bytes stdin/out/err through util module
Yuya Nishihara <yuya@tcha.org>
parents:
31113
diff
changeset
|
405 stderr = sys.stderr |
32056
86cd1f2cfff5
pycompat: verify sys.argv exists before forwarding it (issue5493)
Augie Fackler <augie@google.com>
parents:
31599
diff
changeset
|
406 if getattr(sys, 'argv', None) is not None: |
86cd1f2cfff5
pycompat: verify sys.argv exists before forwarding it (issue5493)
Augie Fackler <augie@google.com>
parents:
31599
diff
changeset
|
407 sysargv = sys.argv |
31403
a82a6eee2613
py3: have a bytes version of sys.platform
Pulkit Goyal <7895pulkit@gmail.com>
parents:
31402
diff
changeset
|
408 sysplatform = sys.platform |
31447
3fcaf0f660ce
py3: have bytes version of sys.executable
Pulkit Goyal <7895pulkit@gmail.com>
parents:
31442
diff
changeset
|
409 sysexecutable = sys.executable |
31457
caf7e1c5efe4
py3: have a bytes version of shlex.split()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
31447
diff
changeset
|
410 shlexsplit = shlex.split |
37744
644a02f6b34f
util: prefer "bytesio" to "stringio"
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37720
diff
changeset
|
411 bytesio = cStringIO.StringIO |
644a02f6b34f
util: prefer "bytesio" to "stringio"
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37720
diff
changeset
|
412 stringio = bytesio |
32280
a1e40ceee640
pycompat: add maplist alias for old map behavior
Augie Fackler <augie@google.com>
parents:
32218
diff
changeset
|
413 maplist = map |
37850
434e520adb8c
annotate: do not construct attr.s object per line while computing history
Yuya Nishihara <yuya@tcha.org>
parents:
37744
diff
changeset
|
414 rangelist = range |
36188
e66d6e938d2d
py3: introduce pycompat.ziplist as zip is a generator on Python 3
Pulkit Goyal <7895pulkit@gmail.com>
parents:
36009
diff
changeset
|
415 ziplist = zip |
34636
cfcfbe6c96f8
py3: select input or raw_input by pycompat
Yuya Nishihara <yuya@tcha.org>
parents:
34409
diff
changeset
|
416 rawinput = raw_input |
36964
646002338365
py3: introduce and use pycompat.getargspec
Augie Fackler <augie@google.com>
parents:
36831
diff
changeset
|
417 getargspec = inspect.getargspec |
43892
649d3ac37a12
py3: define and use pycompat.iteritems() for hgext/
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43891
diff
changeset
|
418 iteritems = lambda x: x.iteritems() |
43891
74802979dd9d
py3: define and use pycompat.itervalues()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43890
diff
changeset
|
419 itervalues = lambda x: x.itervalues() |
35422
a568a46751b6
selectors2: do not use platform.system()
Jun Wu <quark@fb.com>
parents:
35250
diff
changeset
|
420 |
40428
a407f9009392
py3: byteify strings in pycompat
Matt Harbison <matt_harbison@yahoo.com>
parents:
40242
diff
changeset
|
421 isjython = sysplatform.startswith(b'java') |
35427
c0a6c19690ff
pycompat: define operating system constants
Jun Wu <quark@fb.com>
parents:
35422
diff
changeset
|
422 |
41313
1b49b84d5ed5
pycompat: adding Linux detection and fixing Mac
rdamazio@google.com
parents:
40604
diff
changeset
|
423 isdarwin = sysplatform.startswith(b'darwin') |
1b49b84d5ed5
pycompat: adding Linux detection and fixing Mac
rdamazio@google.com
parents:
40604
diff
changeset
|
424 islinux = sysplatform.startswith(b'linux') |
40428
a407f9009392
py3: byteify strings in pycompat
Matt Harbison <matt_harbison@yahoo.com>
parents:
40242
diff
changeset
|
425 isposix = osname == b'posix' |
a407f9009392
py3: byteify strings in pycompat
Matt Harbison <matt_harbison@yahoo.com>
parents:
40242
diff
changeset
|
426 iswindows = osname == b'nt' |
36009
5b569d512fbd
fancyopts: use getopt.gnu_getopt()
Yuya Nishihara <yuya@tcha.org>
parents:
35427
diff
changeset
|
427 |
43863
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43806
diff
changeset
|
428 |
36009
5b569d512fbd
fancyopts: use getopt.gnu_getopt()
Yuya Nishihara <yuya@tcha.org>
parents:
35427
diff
changeset
|
429 def getoptb(args, shortlist, namelist): |
5b569d512fbd
fancyopts: use getopt.gnu_getopt()
Yuya Nishihara <yuya@tcha.org>
parents:
35427
diff
changeset
|
430 return _getoptbwrapper(getopt.getopt, args, shortlist, namelist) |
5b569d512fbd
fancyopts: use getopt.gnu_getopt()
Yuya Nishihara <yuya@tcha.org>
parents:
35427
diff
changeset
|
431 |
43863
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43806
diff
changeset
|
432 |
36009
5b569d512fbd
fancyopts: use getopt.gnu_getopt()
Yuya Nishihara <yuya@tcha.org>
parents:
35427
diff
changeset
|
433 def gnugetoptb(args, shortlist, namelist): |
5b569d512fbd
fancyopts: use getopt.gnu_getopt()
Yuya Nishihara <yuya@tcha.org>
parents:
35427
diff
changeset
|
434 return _getoptbwrapper(getopt.gnu_getopt, args, shortlist, namelist) |
38950
aac4be30e250
py3: wrap tempfile.mkstemp() to use bytes path
Yuya Nishihara <yuya@tcha.org>
parents:
38630
diff
changeset
|
435 |
43863
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43806
diff
changeset
|
436 |
38951
2ce60954b1b7
py3: wrap tempfile.mkdtemp() to use bytes path
Yuya Nishihara <yuya@tcha.org>
parents:
38950
diff
changeset
|
437 def mkdtemp(suffix=b'', prefix=b'tmp', dir=None): |
2ce60954b1b7
py3: wrap tempfile.mkdtemp() to use bytes path
Yuya Nishihara <yuya@tcha.org>
parents:
38950
diff
changeset
|
438 return tempfile.mkdtemp(suffix, prefix, dir) |
2ce60954b1b7
py3: wrap tempfile.mkdtemp() to use bytes path
Yuya Nishihara <yuya@tcha.org>
parents:
38950
diff
changeset
|
439 |
43863
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43806
diff
changeset
|
440 |
38950
aac4be30e250
py3: wrap tempfile.mkstemp() to use bytes path
Yuya Nishihara <yuya@tcha.org>
parents:
38630
diff
changeset
|
441 # text=True is not supported; use util.from/tonativeeol() instead |
aac4be30e250
py3: wrap tempfile.mkstemp() to use bytes path
Yuya Nishihara <yuya@tcha.org>
parents:
38630
diff
changeset
|
442 def mkstemp(suffix=b'', prefix=b'tmp', dir=None): |
aac4be30e250
py3: wrap tempfile.mkstemp() to use bytes path
Yuya Nishihara <yuya@tcha.org>
parents:
38630
diff
changeset
|
443 return tempfile.mkstemp(suffix, prefix, dir) |
38952
cc9aa88792fe
py3: wrap tempfile.NamedTemporaryFile() to return bytes fp.name
Yuya Nishihara <yuya@tcha.org>
parents:
38951
diff
changeset
|
444 |
43863
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43806
diff
changeset
|
445 |
38952
cc9aa88792fe
py3: wrap tempfile.NamedTemporaryFile() to return bytes fp.name
Yuya Nishihara <yuya@tcha.org>
parents:
38951
diff
changeset
|
446 # mode must include 'b'ytes as encoding= is not supported |
43863
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43806
diff
changeset
|
447 def namedtempfile( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43806
diff
changeset
|
448 mode=b'w+b', bufsize=-1, suffix=b'', prefix=b'tmp', dir=None, delete=True |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43806
diff
changeset
|
449 ): |
38952
cc9aa88792fe
py3: wrap tempfile.NamedTemporaryFile() to return bytes fp.name
Yuya Nishihara <yuya@tcha.org>
parents:
38951
diff
changeset
|
450 mode = sysstr(mode) |
cc9aa88792fe
py3: wrap tempfile.NamedTemporaryFile() to return bytes fp.name
Yuya Nishihara <yuya@tcha.org>
parents:
38951
diff
changeset
|
451 assert r'b' in mode |
43863
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43806
diff
changeset
|
452 return tempfile.NamedTemporaryFile( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43806
diff
changeset
|
453 mode, bufsize, suffix=suffix, prefix=prefix, dir=dir, delete=delete |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43806
diff
changeset
|
454 ) |