misc.py 4.4 KB

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