Skip to content

Commit 0bfc8e5

Browse files
fangererlukasstadler
authored andcommitted
Do not use PyBytes_FromObject if NULL was passed to PyObject_Bytes
1 parent 96e1f94 commit 0bfc8e5

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/cext/PythonCextObjectBuiltins.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
import com.oracle.graal.python.builtins.modules.cext.PythonCextBytesBuiltins.PyBytes_FromObject;
7575
import com.oracle.graal.python.builtins.objects.PNone;
7676
import com.oracle.graal.python.builtins.objects.PNotImplemented;
77+
import com.oracle.graal.python.builtins.objects.bytes.BytesUtils;
7778
import com.oracle.graal.python.builtins.objects.bytes.PBytesLike;
7879
import com.oracle.graal.python.builtins.objects.cext.capi.CApiContext;
7980
import com.oracle.graal.python.builtins.objects.cext.capi.CApiGuards;
@@ -478,9 +479,12 @@ static Object bytes(Object obj,
478479
}
479480

480481
@Specialization(guards = "isNoValue(obj)")
481-
static Object bytesNoValue(@SuppressWarnings("unused") Object obj,
482-
@Cached PyBytes_FromObject fromObjectNode) {
483-
return fromObjectNode.execute(StringLiterals.T_NULL_RESULT);
482+
Object bytesNoValue(@SuppressWarnings("unused") Object obj) {
483+
/*
484+
* Note: CPython calls PyBytes_FromString("<NULL>") but we do not directly have it.
485+
* Therefore, we directly create the bytes object with string "<NULL>" here.
486+
*/
487+
return factory().createBytes(BytesUtils.NULL_STRING);
484488
}
485489

486490
protected static boolean hasBytes(Object obj, PyObjectLookupAttr lookupAttrNode) {

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2022, Oracle and/or its affiliates.
2+
* Copyright (c) 2017, 2023, Oracle and/or its affiliates.
33
* Copyright (c) 2014, Regents of the University of California
44
*
55
* All rights reserved.
@@ -39,6 +39,9 @@ public final class BytesUtils {
3939

4040
@CompilationFinal(dimensions = 1) public static final byte[] HEXDIGITS = "0123456789abcdef".getBytes();
4141

42+
/** Bytes of ASCII string {@code "<NULL>"} */
43+
@CompilationFinal(dimensions = 1) public static final byte[] NULL_STRING = {60, 78, 85, 76, 76, 62};
44+
4245
// tables are copied from CPython/Python/pyctype.c
4346
static final byte PY_CTF_LOWER = 0x01;
4447
static final byte PY_CTF_UPPER = 0x02;

0 commit comments

Comments
 (0)