Mercurial > hg-stable
annotate mercurial/pycompat.py @ 47345:0654b3b3d2b5
dirstate-v2: Parse the dirstate lazily, with copy-on-write nodes
TODO: more description
Differential Revision: https://phab.mercurial-scm.org/D10751
author | Simon Sapin <simon.sapin@octobus.net> |
---|---|
date | Wed, 19 May 2021 13:15:00 +0200 |
parents | 7a29d9002250 |
children | 2b76255a4f74 |
rev | line source |
---|---|
28818
6041fb8f2da8
pycompat: add empty and queue to handle py3 divergence
timeless <timeless@mozdev.org>
parents:
diff
changeset
|
1 # pycompat.py - portability shim for python 3 |
6041fb8f2da8
pycompat: add empty and queue to handle py3 divergence
timeless <timeless@mozdev.org>
parents:
diff
changeset
|
2 # |
6041fb8f2da8
pycompat: add empty and queue to handle py3 divergence
timeless <timeless@mozdev.org>
parents:
diff
changeset
|
3 # This software may be used and distributed according to the terms of the |
6041fb8f2da8
pycompat: add empty and queue to handle py3 divergence
timeless <timeless@mozdev.org>
parents:
diff
changeset
|
4 # GNU General Public License version 2 or any later version. |
6041fb8f2da8
pycompat: add empty and queue to handle py3 divergence
timeless <timeless@mozdev.org>
parents:
diff
changeset
|
5 |
6041fb8f2da8
pycompat: add empty and queue to handle py3 divergence
timeless <timeless@mozdev.org>
parents:
diff
changeset
|
6 """Mercurial portability shim for python 3. |
6041fb8f2da8
pycompat: add empty and queue to handle py3 divergence
timeless <timeless@mozdev.org>
parents:
diff
changeset
|
7 |
6041fb8f2da8
pycompat: add empty and queue to handle py3 divergence
timeless <timeless@mozdev.org>
parents:
diff
changeset
|
8 This contains aliases to hide python version-specific details from the core. |
6041fb8f2da8
pycompat: add empty and queue to handle py3 divergence
timeless <timeless@mozdev.org>
parents:
diff
changeset
|
9 """ |
6041fb8f2da8
pycompat: add empty and queue to handle py3 divergence
timeless <timeless@mozdev.org>
parents:
diff
changeset
|
10 |
6041fb8f2da8
pycompat: add empty and queue to handle py3 divergence
timeless <timeless@mozdev.org>
parents:
diff
changeset
|
11 from __future__ import absolute_import |
6041fb8f2da8
pycompat: add empty and queue to handle py3 divergence
timeless <timeless@mozdev.org>
parents:
diff
changeset
|
12 |
30583
c6ce11f2ee50
py3: make a bytes version of getopt.getopt()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30509
diff
changeset
|
13 import getopt |
36216
646002338365
py3: introduce and use pycompat.getargspec
Augie Fackler <augie@google.com>
parents:
36083
diff
changeset
|
14 import inspect |
43380
579672b347d2
py3: define and use json.loads polyfill
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43117
diff
changeset
|
15 import json |
30315
3874ddba1ab4
py3: add a bytes version of os.name
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30313
diff
changeset
|
16 import os |
30681
caf7e1c5efe4
py3: have a bytes version of shlex.split()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30671
diff
changeset
|
17 import shlex |
29584
06587edd1233
pycompat: make pycompat demandimport friendly
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29566
diff
changeset
|
18 import sys |
38197
aac4be30e250
py3: wrap tempfile.mkstemp() to use bytes path
Yuya Nishihara <yuya@tcha.org>
parents:
37890
diff
changeset
|
19 import tempfile |
29584
06587edd1233
pycompat: make pycompat demandimport friendly
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29566
diff
changeset
|
20 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43021
diff
changeset
|
21 ispy3 = sys.version_info[0] >= 3 |
43554
9f70512ae2cf
cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents:
43551
diff
changeset
|
22 ispypy = '__pypy__' in sys.builtin_module_names |
43793
7b14d649af1b
typing: consolidate "if not globals():" trick
Yuya Nishihara <yuya@tcha.org>
parents:
43788
diff
changeset
|
23 TYPE_CHECKING = False |
7b14d649af1b
typing: consolidate "if not globals():" trick
Yuya Nishihara <yuya@tcha.org>
parents:
43788
diff
changeset
|
24 |
7b14d649af1b
typing: consolidate "if not globals():" trick
Yuya Nishihara <yuya@tcha.org>
parents:
43788
diff
changeset
|
25 if not globals(): # hide this from non-pytype users |
7b14d649af1b
typing: consolidate "if not globals():" trick
Yuya Nishihara <yuya@tcha.org>
parents:
43788
diff
changeset
|
26 import typing |
7b14d649af1b
typing: consolidate "if not globals():" trick
Yuya Nishihara <yuya@tcha.org>
parents:
43788
diff
changeset
|
27 |
7b14d649af1b
typing: consolidate "if not globals():" trick
Yuya Nishihara <yuya@tcha.org>
parents:
43788
diff
changeset
|
28 TYPE_CHECKING = typing.TYPE_CHECKING |
30031
0f6d6fdd3c2a
pycompat: provide 'ispy3' constant
Yuya Nishihara <yuya@tcha.org>
parents:
29812
diff
changeset
|
29 |
0f6d6fdd3c2a
pycompat: provide 'ispy3' constant
Yuya Nishihara <yuya@tcha.org>
parents:
29812
diff
changeset
|
30 if not ispy3: |
31934
12aca6770046
util: make cookielib module available
Gregory Szorc <gregory.szorc@gmail.com>
parents:
31843
diff
changeset
|
31 import cookielib |
29324
b501579147f1
py3: conditionalize cPickle import by adding in util
Pulkit Goyal <7895pulkit@gmail.com>
parents:
28882
diff
changeset
|
32 import cPickle as pickle |
29455
0c741fd6158a
py3: conditionalize httplib import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29433
diff
changeset
|
33 import httplib |
37890
8fb9985382be
pycompat: export queue module instead of symbols in module (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37669
diff
changeset
|
34 import Queue as queue |
29433
33770d2b6cf9
py3: conditionalize SocketServer import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29432
diff
changeset
|
35 import SocketServer as socketserver |
29432
34b914ac573e
py3: conditionalize xmlrpclib import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29431
diff
changeset
|
36 import xmlrpclib |
37628
8da30ceae88f
pycompat: export a handle on concurrent.futures
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37102
diff
changeset
|
37 |
8da30ceae88f
pycompat: export a handle on concurrent.futures
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37102
diff
changeset
|
38 from .thirdparty.concurrent import futures |
37669
1cb54e6193a6
py3: paper over differences in future exception handling
Augie Fackler <augie@google.com>
parents:
37628
diff
changeset
|
39 |
1cb54e6193a6
py3: paper over differences in future exception handling
Augie Fackler <augie@google.com>
parents:
37628
diff
changeset
|
40 def future_set_exception_info(f, exc_info): |
1cb54e6193a6
py3: paper over differences in future exception handling
Augie Fackler <augie@google.com>
parents:
37628
diff
changeset
|
41 f.set_exception_info(*exc_info) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43021
diff
changeset
|
42 |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43021
diff
changeset
|
43 |
29584
06587edd1233
pycompat: make pycompat demandimport friendly
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29566
diff
changeset
|
44 else: |
37628
8da30ceae88f
pycompat: export a handle on concurrent.futures
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37102
diff
changeset
|
45 import concurrent.futures as futures |
31942
bc0579a25f82
pycompat: import correct cookie module on Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
31934
diff
changeset
|
46 import http.cookiejar as cookielib |
29584
06587edd1233
pycompat: make pycompat demandimport friendly
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29566
diff
changeset
|
47 import http.client as httplib |
06587edd1233
pycompat: make pycompat demandimport friendly
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29566
diff
changeset
|
48 import pickle |
37890
8fb9985382be
pycompat: export queue module instead of symbols in module (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37669
diff
changeset
|
49 import queue as queue |
29584
06587edd1233
pycompat: make pycompat demandimport friendly
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29566
diff
changeset
|
50 import socketserver |
29432
34b914ac573e
py3: conditionalize xmlrpclib import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29431
diff
changeset
|
51 import xmlrpc.client as xmlrpclib |
29431
80880ad3fccd
py3: conditionalize the urlparse import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29414
diff
changeset
|
52 |
37669
1cb54e6193a6
py3: paper over differences in future exception handling
Augie Fackler <augie@google.com>
parents:
37628
diff
changeset
|
53 def future_set_exception_info(f, exc_info): |
1cb54e6193a6
py3: paper over differences in future exception handling
Augie Fackler <augie@google.com>
parents:
37628
diff
changeset
|
54 f.set_exception(exc_info[0]) |
1cb54e6193a6
py3: paper over differences in future exception handling
Augie Fackler <augie@google.com>
parents:
37628
diff
changeset
|
55 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43021
diff
changeset
|
56 |
31777
7d2cbe11ae48
pycompat: introduce identity function as a compat stub
Yuya Nishihara <yuya@tcha.org>
parents:
31579
diff
changeset
|
57 def identity(a): |
7d2cbe11ae48
pycompat: introduce identity function as a compat stub
Yuya Nishihara <yuya@tcha.org>
parents:
31579
diff
changeset
|
58 return a |
7d2cbe11ae48
pycompat: introduce identity function as a compat stub
Yuya Nishihara <yuya@tcha.org>
parents:
31579
diff
changeset
|
59 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43021
diff
changeset
|
60 |
38576
152f4822d210
pycompat: move rapply() from util
Yuya Nishihara <yuya@tcha.org>
parents:
38341
diff
changeset
|
61 def _rapply(f, xs): |
152f4822d210
pycompat: move rapply() from util
Yuya Nishihara <yuya@tcha.org>
parents:
38341
diff
changeset
|
62 if xs is None: |
152f4822d210
pycompat: move rapply() from util
Yuya Nishihara <yuya@tcha.org>
parents:
38341
diff
changeset
|
63 # assume None means non-value of optional data |
152f4822d210
pycompat: move rapply() from util
Yuya Nishihara <yuya@tcha.org>
parents:
38341
diff
changeset
|
64 return xs |
152f4822d210
pycompat: move rapply() from util
Yuya Nishihara <yuya@tcha.org>
parents:
38341
diff
changeset
|
65 if isinstance(xs, (list, set, tuple)): |
152f4822d210
pycompat: move rapply() from util
Yuya Nishihara <yuya@tcha.org>
parents:
38341
diff
changeset
|
66 return type(xs)(_rapply(f, x) for x in xs) |
152f4822d210
pycompat: move rapply() from util
Yuya Nishihara <yuya@tcha.org>
parents:
38341
diff
changeset
|
67 if isinstance(xs, dict): |
152f4822d210
pycompat: move rapply() from util
Yuya Nishihara <yuya@tcha.org>
parents:
38341
diff
changeset
|
68 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:
38341
diff
changeset
|
69 return f(xs) |
152f4822d210
pycompat: move rapply() from util
Yuya Nishihara <yuya@tcha.org>
parents:
38341
diff
changeset
|
70 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43021
diff
changeset
|
71 |
38576
152f4822d210
pycompat: move rapply() from util
Yuya Nishihara <yuya@tcha.org>
parents:
38341
diff
changeset
|
72 def rapply(f, xs): |
152f4822d210
pycompat: move rapply() from util
Yuya Nishihara <yuya@tcha.org>
parents:
38341
diff
changeset
|
73 """Apply function recursively to every item preserving the data structure |
152f4822d210
pycompat: move rapply() from util
Yuya Nishihara <yuya@tcha.org>
parents:
38341
diff
changeset
|
74 |
152f4822d210
pycompat: move rapply() from util
Yuya Nishihara <yuya@tcha.org>
parents:
38341
diff
changeset
|
75 >>> def f(x): |
152f4822d210
pycompat: move rapply() from util
Yuya Nishihara <yuya@tcha.org>
parents:
38341
diff
changeset
|
76 ... return 'f(%s)' % x |
152f4822d210
pycompat: move rapply() from util
Yuya Nishihara <yuya@tcha.org>
parents:
38341
diff
changeset
|
77 >>> rapply(f, None) is None |
152f4822d210
pycompat: move rapply() from util
Yuya Nishihara <yuya@tcha.org>
parents:
38341
diff
changeset
|
78 True |
152f4822d210
pycompat: move rapply() from util
Yuya Nishihara <yuya@tcha.org>
parents:
38341
diff
changeset
|
79 >>> rapply(f, 'a') |
152f4822d210
pycompat: move rapply() from util
Yuya Nishihara <yuya@tcha.org>
parents:
38341
diff
changeset
|
80 'f(a)' |
152f4822d210
pycompat: move rapply() from util
Yuya Nishihara <yuya@tcha.org>
parents:
38341
diff
changeset
|
81 >>> rapply(f, {'a'}) == {'f(a)'} |
152f4822d210
pycompat: move rapply() from util
Yuya Nishihara <yuya@tcha.org>
parents:
38341
diff
changeset
|
82 True |
152f4822d210
pycompat: move rapply() from util
Yuya Nishihara <yuya@tcha.org>
parents:
38341
diff
changeset
|
83 >>> rapply(f, ['a', 'b', None, {'c': 'd'}, []]) |
152f4822d210
pycompat: move rapply() from util
Yuya Nishihara <yuya@tcha.org>
parents:
38341
diff
changeset
|
84 ['f(a)', 'f(b)', None, {'f(c)': 'f(d)'}, []] |
152f4822d210
pycompat: move rapply() from util
Yuya Nishihara <yuya@tcha.org>
parents:
38341
diff
changeset
|
85 |
152f4822d210
pycompat: move rapply() from util
Yuya Nishihara <yuya@tcha.org>
parents:
38341
diff
changeset
|
86 >>> xs = [object()] |
152f4822d210
pycompat: move rapply() from util
Yuya Nishihara <yuya@tcha.org>
parents:
38341
diff
changeset
|
87 >>> rapply(identity, xs) is xs |
152f4822d210
pycompat: move rapply() from util
Yuya Nishihara <yuya@tcha.org>
parents:
38341
diff
changeset
|
88 True |
152f4822d210
pycompat: move rapply() from util
Yuya Nishihara <yuya@tcha.org>
parents:
38341
diff
changeset
|
89 """ |
152f4822d210
pycompat: move rapply() from util
Yuya Nishihara <yuya@tcha.org>
parents:
38341
diff
changeset
|
90 if f is identity: |
152f4822d210
pycompat: move rapply() from util
Yuya Nishihara <yuya@tcha.org>
parents:
38341
diff
changeset
|
91 # fast path mainly for py2 |
152f4822d210
pycompat: move rapply() from util
Yuya Nishihara <yuya@tcha.org>
parents:
38341
diff
changeset
|
92 return xs |
152f4822d210
pycompat: move rapply() from util
Yuya Nishihara <yuya@tcha.org>
parents:
38341
diff
changeset
|
93 return _rapply(f, xs) |
152f4822d210
pycompat: move rapply() from util
Yuya Nishihara <yuya@tcha.org>
parents:
38341
diff
changeset
|
94 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43021
diff
changeset
|
95 |
30031
0f6d6fdd3c2a
pycompat: provide 'ispy3' constant
Yuya Nishihara <yuya@tcha.org>
parents:
29812
diff
changeset
|
96 if ispy3: |
29808
965c91bad9e3
py3: move xrange alias next to import lines
Yuya Nishihara <yuya@tcha.org>
parents:
29790
diff
changeset
|
97 import builtins |
43380
579672b347d2
py3: define and use json.loads polyfill
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43117
diff
changeset
|
98 import codecs |
29810
45fa8de47a0f
py3: provide (del|get|has|set)attr wrappers that accepts bytes
Yuya Nishihara <yuya@tcha.org>
parents:
29809
diff
changeset
|
99 import functools |
31381
06440ba06bc0
pycompat: move imports of cStringIO/io to where they are used
Yuya Nishihara <yuya@tcha.org>
parents:
31368
diff
changeset
|
100 import io |
31433
4acc49335a6e
py3: optimize py3 compat.bytechr using Struct.pack
Martin von Zweigbergk <martinvonz@google.com>
parents:
31409
diff
changeset
|
101 import struct |
31381
06440ba06bc0
pycompat: move imports of cStringIO/io to where they are used
Yuya Nishihara <yuya@tcha.org>
parents:
31368
diff
changeset
|
102 |
43484
8d5489b048b7
py3: enable legacy fs encoding to fix filename compatibility on Windows
Yuya Nishihara <yuya@tcha.org>
parents:
43117
diff
changeset
|
103 if os.name == r'nt' and sys.version_info >= (3, 6): |
8d5489b048b7
py3: enable legacy fs encoding to fix filename compatibility on Windows
Yuya Nishihara <yuya@tcha.org>
parents:
43117
diff
changeset
|
104 # MBCS (or ANSI) filesystem encoding must be used as before. |
8d5489b048b7
py3: enable legacy fs encoding to fix filename compatibility on Windows
Yuya Nishihara <yuya@tcha.org>
parents:
43117
diff
changeset
|
105 # Otherwise non-ASCII filenames in existing repositories would be |
8d5489b048b7
py3: enable legacy fs encoding to fix filename compatibility on Windows
Yuya Nishihara <yuya@tcha.org>
parents:
43117
diff
changeset
|
106 # corrupted. |
8d5489b048b7
py3: enable legacy fs encoding to fix filename compatibility on Windows
Yuya Nishihara <yuya@tcha.org>
parents:
43117
diff
changeset
|
107 # This must be set once prior to any fsencode/fsdecode calls. |
43788
fe73ec69350e
windows: suppress pytype warnings for Windows imports and functions
Matt Harbison <matt_harbison@yahoo.com>
parents:
43554
diff
changeset
|
108 sys._enablelegacywindowsfsencoding() # pytype: disable=module-attr |
43484
8d5489b048b7
py3: enable legacy fs encoding to fix filename compatibility on Windows
Yuya Nishihara <yuya@tcha.org>
parents:
43117
diff
changeset
|
109 |
30119
f4a5e0e86a7e
py3: add an os.fsencode backport to ease path handling
Martijn Pieters <mjpieters@fb.com>
parents:
30086
diff
changeset
|
110 fsencode = os.fsencode |
30313
42af0590f4b9
py3: add os.fsdecode() as pycompat.fsdecode()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30133
diff
changeset
|
111 fsdecode = os.fsdecode |
36677
6585ac350fd9
py3: make os.curdir a bytes
Yuya Nishihara <yuya@tcha.org>
parents:
36676
diff
changeset
|
112 oscurdir = os.curdir.encode('ascii') |
31778
8181f378b073
pycompat: provide bytes os.linesep
Yuya Nishihara <yuya@tcha.org>
parents:
31777
diff
changeset
|
113 oslinesep = os.linesep.encode('ascii') |
30315
3874ddba1ab4
py3: add a bytes version of os.name
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30313
diff
changeset
|
114 osname = os.name.encode('ascii') |
30316
ad40d307a9f0
py3: have pycompat.ospathsep and pycompat.ossep
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30315
diff
changeset
|
115 ospathsep = os.pathsep.encode('ascii') |
36676
052351e3e1cd
py3: make os.pardir a bytes
Yuya Nishihara <yuya@tcha.org>
parents:
36673
diff
changeset
|
116 ospardir = os.pardir.encode('ascii') |
30316
ad40d307a9f0
py3: have pycompat.ospathsep and pycompat.ossep
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30315
diff
changeset
|
117 ossep = os.sep.encode('ascii') |
30628
c6026c20a3ce
py3: have a bytes version of os.altsep
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30584
diff
changeset
|
118 osaltsep = os.altsep |
c6026c20a3ce
py3: have a bytes version of os.altsep
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30584
diff
changeset
|
119 if osaltsep: |
c6026c20a3ce
py3: have a bytes version of os.altsep
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30584
diff
changeset
|
120 osaltsep = osaltsep.encode('ascii') |
43810
765a9c299c44
py3: make a pycompat.osdevnull, use it in extdiff
Kyle Lippincott <spectral@google.com>
parents:
43793
diff
changeset
|
121 osdevnull = os.devnull.encode('ascii') |
39823
24e493ec2229
py3: rename pycompat.getcwd() to encoding.getcwd() (API)
Matt Harbison <matt_harbison@yahoo.com>
parents:
39658
diff
changeset
|
122 |
30629
a82a6eee2613
py3: have a bytes version of sys.platform
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30628
diff
changeset
|
123 sysplatform = sys.platform.encode('ascii') |
30671
3fcaf0f660ce
py3: have bytes version of sys.executable
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30666
diff
changeset
|
124 sysexecutable = sys.executable |
3fcaf0f660ce
py3: have bytes version of sys.executable
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30666
diff
changeset
|
125 if sysexecutable: |
3fcaf0f660ce
py3: have bytes version of sys.executable
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30666
diff
changeset
|
126 sysexecutable = os.fsencode(sysexecutable) |
36964
644a02f6b34f
util: prefer "bytesio" to "stringio"
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36940
diff
changeset
|
127 bytesio = io.BytesIO |
644a02f6b34f
util: prefer "bytesio" to "stringio"
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36940
diff
changeset
|
128 # 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:
36940
diff
changeset
|
129 stringio = bytesio |
36940
dbae581010ea
pycompat: name maplist() and ziplist() for better traceback message
Yuya Nishihara <yuya@tcha.org>
parents:
36677
diff
changeset
|
130 |
dbae581010ea
pycompat: name maplist() and ziplist() for better traceback message
Yuya Nishihara <yuya@tcha.org>
parents:
36677
diff
changeset
|
131 def maplist(*args): |
dbae581010ea
pycompat: name maplist() and ziplist() for better traceback message
Yuya Nishihara <yuya@tcha.org>
parents:
36677
diff
changeset
|
132 return list(map(*args)) |
dbae581010ea
pycompat: name maplist() and ziplist() for better traceback message
Yuya Nishihara <yuya@tcha.org>
parents:
36677
diff
changeset
|
133 |
37067
434e520adb8c
annotate: do not construct attr.s object per line while computing history
Yuya Nishihara <yuya@tcha.org>
parents:
36964
diff
changeset
|
134 def rangelist(*args): |
434e520adb8c
annotate: do not construct attr.s object per line while computing history
Yuya Nishihara <yuya@tcha.org>
parents:
36964
diff
changeset
|
135 return list(range(*args)) |
434e520adb8c
annotate: do not construct attr.s object per line while computing history
Yuya Nishihara <yuya@tcha.org>
parents:
36964
diff
changeset
|
136 |
36940
dbae581010ea
pycompat: name maplist() and ziplist() for better traceback message
Yuya Nishihara <yuya@tcha.org>
parents:
36677
diff
changeset
|
137 def ziplist(*args): |
dbae581010ea
pycompat: name maplist() and ziplist() for better traceback message
Yuya Nishihara <yuya@tcha.org>
parents:
36677
diff
changeset
|
138 return list(zip(*args)) |
dbae581010ea
pycompat: name maplist() and ziplist() for better traceback message
Yuya Nishihara <yuya@tcha.org>
parents:
36677
diff
changeset
|
139 |
33874
cfcfbe6c96f8
py3: select input or raw_input by pycompat
Yuya Nishihara <yuya@tcha.org>
parents:
33624
diff
changeset
|
140 rawinput = input |
36216
646002338365
py3: introduce and use pycompat.getargspec
Augie Fackler <augie@google.com>
parents:
36083
diff
changeset
|
141 getargspec = inspect.getfullargspec |
30344
19d8e19fde5b
py3: document why os.fsencode() can be used to get back bytes argv
Yuya Nishihara <yuya@tcha.org>
parents:
30340
diff
changeset
|
142 |
39473
8d858fbf2759
cbor: teach the encoder to handle python `long` type for Windows
Matt Harbison <matt_harbison@yahoo.com>
parents:
39329
diff
changeset
|
143 long = int |
8d858fbf2759
cbor: teach the encoder to handle python `long` type for Windows
Matt Harbison <matt_harbison@yahoo.com>
parents:
39329
diff
changeset
|
144 |
30888
86cd1f2cfff5
pycompat: verify sys.argv exists before forwarding it (issue5493)
Augie Fackler <augie@google.com>
parents:
30820
diff
changeset
|
145 if getattr(sys, 'argv', None) is not None: |
44653
00e0c5c06ed5
pycompat: change argv conversion semantics
Gregory Szorc <gregory.szorc@gmail.com>
parents:
44470
diff
changeset
|
146 # On POSIX, the char** argv array is converted to Python str using |
45018
f2de8f31cb59
pycompat: use os.fsencode() to re-encode sys.argv
Manuel Jacob <me@manueljacob.de>
parents:
44821
diff
changeset
|
147 # Py_DecodeLocale(). The inverse of this is Py_EncodeLocale(), which |
f2de8f31cb59
pycompat: use os.fsencode() to re-encode sys.argv
Manuel Jacob <me@manueljacob.de>
parents:
44821
diff
changeset
|
148 # isn't directly callable from Python code. In practice, os.fsencode() |
f2de8f31cb59
pycompat: use os.fsencode() to re-encode sys.argv
Manuel Jacob <me@manueljacob.de>
parents:
44821
diff
changeset
|
149 # can be used instead (this is recommended by Python's documentation |
f2de8f31cb59
pycompat: use os.fsencode() to re-encode sys.argv
Manuel Jacob <me@manueljacob.de>
parents:
44821
diff
changeset
|
150 # for sys.argv). |
44653
00e0c5c06ed5
pycompat: change argv conversion semantics
Gregory Szorc <gregory.szorc@gmail.com>
parents:
44470
diff
changeset
|
151 # |
00e0c5c06ed5
pycompat: change argv conversion semantics
Gregory Szorc <gregory.szorc@gmail.com>
parents:
44470
diff
changeset
|
152 # On Windows, the wchar_t **argv is passed into the interpreter as-is. |
00e0c5c06ed5
pycompat: change argv conversion semantics
Gregory Szorc <gregory.szorc@gmail.com>
parents:
44470
diff
changeset
|
153 # Like POSIX, we need to emulate what Py_EncodeLocale() would do. But |
00e0c5c06ed5
pycompat: change argv conversion semantics
Gregory Szorc <gregory.szorc@gmail.com>
parents:
44470
diff
changeset
|
154 # there's an additional wrinkle. What we really want to access is the |
00e0c5c06ed5
pycompat: change argv conversion semantics
Gregory Szorc <gregory.szorc@gmail.com>
parents:
44470
diff
changeset
|
155 # ANSI codepage representation of the arguments, as this is what |
00e0c5c06ed5
pycompat: change argv conversion semantics
Gregory Szorc <gregory.szorc@gmail.com>
parents:
44470
diff
changeset
|
156 # `int main()` would receive if Python 3 didn't define `int wmain()` |
00e0c5c06ed5
pycompat: change argv conversion semantics
Gregory Szorc <gregory.szorc@gmail.com>
parents:
44470
diff
changeset
|
157 # (this is how Python 2 worked). To get that, we encode with the mbcs |
00e0c5c06ed5
pycompat: change argv conversion semantics
Gregory Szorc <gregory.szorc@gmail.com>
parents:
44470
diff
changeset
|
158 # encoding, which will pass CP_ACP to the underlying Windows API to |
00e0c5c06ed5
pycompat: change argv conversion semantics
Gregory Szorc <gregory.szorc@gmail.com>
parents:
44470
diff
changeset
|
159 # produce bytes. |
00e0c5c06ed5
pycompat: change argv conversion semantics
Gregory Szorc <gregory.szorc@gmail.com>
parents:
44470
diff
changeset
|
160 if os.name == r'nt': |
00e0c5c06ed5
pycompat: change argv conversion semantics
Gregory Szorc <gregory.szorc@gmail.com>
parents:
44470
diff
changeset
|
161 sysargv = [a.encode("mbcs", "ignore") for a in sys.argv] |
00e0c5c06ed5
pycompat: change argv conversion semantics
Gregory Szorc <gregory.szorc@gmail.com>
parents:
44470
diff
changeset
|
162 else: |
45018
f2de8f31cb59
pycompat: use os.fsencode() to re-encode sys.argv
Manuel Jacob <me@manueljacob.de>
parents:
44821
diff
changeset
|
163 sysargv = [fsencode(a) for a in sys.argv] |
29808
965c91bad9e3
py3: move xrange alias next to import lines
Yuya Nishihara <yuya@tcha.org>
parents:
29790
diff
changeset
|
164 |
43554
9f70512ae2cf
cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents:
43551
diff
changeset
|
165 bytechr = struct.Struct('>B').pack |
36298
b44fac3a49fb
py3: factor out byterepr() which returns an asciified value on py3
Yuya Nishihara <yuya@tcha.org>
parents:
36216
diff
changeset
|
166 byterepr = b'%r'.__mod__ |
31263
64596338ba10
py3: factor out bytechr() function
Yuya Nishihara <yuya@tcha.org>
parents:
31159
diff
changeset
|
167 |
31448
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
168 class bytestr(bytes): |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
169 """A bytes which mostly acts as a Python 2 str |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
170 |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
171 >>> bytestr(), bytestr(bytearray(b'foo')), bytestr(u'ascii'), bytestr(1) |
35943
1a31111e6239
py3: always drop b'' prefix from repr() of bytestr
Yuya Nishihara <yuya@tcha.org>
parents:
35412
diff
changeset
|
172 ('', 'foo', 'ascii', '1') |
31448
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
173 >>> s = bytestr(b'foo') |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
174 >>> assert s is bytestr(s) |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
175 |
32488
548478efc46c
pycompat: try __bytes__() to convert object to bytestr
Yuya Nishihara <yuya@tcha.org>
parents:
32232
diff
changeset
|
176 __bytes__() should be called if provided: |
548478efc46c
pycompat: try __bytes__() to convert object to bytestr
Yuya Nishihara <yuya@tcha.org>
parents:
32232
diff
changeset
|
177 |
548478efc46c
pycompat: try __bytes__() to convert object to bytestr
Yuya Nishihara <yuya@tcha.org>
parents:
32232
diff
changeset
|
178 >>> class bytesable(object): |
548478efc46c
pycompat: try __bytes__() to convert object to bytestr
Yuya Nishihara <yuya@tcha.org>
parents:
32232
diff
changeset
|
179 ... def __bytes__(self): |
548478efc46c
pycompat: try __bytes__() to convert object to bytestr
Yuya Nishihara <yuya@tcha.org>
parents:
32232
diff
changeset
|
180 ... return b'bytes' |
548478efc46c
pycompat: try __bytes__() to convert object to bytestr
Yuya Nishihara <yuya@tcha.org>
parents:
32232
diff
changeset
|
181 >>> bytestr(bytesable()) |
35943
1a31111e6239
py3: always drop b'' prefix from repr() of bytestr
Yuya Nishihara <yuya@tcha.org>
parents:
35412
diff
changeset
|
182 'bytes' |
32488
548478efc46c
pycompat: try __bytes__() to convert object to bytestr
Yuya Nishihara <yuya@tcha.org>
parents:
32232
diff
changeset
|
183 |
31448
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
184 There's no implicit conversion from non-ascii str as its encoding is |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
185 unknown: |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
186 |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
187 >>> bytestr(chr(0x80)) # doctest: +ELLIPSIS |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
188 Traceback (most recent call last): |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
189 ... |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
190 UnicodeEncodeError: ... |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
191 |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
192 Comparison between bytestr and bytes should work: |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
193 |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
194 >>> assert bytestr(b'foo') == b'foo' |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
195 >>> assert b'foo' == bytestr(b'foo') |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
196 >>> assert b'f' in bytestr(b'foo') |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
197 >>> assert bytestr(b'f') in b'foo' |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
198 |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
199 Sliced elements should be bytes, not integer: |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
200 |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
201 >>> s[1], s[:2] |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
202 (b'o', b'fo') |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
203 >>> list(s), list(reversed(s)) |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
204 ([b'f', b'o', b'o'], [b'o', b'o', b'f']) |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
205 |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
206 As bytestr type isn't propagated across operations, you need to cast |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
207 bytes to bytestr explicitly: |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
208 |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
209 >>> s = bytestr(b'foo').upper() |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
210 >>> t = bytestr(s) |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
211 >>> s[0], t[0] |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
212 (70, b'F') |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
213 |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
214 Be careful to not pass a bytestr object to a function which expects |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
215 bytearray-like behavior. |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
216 |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
217 >>> t = bytes(t) # cast to bytes |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
218 >>> assert type(t) is bytes |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
219 """ |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
220 |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
221 def __new__(cls, s=b''): |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
222 if isinstance(s, bytestr): |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
223 return s |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43021
diff
changeset
|
224 if not isinstance( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43021
diff
changeset
|
225 s, (bytes, bytearray) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43021
diff
changeset
|
226 ) and not hasattr( # hasattr-py3-only |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43021
diff
changeset
|
227 s, u'__bytes__' |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43021
diff
changeset
|
228 ): |
43091
127cc1f72e70
py3: stop normalizing .encode()/.decode() arguments to unicode
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43090
diff
changeset
|
229 s = str(s).encode('ascii') |
31448
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
230 return bytes.__new__(cls, s) |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
231 |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
232 def __getitem__(self, key): |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
233 s = bytes.__getitem__(self, key) |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
234 if not isinstance(s, bytes): |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
235 s = bytechr(s) |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
236 return s |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
237 |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
238 def __iter__(self): |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
239 return iterbytestr(bytes.__iter__(self)) |
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
240 |
35943
1a31111e6239
py3: always drop b'' prefix from repr() of bytestr
Yuya Nishihara <yuya@tcha.org>
parents:
35412
diff
changeset
|
241 def __repr__(self): |
1a31111e6239
py3: always drop b'' prefix from repr() of bytestr
Yuya Nishihara <yuya@tcha.org>
parents:
35412
diff
changeset
|
242 return bytes.__repr__(self)[1:] # drop b'' |
1a31111e6239
py3: always drop b'' prefix from repr() of bytestr
Yuya Nishihara <yuya@tcha.org>
parents:
35412
diff
changeset
|
243 |
31391
c9fd842dc886
pycompat: add helper to iterate each char in bytes
Yuya Nishihara <yuya@tcha.org>
parents:
31381
diff
changeset
|
244 def iterbytestr(s): |
c9fd842dc886
pycompat: add helper to iterate each char in bytes
Yuya Nishihara <yuya@tcha.org>
parents:
31381
diff
changeset
|
245 """Iterate bytes as if it were a str object of Python 2""" |
31434
63a39d647888
py3: make py3 compat.iterbytestr simpler and faster
Martin von Zweigbergk <martinvonz@google.com>
parents:
31433
diff
changeset
|
246 return map(bytechr, s) |
31391
c9fd842dc886
pycompat: add helper to iterate each char in bytes
Yuya Nishihara <yuya@tcha.org>
parents:
31381
diff
changeset
|
247 |
35944
fc44c2657dc5
py3: drop b'' from repr() of smartset
Yuya Nishihara <yuya@tcha.org>
parents:
35943
diff
changeset
|
248 def maybebytestr(s): |
fc44c2657dc5
py3: drop b'' from repr() of smartset
Yuya Nishihara <yuya@tcha.org>
parents:
35943
diff
changeset
|
249 """Promote bytes to bytestr""" |
fc44c2657dc5
py3: drop b'' from repr() of smartset
Yuya Nishihara <yuya@tcha.org>
parents:
35943
diff
changeset
|
250 if isinstance(s, bytes): |
fc44c2657dc5
py3: drop b'' from repr() of smartset
Yuya Nishihara <yuya@tcha.org>
parents:
35943
diff
changeset
|
251 return bytestr(s) |
fc44c2657dc5
py3: drop b'' from repr() of smartset
Yuya Nishihara <yuya@tcha.org>
parents:
35943
diff
changeset
|
252 return s |
fc44c2657dc5
py3: drop b'' from repr() of smartset
Yuya Nishihara <yuya@tcha.org>
parents:
35943
diff
changeset
|
253 |
31820
45761ef1bc93
py3: have registrar process docstrings in bytes
Yuya Nishihara <yuya@tcha.org>
parents:
31778
diff
changeset
|
254 def sysbytes(s): |
45761ef1bc93
py3: have registrar process docstrings in bytes
Yuya Nishihara <yuya@tcha.org>
parents:
31778
diff
changeset
|
255 """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:
31778
diff
changeset
|
256 |
45761ef1bc93
py3: have registrar process docstrings in bytes
Yuya Nishihara <yuya@tcha.org>
parents:
31778
diff
changeset
|
257 This never raises UnicodeEncodeError, but only ASCII characters |
45761ef1bc93
py3: have registrar process docstrings in bytes
Yuya Nishihara <yuya@tcha.org>
parents:
31778
diff
changeset
|
258 can be round-trip by sysstr(sysbytes(s)). |
45761ef1bc93
py3: have registrar process docstrings in bytes
Yuya Nishihara <yuya@tcha.org>
parents:
31778
diff
changeset
|
259 """ |
43876
66af68d4c751
pycompat: allow pycompat.sysbytes() even if input already is bytes
Martin von Zweigbergk <martinvonz@google.com>
parents:
43810
diff
changeset
|
260 if isinstance(s, bytes): |
66af68d4c751
pycompat: allow pycompat.sysbytes() even if input already is bytes
Martin von Zweigbergk <martinvonz@google.com>
parents:
43810
diff
changeset
|
261 return s |
43091
127cc1f72e70
py3: stop normalizing .encode()/.decode() arguments to unicode
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43090
diff
changeset
|
262 return s.encode('utf-8') |
31820
45761ef1bc93
py3: have registrar process docstrings in bytes
Yuya Nishihara <yuya@tcha.org>
parents:
31778
diff
changeset
|
263 |
30032
2219f4f82ede
pycompat: extract function that converts attribute or encoding name to str
Yuya Nishihara <yuya@tcha.org>
parents:
30031
diff
changeset
|
264 def sysstr(s): |
2219f4f82ede
pycompat: extract function that converts attribute or encoding name to str
Yuya Nishihara <yuya@tcha.org>
parents:
30031
diff
changeset
|
265 """Return a keyword str to be passed to Python functions such as |
2219f4f82ede
pycompat: extract function that converts attribute or encoding name to str
Yuya Nishihara <yuya@tcha.org>
parents:
30031
diff
changeset
|
266 getattr() and str.encode() |
2219f4f82ede
pycompat: extract function that converts attribute or encoding name to str
Yuya Nishihara <yuya@tcha.org>
parents:
30031
diff
changeset
|
267 |
2219f4f82ede
pycompat: extract function that converts attribute or encoding name to str
Yuya Nishihara <yuya@tcha.org>
parents:
30031
diff
changeset
|
268 This never raises UnicodeDecodeError. Non-ascii characters are |
2219f4f82ede
pycompat: extract function that converts attribute or encoding name to str
Yuya Nishihara <yuya@tcha.org>
parents:
30031
diff
changeset
|
269 considered invalid and mapped to arbitrary but unique code points |
2219f4f82ede
pycompat: extract function that converts attribute or encoding name to str
Yuya Nishihara <yuya@tcha.org>
parents:
30031
diff
changeset
|
270 such that 'sysstr(a) != sysstr(b)' for all 'a != b'. |
2219f4f82ede
pycompat: extract function that converts attribute or encoding name to str
Yuya Nishihara <yuya@tcha.org>
parents:
30031
diff
changeset
|
271 """ |
2219f4f82ede
pycompat: extract function that converts attribute or encoding name to str
Yuya Nishihara <yuya@tcha.org>
parents:
30031
diff
changeset
|
272 if isinstance(s, builtins.str): |
2219f4f82ede
pycompat: extract function that converts attribute or encoding name to str
Yuya Nishihara <yuya@tcha.org>
parents:
30031
diff
changeset
|
273 return s |
43091
127cc1f72e70
py3: stop normalizing .encode()/.decode() arguments to unicode
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43090
diff
changeset
|
274 return s.decode('latin-1') |
30032
2219f4f82ede
pycompat: extract function that converts attribute or encoding name to str
Yuya Nishihara <yuya@tcha.org>
parents:
30031
diff
changeset
|
275 |
32877
a05f3675c46a
py3: add a new strurl() which will convert a bytes url to str
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32638
diff
changeset
|
276 def strurl(url): |
a05f3675c46a
py3: add a new strurl() which will convert a bytes url to str
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32638
diff
changeset
|
277 """Converts a bytes url back to str""" |
36673
e2b87e19c6ef
pycompat: prevent encoding or decoding values if not required
Pulkit Goyal <7895pulkit@gmail.com>
parents:
36585
diff
changeset
|
278 if isinstance(url, bytes): |
43091
127cc1f72e70
py3: stop normalizing .encode()/.decode() arguments to unicode
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43090
diff
changeset
|
279 return url.decode('ascii') |
36673
e2b87e19c6ef
pycompat: prevent encoding or decoding values if not required
Pulkit Goyal <7895pulkit@gmail.com>
parents:
36585
diff
changeset
|
280 return url |
32877
a05f3675c46a
py3: add a new strurl() which will convert a bytes url to str
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32638
diff
changeset
|
281 |
32878
f22f39d56bb5
py3: add a new bytesurl() to convert a str url into bytes
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32877
diff
changeset
|
282 def bytesurl(url): |
f22f39d56bb5
py3: add a new bytesurl() to convert a str url into bytes
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32877
diff
changeset
|
283 """Converts a str url to bytes by encoding in ascii""" |
36673
e2b87e19c6ef
pycompat: prevent encoding or decoding values if not required
Pulkit Goyal <7895pulkit@gmail.com>
parents:
36585
diff
changeset
|
284 if isinstance(url, str): |
43091
127cc1f72e70
py3: stop normalizing .encode()/.decode() arguments to unicode
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43090
diff
changeset
|
285 return url.encode('ascii') |
36673
e2b87e19c6ef
pycompat: prevent encoding or decoding values if not required
Pulkit Goyal <7895pulkit@gmail.com>
parents:
36585
diff
changeset
|
286 return url |
32878
f22f39d56bb5
py3: add a new bytesurl() to convert a str url into bytes
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32877
diff
changeset
|
287 |
32232
76f9a0009b4b
pycompat: extract helper to raise exception with traceback
Yuya Nishihara <yuya@tcha.org>
parents:
31942
diff
changeset
|
288 def raisewithtb(exc, tb): |
76f9a0009b4b
pycompat: extract helper to raise exception with traceback
Yuya Nishihara <yuya@tcha.org>
parents:
31942
diff
changeset
|
289 """Raise exception with the given traceback""" |
76f9a0009b4b
pycompat: extract helper to raise exception with traceback
Yuya Nishihara <yuya@tcha.org>
parents:
31942
diff
changeset
|
290 raise exc.with_traceback(tb) |
76f9a0009b4b
pycompat: extract helper to raise exception with traceback
Yuya Nishihara <yuya@tcha.org>
parents:
31942
diff
changeset
|
291 |
32638
c9318beb7c1a
py3: convert __doc__ back to bytes in help.py
Yuya Nishihara <yuya@tcha.org>
parents:
32488
diff
changeset
|
292 def getdoc(obj): |
c9318beb7c1a
py3: convert __doc__ back to bytes in help.py
Yuya Nishihara <yuya@tcha.org>
parents:
32488
diff
changeset
|
293 """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:
32488
diff
changeset
|
294 with _('')""" |
43103
c95b2f40db7c
py3: stop normalizing 2nd argument of *attr() to unicode
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43096
diff
changeset
|
295 doc = getattr(obj, '__doc__', None) |
32638
c9318beb7c1a
py3: convert __doc__ back to bytes in help.py
Yuya Nishihara <yuya@tcha.org>
parents:
32488
diff
changeset
|
296 if doc is None: |
c9318beb7c1a
py3: convert __doc__ back to bytes in help.py
Yuya Nishihara <yuya@tcha.org>
parents:
32488
diff
changeset
|
297 return doc |
c9318beb7c1a
py3: convert __doc__ back to bytes in help.py
Yuya Nishihara <yuya@tcha.org>
parents:
32488
diff
changeset
|
298 return sysbytes(doc) |
c9318beb7c1a
py3: convert __doc__ back to bytes in help.py
Yuya Nishihara <yuya@tcha.org>
parents:
32488
diff
changeset
|
299 |
29810
45fa8de47a0f
py3: provide (del|get|has|set)attr wrappers that accepts bytes
Yuya Nishihara <yuya@tcha.org>
parents:
29809
diff
changeset
|
300 def _wrapattrfunc(f): |
45fa8de47a0f
py3: provide (del|get|has|set)attr wrappers that accepts bytes
Yuya Nishihara <yuya@tcha.org>
parents:
29809
diff
changeset
|
301 @functools.wraps(f) |
45fa8de47a0f
py3: provide (del|get|has|set)attr wrappers that accepts bytes
Yuya Nishihara <yuya@tcha.org>
parents:
29809
diff
changeset
|
302 def w(object, name, *args): |
30032
2219f4f82ede
pycompat: extract function that converts attribute or encoding name to str
Yuya Nishihara <yuya@tcha.org>
parents:
30031
diff
changeset
|
303 return f(object, sysstr(name), *args) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43021
diff
changeset
|
304 |
29810
45fa8de47a0f
py3: provide (del|get|has|set)attr wrappers that accepts bytes
Yuya Nishihara <yuya@tcha.org>
parents:
29809
diff
changeset
|
305 return w |
45fa8de47a0f
py3: provide (del|get|has|set)attr wrappers that accepts bytes
Yuya Nishihara <yuya@tcha.org>
parents:
29809
diff
changeset
|
306 |
29811
178c89e8519a
py3: import builtin wrappers automagically by code transformer
Yuya Nishihara <yuya@tcha.org>
parents:
29810
diff
changeset
|
307 # these wrappers are automagically imported by hgloader |
29810
45fa8de47a0f
py3: provide (del|get|has|set)attr wrappers that accepts bytes
Yuya Nishihara <yuya@tcha.org>
parents:
29809
diff
changeset
|
308 delattr = _wrapattrfunc(builtins.delattr) |
45fa8de47a0f
py3: provide (del|get|has|set)attr wrappers that accepts bytes
Yuya Nishihara <yuya@tcha.org>
parents:
29809
diff
changeset
|
309 getattr = _wrapattrfunc(builtins.getattr) |
45fa8de47a0f
py3: provide (del|get|has|set)attr wrappers that accepts bytes
Yuya Nishihara <yuya@tcha.org>
parents:
29809
diff
changeset
|
310 hasattr = _wrapattrfunc(builtins.hasattr) |
45fa8de47a0f
py3: provide (del|get|has|set)attr wrappers that accepts bytes
Yuya Nishihara <yuya@tcha.org>
parents:
29809
diff
changeset
|
311 setattr = _wrapattrfunc(builtins.setattr) |
29811
178c89e8519a
py3: import builtin wrappers automagically by code transformer
Yuya Nishihara <yuya@tcha.org>
parents:
29810
diff
changeset
|
312 xrange = builtins.range |
31843
526e4597cca5
py3: add pycompat.unicode and add it to importer
Pulkit Goyal <7895pulkit@gmail.com>
parents:
31842
diff
changeset
|
313 unicode = str |
29810
45fa8de47a0f
py3: provide (del|get|has|set)attr wrappers that accepts bytes
Yuya Nishihara <yuya@tcha.org>
parents:
29809
diff
changeset
|
314 |
39658
a407f9009392
py3: byteify strings in pycompat
Matt Harbison <matt_harbison@yahoo.com>
parents:
39473
diff
changeset
|
315 def open(name, mode=b'r', buffering=-1, encoding=None): |
36585
63fe5ca93b13
pycompat: add support for encoding argument to our wrapper
Augie Fackler <augie@google.com>
parents:
36367
diff
changeset
|
316 return builtins.open(name, sysstr(mode), buffering, encoding) |
31159
76a64c1e5439
py3: add pycompat.open and replace open() calls
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30820
diff
changeset
|
317 |
37102
6ca5f825a0ca
util: make safehasattr() a pycompat function
Yuya Nishihara <yuya@tcha.org>
parents:
37067
diff
changeset
|
318 safehasattr = _wrapattrfunc(builtins.hasattr) |
6ca5f825a0ca
util: make safehasattr() a pycompat function
Yuya Nishihara <yuya@tcha.org>
parents:
37067
diff
changeset
|
319 |
35235
5b569d512fbd
fancyopts: use getopt.gnu_getopt()
Yuya Nishihara <yuya@tcha.org>
parents:
34644
diff
changeset
|
320 def _getoptbwrapper(orig, args, shortlist, namelist): |
32882
f57f1f37290d
pycompat: move multiline comments above a function to function doc
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32878
diff
changeset
|
321 """ |
f57f1f37290d
pycompat: move multiline comments above a function to function doc
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32878
diff
changeset
|
322 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:
32878
diff
changeset
|
323 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:
32878
diff
changeset
|
324 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:
32878
diff
changeset
|
325 bytes on Python 3. |
f57f1f37290d
pycompat: move multiline comments above a function to function doc
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32878
diff
changeset
|
326 """ |
30583
c6ce11f2ee50
py3: make a bytes version of getopt.getopt()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30509
diff
changeset
|
327 args = [a.decode('latin-1') for a in args] |
c6ce11f2ee50
py3: make a bytes version of getopt.getopt()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30509
diff
changeset
|
328 shortlist = shortlist.decode('latin-1') |
c6ce11f2ee50
py3: make a bytes version of getopt.getopt()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30509
diff
changeset
|
329 namelist = [a.decode('latin-1') for a in namelist] |
35235
5b569d512fbd
fancyopts: use getopt.gnu_getopt()
Yuya Nishihara <yuya@tcha.org>
parents:
34644
diff
changeset
|
330 opts, args = orig(args, shortlist, namelist) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43021
diff
changeset
|
331 opts = [(a[0].encode('latin-1'), a[1].encode('latin-1')) for a in opts] |
30583
c6ce11f2ee50
py3: make a bytes version of getopt.getopt()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30509
diff
changeset
|
332 args = [a.encode('latin-1') for a in args] |
c6ce11f2ee50
py3: make a bytes version of getopt.getopt()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30509
diff
changeset
|
333 return opts, args |
c6ce11f2ee50
py3: make a bytes version of getopt.getopt()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30509
diff
changeset
|
334 |
30584
fbc3f73dc802
py3: utility functions to convert keys of kwargs to bytes/unicodes
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30583
diff
changeset
|
335 def strkwargs(dic): |
32882
f57f1f37290d
pycompat: move multiline comments above a function to function doc
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32878
diff
changeset
|
336 """ |
f57f1f37290d
pycompat: move multiline comments above a function to function doc
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32878
diff
changeset
|
337 Converts the keys of a python dictonary to str i.e. unicodes so that |
46178 | 338 they can be passed as keyword arguments as dictionaries with bytes keys |
32882
f57f1f37290d
pycompat: move multiline comments above a function to function doc
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32878
diff
changeset
|
339 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:
32878
diff
changeset
|
340 """ |
44470
9d2b2df2c2ba
cleanup: run pyupgrade on our source tree to clean up varying things
Augie Fackler <augie@google.com>
parents:
43876
diff
changeset
|
341 dic = {k.decode('latin-1'): v for k, v in dic.items()} |
30584
fbc3f73dc802
py3: utility functions to convert keys of kwargs to bytes/unicodes
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30583
diff
changeset
|
342 return dic |
fbc3f73dc802
py3: utility functions to convert keys of kwargs to bytes/unicodes
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30583
diff
changeset
|
343 |
fbc3f73dc802
py3: utility functions to convert keys of kwargs to bytes/unicodes
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30583
diff
changeset
|
344 def byteskwargs(dic): |
32882
f57f1f37290d
pycompat: move multiline comments above a function to function doc
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32878
diff
changeset
|
345 """ |
46178 | 346 Converts keys of python dictionaries to bytes as they were converted to |
32882
f57f1f37290d
pycompat: move multiline comments above a function to function doc
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32878
diff
changeset
|
347 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:
32878
diff
changeset
|
348 """ |
44470
9d2b2df2c2ba
cleanup: run pyupgrade on our source tree to clean up varying things
Augie Fackler <augie@google.com>
parents:
43876
diff
changeset
|
349 dic = {k.encode('latin-1'): v for k, v in dic.items()} |
30584
fbc3f73dc802
py3: utility functions to convert keys of kwargs to bytes/unicodes
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30583
diff
changeset
|
350 return dic |
fbc3f73dc802
py3: utility functions to convert keys of kwargs to bytes/unicodes
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30583
diff
changeset
|
351 |
30681
caf7e1c5efe4
py3: have a bytes version of shlex.split()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30671
diff
changeset
|
352 # TODO: handle shlex.shlex(). |
36367
4cd2d1cc2a31
pycompat: correct the shlex.split() proxy method signature in py3
Matt Harbison <matt_harbison@yahoo.com>
parents:
36298
diff
changeset
|
353 def shlexsplit(s, comments=False, posix=True): |
32882
f57f1f37290d
pycompat: move multiline comments above a function to function doc
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32878
diff
changeset
|
354 """ |
f57f1f37290d
pycompat: move multiline comments above a function to function doc
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32878
diff
changeset
|
355 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:
32878
diff
changeset
|
356 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:
32878
diff
changeset
|
357 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:
32878
diff
changeset
|
358 """ |
36367
4cd2d1cc2a31
pycompat: correct the shlex.split() proxy method signature in py3
Matt Harbison <matt_harbison@yahoo.com>
parents:
36298
diff
changeset
|
359 ret = shlex.split(s.decode('latin-1'), comments, posix) |
30681
caf7e1c5efe4
py3: have a bytes version of shlex.split()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30671
diff
changeset
|
360 return [a.encode('latin-1') for a in ret] |
caf7e1c5efe4
py3: have a bytes version of shlex.split()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30671
diff
changeset
|
361 |
43105
649d3ac37a12
py3: define and use pycompat.iteritems() for hgext/
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43104
diff
changeset
|
362 iteritems = lambda x: x.items() |
43104
74802979dd9d
py3: define and use pycompat.itervalues()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43103
diff
changeset
|
363 itervalues = lambda x: x.values() |
43021
2cc453284d5c
patchbomb: protect email addresses from shell
Floris Bruynooghe <flub@google.com>
parents:
40577
diff
changeset
|
364 |
43380
579672b347d2
py3: define and use json.loads polyfill
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43117
diff
changeset
|
365 # Python 3.5's json.load and json.loads require str. We polyfill its |
579672b347d2
py3: define and use json.loads polyfill
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43117
diff
changeset
|
366 # code for detecting encoding from bytes. |
579672b347d2
py3: define and use json.loads polyfill
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43117
diff
changeset
|
367 if sys.version_info[0:2] < (3, 6): |
579672b347d2
py3: define and use json.loads polyfill
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43117
diff
changeset
|
368 |
579672b347d2
py3: define and use json.loads polyfill
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43117
diff
changeset
|
369 def _detect_encoding(b): |
579672b347d2
py3: define and use json.loads polyfill
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43117
diff
changeset
|
370 bstartswith = b.startswith |
579672b347d2
py3: define and use json.loads polyfill
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43117
diff
changeset
|
371 if bstartswith((codecs.BOM_UTF32_BE, codecs.BOM_UTF32_LE)): |
579672b347d2
py3: define and use json.loads polyfill
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43117
diff
changeset
|
372 return 'utf-32' |
579672b347d2
py3: define and use json.loads polyfill
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43117
diff
changeset
|
373 if bstartswith((codecs.BOM_UTF16_BE, codecs.BOM_UTF16_LE)): |
579672b347d2
py3: define and use json.loads polyfill
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43117
diff
changeset
|
374 return 'utf-16' |
579672b347d2
py3: define and use json.loads polyfill
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43117
diff
changeset
|
375 if bstartswith(codecs.BOM_UTF8): |
579672b347d2
py3: define and use json.loads polyfill
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43117
diff
changeset
|
376 return 'utf-8-sig' |
579672b347d2
py3: define and use json.loads polyfill
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43117
diff
changeset
|
377 |
579672b347d2
py3: define and use json.loads polyfill
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43117
diff
changeset
|
378 if len(b) >= 4: |
579672b347d2
py3: define and use json.loads polyfill
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43117
diff
changeset
|
379 if not b[0]: |
579672b347d2
py3: define and use json.loads polyfill
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43117
diff
changeset
|
380 # 00 00 -- -- - utf-32-be |
579672b347d2
py3: define and use json.loads polyfill
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43117
diff
changeset
|
381 # 00 XX -- -- - utf-16-be |
579672b347d2
py3: define and use json.loads polyfill
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43117
diff
changeset
|
382 return 'utf-16-be' if b[1] else 'utf-32-be' |
579672b347d2
py3: define and use json.loads polyfill
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43117
diff
changeset
|
383 if not b[1]: |
579672b347d2
py3: define and use json.loads polyfill
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43117
diff
changeset
|
384 # XX 00 00 00 - utf-32-le |
579672b347d2
py3: define and use json.loads polyfill
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43117
diff
changeset
|
385 # XX 00 00 XX - utf-16-le |
579672b347d2
py3: define and use json.loads polyfill
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43117
diff
changeset
|
386 # XX 00 XX -- - utf-16-le |
579672b347d2
py3: define and use json.loads polyfill
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43117
diff
changeset
|
387 return 'utf-16-le' if b[2] or b[3] else 'utf-32-le' |
579672b347d2
py3: define and use json.loads polyfill
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43117
diff
changeset
|
388 elif len(b) == 2: |
579672b347d2
py3: define and use json.loads polyfill
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43117
diff
changeset
|
389 if not b[0]: |
579672b347d2
py3: define and use json.loads polyfill
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43117
diff
changeset
|
390 # 00 XX - utf-16-be |
579672b347d2
py3: define and use json.loads polyfill
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43117
diff
changeset
|
391 return 'utf-16-be' |
579672b347d2
py3: define and use json.loads polyfill
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43117
diff
changeset
|
392 if not b[1]: |
579672b347d2
py3: define and use json.loads polyfill
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43117
diff
changeset
|
393 # XX 00 - utf-16-le |
579672b347d2
py3: define and use json.loads polyfill
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43117
diff
changeset
|
394 return 'utf-16-le' |
579672b347d2
py3: define and use json.loads polyfill
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43117
diff
changeset
|
395 # default |
579672b347d2
py3: define and use json.loads polyfill
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43117
diff
changeset
|
396 return 'utf-8' |
579672b347d2
py3: define and use json.loads polyfill
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43117
diff
changeset
|
397 |
579672b347d2
py3: define and use json.loads polyfill
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43117
diff
changeset
|
398 def json_loads(s, *args, **kwargs): |
579672b347d2
py3: define and use json.loads polyfill
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43117
diff
changeset
|
399 if isinstance(s, (bytes, bytearray)): |
579672b347d2
py3: define and use json.loads polyfill
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43117
diff
changeset
|
400 s = s.decode(_detect_encoding(s), 'surrogatepass') |
579672b347d2
py3: define and use json.loads polyfill
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43117
diff
changeset
|
401 |
579672b347d2
py3: define and use json.loads polyfill
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43117
diff
changeset
|
402 return json.loads(s, *args, **kwargs) |
579672b347d2
py3: define and use json.loads polyfill
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43117
diff
changeset
|
403 |
579672b347d2
py3: define and use json.loads polyfill
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43117
diff
changeset
|
404 else: |
579672b347d2
py3: define and use json.loads polyfill
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43117
diff
changeset
|
405 json_loads = json.loads |
579672b347d2
py3: define and use json.loads polyfill
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43117
diff
changeset
|
406 |
30032
2219f4f82ede
pycompat: extract function that converts attribute or encoding name to str
Yuya Nishihara <yuya@tcha.org>
parents:
30031
diff
changeset
|
407 else: |
31381
06440ba06bc0
pycompat: move imports of cStringIO/io to where they are used
Yuya Nishihara <yuya@tcha.org>
parents:
31368
diff
changeset
|
408 import cStringIO |
06440ba06bc0
pycompat: move imports of cStringIO/io to where they are used
Yuya Nishihara <yuya@tcha.org>
parents:
31368
diff
changeset
|
409 |
38822
7eba8f83129b
pycompat: add xrange alias for Python 2
Gregory Szorc <gregory.szorc@gmail.com>
parents:
38576
diff
changeset
|
410 xrange = xrange |
38321
79dd61a4554f
py3: replace `unicode` with pycompat.unicode
Pulkit Goyal <7895pulkit@gmail.com>
parents:
38199
diff
changeset
|
411 unicode = unicode |
31263
64596338ba10
py3: factor out bytechr() function
Yuya Nishihara <yuya@tcha.org>
parents:
31159
diff
changeset
|
412 bytechr = chr |
36298
b44fac3a49fb
py3: factor out byterepr() which returns an asciified value on py3
Yuya Nishihara <yuya@tcha.org>
parents:
36216
diff
changeset
|
413 byterepr = repr |
31448
b70407bd84d5
pycompat: add bytestr wrapper which mostly acts as a Python 2 str
Yuya Nishihara <yuya@tcha.org>
parents:
31434
diff
changeset
|
414 bytestr = str |
31391
c9fd842dc886
pycompat: add helper to iterate each char in bytes
Yuya Nishihara <yuya@tcha.org>
parents:
31381
diff
changeset
|
415 iterbytestr = iter |
35944
fc44c2657dc5
py3: drop b'' from repr() of smartset
Yuya Nishihara <yuya@tcha.org>
parents:
35943
diff
changeset
|
416 maybebytestr = identity |
31820
45761ef1bc93
py3: have registrar process docstrings in bytes
Yuya Nishihara <yuya@tcha.org>
parents:
31778
diff
changeset
|
417 sysbytes = identity |
31777
7d2cbe11ae48
pycompat: introduce identity function as a compat stub
Yuya Nishihara <yuya@tcha.org>
parents:
31579
diff
changeset
|
418 sysstr = identity |
32877
a05f3675c46a
py3: add a new strurl() which will convert a bytes url to str
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32638
diff
changeset
|
419 strurl = identity |
32878
f22f39d56bb5
py3: add a new bytesurl() to convert a str url into bytes
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32877
diff
changeset
|
420 bytesurl = identity |
43085
eef9a2d67051
py3: manually import pycompat.open into files that need it
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43077
diff
changeset
|
421 open = open |
43090
1f339b503a40
py3: manually import pycompat.delattr where it is needed
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43089
diff
changeset
|
422 delattr = delattr |
43089
c59eb1560c44
py3: manually import getattr where it is needed
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43088
diff
changeset
|
423 getattr = getattr |
43088
0d612db7047c
py3: stop injecting pycompat.hasattr into modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43087
diff
changeset
|
424 hasattr = hasattr |
43087
66f2cc210a29
py3: manually import pycompat.setattr where it is needed
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43085
diff
changeset
|
425 setattr = setattr |
30032
2219f4f82ede
pycompat: extract function that converts attribute or encoding name to str
Yuya Nishihara <yuya@tcha.org>
parents:
30031
diff
changeset
|
426 |
32232
76f9a0009b4b
pycompat: extract helper to raise exception with traceback
Yuya Nishihara <yuya@tcha.org>
parents:
31942
diff
changeset
|
427 # this can't be parsed on Python 3 |
43117
8ff1ecfadcd1
cleanup: join string literals that are already on one line
Martin von Zweigbergk <martinvonz@google.com>
parents:
43106
diff
changeset
|
428 exec(b'def raisewithtb(exc, tb):\n raise exc, None, tb\n') |
32232
76f9a0009b4b
pycompat: extract helper to raise exception with traceback
Yuya Nishihara <yuya@tcha.org>
parents:
31942
diff
changeset
|
429 |
30133
f6dcda7505f9
pycompat: only accept a bytestring filepath in Python 2
Martijn Pieters <mjpieters@fb.com>
parents:
30119
diff
changeset
|
430 def fsencode(filename): |
32882
f57f1f37290d
pycompat: move multiline comments above a function to function doc
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32878
diff
changeset
|
431 """ |
f57f1f37290d
pycompat: move multiline comments above a function to function doc
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32878
diff
changeset
|
432 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:
32878
diff
changeset
|
433 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:
32878
diff
changeset
|
434 indicates a bug. |
f57f1f37290d
pycompat: move multiline comments above a function to function doc
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32878
diff
changeset
|
435 """ |
30133
f6dcda7505f9
pycompat: only accept a bytestring filepath in Python 2
Martijn Pieters <mjpieters@fb.com>
parents:
30119
diff
changeset
|
436 if isinstance(filename, str): |
f6dcda7505f9
pycompat: only accept a bytestring filepath in Python 2
Martijn Pieters <mjpieters@fb.com>
parents:
30119
diff
changeset
|
437 return filename |
30119
f4a5e0e86a7e
py3: add an os.fsencode backport to ease path handling
Martijn Pieters <mjpieters@fb.com>
parents:
30086
diff
changeset
|
438 else: |
43551
313e3a279828
cleanup: remove pointless r-prefixes on double-quoted strings
Augie Fackler <augie@google.com>
parents:
43489
diff
changeset
|
439 raise TypeError("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
|
440 |
30313
42af0590f4b9
py3: add os.fsdecode() as pycompat.fsdecode()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30133
diff
changeset
|
441 # 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
|
442 # better not to touch Python 2 part as it's already working fine. |
31777
7d2cbe11ae48
pycompat: introduce identity function as a compat stub
Yuya Nishihara <yuya@tcha.org>
parents:
31579
diff
changeset
|
443 fsdecode = identity |
30313
42af0590f4b9
py3: add os.fsdecode() as pycompat.fsdecode()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30133
diff
changeset
|
444 |
32638
c9318beb7c1a
py3: convert __doc__ back to bytes in help.py
Yuya Nishihara <yuya@tcha.org>
parents:
32488
diff
changeset
|
445 def getdoc(obj): |
c9318beb7c1a
py3: convert __doc__ back to bytes in help.py
Yuya Nishihara <yuya@tcha.org>
parents:
32488
diff
changeset
|
446 return getattr(obj, '__doc__', None) |
c9318beb7c1a
py3: convert __doc__ back to bytes in help.py
Yuya Nishihara <yuya@tcha.org>
parents:
32488
diff
changeset
|
447 |
37102
6ca5f825a0ca
util: make safehasattr() a pycompat function
Yuya Nishihara <yuya@tcha.org>
parents:
37067
diff
changeset
|
448 _notset = object() |
6ca5f825a0ca
util: make safehasattr() a pycompat function
Yuya Nishihara <yuya@tcha.org>
parents:
37067
diff
changeset
|
449 |
6ca5f825a0ca
util: make safehasattr() a pycompat function
Yuya Nishihara <yuya@tcha.org>
parents:
37067
diff
changeset
|
450 def safehasattr(thing, attr): |
6ca5f825a0ca
util: make safehasattr() a pycompat function
Yuya Nishihara <yuya@tcha.org>
parents:
37067
diff
changeset
|
451 return getattr(thing, attr, _notset) is not _notset |
6ca5f825a0ca
util: make safehasattr() a pycompat function
Yuya Nishihara <yuya@tcha.org>
parents:
37067
diff
changeset
|
452 |
35235
5b569d512fbd
fancyopts: use getopt.gnu_getopt()
Yuya Nishihara <yuya@tcha.org>
parents:
34644
diff
changeset
|
453 def _getoptbwrapper(orig, args, shortlist, namelist): |
5b569d512fbd
fancyopts: use getopt.gnu_getopt()
Yuya Nishihara <yuya@tcha.org>
parents:
34644
diff
changeset
|
454 return orig(args, shortlist, namelist) |
30583
c6ce11f2ee50
py3: make a bytes version of getopt.getopt()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30509
diff
changeset
|
455 |
31777
7d2cbe11ae48
pycompat: introduce identity function as a compat stub
Yuya Nishihara <yuya@tcha.org>
parents:
31579
diff
changeset
|
456 strkwargs = identity |
7d2cbe11ae48
pycompat: introduce identity function as a compat stub
Yuya Nishihara <yuya@tcha.org>
parents:
31579
diff
changeset
|
457 byteskwargs = identity |
30584
fbc3f73dc802
py3: utility functions to convert keys of kwargs to bytes/unicodes
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30583
diff
changeset
|
458 |
36677
6585ac350fd9
py3: make os.curdir a bytes
Yuya Nishihara <yuya@tcha.org>
parents:
36676
diff
changeset
|
459 oscurdir = os.curdir |
31778
8181f378b073
pycompat: provide bytes os.linesep
Yuya Nishihara <yuya@tcha.org>
parents:
31777
diff
changeset
|
460 oslinesep = os.linesep |
30315
3874ddba1ab4
py3: add a bytes version of os.name
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30313
diff
changeset
|
461 osname = os.name |
30316
ad40d307a9f0
py3: have pycompat.ospathsep and pycompat.ossep
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30315
diff
changeset
|
462 ospathsep = os.pathsep |
36676
052351e3e1cd
py3: make os.pardir a bytes
Yuya Nishihara <yuya@tcha.org>
parents:
36673
diff
changeset
|
463 ospardir = os.pardir |
30316
ad40d307a9f0
py3: have pycompat.ospathsep and pycompat.ossep
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30315
diff
changeset
|
464 ossep = os.sep |
30628
c6026c20a3ce
py3: have a bytes version of os.altsep
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30584
diff
changeset
|
465 osaltsep = os.altsep |
43810
765a9c299c44
py3: make a pycompat.osdevnull, use it in extdiff
Kyle Lippincott <spectral@google.com>
parents:
43793
diff
changeset
|
466 osdevnull = os.devnull |
39473
8d858fbf2759
cbor: teach the encoder to handle python `long` type for Windows
Matt Harbison <matt_harbison@yahoo.com>
parents:
39329
diff
changeset
|
467 long = long |
30888
86cd1f2cfff5
pycompat: verify sys.argv exists before forwarding it (issue5493)
Augie Fackler <augie@google.com>
parents:
30820
diff
changeset
|
468 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
|
469 sysargv = sys.argv |
30629
a82a6eee2613
py3: have a bytes version of sys.platform
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30628
diff
changeset
|
470 sysplatform = sys.platform |
30671
3fcaf0f660ce
py3: have bytes version of sys.executable
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30666
diff
changeset
|
471 sysexecutable = sys.executable |
30681
caf7e1c5efe4
py3: have a bytes version of shlex.split()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30671
diff
changeset
|
472 shlexsplit = shlex.split |
36964
644a02f6b34f
util: prefer "bytesio" to "stringio"
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36940
diff
changeset
|
473 bytesio = cStringIO.StringIO |
644a02f6b34f
util: prefer "bytesio" to "stringio"
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36940
diff
changeset
|
474 stringio = bytesio |
31508
a1e40ceee640
pycompat: add maplist alias for old map behavior
Augie Fackler <augie@google.com>
parents:
31448
diff
changeset
|
475 maplist = map |
37067
434e520adb8c
annotate: do not construct attr.s object per line while computing history
Yuya Nishihara <yuya@tcha.org>
parents:
36964
diff
changeset
|
476 rangelist = range |
35412
e66d6e938d2d
py3: introduce pycompat.ziplist as zip is a generator on Python 3
Pulkit Goyal <7895pulkit@gmail.com>
parents:
35235
diff
changeset
|
477 ziplist = zip |
33874
cfcfbe6c96f8
py3: select input or raw_input by pycompat
Yuya Nishihara <yuya@tcha.org>
parents:
33624
diff
changeset
|
478 rawinput = raw_input |
36216
646002338365
py3: introduce and use pycompat.getargspec
Augie Fackler <augie@google.com>
parents:
36083
diff
changeset
|
479 getargspec = inspect.getargspec |
43105
649d3ac37a12
py3: define and use pycompat.iteritems() for hgext/
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43104
diff
changeset
|
480 iteritems = lambda x: x.iteritems() |
43104
74802979dd9d
py3: define and use pycompat.itervalues()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43103
diff
changeset
|
481 itervalues = lambda x: x.itervalues() |
43380
579672b347d2
py3: define and use json.loads polyfill
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43117
diff
changeset
|
482 json_loads = json.loads |
34639
a568a46751b6
selectors2: do not use platform.system()
Jun Wu <quark@fb.com>
parents:
34468
diff
changeset
|
483 |
39658
a407f9009392
py3: byteify strings in pycompat
Matt Harbison <matt_harbison@yahoo.com>
parents:
39473
diff
changeset
|
484 isjython = sysplatform.startswith(b'java') |
34644
c0a6c19690ff
pycompat: define operating system constants
Jun Wu <quark@fb.com>
parents:
34639
diff
changeset
|
485 |
40577
1b49b84d5ed5
pycompat: adding Linux detection and fixing Mac
rdamazio@google.com
parents:
39823
diff
changeset
|
486 isdarwin = sysplatform.startswith(b'darwin') |
1b49b84d5ed5
pycompat: adding Linux detection and fixing Mac
rdamazio@google.com
parents:
39823
diff
changeset
|
487 islinux = sysplatform.startswith(b'linux') |
39658
a407f9009392
py3: byteify strings in pycompat
Matt Harbison <matt_harbison@yahoo.com>
parents:
39473
diff
changeset
|
488 isposix = osname == b'posix' |
a407f9009392
py3: byteify strings in pycompat
Matt Harbison <matt_harbison@yahoo.com>
parents:
39473
diff
changeset
|
489 iswindows = osname == b'nt' |
35235
5b569d512fbd
fancyopts: use getopt.gnu_getopt()
Yuya Nishihara <yuya@tcha.org>
parents:
34644
diff
changeset
|
490 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43021
diff
changeset
|
491 |
35235
5b569d512fbd
fancyopts: use getopt.gnu_getopt()
Yuya Nishihara <yuya@tcha.org>
parents:
34644
diff
changeset
|
492 def getoptb(args, shortlist, namelist): |
5b569d512fbd
fancyopts: use getopt.gnu_getopt()
Yuya Nishihara <yuya@tcha.org>
parents:
34644
diff
changeset
|
493 return _getoptbwrapper(getopt.getopt, args, shortlist, namelist) |
5b569d512fbd
fancyopts: use getopt.gnu_getopt()
Yuya Nishihara <yuya@tcha.org>
parents:
34644
diff
changeset
|
494 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43021
diff
changeset
|
495 |
35235
5b569d512fbd
fancyopts: use getopt.gnu_getopt()
Yuya Nishihara <yuya@tcha.org>
parents:
34644
diff
changeset
|
496 def gnugetoptb(args, shortlist, namelist): |
5b569d512fbd
fancyopts: use getopt.gnu_getopt()
Yuya Nishihara <yuya@tcha.org>
parents:
34644
diff
changeset
|
497 return _getoptbwrapper(getopt.gnu_getopt, args, shortlist, namelist) |
38197
aac4be30e250
py3: wrap tempfile.mkstemp() to use bytes path
Yuya Nishihara <yuya@tcha.org>
parents:
37890
diff
changeset
|
498 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43021
diff
changeset
|
499 |
38198
2ce60954b1b7
py3: wrap tempfile.mkdtemp() to use bytes path
Yuya Nishihara <yuya@tcha.org>
parents:
38197
diff
changeset
|
500 def mkdtemp(suffix=b'', prefix=b'tmp', dir=None): |
2ce60954b1b7
py3: wrap tempfile.mkdtemp() to use bytes path
Yuya Nishihara <yuya@tcha.org>
parents:
38197
diff
changeset
|
501 return tempfile.mkdtemp(suffix, prefix, dir) |
2ce60954b1b7
py3: wrap tempfile.mkdtemp() to use bytes path
Yuya Nishihara <yuya@tcha.org>
parents:
38197
diff
changeset
|
502 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43021
diff
changeset
|
503 |
38197
aac4be30e250
py3: wrap tempfile.mkstemp() to use bytes path
Yuya Nishihara <yuya@tcha.org>
parents:
37890
diff
changeset
|
504 # 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:
37890
diff
changeset
|
505 def mkstemp(suffix=b'', prefix=b'tmp', dir=None): |
aac4be30e250
py3: wrap tempfile.mkstemp() to use bytes path
Yuya Nishihara <yuya@tcha.org>
parents:
37890
diff
changeset
|
506 return tempfile.mkstemp(suffix, prefix, dir) |
38199
cc9aa88792fe
py3: wrap tempfile.NamedTemporaryFile() to return bytes fp.name
Yuya Nishihara <yuya@tcha.org>
parents:
38198
diff
changeset
|
507 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43021
diff
changeset
|
508 |
45727
d4c4391aa7f2
pycompat: add an entry for unnamedtmpfile
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
45726
diff
changeset
|
509 # TemporaryFile does not support an "encoding=" argument on python2. |
d4c4391aa7f2
pycompat: add an entry for unnamedtmpfile
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
45726
diff
changeset
|
510 # This wrapper file are always open in byte mode. |
d4c4391aa7f2
pycompat: add an entry for unnamedtmpfile
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
45726
diff
changeset
|
511 def unnamedtempfile(mode=None, *args, **kwargs): |
d4c4391aa7f2
pycompat: add an entry for unnamedtmpfile
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
45726
diff
changeset
|
512 if mode is None: |
46380
7a29d9002250
pycompat: fix a bytes vs str issue in `unnamedtempfile()`
Matt Harbison <matt_harbison@yahoo.com>
parents:
46178
diff
changeset
|
513 mode = 'w+b' |
45727
d4c4391aa7f2
pycompat: add an entry for unnamedtmpfile
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
45726
diff
changeset
|
514 else: |
d4c4391aa7f2
pycompat: add an entry for unnamedtmpfile
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
45726
diff
changeset
|
515 mode = sysstr(mode) |
d4c4391aa7f2
pycompat: add an entry for unnamedtmpfile
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
45726
diff
changeset
|
516 assert 'b' in mode |
d4c4391aa7f2
pycompat: add an entry for unnamedtmpfile
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
45726
diff
changeset
|
517 return tempfile.TemporaryFile(mode, *args, **kwargs) |
d4c4391aa7f2
pycompat: add an entry for unnamedtmpfile
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
45726
diff
changeset
|
518 |
d4c4391aa7f2
pycompat: add an entry for unnamedtmpfile
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
45726
diff
changeset
|
519 |
45726
d1072cba8aff
pycompat: update comment about unnamedtempfile
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
45057
diff
changeset
|
520 # NamedTemporaryFile does not support an "encoding=" argument on python2. |
d1072cba8aff
pycompat: update comment about unnamedtempfile
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
45057
diff
changeset
|
521 # This wrapper file are always open in byte mode. |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43021
diff
changeset
|
522 def namedtempfile( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43021
diff
changeset
|
523 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:
43021
diff
changeset
|
524 ): |
38199
cc9aa88792fe
py3: wrap tempfile.NamedTemporaryFile() to return bytes fp.name
Yuya Nishihara <yuya@tcha.org>
parents:
38198
diff
changeset
|
525 mode = sysstr(mode) |
43554
9f70512ae2cf
cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents:
43551
diff
changeset
|
526 assert 'b' in mode |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43021
diff
changeset
|
527 return tempfile.NamedTemporaryFile( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43021
diff
changeset
|
528 mode, bufsize, suffix=suffix, prefix=prefix, dir=dir, delete=delete |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43021
diff
changeset
|
529 ) |