misc.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. from __future__ import unicode_literals
  2. import inspect
  3. from future.utils import PY2, PY3, exec_
  4. if PY2:
  5. from collections import Mapping
  6. else:
  7. from collections.abc import Mapping
  8. if PY3:
  9. import builtins
  10. from collections.abc import Mapping
  11. def apply(f, *args, **kw):
  12. return f(*args, **kw)
  13. from past.builtins import str as oldstr
  14. def chr(i):
  15. """
  16. Return a byte-string of one character with ordinal i; 0 <= i <= 256
  17. """
  18. return oldstr(bytes((i,)))
  19. def cmp(x, y):
  20. """
  21. cmp(x, y) -> integer
  22. Return negative if x<y, zero if x==y, positive if x>y.
  23. """
  24. return (x > y) - (x < y)
  25. from sys import intern
  26. def oct(number):
  27. """oct(number) -> string
  28. Return the octal representation of an integer
  29. """
  30. return '0' + builtins.oct(number)[2:]
  31. import warnings
  32. warnings.filterwarnings("ignore", category=DeprecationWarning, module="past.builtins.misc")
  33. raw_input = input
  34. from imp import reload
  35. unicode = str
  36. unichr = chr
  37. xrange = range
  38. else:
  39. import __builtin__
  40. from collections import Mapping
  41. apply = __builtin__.apply
  42. chr = __builtin__.chr
  43. cmp = __builtin__.cmp
  44. execfile = __builtin__.execfile
  45. intern = __builtin__.intern
  46. oct = __builtin__.oct
  47. raw_input = __builtin__.raw_input
  48. reload = __builtin__.reload
  49. unicode = __builtin__.unicode
  50. unichr = __builtin__.unichr
  51. xrange = __builtin__.xrange
  52. if PY3:
  53. def execfile(filename, myglobals=None, mylocals=None):
  54. """
  55. Read and execute a Python script from a file in the given namespaces.
  56. The globals and locals are dictionaries, defaulting to the current
  57. globals and locals. If only globals is given, locals defaults to it.
  58. """
  59. if myglobals is None:
  60. # There seems to be no alternative to frame hacking here.
  61. caller_frame = inspect.stack()[1]
  62. myglobals = caller_frame[0].f_globals
  63. mylocals = caller_frame[0].f_locals
  64. elif mylocals is None:
  65. # Only if myglobals is given do we set mylocals to it.
  66. mylocals = myglobals
  67. if not isinstance(myglobals, Mapping):
  68. raise TypeError('globals must be a mapping')
  69. if not isinstance(mylocals, Mapping):
  70. raise TypeError('locals must be a mapping')
  71. with open(filename, "rb") as fin:
  72. source = fin.read()
  73. code = compile(source, filename, "exec")
  74. exec_(code, myglobals, mylocals)
  75. if PY3:
  76. __all__ = ['apply', 'chr', 'cmp', 'execfile', 'intern', 'raw_input',
  77. 'reload', 'unichr', 'unicode', 'xrange']
  78. else:
  79. __all__ = []