Skip to content

Commit 4ef878c

Browse files
committed
Fix: allow NULL for 'PyBytes_FromStringAndSize'.
1 parent 52d9340 commit 4ef878c

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,20 @@ def compile_module(self, name):
7676
arguments=("char* str", "Py_ssize_t sz"),
7777
)
7878

79+
# PyBytes_FromStringAndSize
80+
test_PyBytes_FromStringAndSizeNULL = CPyExtFunction(
81+
lambda args: len(b"\x00"*args[0]),
82+
lambda: ( (128, ), ),
83+
code = """PyObject* PyBytes_FromStringAndSizeNULL(Py_ssize_t n) {
84+
// we are return the length because the content is random (uninitialized)
85+
return PyBytes_Size(PyBytes_FromStringAndSize(NULL, n));
86+
}
87+
""",
88+
resultspec="n",
89+
argspec='n',
90+
arguments=["Py_ssize_t n"],
91+
)
92+
7993
# PyBytes_FromString
8094
test_PyBytes_FromString = CPyExtFunction(
8195
lambda arg: bytes(arg[0], "utf-8"),

graalpython/lib-graalpython/python_cext.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ def PyTruffle_Object_LEN(obj):
216216
##################### BYTES
217217

218218
def PyBytes_FromStringAndSize(string, size, encoding):
219-
if string is not None:
219+
if string is not None and string is not Py_NoValue():
220220
return bytes(string, encoding)
221221
assert size >= 0;
222222
return PyTruffle_Bytes_EmptyWithCapacity(size, native_null)

0 commit comments

Comments
 (0)