##// END OF EJS Templates
Fix Python 3.2 test failures (ResourceWarnings from unclosed files)....
Bradley M. Froehle -
Show More
@@ -1,178 +1,179 b''
1 1 # coding: utf-8
2 2 """Compatibility tricks for Python 3. Mainly to do with unicode."""
3 3 import __builtin__
4 4 import functools
5 5 import sys
6 6 import re
7 7 import types
8 8
9 9 from .encoding import DEFAULT_ENCODING
10 10
11 11 orig_open = open
12 12
13 13 def no_code(x, encoding=None):
14 14 return x
15 15
16 16 def decode(s, encoding=None):
17 17 encoding = encoding or DEFAULT_ENCODING
18 18 return s.decode(encoding, "replace")
19 19
20 20 def encode(u, encoding=None):
21 21 encoding = encoding or DEFAULT_ENCODING
22 22 return u.encode(encoding, "replace")
23 23
24 24
25 25 def cast_unicode(s, encoding=None):
26 26 if isinstance(s, bytes):
27 27 return decode(s, encoding)
28 28 return s
29 29
30 30 def cast_bytes(s, encoding=None):
31 31 if not isinstance(s, bytes):
32 32 return encode(s, encoding)
33 33 return s
34 34
35 35 def _modify_str_or_docstring(str_change_func):
36 36 @functools.wraps(str_change_func)
37 37 def wrapper(func_or_str):
38 38 if isinstance(func_or_str, basestring):
39 39 func = None
40 40 doc = func_or_str
41 41 else:
42 42 func = func_or_str
43 43 doc = func.__doc__
44 44
45 45 doc = str_change_func(doc)
46 46
47 47 if func:
48 48 func.__doc__ = doc
49 49 return func
50 50 return doc
51 51 return wrapper
52 52
53 53 if sys.version_info[0] >= 3:
54 54 PY3 = True
55 55
56 56 input = input
57 57 builtin_mod_name = "builtins"
58 58
59 59 str_to_unicode = no_code
60 60 unicode_to_str = no_code
61 61 str_to_bytes = encode
62 62 bytes_to_str = decode
63 63 cast_bytes_py2 = no_code
64 64
65 65 def isidentifier(s, dotted=False):
66 66 if dotted:
67 67 return all(isidentifier(a) for a in s.split("."))
68 68 return s.isidentifier()
69 69
70 70 open = orig_open
71 71
72 72 MethodType = types.MethodType
73 73
74 74 def execfile(fname, glob, loc=None):
75 75 loc = loc if (loc is not None) else glob
76 exec compile(open(fname, 'rb').read(), fname, 'exec') in glob, loc
76 with open(fname, 'rb') as f:
77 exec compile(f.read(), fname, 'exec') in glob, loc
77 78
78 79 # Refactor print statements in doctests.
79 80 _print_statement_re = re.compile(r"\bprint (?P<expr>.*)$", re.MULTILINE)
80 81 def _print_statement_sub(match):
81 82 expr = match.groups('expr')
82 83 return "print(%s)" % expr
83 84
84 85 @_modify_str_or_docstring
85 86 def doctest_refactor_print(doc):
86 87 """Refactor 'print x' statements in a doctest to print(x) style. 2to3
87 88 unfortunately doesn't pick up on our doctests.
88 89
89 90 Can accept a string or a function, so it can be used as a decorator."""
90 91 return _print_statement_re.sub(_print_statement_sub, doc)
91 92
92 93 # Abstract u'abc' syntax:
93 94 @_modify_str_or_docstring
94 95 def u_format(s):
95 96 """"{u}'abc'" --> "'abc'" (Python 3)
96 97
97 98 Accepts a string or a function, so it can be used as a decorator."""
98 99 return s.format(u='')
99 100
100 101 else:
101 102 PY3 = False
102 103
103 104 input = raw_input
104 105 builtin_mod_name = "__builtin__"
105 106
106 107 str_to_unicode = decode
107 108 unicode_to_str = encode
108 109 str_to_bytes = no_code
109 110 bytes_to_str = no_code
110 111 cast_bytes_py2 = cast_bytes
111 112
112 113 import re
113 114 _name_re = re.compile(r"[a-zA-Z_][a-zA-Z0-9_]*$")
114 115 def isidentifier(s, dotted=False):
115 116 if dotted:
116 117 return all(isidentifier(a) for a in s.split("."))
117 118 return bool(_name_re.match(s))
118 119
119 120 class open(object):
120 121 """Wrapper providing key part of Python 3 open() interface."""
121 122 def __init__(self, fname, mode="r", encoding="utf-8"):
122 123 self.f = orig_open(fname, mode)
123 124 self.enc = encoding
124 125
125 126 def write(self, s):
126 127 return self.f.write(s.encode(self.enc))
127 128
128 129 def read(self, size=-1):
129 130 return self.f.read(size).decode(self.enc)
130 131
131 132 def close(self):
132 133 return self.f.close()
133 134
134 135 def __enter__(self):
135 136 return self
136 137
137 138 def __exit__(self, etype, value, traceback):
138 139 self.f.close()
139 140
140 141 def MethodType(func, instance):
141 142 return types.MethodType(func, instance, type(instance))
142 143
143 144 # don't override system execfile on 2.x:
144 145 execfile = execfile
145 146
146 147 def doctest_refactor_print(func_or_str):
147 148 return func_or_str
148 149
149 150
150 151 # Abstract u'abc' syntax:
151 152 @_modify_str_or_docstring
152 153 def u_format(s):
153 154 """"{u}'abc'" --> "u'abc'" (Python 2)
154 155
155 156 Accepts a string or a function, so it can be used as a decorator."""
156 157 return s.format(u='u')
157 158
158 159 if sys.platform == 'win32':
159 160 def execfile(fname, glob=None, loc=None):
160 161 loc = loc if (loc is not None) else glob
161 162 # The rstrip() is necessary b/c trailing whitespace in files will
162 163 # cause an IndentationError in Python 2.6 (this was fixed in 2.7,
163 164 # but we still support 2.6). See issue 1027.
164 165 scripttext = __builtin__.open(fname).read().rstrip() + '\n'
165 166 # compile converts unicode filename to str assuming
166 167 # ascii. Let's do the conversion before calling compile
167 168 if isinstance(fname, unicode):
168 169 filename = unicode_to_str(fname)
169 170 else:
170 171 filename = fname
171 172 exec compile(scripttext, filename, 'exec') in glob, loc
172 173 else:
173 174 def execfile(fname, *where):
174 175 if isinstance(fname, unicode):
175 176 filename = fname.encode(sys.getfilesystemencoding())
176 177 else:
177 178 filename = fname
178 179 __builtin__.execfile(filename, *where)
General Comments 0
You need to be logged in to leave comments. Login now