@@ -120,8 +120,25 @@ def UnicodeEncodeError__init__(self, encoding, object, start, end, reason):
120
120
self .reason = reason
121
121
122
122
123
+ def UnicodeEncodeError__str__ (self ):
124
+ if not hasattr (self , 'object' ):
125
+ return ""
126
+ if self .start < len (self .object ) and self .start + 1 == self .end :
127
+ badchar = ord (self .object [self .start ])
128
+ if badchar <= 0xff :
129
+ fmt = "'%s' codec can't encode character '\\ x%02x' in position %d: %s"
130
+ elif badchar <= 0xffff :
131
+ fmt = "'%s' codec can't encode character '\\ u%04x' in position %d: %s"
132
+ else :
133
+ fmt = "'%s' codec can't encode character '\\ U%08x' in position %d: %s"
134
+ return fmt % (self .encoding , badchar , self .start , self .reason )
135
+ return "'%s' codec can't encode characters in position %d-%d: %s" % (self .encoding , self .start , self .end - 1 , self .reason )
136
+
137
+
123
138
UnicodeEncodeError .__init__ = UnicodeEncodeError__init__
139
+ UnicodeEncodeError .__str__ = UnicodeEncodeError__str__
124
140
del UnicodeEncodeError__init__
141
+ del UnicodeEncodeError__str__
125
142
126
143
127
144
def UnicodeDecodeError__init__ (self , encoding , object , start , end , reason ):
@@ -135,9 +152,28 @@ def UnicodeDecodeError__init__(self, encoding, object, start, end, reason):
135
152
self .end = end
136
153
self .reason = reason
137
154
155
+ def UnicodeEncodeError__init__ (self , encoding , object , start , end , reason ):
156
+ BaseException .__init__ (self , encoding , object , start , end , reason )
157
+ self .encoding = encoding
158
+ self .object = object
159
+ self .start = start
160
+ self .end = end
161
+ self .reason = reason
162
+
163
+
164
+ def UnicodeDecodeError__str__ (self ):
165
+ if not hasattr (self , 'object' ):
166
+ return ""
167
+ if self .start < len (self .object ) and self .start + 1 == self .end :
168
+ byte = self .object [self .start ]
169
+ return "'%s' codec can't decode byte 0x%02x in position %d: %s" % (self .encoding , byte , self .start , self .reason )
170
+ return "'%s' codec can't decode bytes in position %d-%d: %s" % (self .encoding , self .start , self .end - 1 , self .reason )
171
+
138
172
139
173
UnicodeDecodeError .__init__ = UnicodeDecodeError__init__
174
+ UnicodeDecodeError .__str__ = UnicodeDecodeError__str__
140
175
del UnicodeDecodeError__init__
176
+ del UnicodeDecodeError__str__
141
177
142
178
143
179
def UnicodeTranslateError__init__ (self , object , start , end , reason ):
@@ -147,8 +183,25 @@ def UnicodeTranslateError__init__(self, object, start, end, reason):
147
183
self .reason = reason
148
184
149
185
186
+ def UnicodeTranslateError__str__ (self ):
187
+ if not hasattr (self , 'object' ):
188
+ return ""
189
+ if self .start < len (self .object ) and self .start + 1 == self .end :
190
+ badchar = ord (self .object [self .start ])
191
+ if badchar <= 0xff :
192
+ fmt = "can't translate character '\\ x%02x' in position %d: %s"
193
+ elif badchar <= 0xffff :
194
+ fmt = "can't translate character '\\ u%04x' in position %d: %s"
195
+ else :
196
+ fmt = "can't translate character '\\ U%08x' in position %d: %s"
197
+ return fmt % (badchar , self .start , self .reason )
198
+ return "can't translate characters in position %d-%d: %s" % (self .start , self .end - 1 , self .reason )
199
+
200
+
150
201
UnicodeTranslateError .__init__ = UnicodeTranslateError__init__
202
+ UnicodeTranslateError .__str__ = UnicodeTranslateError__str__
151
203
del UnicodeTranslateError__init__
204
+ del UnicodeTranslateError__str__
152
205
153
206
154
207
# These errors are just an alias of OSError (i.e. 'EnvironmentError is OSError == True')
0 commit comments