Skip to content

Commit bc556b5

Browse files
fangerertimfel
authored andcommitted
Test non-bytes objects passed to PyBytes_AsString(AndSize)
1 parent 7c8bfd1 commit bc556b5

File tree

1 file changed

+25
-6
lines changed
  • graalpython/com.oracle.graal.python.test/src/tests/cpyext

1 file changed

+25
-6
lines changed

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

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,17 @@ def _reference_from_object(args):
5656
return bytes(obj)
5757
raise TypeError("cannot convert '%s' object to bytes" % type(obj).__name__)
5858

59+
def _as_string(args):
60+
if not isinstance(args[0], bytes):
61+
return TypeError()
62+
return args[0].decode()
5963

6064
def _as_string_and_size(args):
65+
if not isinstance(args[0], bytes):
66+
return TypeError()
6167
arg_bytes = args[0]
6268
s = arg_bytes.decode("utf-8")
63-
return (0, s, len(s))
69+
return s, len(s)
6470

6571

6672
def _reference_format(args):
@@ -127,30 +133,43 @@ def compile_module(self, name):
127133

128134
# PyBytes_AsString
129135
test_PyBytes_AsString = CPyExtFunction(
130-
lambda b: b[0].decode(),
136+
_as_string,
131137
lambda: (
132138
(b"hello",),
133139
(b"world",),
134140
(BytesSubclass(b"hello"),),
141+
(list(),),
142+
("hello",)
135143
),
136144
resultspec="s",
137145
argspec="O",
138146
arguments=["PyObject* arg"],
147+
cmpfunc=unhandled_error_compare
139148
)
140149

141150
# PyBytes_AsStringAndSize
142-
test_PyBytes_AsStringAndSize = CPyExtFunctionOutVars(
151+
test_PyBytes_AsStringAndSize = CPyExtFunction(
143152
_as_string_and_size,
144153
lambda: (
145154
(b"hello",),
146155
(b"world",),
147156
(BytesSubclass(b"hello"),),
157+
(list(),),
158+
("hello",)
148159
),
149-
resultspec="isn",
160+
code="""
161+
static PyObject* wrap_PyBytes_AsStringAndSize(PyObject* arg) {
162+
char* s;
163+
Py_ssize_t sz;
164+
if (PyBytes_AsStringAndSize(arg, &s, &sz) < 0)
165+
return NULL;
166+
return Py_BuildValue("sn", s, sz);
167+
}
168+
""",
169+
callfunction='wrap_PyBytes_AsStringAndSize',
150170
argspec="O",
151171
arguments=["PyObject* arg"],
152-
resultvars=("char* s", "Py_ssize_t sz"),
153-
resulttype="int"
172+
cmpfunc=unhandled_error_compare
154173
)
155174

156175
test_native_storage = CPyExtFunction(

0 commit comments

Comments
 (0)