Skip to content

Commit a5e7761

Browse files
committed
Skip the codec lookup when decoding empty bytes
BytesCommonBuiltins.DecodeNode, which backs bytes.decode, bytearray.decode and str(obj, encoding), always went through the codec, so b''.decode('bogus') and str(b'', 'bogus') raised LookupError. CPython's bytes and bytearray decode call PyUnicode_FromEncodedObject, which returns '' for empty input without looking up the codec outside dev mode. GraalPy's own PyUnicodeFromEncodedObject already does this for the C API; DecodeNode now has the same shortcut. Signed-off-by: Jeongseop Lim <jeongseop_lim@korea.ac.kr>
1 parent 7e7182b commit a5e7761

2 files changed

Lines changed: 16 additions & 0 deletions

File tree

‎graalpython/com.oracle.graal.python.test/src/tests/test_bytes.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,16 @@ def __bytes__(self):
8989
assert_raises(TypeError, bytes, errors="strict")
9090

9191

92+
def test_decode_empty_skips_codec_lookup():
93+
assert str(b'', 'x') == ''
94+
assert str(bytearray(), 'x', 'y') == ''
95+
assert str(memoryview(b''), 'x') == ''
96+
assert b''.decode('x') == ''
97+
assert bytearray().decode('x') == ''
98+
assert_raises(LookupError, str, b'a', 'x')
99+
assert_raises(TypeError, str, 1, 'x')
100+
101+
92102
def test_from_ssize():
93103
assert bytes(0) == b''
94104
assert bytes(1) == b'\x00'

‎graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/bytes/BytesCommonBuiltins.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,15 @@ protected ArgumentClinicProvider getArgumentClinic() {
203203
@Specialization
204204
static Object decode(VirtualFrame frame, Object self, TruffleString encoding, TruffleString errors,
205205
@Bind Node inliningTarget,
206+
@Cached GetBytesStorage getBytesStorage,
207+
@Cached InlinedConditionProfile emptyStringProfile,
206208
@Cached CodecsModuleBuiltins.DecodeNode decodeNode,
207209
@Cached PyUnicodeCheckNode unicodeCheckNode,
208210
@Cached PRaiseNode raiseNode) {
211+
// like CPython's PyUnicode_FromEncodedObject: no codec lookup for empty input
212+
if (emptyStringProfile.profile(inliningTarget, getBytesStorage.execute(inliningTarget, self).length() == 0)) {
213+
return T_EMPTY_STRING;
214+
}
209215
Object result = decodeNode.executeWithStrings(frame, self, encoding, errors);
210216
if (!unicodeCheckNode.execute(inliningTarget, result)) {
211217
throw raiseNode.raise(inliningTarget, TypeError, DECODER_RETURNED_P_INSTEAD_OF_BYTES, encoding, result);

0 commit comments

Comments
 (0)