Skip to content

Commit 795724c

Browse files
committed
Move the encoding result check to correct place
1 parent fa3ed67 commit 795724c

File tree

3 files changed

+14
-12
lines changed

3 files changed

+14
-12
lines changed

graalpython/lib-graalpython/_codecs.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,7 @@ def encode(obj, encoding='utf-8', errors='strict'):
121121
result = encoder(obj, errors)
122122
if not (isinstance(result, tuple) and len(result) == 2):
123123
raise TypeError('encoder must return a tuple (object, integer)')
124-
encoded = result[0]
125-
if not isinstance(encoded, bytes):
126-
raise TypeError("'%s' encoder returned '%s' instead of 'bytes'; use codecs.encode() to encode to arbitrary types"
127-
% (encoding, type(encoded).__name__))
128-
return encoded
124+
return result[0]
129125

130126

131127
@__graalpython__.builtin
@@ -135,11 +131,7 @@ def decode(obj, encoding='utf-8', errors='strict'):
135131
result = decoder(obj, errors)
136132
if not (isinstance(result, tuple) and len(result) == 2):
137133
raise TypeError('decoder must return a tuple (object, integer)')
138-
decoded = result[0]
139-
if not isinstance(decoded, str):
140-
raise TypeError("'%s' encoder returned '%s' instead of 'str'; use codecs.encode() to encode to arbitrary types"
141-
% (encoding, type(decoded).__name__))
142-
return decoded
134+
return result[0]
143135

144136

145137
@__graalpython__.builtin

graalpython/lib-graalpython/bytes.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@ def decode(self, encoding="utf-8", errors="strict"):
5252
as well as any other name registered with codecs.register_error that
5353
can handle UnicodeDecodeErrors.
5454
"""
55-
return _codecs.decode(self, encoding=encoding, errors=errors)
55+
result = _codecs.decode(self, encoding=encoding, errors=errors)
56+
if not isinstance(result, str):
57+
raise TypeError("'%s' encoder returned '%s' instead of 'str'; use codecs.decode() to decode to arbitrary types"
58+
% (encoding, type(result).__name__))
59+
return result
5660

5761

5862
bytes.decode = __graalpython__.builtin_method(decode)

graalpython/lib-graalpython/str.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,13 @@ def encode(self, encoding="utf-8", errors="strict"):
375375
as well as any other name registered with codecs.register_error that
376376
can handle UnicodeDecodeErrors.
377377
"""
378-
return _codecs.encode(self, encoding=encoding, errors=errors)
378+
result = _codecs.encode(self, encoding=encoding, errors=errors)
379+
if not isinstance(result, bytes):
380+
if isinstance(result, bytearray):
381+
return bytes(result)
382+
raise TypeError("'%s' encoder returned '%s' instead of 'bytes'; use codecs.encode() to encode to arbitrary types"
383+
% (encoding, type(result).__name__))
384+
return result
379385

380386

381387
str.encode = encode

0 commit comments

Comments
 (0)