Skip to content

Commit aef4498

Browse files
committed
[GR-44984] Fix and expand test for PyList_SET_ITEM
PullRequest: graalpython/2837
2 parents 93b6fcb + b88dd90 commit aef4498

File tree

23 files changed

+1071
-608
lines changed

23 files changed

+1071
-608
lines changed

graalpython/com.oracle.graal.python.cext/include/cpython/listobject.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2022, Oracle and/or its affiliates.
1+
/* Copyright (c) 2022, 2023, Oracle and/or its affiliates.
22
* Copyright (C) 1996-2022 Python Software Foundation
33
*
44
* Licensed under the PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2
@@ -35,5 +35,6 @@ PyAPI_FUNC(void) _PyList_DebugMallocStats(FILE *out);
3535
#define _PyList_CAST(op) (assert(PyList_Check(op)), (PyListObject *)(op))
3636

3737
#define PyList_GET_ITEM(op, i) (PyList_GetItem((PyObject *)(op), (i)))
38-
#define PyList_SET_ITEM(op, i, v) (PyList_SetItem((PyObject *)(op), (i), (v)))
38+
PyAPI_FUNC(void) _PyList_SET_ITEM(PyObject *, Py_ssize_t, PyObject *);
39+
#define PyList_SET_ITEM(op, i, v) (_PyList_SET_ITEM((PyObject *)(op), (i), (v)))
3940
#define PyList_GET_SIZE(op) Py_SIZE(_PyList_CAST(op))

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.cext/src/capi.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2361,6 +2361,14 @@ PyAPI_FUNC(int) Py_AtExit(void (*a)(void)) {
23612361
PyAPI_FUNC(PyObject*) Py_CompileString(const char* a, const char* b, int c) {
23622362
return GraalPy_CompileString(truffleString(a), truffleString(b), c);
23632363
}
2364+
#undef Py_CompileStringExFlags
2365+
PyAPI_FUNC(PyObject*) Py_CompileStringExFlags(const char* a, const char* b, int c, PyCompilerFlags* d, int e) {
2366+
return GraalPy_CompileStringExFlags(truffleString(a), truffleString(b), c, d, e);
2367+
}
2368+
#undef Py_CompileStringObject
2369+
PyAPI_FUNC(PyObject*) Py_CompileStringObject(const char* a, PyObject* b, int c, PyCompilerFlags* d, int e) {
2370+
return GraalPy_CompileStringObject(truffleString(a), b, c, d, e);
2371+
}
23642372
#undef Py_EnterRecursiveCall
23652373
PyAPI_FUNC(int) Py_EnterRecursiveCall(const char* a) {
23662374
return GraalPy_EnterRecursiveCall(a);
@@ -2397,6 +2405,10 @@ PyAPI_FUNC(void) _PyErr_WriteUnraisableMsg(const char* a, PyObject* b) {
23972405
PyAPI_FUNC(PyObject*) _PyList_Extend(PyListObject* a, PyObject* b) {
23982406
return Graal_PyList_Extend(a, b);
23992407
}
2408+
#undef _PyList_SET_ITEM
2409+
PyAPI_FUNC(void) _PyList_SET_ITEM(PyObject* a, Py_ssize_t b, PyObject* c) {
2410+
Graal_PyList_SET_ITEM(a, b, c);
2411+
}
24002412
#undef _PyLong_Sign
24012413
PyAPI_FUNC(int) _PyLong_Sign(PyObject* a) {
24022414
return Graal_PyLong_Sign(a);

graalpython/com.oracle.graal.python.cext/src/capi.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,8 @@ typedef struct {
416416
BUILTIN(PyWeakref_NewRef, PyObject*, PyObject*, PyObject*) \
417417
BUILTIN(Py_AtExit, int, void (*)(void)) \
418418
BUILTIN(Py_CompileString, PyObject*, const char*, const char*, int) \
419+
BUILTIN(Py_CompileStringExFlags, PyObject*, const char*, const char*, int, PyCompilerFlags*, int) \
420+
BUILTIN(Py_CompileStringObject, PyObject*, const char*, PyObject*, int, PyCompilerFlags*, int) \
419421
BUILTIN(Py_EnterRecursiveCall, int, const char*) \
420422
BUILTIN(Py_GenericAlias, PyObject*, PyObject*, PyObject*) \
421423
BUILTIN(Py_LeaveRecursiveCall, void) \
@@ -607,6 +609,7 @@ typedef struct {
607609
BUILTIN(_PyErr_BadInternalCall, void, const char*, int) \
608610
BUILTIN(_PyErr_WriteUnraisableMsg, void, const char*, PyObject*) \
609611
BUILTIN(_PyList_Extend, PyObject*, PyListObject*, PyObject*) \
612+
BUILTIN(_PyList_SET_ITEM, void, PyObject*, Py_ssize_t, PyObject*) \
610613
BUILTIN(_PyLong_Sign, int, PyObject*) \
611614
BUILTIN(_PyNamespace_New, PyObject*, PyObject*) \
612615
BUILTIN(_PyNumber_Index, PyObject*, PyObject*) \

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -40,4 +40,6 @@
4040
*/
4141
#include "capi.h"
4242

43-
43+
void* PyTruffle_NativeListItems(PyListObject* list) {
44+
return polyglot_from_PyObjectPtr_array(list->ob_item, list->ob_base.ob_size);
45+
}

graalpython/com.oracle.graal.python.jni/src/capi_forwards.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1363,6 +1363,7 @@ void unimplemented(const char* name) {
13631363
#undef _PyInterpreterState_SetEvalFrameFunc
13641364
#undef _PyList_DebugMallocStats
13651365
#undef _PyList_Extend
1366+
#undef _PyList_SET_ITEM
13661367
#undef _PyLong_AsByteArray
13671368
#undef _PyLong_AsInt
13681369
#undef _PyLong_AsTime_t
@@ -4561,11 +4562,15 @@ PyAPI_FUNC(PyObject*) Py_CompileString(const char* a, const char* b, int c) {
45614562
PyObject* result = (PyObject*) __target__Py_CompileString(a, b, c);
45624563
return result;
45634564
}
4565+
PyObject* (*__target__Py_CompileStringExFlags)(const char*, const char*, int, PyCompilerFlags*, int) = NULL;
45644566
PyAPI_FUNC(PyObject*) Py_CompileStringExFlags(const char* a, const char* b, int c, PyCompilerFlags* d, int e) {
4565-
unimplemented("Py_CompileStringExFlags"); exit(-1);
4567+
PyObject* result = (PyObject*) __target__Py_CompileStringExFlags(a, b, c, d, e);
4568+
return result;
45664569
}
4570+
PyObject* (*__target__Py_CompileStringObject)(const char*, PyObject*, int, PyCompilerFlags*, int) = NULL;
45674571
PyAPI_FUNC(PyObject*) Py_CompileStringObject(const char* a, PyObject* b, int c, PyCompilerFlags* d, int e) {
4568-
unimplemented("Py_CompileStringObject"); exit(-1);
4572+
PyObject* result = (PyObject*) __target__Py_CompileStringObject(a, b, c, d, e);
4573+
return result;
45694574
}
45704575
PyAPI_FUNC(wchar_t*) Py_DecodeLocale(const char* a, size_t* b) {
45714576
unimplemented("Py_DecodeLocale"); exit(-1);
@@ -5165,6 +5170,9 @@ PyAPI_FUNC(PyObject*) _PyList_Extend(PyListObject* a, PyObject* b) {
51655170
PyObject* result = (PyObject*) Graal_PyList_Extend(a, b);
51665171
return result;
51675172
}
5173+
PyAPI_FUNC(void) _PyList_SET_ITEM(PyObject* a, Py_ssize_t b, PyObject* c) {
5174+
Graal_PyList_SET_ITEM(a, b, c);
5175+
}
51685176
PyAPI_FUNC(time_t) _PyLong_AsTime_t(PyObject* a) {
51695177
unimplemented("_PyLong_AsTime_t"); exit(-1);
51705178
}
@@ -6438,6 +6446,8 @@ void initializeCAPIForwards(void* (*getAPI)(const char*)) {
64386446
__target__PyUnicode_New = getAPI("PyUnicode_New");
64396447
__target__PyVectorcall_Call = getAPI("PyVectorcall_Call");
64406448
__target__Py_CompileString = getAPI("Py_CompileString");
6449+
__target__Py_CompileStringExFlags = getAPI("Py_CompileStringExFlags");
6450+
__target__Py_CompileStringObject = getAPI("Py_CompileStringObject");
64416451
__target__Py_GetBuildInfo = getAPI("Py_GetBuildInfo");
64426452
__target__Py_GetCompiler = getAPI("Py_GetCompiler");
64436453
__target__Py_GetVersion = getAPI("Py_GetVersion");

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

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

666666
#include "_warnings.c"
667667
#include "boolobject.c"
668+
#include "bytearrayobject_shared.c"
668669
#include "longobject_shared.c"
669670
#include "complexobject.c"
670671
#include "dictobject.c"

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/interop/JavaInteropTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,11 @@ public void builtinFunctionsAreImporteable() throws UnsupportedEncodingException
257257
assertEquals("Hello World\n", out.toString("UTF-8"));
258258
}
259259

260+
@Test
261+
public void enumeratingMainBindingsWorks() throws Exception {
262+
assertTrue(context.getBindings("python").getMemberKeys().contains("__builtins__"));
263+
}
264+
260265
@Test
261266
public void testMultipleInvocationsAreInSameScope() throws UnsupportedEncodingException {
262267
String source = "def foo(a, b):\n" +

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

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1219,7 +1219,102 @@ def compile_module(self, name):
12191219
arguments=["PyObject* haystack", "PyObject* needle"],
12201220
cmpfunc=unhandled_error_compare
12211221
)
1222-
1222+
1223+
def index_func(args):
1224+
for idx,obj in enumerate(args[0]):
1225+
if obj == args[1]:
1226+
return idx
1227+
raise ValueError
1228+
1229+
test_PySequence_Index = CPyExtFunction(
1230+
index_func,
1231+
lambda: (
1232+
(tuple(), 1),
1233+
((1, 2, 3), 1),
1234+
((1, 2, 3), 4),
1235+
((None,), 1),
1236+
([], 1),
1237+
(['a', 'b', 'c'], 'a'),
1238+
(['a', 'b', 'c'], 'd'),
1239+
([None], 1),
1240+
(set(), 1),
1241+
({'a', 'b'}, 'a'),
1242+
({'a', 'b'}, 'c'),
1243+
(DummyListSubclass(), 1),
1244+
('hello', 'e'),
1245+
('hello', 'x'),
1246+
),
1247+
resultspec="i",
1248+
argspec='OO',
1249+
arguments=["PyObject* haystack", "PyObject* needle"],
1250+
cmpfunc=unhandled_error_compare
1251+
)
1252+
1253+
def count_func(args):
1254+
c = 0
1255+
for obj in args[0]:
1256+
if obj == args[1]:
1257+
c += 1
1258+
return c
1259+
1260+
test_PySequence_Count = CPyExtFunction(
1261+
count_func,
1262+
lambda: (
1263+
(tuple(), 1),
1264+
((1, 2, 3), 1),
1265+
((1, 2, 3), 4),
1266+
((None,), 1),
1267+
([], 1),
1268+
(['a', 'b', 'c'], 'a'),
1269+
(['a', 'b', 'c'], 'd'),
1270+
([None], 1),
1271+
(set(), 1),
1272+
({'a', 'b'}, 'a'),
1273+
({'a', 'b'}, 'c'),
1274+
(DummyListSubclass(), 1),
1275+
('hello', 'e'),
1276+
('hello', 'x'),
1277+
),
1278+
resultspec="i",
1279+
argspec='OO',
1280+
arguments=["PyObject* haystack", "PyObject* needle"],
1281+
cmpfunc=unhandled_error_compare
1282+
)
1283+
1284+
def _reference_setslice(args):
1285+
args[0][args[1]:args[2]] = args[3]
1286+
return 0;
1287+
1288+
test_PySequence_SetSlice = CPyExtFunction(
1289+
_reference_setslice,
1290+
lambda: (
1291+
([1,2,3,4],0,4,[5,6,7,8]),
1292+
([],1,2, [5,6]),
1293+
([1,2,3,4],10,20,[5,6,7,8]),
1294+
),
1295+
resultspec="i",
1296+
argspec='OnnO',
1297+
arguments=["PyObject* op", "Py_ssize_t ilow", "Py_ssize_t ihigh", "PyObject* v"],
1298+
cmpfunc=unhandled_error_compare
1299+
)
1300+
1301+
def _reference_delslice(args):
1302+
del args[0][args[1]:args[2]]
1303+
return 0;
1304+
1305+
test_PySequence_DelSlice = CPyExtFunction(
1306+
_reference_delslice,
1307+
lambda: (
1308+
([1,2,3,4],0,4),
1309+
([],1,2),
1310+
([1,2,3,4],10,20),
1311+
),
1312+
resultspec="i",
1313+
argspec='Onn',
1314+
arguments=["PyObject* op", "Py_ssize_t ilow", "Py_ssize_t ihigh"],
1315+
cmpfunc=unhandled_error_compare
1316+
)
1317+
12231318
test_PySequence_ITEM = CPyExtFunction(
12241319
_reference_getitem,
12251320
lambda: (

0 commit comments

Comments
 (0)