##// END OF EJS Templates
don't override execfile on Python 2
MinRK -
Show More
@@ -1,95 +1,99 b''
1 # coding: utf-8
1 # coding: utf-8
2 """Compatibility tricks for Python 3. Mainly to do with unicode."""
2 """Compatibility tricks for Python 3. Mainly to do with unicode."""
3 import sys
3 import sys
4 import types
4 import types
5
5
6 orig_open = open
6 orig_open = open
7
7
8 def no_code(x, encoding=None):
8 def no_code(x, encoding=None):
9 return x
9 return x
10
10
11 def decode(s, encoding=None):
11 def decode(s, encoding=None):
12 encoding = encoding or sys.stdin.encoding or sys.getdefaultencoding()
12 encoding = encoding or sys.stdin.encoding or sys.getdefaultencoding()
13 return s.decode(encoding, "replace")
13 return s.decode(encoding, "replace")
14
14
15 def encode(u, encoding=None):
15 def encode(u, encoding=None):
16 encoding = encoding or sys.stdin.encoding or sys.getdefaultencoding()
16 encoding = encoding or sys.stdin.encoding or sys.getdefaultencoding()
17 return u.encode(encoding, "replace")
17 return u.encode(encoding, "replace")
18
18
19 def cast_unicode(s, encoding=None):
19 def cast_unicode(s, encoding=None):
20 if isinstance(s, bytes):
20 if isinstance(s, bytes):
21 return decode(s, encoding)
21 return decode(s, encoding)
22 return s
22 return s
23
23
24 def cast_bytes(s, encoding=None):
24 def cast_bytes(s, encoding=None):
25 if not isinstance(s, bytes):
25 if not isinstance(s, bytes):
26 return encode(s, encoding)
26 return encode(s, encoding)
27 return s
27 return s
28
28
29 if sys.version_info[0] >= 3:
29 if sys.version_info[0] >= 3:
30 PY3 = True
30 PY3 = True
31
31
32 input = input
32 input = input
33 builtin_mod_name = "builtins"
33 builtin_mod_name = "builtins"
34
34
35 str_to_unicode = no_code
35 str_to_unicode = no_code
36 unicode_to_str = no_code
36 unicode_to_str = no_code
37 str_to_bytes = encode
37 str_to_bytes = encode
38 bytes_to_str = decode
38 bytes_to_str = decode
39 cast_bytes_py2 = no_code
39 cast_bytes_py2 = no_code
40
40
41 def isidentifier(s, dotted=False):
41 def isidentifier(s, dotted=False):
42 if dotted:
42 if dotted:
43 return all(isidentifier(a) for a in s.split("."))
43 return all(isidentifier(a) for a in s.split("."))
44 return s.isidentifier()
44 return s.isidentifier()
45
45
46 open = orig_open
46 open = orig_open
47
47
48 MethodType = types.MethodType
48 MethodType = types.MethodType
49
50 def execfile(fname, glob, loc=None):
51 loc = loc if (loc is not None) else glob
52 exec compile(open(fname).read(), fname, 'exec') in glob, loc
49
53
50 else:
54 else:
51 PY3 = False
55 PY3 = False
52
56
53 input = raw_input
57 input = raw_input
54 builtin_mod_name = "__builtin__"
58 builtin_mod_name = "__builtin__"
55
59
56 str_to_unicode = decode
60 str_to_unicode = decode
57 unicode_to_str = encode
61 unicode_to_str = encode
58 str_to_bytes = no_code
62 str_to_bytes = no_code
59 bytes_to_str = no_code
63 bytes_to_str = no_code
60 cast_bytes_py2 = cast_bytes
64 cast_bytes_py2 = cast_bytes
61
65
62 import re
66 import re
63 _name_re = re.compile(r"[a-zA-Z_][a-zA-Z0-9_]*$")
67 _name_re = re.compile(r"[a-zA-Z_][a-zA-Z0-9_]*$")
64 def isidentifier(s, dotted=False):
68 def isidentifier(s, dotted=False):
65 if dotted:
69 if dotted:
66 return all(isidentifier(a) for a in s.split("."))
70 return all(isidentifier(a) for a in s.split("."))
67 return bool(_name_re.match(s))
71 return bool(_name_re.match(s))
68
72
69 class open(object):
73 class open(object):
70 """Wrapper providing key part of Python 3 open() interface."""
74 """Wrapper providing key part of Python 3 open() interface."""
71 def __init__(self, fname, mode="r", encoding="utf-8"):
75 def __init__(self, fname, mode="r", encoding="utf-8"):
72 self.f = orig_open(fname, mode)
76 self.f = orig_open(fname, mode)
73 self.enc = encoding
77 self.enc = encoding
74
78
75 def write(self, s):
79 def write(self, s):
76 return self.f.write(s.encode(self.enc))
80 return self.f.write(s.encode(self.enc))
77
81
78 def read(self, size=-1):
82 def read(self, size=-1):
79 return self.f.read(size).decode(self.enc)
83 return self.f.read(size).decode(self.enc)
80
84
81 def close(self):
85 def close(self):
82 return self.f.close()
86 return self.f.close()
83
87
84 def __enter__(self):
88 def __enter__(self):
85 return self
89 return self
86
90
87 def __exit__(self, etype, value, traceback):
91 def __exit__(self, etype, value, traceback):
88 self.f.close()
92 self.f.close()
89
93
90 def MethodType(func, instance):
94 def MethodType(func, instance):
91 return types.MethodType(func, instance, type(instance))
95 return types.MethodType(func, instance, type(instance))
96
97 # don't override system execfile on 2.x:
98 execfile = execfile
92
99
93 def execfile(fname, glob, loc=None):
94 loc = loc if (loc is not None) else glob
95 exec compile(open(fname).read(), fname, 'exec') in glob, loc
General Comments 0
You need to be logged in to leave comments. Login now