/usr/lib/python3/dist-packages/wand/compat.py is in python3-wand 0.3.9-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | """:mod:`wand.compat` --- Compatibility layer
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This module provides several subtle things to support
multiple Python versions (2.6, 2.7, 3.2, 3.3) and VM implementations
(CPython, PyPy).
"""
import contextlib
import io
import sys
import types
__all__ = ('PY3', 'binary', 'binary_type', 'encode_filename', 'file_types',
'nested', 'string_type', 'text', 'text_type', 'xrange')
#: (:class:`bool`) Whether it is Python 3.x or not.
PY3 = sys.version_info >= (3,)
#: (:class:`type`) Type for representing binary data. :class:`str` in Python 2
#: and :class:`bytes` in Python 3.
binary_type = bytes if PY3 else str
#: (:class:`type`) Type for text data. :class:`basestring` in Python 2
#: and :class:`str` in Python 3.
string_type = str if PY3 else basestring
#: (:class:`type`) Type for representing Unicode textual data.
#: :class:`unicode` in Python 2 and :class:`str` in Python 3.
text_type = str if PY3 else unicode
def binary(string, var=None):
"""Makes ``string`` to :class:`str` in Python 2.
Makes ``string`` to :class:`bytes` in Python 3.
:param string: a string to cast it to :data:`binary_type`
:type string: :class:`bytes`, :class:`str`, :class:`unicode`
:param var: an optional variable name to be used for error message
:type var: :class:`str`
"""
if isinstance(string, text_type):
return string.encode()
elif isinstance(string, binary_type):
return string
if var:
raise TypeError('{0} must be a string, not {1!r}'.format(var, string))
raise TypeError('expected a string, not ' + repr(string))
def text(string):
"""Makes ``string`` to :class:`str` in Python 3.
Does nothing in Python 2.
:param string: a string to cast it to :data:`text_type`
:type string: :class:`bytes`, :class:`str`, :class:`unicode`
"""
return string
if PY3:
def text(string):
if isinstance(string, bytes):
return string.decode('utf-8')
return string
#: The :func:`xrange()` function. Alias for :func:`range()` in Python 3.
xrange = range if PY3 else xrange
#: (:class:`type`, :class:`tuple`) Types for file objects that have
#: ``fileno()``.
file_types = io.RawIOBase if PY3 else (io.RawIOBase, types.FileType)
def encode_filename(filename):
"""If ``filename`` is a :data:`text_type`, encode it to
:data:`binary_type` according to filesystem's default encoding.
"""
if isinstance(filename, text_type):
return filename.encode(sys.getfilesystemencoding())
return filename
try:
nested = contextlib.nested
except AttributeError:
# http://hg.python.org/cpython/file/v2.7.6/Lib/contextlib.py#l88
@contextlib.contextmanager
def nested(*managers):
exits = []
vars = []
exc = (None, None, None)
try:
for mgr in managers:
exit = mgr.__exit__
enter = mgr.__enter__
vars.append(enter())
exits.append(exit)
yield vars
except:
exc = sys.exc_info()
finally:
while exits:
exit = exits.pop()
try:
if exit(*exc):
exc = (None, None, None)
except:
exc = sys.exc_info()
if exc != (None, None, None):
# PEP 3109
e = exc[0](exc[1])
e.__traceback__ = e[2]
raise e
|