Show More
@@ -1,527 +1,471 b'' | |||
|
1 | 1 | # pycompat.py - portability shim for python 3 |
|
2 | 2 | # |
|
3 | 3 | # This software may be used and distributed according to the terms of the |
|
4 | 4 | # GNU General Public License version 2 or any later version. |
|
5 | 5 | |
|
6 | 6 | """Mercurial portability shim for python 3. |
|
7 | 7 | |
|
8 | 8 | This contains aliases to hide python version-specific details from the core. |
|
9 | 9 | """ |
|
10 | 10 | |
|
11 | 11 | from __future__ import absolute_import |
|
12 | 12 | |
|
13 | 13 | import builtins |
|
14 | import codecs | |
|
14 | 15 | import concurrent.futures as futures |
|
16 | import functools | |
|
15 | 17 | import getopt |
|
16 | 18 | import http.client as httplib |
|
17 | 19 | import http.cookiejar as cookielib |
|
18 | 20 | import inspect |
|
21 | import io | |
|
19 | 22 | import json |
|
20 | 23 | import os |
|
21 | 24 | import pickle |
|
22 | 25 | import queue |
|
23 | 26 | import shlex |
|
24 | 27 | import socketserver |
|
28 | import struct | |
|
25 | 29 | import sys |
|
26 | 30 | import tempfile |
|
27 | 31 | import xmlrpc.client as xmlrpclib |
|
28 | 32 | |
|
33 | ||
|
29 | 34 | ispy3 = sys.version_info[0] >= 3 |
|
30 | 35 | ispypy = '__pypy__' in sys.builtin_module_names |
|
31 | 36 | TYPE_CHECKING = False |
|
32 | 37 | |
|
33 | 38 | if not globals(): # hide this from non-pytype users |
|
34 | 39 | import typing |
|
35 | 40 | |
|
36 | 41 | TYPE_CHECKING = typing.TYPE_CHECKING |
|
37 | 42 | |
|
38 | 43 | |
|
39 | 44 | def future_set_exception_info(f, exc_info): |
|
40 | 45 | f.set_exception(exc_info[0]) |
|
41 | 46 | |
|
42 | 47 | |
|
43 | 48 | FileNotFoundError = builtins.FileNotFoundError |
|
44 | 49 | |
|
45 | 50 | |
|
46 | 51 | def identity(a): |
|
47 | 52 | return a |
|
48 | 53 | |
|
49 | 54 | |
|
50 | 55 | def _rapply(f, xs): |
|
51 | 56 | if xs is None: |
|
52 | 57 | # assume None means non-value of optional data |
|
53 | 58 | return xs |
|
54 | 59 | if isinstance(xs, (list, set, tuple)): |
|
55 | 60 | return type(xs)(_rapply(f, x) for x in xs) |
|
56 | 61 | if isinstance(xs, dict): |
|
57 | 62 | return type(xs)((_rapply(f, k), _rapply(f, v)) for k, v in xs.items()) |
|
58 | 63 | return f(xs) |
|
59 | 64 | |
|
60 | 65 | |
|
61 | 66 | def rapply(f, xs): |
|
62 | 67 | """Apply function recursively to every item preserving the data structure |
|
63 | 68 | |
|
64 | 69 | >>> def f(x): |
|
65 | 70 | ... return 'f(%s)' % x |
|
66 | 71 | >>> rapply(f, None) is None |
|
67 | 72 | True |
|
68 | 73 | >>> rapply(f, 'a') |
|
69 | 74 | 'f(a)' |
|
70 | 75 | >>> rapply(f, {'a'}) == {'f(a)'} |
|
71 | 76 | True |
|
72 | 77 | >>> rapply(f, ['a', 'b', None, {'c': 'd'}, []]) |
|
73 | 78 | ['f(a)', 'f(b)', None, {'f(c)': 'f(d)'}, []] |
|
74 | 79 | |
|
75 | 80 | >>> xs = [object()] |
|
76 | 81 | >>> rapply(identity, xs) is xs |
|
77 | 82 | True |
|
78 | 83 | """ |
|
79 | 84 | if f is identity: |
|
80 | 85 | # fast path mainly for py2 |
|
81 | 86 | return xs |
|
82 | 87 | return _rapply(f, xs) |
|
83 | 88 | |
|
84 | 89 | |
|
85 | if ispy3: | |
|
86 | import builtins | |
|
87 | import codecs | |
|
88 | import functools | |
|
89 | import io | |
|
90 | import struct | |
|
91 | ||
|
92 | 90 |
|
|
93 | 91 |
|
|
94 | 92 |
|
|
95 | 93 |
|
|
96 | 94 |
|
|
97 | 95 |
|
|
98 | 96 | |
|
99 | 97 |
|
|
100 | 98 |
|
|
101 | 99 |
|
|
102 | 100 |
|
|
103 | 101 |
|
|
104 | 102 |
|
|
105 | 103 |
|
|
106 | 104 |
|
|
107 | 105 |
|
|
108 | 106 |
|
|
109 | 107 |
|
|
110 | 108 |
|
|
111 | 109 | |
|
112 | 110 |
|
|
113 | 111 |
|
|
114 | 112 |
|
|
115 | 113 |
|
|
116 | 114 |
|
|
117 | 115 |
|
|
118 | 116 |
|
|
119 | 117 | |
|
118 | ||
|
120 | 119 |
|
|
121 | 120 |
|
|
122 | 121 | |
|
122 | ||
|
123 | 123 |
|
|
124 | 124 |
|
|
125 | 125 | |
|
126 | ||
|
126 | 127 |
|
|
127 | 128 |
|
|
128 | 129 | |
|
130 | ||
|
129 | 131 |
|
|
130 | 132 |
|
|
131 | 133 | |
|
132 | 134 |
|
|
133 | 135 | |
|
134 | 136 |
|
|
135 | 137 |
|
|
136 | 138 |
|
|
137 | 139 |
|
|
138 | 140 |
|
|
139 | 141 |
|
|
140 | 142 |
|
|
141 | 143 |
|
|
142 | 144 |
|
|
143 | 145 |
|
|
144 | 146 |
|
|
145 | 147 |
|
|
146 | 148 |
|
|
147 | 149 |
|
|
148 | 150 |
|
|
149 | 151 |
|
|
150 | 152 |
|
|
151 | 153 |
|
|
152 | 154 |
|
|
153 | 155 | |
|
154 | 156 |
|
|
155 | 157 |
|
|
156 | 158 | |
|
159 | ||
|
157 | 160 |
|
|
158 | 161 |
|
|
159 | 162 | |
|
160 | 163 |
|
|
161 | 164 |
|
|
162 | 165 |
|
|
163 | 166 |
|
|
164 | 167 | |
|
165 | 168 |
|
|
166 | 169 | |
|
167 | 170 |
|
|
168 | 171 |
|
|
169 | 172 |
|
|
170 | 173 |
|
|
171 | 174 |
|
|
172 | 175 | |
|
173 | 176 |
|
|
174 | 177 |
|
|
175 | 178 | |
|
176 | 179 |
|
|
177 | 180 |
|
|
178 | 181 |
|
|
179 | 182 |
|
|
180 | 183 | |
|
181 | 184 |
|
|
182 | 185 | |
|
183 | 186 |
|
|
184 | 187 |
|
|
185 | 188 |
|
|
186 | 189 |
|
|
187 | 190 | |
|
188 | 191 |
|
|
189 | 192 | |
|
190 | 193 |
|
|
191 | 194 |
|
|
192 | 195 |
|
|
193 | 196 |
|
|
194 | 197 | |
|
195 | 198 |
|
|
196 | 199 |
|
|
197 | 200 | |
|
198 | 201 |
|
|
199 | 202 |
|
|
200 | 203 |
|
|
201 | 204 |
|
|
202 | 205 | |
|
203 | 206 |
|
|
204 | 207 |
|
|
205 | 208 | |
|
206 | 209 |
|
|
207 | 210 |
|
|
208 | 211 |
|
|
209 | 212 | |
|
210 | 213 |
|
|
211 | 214 |
|
|
212 | 215 |
|
|
213 | 216 |
|
|
214 | 217 |
|
|
215 | 218 | |
|
216 | 219 |
|
|
217 | 220 |
|
|
218 | 221 | |
|
219 | 222 |
|
|
220 | 223 |
|
|
221 | 224 |
|
|
222 | 225 |
|
|
223 | 226 |
|
|
224 | 227 |
|
|
225 | 228 |
|
|
226 | 229 |
|
|
227 | 230 |
|
|
228 | 231 |
|
|
229 | 232 | |
|
230 | 233 |
|
|
231 | 234 |
|
|
232 | 235 |
|
|
233 | 236 |
|
|
234 | 237 |
|
|
235 | 238 | |
|
236 | 239 |
|
|
237 | 240 |
|
|
238 | 241 | |
|
239 | 242 |
|
|
240 | 243 |
|
|
241 | 244 | |
|
245 | ||
|
242 | 246 |
|
|
243 | 247 |
|
|
244 | 248 |
|
|
245 | 249 | |
|
250 | ||
|
246 | 251 |
|
|
247 | 252 |
|
|
248 | 253 |
|
|
249 | 254 |
|
|
250 | 255 |
|
|
251 | 256 | |
|
257 | ||
|
252 | 258 |
|
|
253 | 259 |
|
|
254 | 260 | |
|
255 | 261 |
|
|
256 | 262 |
|
|
257 | 263 |
|
|
258 | 264 |
|
|
259 | 265 |
|
|
260 | 266 |
|
|
261 | 267 | |
|
268 | ||
|
262 | 269 |
|
|
263 | 270 |
|
|
264 | 271 |
|
|
265 | 272 | |
|
266 | 273 |
|
|
267 | 274 |
|
|
268 | 275 |
|
|
269 | 276 |
|
|
270 | 277 |
|
|
271 | 278 |
|
|
272 | 279 |
|
|
273 | 280 | |
|
281 | ||
|
274 | 282 |
|
|
275 | 283 |
|
|
276 | 284 |
|
|
277 | 285 |
|
|
278 | 286 |
|
|
279 | 287 | |
|
288 | ||
|
280 | 289 |
|
|
281 | 290 |
|
|
282 | 291 |
|
|
283 | 292 |
|
|
284 | 293 |
|
|
285 | 294 | |
|
295 | ||
|
286 | 296 |
|
|
287 | 297 |
|
|
288 | 298 |
|
|
289 | 299 | |
|
300 | ||
|
290 | 301 |
|
|
291 | 302 |
|
|
292 | 303 |
|
|
293 | 304 |
|
|
294 | 305 |
|
|
295 | 306 |
|
|
296 | 307 |
|
|
297 | 308 | |
|
309 | ||
|
298 | 310 |
|
|
299 | 311 |
|
|
300 | 312 |
|
|
301 | 313 |
|
|
302 | 314 | |
|
303 | 315 |
|
|
304 | 316 | |
|
317 | ||
|
305 | 318 |
|
|
306 | 319 |
|
|
307 | 320 |
|
|
308 | 321 |
|
|
309 | 322 |
|
|
310 | 323 |
|
|
311 | 324 |
|
|
312 | 325 | |
|
326 | ||
|
313 | 327 |
|
|
314 | 328 |
|
|
315 | 329 | |
|
330 | ||
|
316 | 331 |
|
|
317 | 332 | |
|
333 | ||
|
318 | 334 |
|
|
319 | 335 |
|
|
320 | 336 |
|
|
321 | 337 |
|
|
322 | 338 |
|
|
323 | 339 |
|
|
324 | 340 |
|
|
325 | 341 |
|
|
326 | 342 |
|
|
327 | 343 |
|
|
328 | 344 |
|
|
329 | 345 |
|
|
330 | 346 |
|
|
331 | 347 |
|
|
332 | 348 | |
|
349 | ||
|
333 | 350 |
|
|
334 | 351 |
|
|
335 | 352 |
|
|
336 | 353 |
|
|
337 | 354 |
|
|
338 | 355 |
|
|
339 | 356 |
|
|
340 | 357 |
|
|
341 | 358 | |
|
359 | ||
|
342 | 360 |
|
|
343 | 361 |
|
|
344 | 362 |
|
|
345 | 363 |
|
|
346 | 364 |
|
|
347 | 365 |
|
|
348 | 366 |
|
|
349 | 367 | |
|
368 | ||
|
350 | 369 |
|
|
351 | 370 |
|
|
352 | 371 |
|
|
353 | 372 |
|
|
354 | 373 |
|
|
355 | 374 |
|
|
356 | 375 |
|
|
357 | 376 |
|
|
358 | 377 |
|
|
359 | 378 | |
|
379 | ||
|
360 | 380 |
|
|
361 | 381 |
|
|
362 | 382 | |
|
363 | 383 |
|
|
364 | 384 |
|
|
365 | 385 |
|
|
366 | 386 | |
|
367 | 387 |
|
|
368 | 388 |
|
|
369 | 389 |
|
|
370 | 390 |
|
|
371 | 391 |
|
|
372 | 392 |
|
|
373 | 393 |
|
|
374 | 394 |
|
|
375 | 395 | |
|
376 | 396 |
|
|
377 | 397 |
|
|
378 | 398 |
|
|
379 | 399 |
|
|
380 | 400 |
|
|
381 | 401 |
|
|
382 | 402 |
|
|
383 | 403 |
|
|
384 | 404 |
|
|
385 | 405 |
|
|
386 | 406 |
|
|
387 | 407 |
|
|
388 | 408 |
|
|
389 | 409 |
|
|
390 | 410 |
|
|
391 | 411 |
|
|
392 | 412 |
|
|
393 | 413 |
|
|
394 | 414 |
|
|
395 | 415 | |
|
396 | 416 |
|
|
397 | 417 |
|
|
398 | 418 |
|
|
399 | 419 | |
|
400 | 420 |
|
|
401 | 421 | |
|
402 | else: | |
|
403 | json_loads = json.loads | |
|
404 | 422 | |
|
405 | 423 | else: |
|
406 | import cStringIO | |
|
407 | ||
|
408 | xrange = xrange | |
|
409 | unicode = unicode | |
|
410 | bytechr = chr | |
|
411 | byterepr = repr | |
|
412 | bytestr = str | |
|
413 | iterbytestr = iter | |
|
414 | maybebytestr = identity | |
|
415 | sysbytes = identity | |
|
416 | sysstr = identity | |
|
417 | strurl = identity | |
|
418 | bytesurl = identity | |
|
419 | open = open | |
|
420 | delattr = delattr | |
|
421 | getattr = getattr | |
|
422 | hasattr = hasattr | |
|
423 | setattr = setattr | |
|
424 | ||
|
425 | # this can't be parsed on Python 3 | |
|
426 | exec(b'def raisewithtb(exc, tb):\n raise exc, None, tb\n') | |
|
427 | ||
|
428 | def fsencode(filename): | |
|
429 | """ | |
|
430 | Partial backport from os.py in Python 3, which only accepts bytes. | |
|
431 | In Python 2, our paths should only ever be bytes, a unicode path | |
|
432 | indicates a bug. | |
|
433 | """ | |
|
434 | if isinstance(filename, str): | |
|
435 | return filename | |
|
436 | else: | |
|
437 | raise TypeError("expect str, not %s" % type(filename).__name__) | |
|
438 | ||
|
439 | # In Python 2, fsdecode() has a very chance to receive bytes. So it's | |
|
440 | # better not to touch Python 2 part as it's already working fine. | |
|
441 | fsdecode = identity | |
|
442 | ||
|
443 | def getdoc(obj): | |
|
444 | return getattr(obj, '__doc__', None) | |
|
445 | ||
|
446 | _notset = object() | |
|
447 | ||
|
448 | def safehasattr(thing, attr): | |
|
449 | return getattr(thing, attr, _notset) is not _notset | |
|
450 | ||
|
451 | def _getoptbwrapper(orig, args, shortlist, namelist): | |
|
452 | return orig(args, shortlist, namelist) | |
|
453 | ||
|
454 | strkwargs = identity | |
|
455 | byteskwargs = identity | |
|
456 | ||
|
457 | oscurdir = os.curdir | |
|
458 | oslinesep = os.linesep | |
|
459 | osname = os.name | |
|
460 | ospathsep = os.pathsep | |
|
461 | ospardir = os.pardir | |
|
462 | ossep = os.sep | |
|
463 | osaltsep = os.altsep | |
|
464 | osdevnull = os.devnull | |
|
465 | long = long | |
|
466 | if getattr(sys, 'argv', None) is not None: | |
|
467 | sysargv = sys.argv | |
|
468 | sysplatform = sys.platform | |
|
469 | sysexecutable = sys.executable | |
|
470 | shlexsplit = shlex.split | |
|
471 | bytesio = cStringIO.StringIO | |
|
472 | stringio = bytesio | |
|
473 | maplist = map | |
|
474 | rangelist = range | |
|
475 | ziplist = zip | |
|
476 | rawinput = raw_input | |
|
477 | getargspec = inspect.getargspec | |
|
478 | iteritems = lambda x: x.iteritems() | |
|
479 | itervalues = lambda x: x.itervalues() | |
|
480 | 424 | json_loads = json.loads |
|
481 | 425 | |
|
482 | 426 | isjython = sysplatform.startswith(b'java') |
|
483 | 427 | |
|
484 | 428 | isdarwin = sysplatform.startswith(b'darwin') |
|
485 | 429 | islinux = sysplatform.startswith(b'linux') |
|
486 | 430 | isposix = osname == b'posix' |
|
487 | 431 | iswindows = osname == b'nt' |
|
488 | 432 | |
|
489 | 433 | |
|
490 | 434 | def getoptb(args, shortlist, namelist): |
|
491 | 435 | return _getoptbwrapper(getopt.getopt, args, shortlist, namelist) |
|
492 | 436 | |
|
493 | 437 | |
|
494 | 438 | def gnugetoptb(args, shortlist, namelist): |
|
495 | 439 | return _getoptbwrapper(getopt.gnu_getopt, args, shortlist, namelist) |
|
496 | 440 | |
|
497 | 441 | |
|
498 | 442 | def mkdtemp(suffix=b'', prefix=b'tmp', dir=None): |
|
499 | 443 | return tempfile.mkdtemp(suffix, prefix, dir) |
|
500 | 444 | |
|
501 | 445 | |
|
502 | 446 | # text=True is not supported; use util.from/tonativeeol() instead |
|
503 | 447 | def mkstemp(suffix=b'', prefix=b'tmp', dir=None): |
|
504 | 448 | return tempfile.mkstemp(suffix, prefix, dir) |
|
505 | 449 | |
|
506 | 450 | |
|
507 | 451 | # TemporaryFile does not support an "encoding=" argument on python2. |
|
508 | 452 | # This wrapper file are always open in byte mode. |
|
509 | 453 | def unnamedtempfile(mode=None, *args, **kwargs): |
|
510 | 454 | if mode is None: |
|
511 | 455 | mode = 'w+b' |
|
512 | 456 | else: |
|
513 | 457 | mode = sysstr(mode) |
|
514 | 458 | assert 'b' in mode |
|
515 | 459 | return tempfile.TemporaryFile(mode, *args, **kwargs) |
|
516 | 460 | |
|
517 | 461 | |
|
518 | 462 | # NamedTemporaryFile does not support an "encoding=" argument on python2. |
|
519 | 463 | # This wrapper file are always open in byte mode. |
|
520 | 464 | def namedtempfile( |
|
521 | 465 | mode=b'w+b', bufsize=-1, suffix=b'', prefix=b'tmp', dir=None, delete=True |
|
522 | 466 | ): |
|
523 | 467 | mode = sysstr(mode) |
|
524 | 468 | assert 'b' in mode |
|
525 | 469 | return tempfile.NamedTemporaryFile( |
|
526 | 470 | mode, bufsize, suffix=suffix, prefix=prefix, dir=dir, delete=delete |
|
527 | 471 | ) |
General Comments 0
You need to be logged in to leave comments.
Login now