Skip to content

Commit 9c99c29

Browse files
committed
[GR-27098] Do not reference run-time objects in C ext root nodes.
PullRequest: graalpython/1367
2 parents c342dcd + cc8dc58 commit 9c99c29

File tree

17 files changed

+373
-220
lines changed

17 files changed

+373
-220
lines changed

graalpython/com.oracle.graal.python.cext/include/Python.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
#include "modsupport.h"
8181
#include "tupleobject.h"
8282
#include "structseq.h"
83+
#include "namespaceobject.h"
8384
#include "structmember.h"
8485
#include "pytime.h"
8586
#include "pymem.h"
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/* Copyright (c) 2020, Oracle and/or its affiliates.
2+
* Copyright (C) 1996-2020 Python Software Foundation
3+
*
4+
* Licensed under the PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2
5+
*/
6+
7+
/* simple namespace object interface */
8+
9+
#ifndef NAMESPACEOBJECT_H
10+
#define NAMESPACEOBJECT_H
11+
#ifdef __cplusplus
12+
extern "C" {
13+
#endif
14+
15+
#ifndef Py_LIMITED_API
16+
PyAPI_DATA(PyTypeObject) _PyNamespace_Type;
17+
18+
PyAPI_FUNC(PyObject *) _PyNamespace_New(PyObject *kwds);
19+
#endif /* !Py_LIMITED_API */
20+
21+
#ifdef __cplusplus
22+
}
23+
#endif
24+
#endif /* !NAMESPACEOBJECT_H */

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,9 @@ PyObject* PyTruffle_MemoryViewFromObject(PyObject *v) {
121121
polyglot_from_string(buffer->format ? buffer->format : "B", "ascii"),
122122
buffer->ndim,
123123
polyglot_from_i8_array(buffer->buf, buffer->len),
124-
buffer->shape ? polyglot_from_size_array(buffer->shape, ndim) : NULL,
125-
buffer->strides ? polyglot_from_size_array(buffer->strides, ndim) : NULL,
126-
buffer->suboffsets ? polyglot_from_size_array(buffer->suboffsets, ndim) : NULL);
124+
buffer->shape ? polyglot_from_size_array((int64_t*)buffer->shape, ndim) : NULL,
125+
buffer->strides ? polyglot_from_size_array((int64_t*)buffer->strides, ndim) : NULL,
126+
buffer->suboffsets ? polyglot_from_size_array((int64_t*)buffer->suboffsets, ndim) : NULL);
127127
if (!needs_release) {
128128
free(buffer);
129129
}
@@ -164,9 +164,9 @@ PyObject* PyMemoryView_FromBuffer(Py_buffer *buffer) {
164164
polyglot_from_string(buffer->format ? buffer->format : "B", "ascii"),
165165
buffer->ndim,
166166
polyglot_from_i8_array(buffer->buf, buffer->len),
167-
buffer->shape ? polyglot_from_size_array(buffer->shape, ndim) : NULL,
168-
buffer->strides ? polyglot_from_size_array(buffer->strides, ndim) : NULL,
169-
buffer->suboffsets ? polyglot_from_size_array(buffer->suboffsets, ndim) : NULL);
167+
buffer->shape ? polyglot_from_size_array((int64_t*)buffer->shape, ndim) : NULL,
168+
buffer->strides ? polyglot_from_size_array((int64_t*)buffer->strides, ndim) : NULL,
169+
buffer->suboffsets ? polyglot_from_size_array((int64_t*)buffer->suboffsets, ndim) : NULL);
170170
}
171171

172172
PyObject *PyMemoryView_FromMemory(char *mem, Py_ssize_t size, int flags) {
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright (c) 2020, 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+
typedef struct {
44+
PyObject_HEAD
45+
PyObject *ns_dict;
46+
} _PyNamespaceObject;
47+
48+
PyTypeObject _PyNamespace_Type = PY_TRUFFLE_TYPE("SimpleNamespace", &PyType_Type, Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE, sizeof(_PyNamespaceObject));
49+
50+
UPCALL_ID(_PyNamespace_New);
51+
PyObject *
52+
_PyNamespace_New(PyObject *kwds) {
53+
return UPCALL_CEXT_O(_jls__PyNamespace_New, native_to_java(kwds));
54+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ def file_not_empty(path):
201201
static PyObject* test_{capifunction}(PyObject* module, PyObject* args) {{
202202
PyObject* ___arg;
203203
{argumentdeclarations};
204+
#ifndef NOARGS
204205
if (!PyArg_ParseTuple(args, "O", &___arg)) {{
205206
return NULL;
206207
}}
@@ -214,6 +215,7 @@ def file_not_empty(path):
214215
else {{
215216
{singleargumentname} = ___arg;
216217
}}
218+
#endif
217219
#endif
218220
{callfunction}({argumentnames});
219221
return Py_BuildValue("{resultspec}", {resultval});

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,7 @@ def compile_module(self, name):
393393
}
394394
""",
395395
resultspec="O",
396+
arguments=[],
396397
callfunction="wrap_PyErr_Fetch_tb_from_c",
397398
cmpfunc=compare_tracebacks,
398399
)

graalpython/com.oracle.graal.python.test/src/tests/test_call.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,16 @@ def f24(*args, a, b=5):
167167
def f25(*args, a, b=5, **kwds):
168168
pass
169169

170+
170171
def f26(**kw):
171172
return kw
172173

174+
175+
class F27:
176+
def f27(*args, a=5):
177+
return (args, a)
178+
179+
173180
def assert_parses(call_expr):
174181
raised = False
175182
try:
@@ -362,3 +369,11 @@ def test_runtime_args():
362369
kw = f26(b = mydict.pop('b', 22), **mydict)
363370
assert 'b' in kw
364371
assert kw['b'] == 2
372+
373+
f27_object = F27()
374+
assert f27_object.f27() == ((f27_object,), 5)
375+
assert f27_object.f27(1,2,3) == ((f27_object,1,2,3), 5)
376+
assert f27_object.f27(1,2,3,a=10) == ((f27_object,1,2,3), 10)
377+
assert F27.f27() == (tuple(), 5)
378+
assert F27.f27(1,2,3) == ((1,2,3), 5)
379+
assert F27.f27(1,2,3,a=10) == ((1,2,3), 10)

0 commit comments

Comments
 (0)