/usr/lib/python2.7/dist-packages/future/types/__init__.py is in python-future 0.15.2-4ubuntu2.
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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 | """
This module contains backports the data types that were significantly changed
in the transition from Python 2 to Python 3.
- an implementation of Python 3's bytes object (pure Python subclass of
Python 2's builtin 8-bit str type)
- an implementation of Python 3's str object (pure Python subclass of
Python 2's builtin unicode type)
- a backport of the range iterator from Py3 with slicing support
It is used as follows::
from __future__ import division, absolute_import, print_function
from builtins import bytes, dict, int, range, str
to bring in the new semantics for these functions from Python 3. And
then, for example::
b = bytes(b'ABCD')
assert list(b) == [65, 66, 67, 68]
assert repr(b) == "b'ABCD'"
assert [65, 66] in b
# These raise TypeErrors:
# b + u'EFGH'
# b.split(u'B')
# bytes(b',').join([u'Fred', u'Bill'])
s = str(u'ABCD')
# These raise TypeErrors:
# s.join([b'Fred', b'Bill'])
# s.startswith(b'A')
# b'B' in s
# s.find(b'A')
# s.replace(u'A', b'a')
# This raises an AttributeError:
# s.decode('utf-8')
assert repr(s) == 'ABCD' # consistent repr with Py3 (no u prefix)
for i in range(10**11)[:10]:
pass
and::
class VerboseList(list):
def append(self, item):
print('Adding an item')
super().append(item) # new simpler super() function
For more information:
---------------------
- future.types.newbytes
- future.types.newdict
- future.types.newint
- future.types.newobject
- future.types.newrange
- future.types.newstr
Notes
=====
range()
-------
``range`` is a custom class that backports the slicing behaviour from
Python 3 (based on the ``xrange`` module by Dan Crosta). See the
``newrange`` module docstring for more details.
super()
-------
``super()`` is based on Ryan Kelly's ``magicsuper`` module. See the
``newsuper`` module docstring for more details.
round()
-------
Python 3 modifies the behaviour of ``round()`` to use "Banker's Rounding".
See http://stackoverflow.com/a/10825998. See the ``newround`` module
docstring for more details.
"""
from __future__ import absolute_import, division, print_function
import functools
from numbers import Integral
from future import utils
# Some utility functions to enforce strict type-separation of unicode str and
# bytes:
def disallow_types(argnums, disallowed_types):
"""
A decorator that raises a TypeError if any of the given numbered
arguments is of the corresponding given type (e.g. bytes or unicode
string).
For example:
@disallow_types([0, 1], [unicode, bytes])
def f(a, b):
pass
raises a TypeError when f is called if a unicode object is passed as
`a` or a bytes object is passed as `b`.
This also skips over keyword arguments, so
@disallow_types([0, 1], [unicode, bytes])
def g(a, b=None):
pass
doesn't raise an exception if g is called with only one argument a,
e.g.:
g(b'Byte string')
Example use:
>>> class newbytes(object):
... @disallow_types([1], [unicode])
... def __add__(self, other):
... pass
>>> newbytes('1234') + u'1234' #doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
TypeError: can't concat 'bytes' to (unicode) str
"""
def decorator(function):
@functools.wraps(function)
def wrapper(*args, **kwargs):
# These imports are just for this decorator, and are defined here
# to prevent circular imports:
from .newbytes import newbytes
from .newint import newint
from .newstr import newstr
errmsg = "argument can't be {0}"
for (argnum, mytype) in zip(argnums, disallowed_types):
# Handle the case where the type is passed as a string like 'newbytes'.
if isinstance(mytype, str) or isinstance(mytype, bytes):
mytype = locals()[mytype]
# Only restrict kw args only if they are passed:
if len(args) <= argnum:
break
# Here we use type() rather than isinstance() because
# __instancecheck__ is being overridden. E.g.
# isinstance(b'abc', newbytes) is True on Py2.
if type(args[argnum]) == mytype:
raise TypeError(errmsg.format(mytype))
return function(*args, **kwargs)
return wrapper
return decorator
def no(mytype, argnums=(1,)):
"""
A shortcut for the disallow_types decorator that disallows only one type
(in any position in argnums).
Example use:
>>> class newstr(object):
... @no('bytes')
... def __add__(self, other):
... pass
>>> newstr(u'1234') + b'1234' #doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
TypeError: argument can't be bytes
The object can also be passed directly, but passing the string helps
to prevent circular import problems.
"""
if isinstance(argnums, Integral):
argnums = (argnums,)
disallowed_types = [mytype] * len(argnums)
return disallow_types(argnums, disallowed_types)
def issubset(list1, list2):
"""
Examples:
>>> issubset([], [65, 66, 67])
True
>>> issubset([65], [65, 66, 67])
True
>>> issubset([65, 66], [65, 66, 67])
True
>>> issubset([65, 67], [65, 66, 67])
False
"""
n = len(list1)
for startpos in range(len(list2) - n + 1):
if list2[startpos:startpos+n] == list1:
return True
return False
if utils.PY3:
import builtins
bytes = builtins.bytes
dict = builtins.dict
int = builtins.int
list = builtins.list
object = builtins.object
range = builtins.range
str = builtins.str
# The identity mapping
newtypes = {bytes: bytes,
dict: dict,
int: int,
list: list,
object: object,
range: range,
str: str}
__all__ = ['newtypes']
else:
from .newbytes import newbytes
from .newdict import newdict
from .newint import newint
from .newlist import newlist
from .newrange import newrange
from .newobject import newobject
from .newstr import newstr
newtypes = {bytes: newbytes,
dict: newdict,
int: newint,
long: newint,
list: newlist,
object: newobject,
range: newrange,
str: newbytes,
unicode: newstr}
__all__ = ['newbytes', 'newdict', 'newint', 'newlist', 'newrange', 'newstr', 'newtypes']
|