1 |
Author: zmedico |
2 |
Date: 2010-02-27 02:57:00 +0000 (Sat, 27 Feb 2010) |
3 |
New Revision: 15473 |
4 |
|
5 |
Added: |
6 |
main/trunk/pym/portage/_ensure_encodings.py |
7 |
Modified: |
8 |
main/trunk/pym/portage/__init__.py |
9 |
Log: |
10 |
Move missing encodings module workarounds (<python-2.6.4 + USE=build) to a |
11 |
_ensure_encodings module that's only imported on demand. |
12 |
|
13 |
|
14 |
Modified: main/trunk/pym/portage/__init__.py |
15 |
=================================================================== |
16 |
--- main/trunk/pym/portage/__init__.py 2010-02-27 00:09:03 UTC (rev 15472) |
17 |
+++ main/trunk/pym/portage/__init__.py 2010-02-27 02:57:00 UTC (rev 15473) |
18 |
@@ -330,89 +330,8 @@ |
19 |
# END OF IMPORTS -- END OF IMPORTS -- END OF IMPORTS -- END OF IMPORTS -- END |
20 |
# =========================================================================== |
21 |
|
22 |
-def _gen_missing_encodings(missing_encodings): |
23 |
- |
24 |
- encodings = {} |
25 |
- |
26 |
- if 'ascii' in missing_encodings: |
27 |
- |
28 |
- class AsciiIncrementalEncoder(codecs.IncrementalEncoder): |
29 |
- def encode(self, input, final=False): |
30 |
- return codecs.ascii_encode(input, self.errors)[0] |
31 |
- |
32 |
- class AsciiIncrementalDecoder(codecs.IncrementalDecoder): |
33 |
- def decode(self, input, final=False): |
34 |
- return codecs.ascii_decode(input, self.errors)[0] |
35 |
- |
36 |
- class AsciiStreamWriter(codecs.StreamWriter): |
37 |
- encode = codecs.ascii_encode |
38 |
- |
39 |
- class AsciiStreamReader(codecs.StreamReader): |
40 |
- decode = codecs.ascii_decode |
41 |
- |
42 |
- codec_info = codecs.CodecInfo( |
43 |
- name='ascii', |
44 |
- encode=codecs.ascii_encode, |
45 |
- decode=codecs.ascii_decode, |
46 |
- incrementalencoder=AsciiIncrementalEncoder, |
47 |
- incrementaldecoder=AsciiIncrementalDecoder, |
48 |
- streamwriter=AsciiStreamWriter, |
49 |
- streamreader=AsciiStreamReader, |
50 |
- ) |
51 |
- |
52 |
- for alias in ('ascii', '646', 'ansi_x3.4_1968', 'ansi_x3_4_1968', |
53 |
- 'ansi_x3.4_1986', 'cp367', 'csascii', 'ibm367', 'iso646_us', |
54 |
- 'iso_646.irv_1991', 'iso_ir_6', 'us', 'us_ascii'): |
55 |
- encodings[alias] = codec_info |
56 |
- |
57 |
- if 'utf_8' in missing_encodings: |
58 |
- |
59 |
- def utf8decode(input, errors='strict'): |
60 |
- return codecs.utf_8_decode(input, errors, True) |
61 |
- |
62 |
- class Utf8IncrementalEncoder(codecs.IncrementalEncoder): |
63 |
- def encode(self, input, final=False): |
64 |
- return codecs.utf_8_encode(input, self.errors)[0] |
65 |
- |
66 |
- class Utf8IncrementalDecoder(codecs.BufferedIncrementalDecoder): |
67 |
- _buffer_decode = codecs.utf_8_decode |
68 |
- |
69 |
- class Utf8StreamWriter(codecs.StreamWriter): |
70 |
- encode = codecs.utf_8_encode |
71 |
- |
72 |
- class Utf8StreamReader(codecs.StreamReader): |
73 |
- decode = codecs.utf_8_decode |
74 |
- |
75 |
- codec_info = codecs.CodecInfo( |
76 |
- name='utf-8', |
77 |
- encode=codecs.utf_8_encode, |
78 |
- decode=utf8decode, |
79 |
- incrementalencoder=Utf8IncrementalEncoder, |
80 |
- incrementaldecoder=Utf8IncrementalDecoder, |
81 |
- streamreader=Utf8StreamReader, |
82 |
- streamwriter=Utf8StreamWriter, |
83 |
- ) |
84 |
- |
85 |
- for alias in ('utf_8', 'u8', 'utf', 'utf8', 'utf8_ucs2', 'utf8_ucs4'): |
86 |
- encodings[alias] = codec_info |
87 |
- |
88 |
- return encodings |
89 |
- |
90 |
def _ensure_default_encoding(): |
91 |
- """ |
92 |
- The python that's inside stage 1 or 2 is built with a minimal |
93 |
- configuration which does not include the /usr/lib/pythonX.Y/encodings |
94 |
- directory. This results in error like the following: |
95 |
|
96 |
- LookupError: no codec search functions registered: can't find encoding |
97 |
- |
98 |
- In order to solve this problem, detect it early and manually register |
99 |
- a search function for the ascii and utf_8 codecs. Starting with python-3.0 |
100 |
- this problem is more noticeable because of stricter handling of encoding |
101 |
- and decoding between strings of characters and bytes. |
102 |
- """ |
103 |
- |
104 |
- default_fallback = 'utf_8' |
105 |
default_encoding = sys.getdefaultencoding().lower().replace('-', '_') |
106 |
filesystem_encoding = _encodings['merge'].lower().replace('-', '_') |
107 |
required_encodings = set(['ascii', 'utf_8']) |
108 |
@@ -428,50 +347,10 @@ |
109 |
if not missing_encodings: |
110 |
return |
111 |
|
112 |
- encodings = _gen_missing_encodings(missing_encodings) |
113 |
+ from portage import _ensure_encodings |
114 |
+ _ensure_encodings._setup_encodings(default_encoding, |
115 |
+ filesystem_encoding, missing_encodings) |
116 |
|
117 |
- if default_encoding in missing_encodings and \ |
118 |
- default_encoding not in encodings: |
119 |
- # Make the fallback codec correspond to whatever name happens |
120 |
- # to be returned by sys.getfilesystemencoding(). |
121 |
- |
122 |
- try: |
123 |
- encodings[default_encoding] = codecs.lookup(default_fallback) |
124 |
- except LookupError: |
125 |
- encodings[default_encoding] = encodings[default_fallback] |
126 |
- |
127 |
- if filesystem_encoding in missing_encodings and \ |
128 |
- filesystem_encoding not in encodings: |
129 |
- # Make the fallback codec correspond to whatever name happens |
130 |
- # to be returned by sys.getdefaultencoding(). |
131 |
- |
132 |
- try: |
133 |
- encodings[filesystem_encoding] = codecs.lookup(default_fallback) |
134 |
- except LookupError: |
135 |
- encodings[filesystem_encoding] = encodings[default_fallback] |
136 |
- |
137 |
- def search_function(name): |
138 |
- name = name.lower() |
139 |
- name = name.replace('-', '_') |
140 |
- codec_info = encodings.get(name) |
141 |
- if codec_info is not None: |
142 |
- return codecs.CodecInfo( |
143 |
- name=codec_info.name, |
144 |
- encode=codec_info.encode, |
145 |
- decode=codec_info.decode, |
146 |
- incrementalencoder=codec_info.incrementalencoder, |
147 |
- incrementaldecoder=codec_info.incrementaldecoder, |
148 |
- streamreader=codec_info.streamreader, |
149 |
- streamwriter=codec_info.streamwriter, |
150 |
- ) |
151 |
- return None |
152 |
- |
153 |
- codecs.register(search_function) |
154 |
- |
155 |
- del codec_name, default_encoding, default_fallback, \ |
156 |
- filesystem_encoding, missing_encodings, \ |
157 |
- required_encodings, search_function |
158 |
- |
159 |
# Do this ASAP since writemsg() might not work without it. |
160 |
_ensure_default_encoding() |
161 |
|
162 |
|
163 |
Added: main/trunk/pym/portage/_ensure_encodings.py |
164 |
=================================================================== |
165 |
--- main/trunk/pym/portage/_ensure_encodings.py (rev 0) |
166 |
+++ main/trunk/pym/portage/_ensure_encodings.py 2010-02-27 02:57:00 UTC (rev 15473) |
167 |
@@ -0,0 +1,133 @@ |
168 |
+# Copyright 2010 Gentoo Foundation |
169 |
+# Distributed under the terms of the GNU General Public License v2 |
170 |
+# $Id$ |
171 |
+ |
172 |
+import codecs |
173 |
+ |
174 |
+_codec_map = None |
175 |
+ |
176 |
+def _setup_encodings(default_encoding, filesystem_encoding, missing_encodings): |
177 |
+ """ |
178 |
+ The <python-2.6.4 that's inside stage 1 or 2 is built with a minimal |
179 |
+ configuration which does not include the /usr/lib/pythonX.Y/encodings |
180 |
+ directory. This results in error like the following: |
181 |
+ |
182 |
+ LookupError: no codec search functions registered: can't find encoding |
183 |
+ |
184 |
+ In order to solve this problem, detect it early and manually register |
185 |
+ a search function for the ascii and utf_8 codecs. Starting with python-3.0 |
186 |
+ this problem is more noticeable because of stricter handling of encoding |
187 |
+ and decoding between strings of characters and bytes. |
188 |
+ """ |
189 |
+ |
190 |
+ global _codec_map |
191 |
+ _codec_map = _gen_missing_encodings(missing_encodings) |
192 |
+ |
193 |
+ default_fallback = 'utf_8' |
194 |
+ |
195 |
+ if default_encoding in missing_encodings and \ |
196 |
+ default_encoding not in _codec_map: |
197 |
+ # Make the fallback codec correspond to whatever name happens |
198 |
+ # to be returned by sys.getfilesystemencoding(). |
199 |
+ |
200 |
+ try: |
201 |
+ _codec_map[default_encoding] = codecs.lookup(default_fallback) |
202 |
+ except LookupError: |
203 |
+ _codec_map[default_encoding] = _codec_map[default_fallback] |
204 |
+ |
205 |
+ if filesystem_encoding in missing_encodings and \ |
206 |
+ filesystem_encoding not in _codec_map: |
207 |
+ # Make the fallback codec correspond to whatever name happens |
208 |
+ # to be returned by sys.getdefaultencoding(). |
209 |
+ |
210 |
+ try: |
211 |
+ _codec_map[filesystem_encoding] = codecs.lookup(default_fallback) |
212 |
+ except LookupError: |
213 |
+ _codec_map[filesystem_encoding] = _codec_map[default_fallback] |
214 |
+ |
215 |
+ codecs.register(_search_function) |
216 |
+ |
217 |
+def _gen_missing_encodings(missing_encodings): |
218 |
+ |
219 |
+ codec_map = {} |
220 |
+ |
221 |
+ if 'ascii' in missing_encodings: |
222 |
+ |
223 |
+ class AsciiIncrementalEncoder(codecs.IncrementalEncoder): |
224 |
+ def encode(self, input, final=False): |
225 |
+ return codecs.ascii_encode(input, self.errors)[0] |
226 |
+ |
227 |
+ class AsciiIncrementalDecoder(codecs.IncrementalDecoder): |
228 |
+ def decode(self, input, final=False): |
229 |
+ return codecs.ascii_decode(input, self.errors)[0] |
230 |
+ |
231 |
+ class AsciiStreamWriter(codecs.StreamWriter): |
232 |
+ encode = codecs.ascii_encode |
233 |
+ |
234 |
+ class AsciiStreamReader(codecs.StreamReader): |
235 |
+ decode = codecs.ascii_decode |
236 |
+ |
237 |
+ codec_info = codecs.CodecInfo( |
238 |
+ name='ascii', |
239 |
+ encode=codecs.ascii_encode, |
240 |
+ decode=codecs.ascii_decode, |
241 |
+ incrementalencoder=AsciiIncrementalEncoder, |
242 |
+ incrementaldecoder=AsciiIncrementalDecoder, |
243 |
+ streamwriter=AsciiStreamWriter, |
244 |
+ streamreader=AsciiStreamReader, |
245 |
+ ) |
246 |
+ |
247 |
+ for alias in ('ascii', '646', 'ansi_x3.4_1968', 'ansi_x3_4_1968', |
248 |
+ 'ansi_x3.4_1986', 'cp367', 'csascii', 'ibm367', 'iso646_us', |
249 |
+ 'iso_646.irv_1991', 'iso_ir_6', 'us', 'us_ascii'): |
250 |
+ codec_map[alias] = codec_info |
251 |
+ |
252 |
+ if 'utf_8' in missing_encodings: |
253 |
+ |
254 |
+ def utf8decode(input, errors='strict'): |
255 |
+ return codecs.utf_8_decode(input, errors, True) |
256 |
+ |
257 |
+ class Utf8IncrementalEncoder(codecs.IncrementalEncoder): |
258 |
+ def encode(self, input, final=False): |
259 |
+ return codecs.utf_8_encode(input, self.errors)[0] |
260 |
+ |
261 |
+ class Utf8IncrementalDecoder(codecs.BufferedIncrementalDecoder): |
262 |
+ _buffer_decode = codecs.utf_8_decode |
263 |
+ |
264 |
+ class Utf8StreamWriter(codecs.StreamWriter): |
265 |
+ encode = codecs.utf_8_encode |
266 |
+ |
267 |
+ class Utf8StreamReader(codecs.StreamReader): |
268 |
+ decode = codecs.utf_8_decode |
269 |
+ |
270 |
+ codec_info = codecs.CodecInfo( |
271 |
+ name='utf-8', |
272 |
+ encode=codecs.utf_8_encode, |
273 |
+ decode=utf8decode, |
274 |
+ incrementalencoder=Utf8IncrementalEncoder, |
275 |
+ incrementaldecoder=Utf8IncrementalDecoder, |
276 |
+ streamreader=Utf8StreamReader, |
277 |
+ streamwriter=Utf8StreamWriter, |
278 |
+ ) |
279 |
+ |
280 |
+ for alias in ('utf_8', 'u8', 'utf', 'utf8', 'utf8_ucs2', 'utf8_ucs4'): |
281 |
+ codec_map[alias] = codec_info |
282 |
+ |
283 |
+ return codec_map |
284 |
+ |
285 |
+def _search_function(name): |
286 |
+ global _codec_map |
287 |
+ name = name.lower() |
288 |
+ name = name.replace('-', '_') |
289 |
+ codec_info = _codec_map.get(name) |
290 |
+ if codec_info is not None: |
291 |
+ return codecs.CodecInfo( |
292 |
+ name=codec_info.name, |
293 |
+ encode=codec_info.encode, |
294 |
+ decode=codec_info.decode, |
295 |
+ incrementalencoder=codec_info.incrementalencoder, |
296 |
+ incrementaldecoder=codec_info.incrementaldecoder, |
297 |
+ streamreader=codec_info.streamreader, |
298 |
+ streamwriter=codec_info.streamwriter, |
299 |
+ ) |
300 |
+ return None |
301 |
|
302 |
|
303 |
Property changes on: main/trunk/pym/portage/_ensure_encodings.py |
304 |
___________________________________________________________________ |
305 |
Added: svn:keywords |
306 |
+ Id |