Skip to content

Commit 1a7cc3e

Browse files
committed
Add __str__ for UnicodeError subclasses
1 parent e96f5f3 commit 1a7cc3e

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

graalpython/lib-graalpython/exceptions.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,25 @@ def UnicodeEncodeError__init__(self, encoding, object, start, end, reason):
120120
self.reason = reason
121121

122122

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+
123138
UnicodeEncodeError.__init__ = UnicodeEncodeError__init__
139+
UnicodeEncodeError.__str__ = UnicodeEncodeError__str__
124140
del UnicodeEncodeError__init__
141+
del UnicodeEncodeError__str__
125142

126143

127144
def UnicodeDecodeError__init__(self, encoding, object, start, end, reason):
@@ -135,9 +152,28 @@ def UnicodeDecodeError__init__(self, encoding, object, start, end, reason):
135152
self.end = end
136153
self.reason = reason
137154

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+
138172

139173
UnicodeDecodeError.__init__ = UnicodeDecodeError__init__
174+
UnicodeDecodeError.__str__ = UnicodeDecodeError__str__
140175
del UnicodeDecodeError__init__
176+
del UnicodeDecodeError__str__
141177

142178

143179
def UnicodeTranslateError__init__(self, object, start, end, reason):
@@ -147,8 +183,25 @@ def UnicodeTranslateError__init__(self, object, start, end, reason):
147183
self.reason = reason
148184

149185

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+
150201
UnicodeTranslateError.__init__ = UnicodeTranslateError__init__
202+
UnicodeTranslateError.__str__ = UnicodeTranslateError__str__
151203
del UnicodeTranslateError__init__
204+
del UnicodeTranslateError__str__
152205

153206

154207
# These errors are just an alias of OSError (i.e. 'EnvironmentError is OSError == True')

0 commit comments

Comments
 (0)