Skip to content

Commit

Permalink
[GR-11778] Fix: allow NULL for 'PyBytes_FromStringAndSize'.
Browse files Browse the repository at this point in the history
PullRequest: graalpython/199
  • Loading branch information
fangerer committed Sep 21, 2018
2 parents 52d9340 + 4ef878c commit 870e9e7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,20 @@ def compile_module(self, name):
arguments=("char* str", "Py_ssize_t sz"),
)

# PyBytes_FromStringAndSize
test_PyBytes_FromStringAndSizeNULL = CPyExtFunction(
lambda args: len(b"\x00"*args[0]),
lambda: ( (128, ), ),
code = """PyObject* PyBytes_FromStringAndSizeNULL(Py_ssize_t n) {
// we are return the length because the content is random (uninitialized)
return PyBytes_Size(PyBytes_FromStringAndSize(NULL, n));
}
""",
resultspec="n",
argspec='n',
arguments=["Py_ssize_t n"],
)

# PyBytes_FromString
test_PyBytes_FromString = CPyExtFunction(
lambda arg: bytes(arg[0], "utf-8"),
Expand Down
2 changes: 1 addition & 1 deletion graalpython/lib-graalpython/python_cext.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def PyTruffle_Object_LEN(obj):
##################### BYTES

def PyBytes_FromStringAndSize(string, size, encoding):
if string is not None:
if string is not None and string is not Py_NoValue():
return bytes(string, encoding)
assert size >= 0;
return PyTruffle_Bytes_EmptyWithCapacity(size, native_null)
Expand Down

0 comments on commit 870e9e7

Please sign in to comment.