_compat.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  1. import re
  2. import io
  3. import os
  4. import sys
  5. import codecs
  6. from weakref import WeakKeyDictionary
  7. PY2 = sys.version_info[0] == 2
  8. CYGWIN = sys.platform.startswith('cygwin')
  9. # Determine local App Engine environment, per Google's own suggestion
  10. APP_ENGINE = ('APPENGINE_RUNTIME' in os.environ and
  11. 'Development/' in os.environ['SERVER_SOFTWARE'])
  12. WIN = sys.platform.startswith('win') and not APP_ENGINE
  13. DEFAULT_COLUMNS = 80
  14. _ansi_re = re.compile(r'\033\[((?:\d|;)*)([a-zA-Z])')
  15. def get_filesystem_encoding():
  16. return sys.getfilesystemencoding() or sys.getdefaultencoding()
  17. def _make_text_stream(stream, encoding, errors,
  18. force_readable=False, force_writable=False):
  19. if encoding is None:
  20. encoding = get_best_encoding(stream)
  21. if errors is None:
  22. errors = 'replace'
  23. return _NonClosingTextIOWrapper(stream, encoding, errors,
  24. line_buffering=True,
  25. force_readable=force_readable,
  26. force_writable=force_writable)
  27. def is_ascii_encoding(encoding):
  28. """Checks if a given encoding is ascii."""
  29. try:
  30. return codecs.lookup(encoding).name == 'ascii'
  31. except LookupError:
  32. return False
  33. def get_best_encoding(stream):
  34. """Returns the default stream encoding if not found."""
  35. rv = getattr(stream, 'encoding', None) or sys.getdefaultencoding()
  36. if is_ascii_encoding(rv):
  37. return 'utf-8'
  38. return rv
  39. class _NonClosingTextIOWrapper(io.TextIOWrapper):
  40. def __init__(self, stream, encoding, errors,
  41. force_readable=False, force_writable=False, **extra):
  42. self._stream = stream = _FixupStream(stream, force_readable,
  43. force_writable)
  44. io.TextIOWrapper.__init__(self, stream, encoding, errors, **extra)
  45. # The io module is a place where the Python 3 text behavior
  46. # was forced upon Python 2, so we need to unbreak
  47. # it to look like Python 2.
  48. if PY2:
  49. def write(self, x):
  50. if isinstance(x, str) or is_bytes(x):
  51. try:
  52. self.flush()
  53. except Exception:
  54. pass
  55. return self.buffer.write(str(x))
  56. return io.TextIOWrapper.write(self, x)
  57. def writelines(self, lines):
  58. for line in lines:
  59. self.write(line)
  60. def __del__(self):
  61. try:
  62. self.detach()
  63. except Exception:
  64. pass
  65. def isatty(self):
  66. # https://bitbucket.org/pypy/pypy/issue/1803
  67. return self._stream.isatty()
  68. class _FixupStream(object):
  69. """The new io interface needs more from streams than streams
  70. traditionally implement. As such, this fix-up code is necessary in
  71. some circumstances.
  72. The forcing of readable and writable flags are there because some tools
  73. put badly patched objects on sys (one such offender are certain version
  74. of jupyter notebook).
  75. """
  76. def __init__(self, stream, force_readable=False, force_writable=False):
  77. self._stream = stream
  78. self._force_readable = force_readable
  79. self._force_writable = force_writable
  80. def __getattr__(self, name):
  81. return getattr(self._stream, name)
  82. def read1(self, size):
  83. f = getattr(self._stream, 'read1', None)
  84. if f is not None:
  85. return f(size)
  86. # We only dispatch to readline instead of read in Python 2 as we
  87. # do not want cause problems with the different implementation
  88. # of line buffering.
  89. if PY2:
  90. return self._stream.readline(size)
  91. return self._stream.read(size)
  92. def readable(self):
  93. if self._force_readable:
  94. return True
  95. x = getattr(self._stream, 'readable', None)
  96. if x is not None:
  97. return x()
  98. try:
  99. self._stream.read(0)
  100. except Exception:
  101. return False
  102. return True
  103. def writable(self):
  104. if self._force_writable:
  105. return True
  106. x = getattr(self._stream, 'writable', None)
  107. if x is not None:
  108. return x()
  109. try:
  110. self._stream.write('')
  111. except Exception:
  112. try:
  113. self._stream.write(b'')
  114. except Exception:
  115. return False
  116. return True
  117. def seekable(self):
  118. x = getattr(self._stream, 'seekable', None)
  119. if x is not None:
  120. return x()
  121. try:
  122. self._stream.seek(self._stream.tell())
  123. except Exception:
  124. return False
  125. return True
  126. if PY2:
  127. text_type = unicode
  128. bytes = str
  129. raw_input = raw_input
  130. string_types = (str, unicode)
  131. int_types = (int, long)
  132. iteritems = lambda x: x.iteritems()
  133. range_type = xrange
  134. def is_bytes(x):
  135. return isinstance(x, (buffer, bytearray))
  136. _identifier_re = re.compile(r'^[a-zA-Z_][a-zA-Z0-9_]*$')
  137. # For Windows, we need to force stdout/stdin/stderr to binary if it's
  138. # fetched for that. This obviously is not the most correct way to do
  139. # it as it changes global state. Unfortunately, there does not seem to
  140. # be a clear better way to do it as just reopening the file in binary
  141. # mode does not change anything.
  142. #
  143. # An option would be to do what Python 3 does and to open the file as
  144. # binary only, patch it back to the system, and then use a wrapper
  145. # stream that converts newlines. It's not quite clear what's the
  146. # correct option here.
  147. #
  148. # This code also lives in _winconsole for the fallback to the console
  149. # emulation stream.
  150. #
  151. # There are also Windows environments where the `msvcrt` module is not
  152. # available (which is why we use try-catch instead of the WIN variable
  153. # here), such as the Google App Engine development server on Windows. In
  154. # those cases there is just nothing we can do.
  155. def set_binary_mode(f):
  156. return f
  157. try:
  158. import msvcrt
  159. except ImportError:
  160. pass
  161. else:
  162. def set_binary_mode(f):
  163. try:
  164. fileno = f.fileno()
  165. except Exception:
  166. pass
  167. else:
  168. msvcrt.setmode(fileno, os.O_BINARY)
  169. return f
  170. try:
  171. import fcntl
  172. except ImportError:
  173. pass
  174. else:
  175. def set_binary_mode(f):
  176. try:
  177. fileno = f.fileno()
  178. except Exception:
  179. pass
  180. else:
  181. flags = fcntl.fcntl(fileno, fcntl.F_GETFL)
  182. fcntl.fcntl(fileno, fcntl.F_SETFL, flags & ~os.O_NONBLOCK)
  183. return f
  184. def isidentifier(x):
  185. return _identifier_re.search(x) is not None
  186. def get_binary_stdin():
  187. return set_binary_mode(sys.stdin)
  188. def get_binary_stdout():
  189. _wrap_std_stream('stdout')
  190. return set_binary_mode(sys.stdout)
  191. def get_binary_stderr():
  192. _wrap_std_stream('stderr')
  193. return set_binary_mode(sys.stderr)
  194. def get_text_stdin(encoding=None, errors=None):
  195. rv = _get_windows_console_stream(sys.stdin, encoding, errors)
  196. if rv is not None:
  197. return rv
  198. return _make_text_stream(sys.stdin, encoding, errors,
  199. force_readable=True)
  200. def get_text_stdout(encoding=None, errors=None):
  201. _wrap_std_stream('stdout')
  202. rv = _get_windows_console_stream(sys.stdout, encoding, errors)
  203. if rv is not None:
  204. return rv
  205. return _make_text_stream(sys.stdout, encoding, errors,
  206. force_writable=True)
  207. def get_text_stderr(encoding=None, errors=None):
  208. _wrap_std_stream('stderr')
  209. rv = _get_windows_console_stream(sys.stderr, encoding, errors)
  210. if rv is not None:
  211. return rv
  212. return _make_text_stream(sys.stderr, encoding, errors,
  213. force_writable=True)
  214. def filename_to_ui(value):
  215. if isinstance(value, bytes):
  216. value = value.decode(get_filesystem_encoding(), 'replace')
  217. return value
  218. else:
  219. import io
  220. text_type = str
  221. raw_input = input
  222. string_types = (str,)
  223. int_types = (int,)
  224. range_type = range
  225. isidentifier = lambda x: x.isidentifier()
  226. iteritems = lambda x: iter(x.items())
  227. def is_bytes(x):
  228. return isinstance(x, (bytes, memoryview, bytearray))
  229. def _is_binary_reader(stream, default=False):
  230. try:
  231. return isinstance(stream.read(0), bytes)
  232. except Exception:
  233. return default
  234. # This happens in some cases where the stream was already
  235. # closed. In this case, we assume the default.
  236. def _is_binary_writer(stream, default=False):
  237. try:
  238. stream.write(b'')
  239. except Exception:
  240. try:
  241. stream.write('')
  242. return False
  243. except Exception:
  244. pass
  245. return default
  246. return True
  247. def _find_binary_reader(stream):
  248. # We need to figure out if the given stream is already binary.
  249. # This can happen because the official docs recommend detaching
  250. # the streams to get binary streams. Some code might do this, so
  251. # we need to deal with this case explicitly.
  252. if _is_binary_reader(stream, False):
  253. return stream
  254. buf = getattr(stream, 'buffer', None)
  255. # Same situation here; this time we assume that the buffer is
  256. # actually binary in case it's closed.
  257. if buf is not None and _is_binary_reader(buf, True):
  258. return buf
  259. def _find_binary_writer(stream):
  260. # We need to figure out if the given stream is already binary.
  261. # This can happen because the official docs recommend detatching
  262. # the streams to get binary streams. Some code might do this, so
  263. # we need to deal with this case explicitly.
  264. if _is_binary_writer(stream, False):
  265. return stream
  266. buf = getattr(stream, 'buffer', None)
  267. # Same situation here; this time we assume that the buffer is
  268. # actually binary in case it's closed.
  269. if buf is not None and _is_binary_writer(buf, True):
  270. return buf
  271. def _stream_is_misconfigured(stream):
  272. """A stream is misconfigured if its encoding is ASCII."""
  273. # If the stream does not have an encoding set, we assume it's set
  274. # to ASCII. This appears to happen in certain unittest
  275. # environments. It's not quite clear what the correct behavior is
  276. # but this at least will force Click to recover somehow.
  277. return is_ascii_encoding(getattr(stream, 'encoding', None) or 'ascii')
  278. def _is_compatible_text_stream(stream, encoding, errors):
  279. stream_encoding = getattr(stream, 'encoding', None)
  280. stream_errors = getattr(stream, 'errors', None)
  281. # Perfect match.
  282. if stream_encoding == encoding and stream_errors == errors:
  283. return True
  284. # Otherwise, it's only a compatible stream if we did not ask for
  285. # an encoding.
  286. if encoding is None:
  287. return stream_encoding is not None
  288. return False
  289. def _force_correct_text_reader(text_reader, encoding, errors,
  290. force_readable=False):
  291. if _is_binary_reader(text_reader, False):
  292. binary_reader = text_reader
  293. else:
  294. # If there is no target encoding set, we need to verify that the
  295. # reader is not actually misconfigured.
  296. if encoding is None and not _stream_is_misconfigured(text_reader):
  297. return text_reader
  298. if _is_compatible_text_stream(text_reader, encoding, errors):
  299. return text_reader
  300. # If the reader has no encoding, we try to find the underlying
  301. # binary reader for it. If that fails because the environment is
  302. # misconfigured, we silently go with the same reader because this
  303. # is too common to happen. In that case, mojibake is better than
  304. # exceptions.
  305. binary_reader = _find_binary_reader(text_reader)
  306. if binary_reader is None:
  307. return text_reader
  308. # At this point, we default the errors to replace instead of strict
  309. # because nobody handles those errors anyways and at this point
  310. # we're so fundamentally fucked that nothing can repair it.
  311. if errors is None:
  312. errors = 'replace'
  313. return _make_text_stream(binary_reader, encoding, errors,
  314. force_readable=force_readable)
  315. def _force_correct_text_writer(text_writer, encoding, errors,
  316. force_writable=False):
  317. if _is_binary_writer(text_writer, False):
  318. binary_writer = text_writer
  319. else:
  320. # If there is no target encoding set, we need to verify that the
  321. # writer is not actually misconfigured.
  322. if encoding is None and not _stream_is_misconfigured(text_writer):
  323. return text_writer
  324. if _is_compatible_text_stream(text_writer, encoding, errors):
  325. return text_writer
  326. # If the writer has no encoding, we try to find the underlying
  327. # binary writer for it. If that fails because the environment is
  328. # misconfigured, we silently go with the same writer because this
  329. # is too common to happen. In that case, mojibake is better than
  330. # exceptions.
  331. binary_writer = _find_binary_writer(text_writer)
  332. if binary_writer is None:
  333. return text_writer
  334. # At this point, we default the errors to replace instead of strict
  335. # because nobody handles those errors anyways and at this point
  336. # we're so fundamentally fucked that nothing can repair it.
  337. if errors is None:
  338. errors = 'replace'
  339. return _make_text_stream(binary_writer, encoding, errors,
  340. force_writable=force_writable)
  341. def get_binary_stdin():
  342. reader = _find_binary_reader(sys.stdin)
  343. if reader is None:
  344. raise RuntimeError('Was not able to determine binary '
  345. 'stream for sys.stdin.')
  346. return reader
  347. def get_binary_stdout():
  348. writer = _find_binary_writer(sys.stdout)
  349. if writer is None:
  350. raise RuntimeError('Was not able to determine binary '
  351. 'stream for sys.stdout.')
  352. return writer
  353. def get_binary_stderr():
  354. writer = _find_binary_writer(sys.stderr)
  355. if writer is None:
  356. raise RuntimeError('Was not able to determine binary '
  357. 'stream for sys.stderr.')
  358. return writer
  359. def get_text_stdin(encoding=None, errors=None):
  360. rv = _get_windows_console_stream(sys.stdin, encoding, errors)
  361. if rv is not None:
  362. return rv
  363. return _force_correct_text_reader(sys.stdin, encoding, errors,
  364. force_readable=True)
  365. def get_text_stdout(encoding=None, errors=None):
  366. rv = _get_windows_console_stream(sys.stdout, encoding, errors)
  367. if rv is not None:
  368. return rv
  369. return _force_correct_text_writer(sys.stdout, encoding, errors,
  370. force_writable=True)
  371. def get_text_stderr(encoding=None, errors=None):
  372. rv = _get_windows_console_stream(sys.stderr, encoding, errors)
  373. if rv is not None:
  374. return rv
  375. return _force_correct_text_writer(sys.stderr, encoding, errors,
  376. force_writable=True)
  377. def filename_to_ui(value):
  378. if isinstance(value, bytes):
  379. value = value.decode(get_filesystem_encoding(), 'replace')
  380. else:
  381. value = value.encode('utf-8', 'surrogateescape') \
  382. .decode('utf-8', 'replace')
  383. return value
  384. def get_streerror(e, default=None):
  385. if hasattr(e, 'strerror'):
  386. msg = e.strerror
  387. else:
  388. if default is not None:
  389. msg = default
  390. else:
  391. msg = str(e)
  392. if isinstance(msg, bytes):
  393. msg = msg.decode('utf-8', 'replace')
  394. return msg
  395. def open_stream(filename, mode='r', encoding=None, errors='strict',
  396. atomic=False):
  397. # Standard streams first. These are simple because they don't need
  398. # special handling for the atomic flag. It's entirely ignored.
  399. if filename == '-':
  400. if any(m in mode for m in ['w', 'a', 'x']):
  401. if 'b' in mode:
  402. return get_binary_stdout(), False
  403. return get_text_stdout(encoding=encoding, errors=errors), False
  404. if 'b' in mode:
  405. return get_binary_stdin(), False
  406. return get_text_stdin(encoding=encoding, errors=errors), False
  407. # Non-atomic writes directly go out through the regular open functions.
  408. if not atomic:
  409. if encoding is None:
  410. return open(filename, mode), True
  411. return io.open(filename, mode, encoding=encoding, errors=errors), True
  412. # Some usability stuff for atomic writes
  413. if 'a' in mode:
  414. raise ValueError(
  415. 'Appending to an existing file is not supported, because that '
  416. 'would involve an expensive `copy`-operation to a temporary '
  417. 'file. Open the file in normal `w`-mode and copy explicitly '
  418. 'if that\'s what you\'re after.'
  419. )
  420. if 'x' in mode:
  421. raise ValueError('Use the `overwrite`-parameter instead.')
  422. if 'w' not in mode:
  423. raise ValueError('Atomic writes only make sense with `w`-mode.')
  424. # Atomic writes are more complicated. They work by opening a file
  425. # as a proxy in the same folder and then using the fdopen
  426. # functionality to wrap it in a Python file. Then we wrap it in an
  427. # atomic file that moves the file over on close.
  428. import tempfile
  429. fd, tmp_filename = tempfile.mkstemp(dir=os.path.dirname(filename),
  430. prefix='.__atomic-write')
  431. if encoding is not None:
  432. f = io.open(fd, mode, encoding=encoding, errors=errors)
  433. else:
  434. f = os.fdopen(fd, mode)
  435. return _AtomicFile(f, tmp_filename, os.path.realpath(filename)), True
  436. # Used in a destructor call, needs extra protection from interpreter cleanup.
  437. if hasattr(os, 'replace'):
  438. _replace = os.replace
  439. _can_replace = True
  440. else:
  441. _replace = os.rename
  442. _can_replace = not WIN
  443. class _AtomicFile(object):
  444. def __init__(self, f, tmp_filename, real_filename):
  445. self._f = f
  446. self._tmp_filename = tmp_filename
  447. self._real_filename = real_filename
  448. self.closed = False
  449. @property
  450. def name(self):
  451. return self._real_filename
  452. def close(self, delete=False):
  453. if self.closed:
  454. return
  455. self._f.close()
  456. if not _can_replace:
  457. try:
  458. os.remove(self._real_filename)
  459. except OSError:
  460. pass
  461. _replace(self._tmp_filename, self._real_filename)
  462. self.closed = True
  463. def __getattr__(self, name):
  464. return getattr(self._f, name)
  465. def __enter__(self):
  466. return self
  467. def __exit__(self, exc_type, exc_value, tb):
  468. self.close(delete=exc_type is not None)
  469. def __repr__(self):
  470. return repr(self._f)
  471. auto_wrap_for_ansi = None
  472. colorama = None
  473. get_winterm_size = None
  474. def strip_ansi(value):
  475. return _ansi_re.sub('', value)
  476. def should_strip_ansi(stream=None, color=None):
  477. if color is None:
  478. if stream is None:
  479. stream = sys.stdin
  480. return not isatty(stream)
  481. return not color
  482. # If we're on Windows, we provide transparent integration through
  483. # colorama. This will make ANSI colors through the echo function
  484. # work automatically.
  485. if WIN:
  486. # Windows has a smaller terminal
  487. DEFAULT_COLUMNS = 79
  488. from ._winconsole import _get_windows_console_stream, _wrap_std_stream
  489. def _get_argv_encoding():
  490. import locale
  491. return locale.getpreferredencoding()
  492. if PY2:
  493. def raw_input(prompt=''):
  494. sys.stderr.flush()
  495. if prompt:
  496. stdout = _default_text_stdout()
  497. stdout.write(prompt)
  498. stdin = _default_text_stdin()
  499. return stdin.readline().rstrip('\r\n')
  500. try:
  501. import colorama
  502. except ImportError:
  503. pass
  504. else:
  505. _ansi_stream_wrappers = WeakKeyDictionary()
  506. def auto_wrap_for_ansi(stream, color=None):
  507. """This function wraps a stream so that calls through colorama
  508. are issued to the win32 console API to recolor on demand. It
  509. also ensures to reset the colors if a write call is interrupted
  510. to not destroy the console afterwards.
  511. """
  512. try:
  513. cached = _ansi_stream_wrappers.get(stream)
  514. except Exception:
  515. cached = None
  516. if cached is not None:
  517. return cached
  518. strip = should_strip_ansi(stream, color)
  519. ansi_wrapper = colorama.AnsiToWin32(stream, strip=strip)
  520. rv = ansi_wrapper.stream
  521. _write = rv.write
  522. def _safe_write(s):
  523. try:
  524. return _write(s)
  525. except:
  526. ansi_wrapper.reset_all()
  527. raise
  528. rv.write = _safe_write
  529. try:
  530. _ansi_stream_wrappers[stream] = rv
  531. except Exception:
  532. pass
  533. return rv
  534. def get_winterm_size():
  535. win = colorama.win32.GetConsoleScreenBufferInfo(
  536. colorama.win32.STDOUT).srWindow
  537. return win.Right - win.Left, win.Bottom - win.Top
  538. else:
  539. def _get_argv_encoding():
  540. return getattr(sys.stdin, 'encoding', None) or get_filesystem_encoding()
  541. _get_windows_console_stream = lambda *x: None
  542. _wrap_std_stream = lambda *x: None
  543. def term_len(x):
  544. return len(strip_ansi(x))
  545. def isatty(stream):
  546. try:
  547. return stream.isatty()
  548. except Exception:
  549. return False
  550. def _make_cached_stream_func(src_func, wrapper_func):
  551. cache = WeakKeyDictionary()
  552. def func():
  553. stream = src_func()
  554. try:
  555. rv = cache.get(stream)
  556. except Exception:
  557. rv = None
  558. if rv is not None:
  559. return rv
  560. rv = wrapper_func()
  561. try:
  562. stream = src_func() # In case wrapper_func() modified the stream
  563. cache[stream] = rv
  564. except Exception:
  565. pass
  566. return rv
  567. return func
  568. _default_text_stdin = _make_cached_stream_func(
  569. lambda: sys.stdin, get_text_stdin)
  570. _default_text_stdout = _make_cached_stream_func(
  571. lambda: sys.stdout, get_text_stdout)
  572. _default_text_stderr = _make_cached_stream_func(
  573. lambda: sys.stderr, get_text_stderr)
  574. binary_streams = {
  575. 'stdin': get_binary_stdin,
  576. 'stdout': get_binary_stdout,
  577. 'stderr': get_binary_stderr,
  578. }
  579. text_streams = {
  580. 'stdin': get_text_stdin,
  581. 'stdout': get_text_stdout,
  582. 'stderr': get_text_stderr,
  583. }