1 |
Author: zmedico |
2 |
Date: 2009-10-26 22:26:31 +0000 (Mon, 26 Oct 2009) |
3 |
New Revision: 14734 |
4 |
|
5 |
Modified: |
6 |
main/trunk/pym/_emerge/JobStatusDisplay.py |
7 |
main/trunk/pym/portage/elog/messages.py |
8 |
main/trunk/pym/portage/output.py |
9 |
main/trunk/pym/portage/util.py |
10 |
Log: |
11 |
Bug #290625 - Manually encode output to stdout in python3, in order to avoid |
12 |
potential UnicodeEncodeError exceptions. |
13 |
|
14 |
|
15 |
Modified: main/trunk/pym/_emerge/JobStatusDisplay.py |
16 |
=================================================================== |
17 |
--- main/trunk/pym/_emerge/JobStatusDisplay.py 2009-10-26 21:45:55 UTC (rev 14733) |
18 |
+++ main/trunk/pym/_emerge/JobStatusDisplay.py 2009-10-26 22:26:31 UTC (rev 14734) |
19 |
@@ -17,6 +17,7 @@ |
20 |
from portage import os |
21 |
from portage import _encodings |
22 |
from portage import _unicode_decode |
23 |
+from portage import _unicode_encode |
24 |
from portage.output import xtermTitle |
25 |
|
26 |
from _emerge.getloadavg import getloadavg |
27 |
@@ -75,11 +76,14 @@ |
28 |
return sys.stdout |
29 |
|
30 |
def _write(self, s): |
31 |
- if sys.hexversion < 0x3000000 and isinstance(s, unicode): |
32 |
- # avoid potential UnicodeEncodeError |
33 |
- s = s.encode(_encodings['stdio'], 'backslashreplace') |
34 |
- self.out.write(s) |
35 |
- self.out.flush() |
36 |
+ # avoid potential UnicodeEncodeError |
37 |
+ s = _unicode_encode(s, |
38 |
+ encoding=_encodings['stdio'], errors='backslashreplace') |
39 |
+ out = self.out |
40 |
+ if sys.hexversion >= 0x3000000: |
41 |
+ out = out.buffer |
42 |
+ out.write(s) |
43 |
+ out.flush() |
44 |
|
45 |
def _init_term(self): |
46 |
""" |
47 |
|
48 |
Modified: main/trunk/pym/portage/elog/messages.py |
49 |
=================================================================== |
50 |
--- main/trunk/pym/portage/elog/messages.py 2009-10-26 21:45:55 UTC (rev 14733) |
51 |
+++ main/trunk/pym/portage/elog/messages.py 2009-10-26 22:26:31 UTC (rev 14734) |
52 |
@@ -96,11 +96,12 @@ |
53 |
|
54 |
formatted_msg = colorize(color, " * ") + msg + "\n" |
55 |
|
56 |
- if sys.hexversion < 0x3000000 and \ |
57 |
- out in (sys.stdout, sys.stderr) and isinstance(formatted_msg, unicode): |
58 |
- # avoid potential UnicodeEncodeError |
59 |
- formatted_msg = formatted_msg.encode( |
60 |
- _encodings['stdio'], 'backslashreplace') |
61 |
+ # avoid potential UnicodeEncodeError |
62 |
+ if out in (sys.stdout, sys.stderr): |
63 |
+ formatted_msg = _unicode_encode(formatted_msg, |
64 |
+ encoding=_encodings['stdio'], errors='backslashreplace') |
65 |
+ if sys.hexversion >= 0x3000000: |
66 |
+ out = out.buffer |
67 |
|
68 |
out.write(formatted_msg) |
69 |
|
70 |
|
71 |
Modified: main/trunk/pym/portage/output.py |
72 |
=================================================================== |
73 |
--- main/trunk/pym/portage/output.py 2009-10-26 21:45:55 UTC (rev 14733) |
74 |
+++ main/trunk/pym/portage/output.py 2009-10-26 22:26:31 UTC (rev 14734) |
75 |
@@ -251,12 +251,16 @@ |
76 |
mystr = mystr[:_max_xtermTitle_len] |
77 |
if not raw: |
78 |
mystr = '\x1b]0;%s\x07' % mystr |
79 |
- if sys.hexversion < 0x3000000 and isinstance(mystr, unicode): |
80 |
- # avoid potential UnicodeEncodeError |
81 |
- mystr = mystr.encode(_encodings['stdio'], 'backslashreplace') |
82 |
- sys.stderr.write(mystr) |
83 |
- sys.stderr.flush() |
84 |
|
85 |
+ # avoid potential UnicodeEncodeError |
86 |
+ mystr = _unicode_encode(mystr, |
87 |
+ encoding=_encodings['stdio'], errors='backslashreplace') |
88 |
+ f = sys.stderr |
89 |
+ if sys.hexversion >= 0x3000000: |
90 |
+ f = f.buffer |
91 |
+ f.write(mystr) |
92 |
+ f.flush() |
93 |
+ |
94 |
default_xterm_title = None |
95 |
|
96 |
def xtermTitleReset(): |
97 |
@@ -374,11 +378,12 @@ |
98 |
self._write(self.write_listener, s) |
99 |
|
100 |
def _write(self, f, s): |
101 |
- if sys.hexversion < 0x3000000 and \ |
102 |
- isinstance(s, unicode) and \ |
103 |
- f in (sys.stdout, sys.stderr): |
104 |
- # avoid potential UnicodeEncodeError |
105 |
- s = s.encode(_encodings['stdio'], 'backslashreplace') |
106 |
+ # avoid potential UnicodeEncodeError |
107 |
+ if f in (sys.stdout, sys.stderr): |
108 |
+ s = _unicode_encode(s, |
109 |
+ encoding=_encodings['stdio'], errors='backslashreplace') |
110 |
+ if sys.hexversion >= 0x3000000: |
111 |
+ f = f.buffer |
112 |
f.write(s) |
113 |
|
114 |
def writelines(self, lines): |
115 |
@@ -484,9 +489,12 @@ |
116 |
sys.stderr.flush() |
117 |
|
118 |
def _write(self, f, s): |
119 |
- if sys.hexversion < 0x3000000 and isinstance(s, unicode): |
120 |
- # avoid potential UnicodeEncodeError |
121 |
- s = s.encode(_encodings['stdio'], 'backslashreplace') |
122 |
+ # avoid potential UnicodeEncodeError |
123 |
+ s = _unicode_encode(s, |
124 |
+ encoding=_encodings['stdio'], errors='backslashreplace') |
125 |
+ f = sys.stderr |
126 |
+ if sys.hexversion >= 0x3000000: |
127 |
+ f = f.buffer |
128 |
f.write(s) |
129 |
f.flush() |
130 |
|
131 |
|
132 |
Modified: main/trunk/pym/portage/util.py |
133 |
=================================================================== |
134 |
--- main/trunk/pym/portage/util.py 2009-10-26 21:45:55 UTC (rev 14733) |
135 |
+++ main/trunk/pym/portage/util.py 2009-10-26 22:26:31 UTC (rev 14734) |
136 |
@@ -67,10 +67,11 @@ |
137 |
if fd is None: |
138 |
fd = sys.stderr |
139 |
if noiselevel <= noiselimit: |
140 |
- if sys.hexversion < 0x3000000: |
141 |
- # avoid potential UnicodeEncodeError |
142 |
- mystr = _unicode_encode(mystr, |
143 |
- encoding=_encodings['stdio'], errors='backslashreplace') |
144 |
+ # avoid potential UnicodeEncodeError |
145 |
+ mystr = _unicode_encode(mystr, |
146 |
+ encoding=_encodings['stdio'], errors='backslashreplace') |
147 |
+ if sys.hexversion >= 0x3000000: |
148 |
+ fd = fd.buffer |
149 |
fd.write(mystr) |
150 |
fd.flush() |