Skip to content

Commit 801a7b8

Browse files
committed
Merge remote-tracking branch 'github/pull/1227' into msimacek/GR-80787-GitHub_PRs_1227-1232
2 parents f30f603 + a5e7761 commit 801a7b8

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)