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