Skip to content

Commit 476fdcb

Browse files
committed
Handle bytearray from None
Fixes #972
1 parent 371b0f1 commit 476fdcb

4 files changed

Lines changed: 22 additions & 12 deletions

File tree

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,22 @@ def test_from_list():
7272
assert list(b) == ints
7373

7474

75+
def test_bytearray_from_none():
76+
assert_raises(TypeError, bytes, None)
77+
assert_raises(TypeError, bytearray, None)
78+
79+
80+
def test_bytes_with_encoding_does_not_call_dunder_bytes():
81+
class BytesLike:
82+
def __bytes__(self):
83+
return b"bytes"
84+
85+
assert bytes(BytesLike()) == b"bytes"
86+
assert_raises(TypeError, bytes, BytesLike(), None)
87+
assert_raises(TypeError, bytes, encoding="utf-8")
88+
assert_raises(TypeError, bytes, errors="strict")
89+
90+
7591
def test_from_ssize():
7692
assert bytes(0) == b''
7793
assert bytes(1) == b'\x00'

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, 2025, Oracle and/or its affiliates.
2+
* Copyright (c) 2020, 2026, Oracle and/or its affiliates.
33
* Copyright (c) 2014, Regents of the University of California
44
*
55
* All rights reserved.
@@ -164,20 +164,14 @@ protected ArgumentClinicProvider getArgumentClinic() {
164164
return ByteArrayBuiltinsClinicProviders.InitNodeClinicProviderGen.INSTANCE;
165165
}
166166

167-
@Specialization(guards = "!isNone(source)")
167+
@Specialization
168168
static PNone doInit(VirtualFrame frame, PByteArray self, Object source, Object encoding, Object errors,
169169
@Bind Node inliningTarget,
170170
@Cached BytesNodes.BytesInitNode toBytesNode) {
171171
self.setSequenceStorage(new ByteSequenceStorage(toBytesNode.execute(frame, inliningTarget, source, encoding, errors)));
172172
return PNone.NONE;
173173
}
174174

175-
@Specialization(guards = "isNone(self)")
176-
static PNone doInit(@SuppressWarnings("unused") PByteArray self, Object source, @SuppressWarnings("unused") Object encoding, @SuppressWarnings("unused") Object errors,
177-
@Bind Node inliningTarget) {
178-
throw PRaiseNode.raiseStatic(inliningTarget, TypeError, ErrorMessages.CANNOT_CONVERT_P_OBJ_TO_S, source, "bytearray");
179-
}
180-
181175
@Specialization(guards = "!isBytes(self)")
182176
static PNone doInit(Object self, @SuppressWarnings("unused") Object source, @SuppressWarnings("unused") Object encoding, @SuppressWarnings("unused") Object errors,
183177
@Bind Node inliningTarget) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,14 @@ protected ArgumentClinicProvider getArgumentClinic() {
131131
}
132132

133133
@SuppressWarnings("unused")
134-
@Specialization(guards = "isNoValue(source)")
134+
@Specialization(guards = {"isNoValue(source)", "isNoValue(encoding)", "isNoValue(errors)"})
135135
static Object doEmpty(Object cls, PNone source, PNone encoding, PNone errors,
136136
@Bind Node inliningTarget,
137137
@Exclusive @Cached CreateBytes createBytes) {
138138
return createBytes.execute(inliningTarget, cls, PythonUtils.EMPTY_BYTE_ARRAY);
139139
}
140140

141-
@Specialization(guards = "!isNoValue(source)")
141+
@Specialization(guards = {"!isNoValue(source)", "isNoValue(encoding)", "isNoValue(errors)"})
142142
static Object doCallBytes(VirtualFrame frame, Object cls, Object source, PNone encoding, PNone errors,
143143
@Bind Node inliningTarget,
144144
@Cached GetClassNode getClassNode,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -629,8 +629,8 @@ public abstract static class BytesInitNode extends PNodeWithContext {
629629

630630
public abstract byte[] execute(VirtualFrame frame, Node inliningTarget, Object source, Object encoding, Object errors);
631631

632-
@Specialization
633-
static byte[] none(@SuppressWarnings("unused") PNone source, @SuppressWarnings("unused") PNone encoding, @SuppressWarnings("unused") PNone errors) {
632+
@Specialization(guards = "isNoValue(source)")
633+
static byte[] noValue(@SuppressWarnings("unused") PNone source, @SuppressWarnings("unused") PNone encoding, @SuppressWarnings("unused") PNone errors) {
634634
return PythonUtils.EMPTY_BYTE_ARRAY;
635635
}
636636

0 commit comments

Comments
 (0)