1"""
2A module that brings in equivalents of various modified Python 3 builtins
3into Py2. Has no effect on Py3.
4
5The builtin functions are:
6
7- ``ascii`` (from Py2's future_builtins module)
8- ``hex`` (from Py2's future_builtins module)
9- ``oct`` (from Py2's future_builtins module)
10- ``chr`` (equivalent to ``unichr`` on Py2)
11- ``input`` (equivalent to ``raw_input`` on Py2)
12- ``next`` (calls ``__next__`` if it exists, else ``next`` method)
13- ``open`` (equivalent to io.open on Py2)
14- ``super`` (backport of Py3's magic zero-argument super() function
15- ``round`` (new "Banker's Rounding" behaviour from Py3)
16- ``max`` (new default option from Py3.4)
17- ``min`` (new default option from Py3.4)
18
19``isinstance`` is also currently exported for backwards compatibility
20with v0.8.2, although this has been deprecated since v0.9.
21
22
23input()
24-------
25Like the new ``input()`` function from Python 3 (without eval()), except
26that it returns bytes. Equivalent to Python 2's ``raw_input()``.
27
28Warning: By default, importing this module *removes* the old Python 2
29input() function entirely from ``__builtin__`` for safety. This is
30because forgetting to import the new ``input`` from ``future`` might
31otherwise lead to a security vulnerability (shell injection) on Python 2.
32
33To restore it, you can retrieve it yourself from
34``__builtin__._old_input``.
35
36Fortunately, ``input()`` seems to be seldom used in the wild in Python
372...
38
39"""
40
41from future import utils
42
43
44if utils.PY2:
45 from io import open
46 from future_builtins import ascii, oct, hex
47 from __builtin__ import unichr as chr, pow as _builtin_pow
48 import __builtin__
49
50 # Only for backward compatibility with future v0.8.2:
51 isinstance = __builtin__.isinstance
52
53 # Warning: Python 2's input() is unsafe and MUST not be able to be used
54 # accidentally by someone who expects Python 3 semantics but forgets
55 # to import it on Python 2. Versions of ``future`` prior to 0.11
56 # deleted it from __builtin__. Now we keep in __builtin__ but shadow
57 # the name like all others. Just be sure to import ``input``.
58
59 input = raw_input
60
61 from future.builtins.newnext import newnext as next
62 from future.builtins.newround import newround as round
63 from future.builtins.newsuper import newsuper as super
64 from future.builtins.new_min_max import newmax as max
65 from future.builtins.new_min_max import newmin as min
66 from future.types.newint import newint
67
68 _SENTINEL = object()
69
70 def pow(x, y, z=_SENTINEL):
71 """
72 pow(x, y[, z]) -> number
73
74 With two arguments, equivalent to x**y. With three arguments,
75 equivalent to (x**y) % z, but may be more efficient (e.g. for ints).
76 """
77 # Handle newints
78 if isinstance(x, newint):
79 x = long(x)
80 if isinstance(y, newint):
81 y = long(y)
82 if isinstance(z, newint):
83 z = long(z)
84
85 try:
86 if z == _SENTINEL:
87 return _builtin_pow(x, y)
88 else:
89 return _builtin_pow(x, y, z)
90 except ValueError:
91 if z == _SENTINEL:
92 return _builtin_pow(x+0j, y)
93 else:
94 return _builtin_pow(x+0j, y, z)
95
96
97 # ``future`` doesn't support Py3.0/3.1. If we ever did, we'd add this:
98 # callable = __builtin__.callable
99
100 __all__ = ['ascii', 'chr', 'hex', 'input', 'isinstance', 'next', 'oct',
101 'open', 'pow', 'round', 'super', 'max', 'min']
102
103else:
104 import builtins
105 ascii = builtins.ascii
106 chr = builtins.chr
107 hex = builtins.hex
108 input = builtins.input
109 next = builtins.next
110 # Only for backward compatibility with future v0.8.2:
111 isinstance = builtins.isinstance
112 oct = builtins.oct
113 open = builtins.open
114 pow = builtins.pow
115 round = builtins.round
116 super = builtins.super
117 if utils.PY34_PLUS:
118 max = builtins.max
119 min = builtins.min
120 __all__ = []
121 else:
122 from future.builtins.new_min_max import newmax as max
123 from future.builtins.new_min_max import newmin as min
124 __all__ = ['min', 'max']
125
126 # The callable() function was removed from Py3.0 and 3.1 and
127 # reintroduced into Py3.2+. ``future`` doesn't support Py3.0/3.1. If we ever
128 # did, we'd add this:
129 # try:
130 # callable = builtins.callable
131 # except AttributeError:
132 # # Definition from Pandas
133 # def callable(obj):
134 # return any("__call__" in klass.__dict__ for klass in type(obj).__mro__)
135 # __all__.append('callable')