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