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