Skip to content

Commit cabe8bb

Browse files
committed
add test and fix for PyByteArray_AsString/_Size
1 parent 0d7f039 commit cabe8bb

File tree

4 files changed

+92
-8
lines changed

4 files changed

+92
-8
lines changed

graalpython/com.oracle.graal.python.cext/src/bytearrayobject.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,6 @@ char* _PyByteArray_Start(PyObject* obj) {
4747
return PyByteArrayObject_ob_start(obj);
4848
}
4949

50-
char* PyByteArray_AsString(PyObject* obj) {
51-
return PyByteArray_AS_STRING(obj);
52-
}
53-
54-
Py_ssize_t PyByteArray_Size(PyObject *self) {
55-
return PyByteArray_GET_SIZE(self);
56-
}
57-
5850
// taken from CPython 3.7.0 "Objects/bytearrayobject.c"
5951
int bytearray_getbuffer(PyByteArrayObject *obj, Py_buffer *view, int flags) {
6052
void *ptr;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright (c) 2017, 2023, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* The Universal Permissive License (UPL), Version 1.0
6+
*
7+
* Subject to the condition set forth below, permission is hereby granted to any
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
14+
*
15+
* (a) the Software, and
16+
*
17+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
20+
*
21+
* without restriction, including without limitation the rights to copy, create
22+
* derivative works of, display, perform, and distribute the Software and make,
23+
* use, sell, offer for sale, import, export, have made, and have sold the
24+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
* either these or other terms.
26+
*
27+
* This license is subject to the following condition:
28+
*
29+
* The above copyright notice and either this complete permission notice or at a
30+
* minimum a reference to the UPL must be included in all copies or substantial
31+
* portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
41+
#include "capi.h"
42+
43+
char* PyByteArray_AsString(PyObject* obj) {
44+
return PyByteArray_AS_STRING(obj);
45+
}
46+
47+
Py_ssize_t PyByteArray_Size(PyObject *self) {
48+
return PyByteArray_GET_SIZE(self);
49+
}

graalpython/com.oracle.graal.python.jni/src/capi_native.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,7 @@ static int polyglot_is_value(const void *value) {
634634

635635
#include "_warnings.c"
636636
#include "boolobject.c"
637+
#include "bytearrayobject_shared.c"
637638
#include "longobject_shared.c"
638639
#include "complexobject.c"
639640
#include "dictobject.c"

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ def _as_string(args):
6161
return TypeError()
6262
return args[0].decode()
6363

64+
def _as_string_ba(args):
65+
return args[0].decode()
66+
6467
def _as_string_and_size(args):
6568
if not isinstance(args[0], bytes):
6669
return TypeError()
@@ -90,6 +93,17 @@ def __iter__(self):
9093
tp_free='0',
9194
)
9295

96+
ByteArraySubclass = CPyExtType(
97+
"ByteArraySubclass",
98+
'',
99+
struct_base='PyByteArrayObject bytes',
100+
tp_itemsize='sizeof(char)',
101+
tp_base='&PyByteArray_Type',
102+
tp_new='0',
103+
tp_alloc='0',
104+
tp_free='0',
105+
)
106+
93107

94108
class TestPyBytes(CPyExtTestCase):
95109

@@ -147,6 +161,20 @@ def compile_module(self, name):
147161
cmpfunc=unhandled_error_compare
148162
)
149163

164+
# PyByteArray_AsString
165+
test_PyByteArray_AsString = CPyExtFunction(
166+
_as_string_ba,
167+
lambda: (
168+
(bytearray(b"hello"),),
169+
(bytearray(b"world"),),
170+
(ByteArraySubclass(b"hello"),),
171+
),
172+
resultspec="s",
173+
argspec="O",
174+
arguments=["PyObject* arg"],
175+
cmpfunc=unhandled_error_compare
176+
)
177+
150178
# PyBytes_AsStringAndSize
151179
test_PyBytes_AsStringAndSize = CPyExtFunction(
152180
_as_string_and_size,
@@ -211,6 +239,20 @@ def compile_module(self, name):
211239
arguments=["PyObject* arg"],
212240
)
213241

242+
# PyByteArray_Size
243+
test_PyByteArray_Size = CPyExtFunction(
244+
lambda b: len(b[0]),
245+
lambda: (
246+
(bytearray(b"hello"),),
247+
(bytearray(b"hello world"),),
248+
(bytearray(b""),),
249+
(ByteArraySubclass(b"hello"),),
250+
),
251+
resultspec="n",
252+
argspec="O",
253+
arguments=["PyObject* arg"],
254+
)
255+
214256
# PyBytes_GET_SIZE
215257
test_PyBytes_GET_SIZE = CPyExtFunction(
216258
lambda b: len(b[0]),

0 commit comments

Comments
 (0)