Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions graalpython/com.oracle.graal.python.test/src/tests/test_bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,16 @@ def __bytes__(self):
assert_raises(TypeError, bytes, errors="strict")


def test_decode_empty_skips_codec_lookup():
assert str(b'', 'x') == ''
assert str(bytearray(), 'x', 'y') == ''
assert str(memoryview(b''), 'x') == ''
assert b''.decode('x') == ''
assert bytearray().decode('x') == ''
assert_raises(LookupError, str, b'a', 'x')
assert_raises(TypeError, str, 1, 'x')


def test_from_ssize():
assert bytes(0) == b''
assert bytes(1) == b'\x00'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,15 @@ protected ArgumentClinicProvider getArgumentClinic() {
@Specialization
static Object decode(VirtualFrame frame, Object self, TruffleString encoding, TruffleString errors,
@Bind Node inliningTarget,
@Cached GetBytesStorage getBytesStorage,
@Cached InlinedConditionProfile emptyStringProfile,
@Cached CodecsModuleBuiltins.DecodeNode decodeNode,
@Cached PyUnicodeCheckNode unicodeCheckNode,
@Cached PRaiseNode raiseNode) {
// like CPython's PyUnicode_FromEncodedObject: no codec lookup for empty input
if (emptyStringProfile.profile(inliningTarget, getBytesStorage.execute(inliningTarget, self).length() == 0)) {
return T_EMPTY_STRING;
}
Object result = decodeNode.executeWithStrings(frame, self, encoding, errors);
if (!unicodeCheckNode.execute(inliningTarget, result)) {
throw raiseNode.raise(inliningTarget, TypeError, DECODER_RETURNED_P_INSTEAD_OF_BYTES, encoding, result);
Expand Down