Skip to content

Commit ac16453

Browse files
committed
[GR-34916] Intrinsify python_cext - PyMemoryView.
PullRequest: graalpython/2095
2 parents 37b184d + d7a4c1f commit ac16453

39 files changed

+2271
-515
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ PyCodeObject* PyCode_NewWithPosOnlyArgs(int argcount, int posonlyargcount, int k
6868
PyObject *varnames, PyObject *freevars, PyObject *cellvars,
6969
PyObject *filename, PyObject *name, int firstlineno,
7070
PyObject *lnotab) {
71-
return (PyCodeObject*)(UPCALL_CEXT_O(_jls_PyCode_NewWithPosOnlyArgs, argcount, kwonlyargcount,
71+
return (PyCodeObject*)(UPCALL_CEXT_O(_jls_PyCode_NewWithPosOnlyArgs, argcount, posonlyargcount, kwonlyargcount,
7272
nlocals, stacksize, flags,
7373
native_to_java(code), native_to_java(consts), native_to_java(names),
7474
native_to_java(varnames), native_to_java(filename), native_to_java(name), firstlineno,

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,28 @@ def compile_module(self, name):
9797
],
9898
cmpfunc=lambda cr, pr: isinstance(cr, types.CodeType),
9999
)
100+
101+
test_PyCode_NewWithPosOnlyArgs = CPyExtFunction(
102+
lambda args: args,
103+
lambda: (
104+
(
105+
1, 0, 2,
106+
3, 4, 0,
107+
b"", tuple(), tuple(),
108+
("a", "b", "c"), tuple(), tuple(),
109+
"filename", "name", 1,
110+
b"",
111+
),
112+
),
113+
resultspec="O",
114+
argspec="iiiiiiOOOOOOOOiO",
115+
arguments=[
116+
"int argcount", "int posonlyargcount", "int kwonlyargcount",
117+
"int nlocals", "int stacksize", "int flags",
118+
"PyObject* code", "PyObject* consts", "PyObject* names",
119+
"PyObject* varnames", "PyObject* freevars", "PyObject* cellvars",
120+
"PyObject* filename", "PyObject* name", "int firstlineno",
121+
"PyObject* lnotab",
122+
],
123+
cmpfunc=lambda cr, pr: isinstance(cr, types.CodeType),
124+
)
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
2+
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3+
#
4+
# The Universal Permissive License (UPL), Version 1.0
5+
#
6+
# Subject to the condition set forth below, permission is hereby granted to any
7+
# person obtaining a copy of this software, associated documentation and/or
8+
# data (collectively the "Software"), free of charge and under any and all
9+
# copyright rights in the Software, and any and all patent rights owned or
10+
# freely licensable by each licensor hereunder covering either (i) the
11+
# unmodified Software as contributed to or provided by such licensor, or (ii)
12+
# the Larger Works (as defined below), to deal in both
13+
#
14+
# (a) the Software, and
15+
#
16+
# (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
17+
# one is included with the Software each a "Larger Work" to which the Software
18+
# is contributed by such licensors),
19+
#
20+
# without restriction, including without limitation the rights to copy, create
21+
# derivative works of, display, perform, and distribute the Software and make,
22+
# use, sell, offer for sale, import, export, have made, and have sold the
23+
# Software and the Larger Work(s), and to sublicense the foregoing rights on
24+
# either these or other terms.
25+
#
26+
# This license is subject to the following condition:
27+
#
28+
# The above copyright notice and either this complete permission notice or at a
29+
# minimum a reference to the UPL must be included in all copies or substantial
30+
# portions of the Software.
31+
#
32+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38+
# SOFTWARE.
39+
40+
import sys
41+
from . import CPyExtTestCase, CPyExtFunction, CPyExtFunctionOutVars, unhandled_error_compare, GRAALPYTHON
42+
__dir__ = __file__.rpartition("/")[0]
43+
44+
import tempfile
45+
46+
tmp_write_file = None
47+
48+
def _reference_write_object(args):
49+
if args[1] is None:
50+
if sys.version_info.minor >= 6:
51+
raise SystemError
52+
else:
53+
raise TypeError
54+
try:
55+
args[1].write(args[0])
56+
except Exception as e:
57+
if sys.version_info.minor >= 6:
58+
raise SystemError
59+
else:
60+
raise e
61+
return 0
62+
63+
class TestPyFile(CPyExtTestCase):
64+
65+
def setUp(self):
66+
tmp_file_path = tempfile.mktemp(prefix="cext-file-test")
67+
global tmp_write_file
68+
open(tmp_file_path, "w")
69+
70+
def tearDown(self):
71+
if tmp_write_file:
72+
tmp_write_file.close()
73+
74+
def compile_module(self, name):
75+
type(self).mro()[1].__dict__["test_%s" % name].create_module(name)
76+
super(TestPyFile, self).compile_module(name)
77+
78+
test_PyFile_WriteObject = CPyExtFunction(
79+
_reference_write_object,
80+
lambda: (
81+
("hello", None, 1),
82+
("hello", "nofile", 1),
83+
("hello", tmp_write_file, 1),
84+
),
85+
resultspec="i",
86+
argspec='OOi',
87+
arguments=["PyObject* o", "PyObject* f", "int flags"],
88+
cmpfunc=unhandled_error_compare
89+
)

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@
4747
class SubModuleType(ModuleType):
4848
pass
4949

50+
def _reference_add_object(args):
51+
if not isinstance(args[0], ModuleType):
52+
if sys.version_info.minor >= 6:
53+
raise SystemError
54+
else:
55+
return -1
56+
args[0].__dict__[args[1]] = args[2]
57+
return 0
5058

5159
class TestPyModule(CPyExtTestCase):
5260

@@ -137,4 +145,19 @@ def compile_module(self, name):
137145
resultspec="O",
138146
argspec='O',
139147
arguments=["PyObject* object"],
140-
)
148+
)
149+
150+
test_PyModule_AddObject = CPyExtFunction(
151+
_reference_add_object,
152+
lambda: (
153+
(1, "testAddObject", None),
154+
(ModuleType("hello"), "testAddObject", None),
155+
(ModuleType("hello"), "testAddObject", "a"),
156+
(SubModuleType("subhello"), "testAddObject", "a"),
157+
),
158+
resultspec="i",
159+
argspec='OsO',
160+
arguments=["PyObject* m", "const char* k", "PyObject* v"],
161+
cmpfunc=unhandled_error_compare
162+
)
163+
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
2+
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3+
#
4+
# The Universal Permissive License (UPL), Version 1.0
5+
#
6+
# Subject to the condition set forth below, permission is hereby granted to any
7+
# person obtaining a copy of this software, associated documentation and/or
8+
# data (collectively the "Software"), free of charge and under any and all
9+
# copyright rights in the Software, and any and all patent rights owned or
10+
# freely licensable by each licensor hereunder covering either (i) the
11+
# unmodified Software as contributed to or provided by such licensor, or (ii)
12+
# the Larger Works (as defined below), to deal in both
13+
#
14+
# (a) the Software, and
15+
#
16+
# (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
17+
# one is included with the Software each a "Larger Work" to which the Software
18+
# is contributed by such licensors),
19+
#
20+
# without restriction, including without limitation the rights to copy, create
21+
# derivative works of, display, perform, and distribute the Software and make,
22+
# use, sell, offer for sale, import, export, have made, and have sold the
23+
# Software and the Larger Work(s), and to sublicense the foregoing rights on
24+
# either these or other terms.
25+
#
26+
# This license is subject to the following condition:
27+
#
28+
# The above copyright notice and either this complete permission notice or at a
29+
# minimum a reference to the UPL must be included in all copies or substantial
30+
# portions of the Software.
31+
#
32+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38+
# SOFTWARE.
39+
40+
import sys
41+
from . import CPyExtTestCase, CPyExtFunction, CPyExtFunctionOutVars, unhandled_error_compare, GRAALPYTHON
42+
__dir__ = __file__.rpartition("/")[0]
43+
44+
#python_run_test_result = None
45+
46+
def _reference_run_string(args):
47+
if not isinstance(args[2], dict):
48+
if sys.version_info.minor >= 6:
49+
raise SystemError
50+
else:
51+
raise TypeError
52+
if not isinstance(args[3], dict):
53+
raise TypeError
54+
return None
55+
56+
def _run_string_compare(x, y):
57+
res = unhandled_error_compare(x, y)
58+
if(isinstance(x, Exception)):
59+
return res
60+
61+
global python_run_test_result
62+
pr = python_run_test_result
63+
res = res and pr == 42
64+
python_run_test_result = None
65+
if not res:
66+
assert False, "python_run_test_result is %s" % pr
67+
return res
68+
69+
class TestPythonRun(CPyExtTestCase):
70+
71+
def compile_module(self, name):
72+
type(self).mro()[1].__dict__["test_%s" % name].create_module(name)
73+
super(TestPythonRun, self).compile_module(name)
74+
75+
test_PyRun_StringFlags = CPyExtFunction(
76+
_reference_run_string,
77+
lambda: (
78+
("globals().update({'python_run_test_result':42})", 256, globals(), locals(), 0),
79+
("globals().update({'python_run_test_result':42})", 256, 'globals()', locals(), 0),
80+
("globals().update({'python_run_test_result':42})", 256, globals(), 'locals()', 0),
81+
),
82+
resultspec="O",
83+
argspec='siOOi',
84+
arguments=["char* source", "int type", "PyObject* globals", "PyObject* locals", "PyCompilerFlags* ignored"],
85+
cmpfunc=unhandled_error_compare
86+
)

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ def compile_module(self, name):
4949
super(TestPySlice, self).compile_module(name)
5050

5151

52+
5253
def reference_get_indices(slize, length):
5354
r = slize.indices(length)
5455
slicelength = 0
@@ -170,3 +171,25 @@ def reference_adjust(length, start, stop, step):
170171
arguments=["int length", "int start", "int stop", "int step"],
171172
cmpfunc=unhandled_error_compare
172173
)
174+
175+
def reference_new_slice(args):
176+
return slice(args[0], args[1], args[2])
177+
178+
test_PySlice_New = CPyExtFunction(
179+
reference_new_slice,
180+
lambda: (
181+
(1, 2, 3,),
182+
(1, 2, "a",),
183+
(1, 2, None,),
184+
(1, None, None,),
185+
(-1, -1, -1,),
186+
("a", "b", "c"),
187+
("a", "b", None),
188+
("a", None, None),
189+
(None, None, None),
190+
),
191+
resultspec="O",
192+
argspec='OOO',
193+
arguments=["PyObject* start", "PyObject* stop", "PyObject* step"],
194+
cmpfunc=unhandled_error_compare
195+
)
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
2+
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3+
#
4+
# The Universal Permissive License (UPL), Version 1.0
5+
#
6+
# Subject to the condition set forth below, permission is hereby granted to any
7+
# person obtaining a copy of this software, associated documentation and/or
8+
# data (collectively the "Software"), free of charge and under any and all
9+
# copyright rights in the Software, and any and all patent rights owned or
10+
# freely licensable by each licensor hereunder covering either (i) the
11+
# unmodified Software as contributed to or provided by such licensor, or (ii)
12+
# the Larger Works (as defined below), to deal in both
13+
#
14+
# (a) the Software, and
15+
#
16+
# (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
17+
# one is included with the Software each a "Larger Work" to which the Software
18+
# is contributed by such licensors),
19+
#
20+
# without restriction, including without limitation the rights to copy, create
21+
# derivative works of, display, perform, and distribute the Software and make,
22+
# use, sell, offer for sale, import, export, have made, and have sold the
23+
# Software and the Larger Work(s), and to sublicense the foregoing rights on
24+
# either these or other terms.
25+
#
26+
# This license is subject to the following condition:
27+
#
28+
# The above copyright notice and either this complete permission notice or at a
29+
# minimum a reference to the UPL must be included in all copies or substantial
30+
# portions of the Software.
31+
#
32+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38+
# SOFTWARE.
39+
40+
import sys
41+
from . import CPyExtTestCase, CPyExtFunction, unhandled_error_compare, GRAALPYTHON
42+
__dir__ = __file__.rpartition("/")[0]
43+
44+
def _reference_get_object(args):
45+
try:
46+
return getattr(sys, args[0])
47+
except AttributeError:
48+
if sys.version_info.minor >= 6:
49+
raise SystemError
50+
else:
51+
raise KeyError(args[0])
52+
53+
class TestPySys(CPyExtTestCase):
54+
55+
def compile_module(self, name):
56+
type(self).mro()[1].__dict__["test_%s" % name].create_module(name)
57+
super(TestPySys, self).compile_module(name)
58+
59+
test_PySys_GetObject = CPyExtFunction(
60+
_reference_get_object,
61+
lambda: (
62+
("hello",),
63+
("byteorder",),
64+
),
65+
resultspec="O",
66+
argspec='s',
67+
arguments=["char* name"],
68+
cmpfunc=unhandled_error_compare
69+
)

0 commit comments

Comments
 (0)