Skip to content

Commit 0f10ce1

Browse files
committed
Define weakref types for C API.
1 parent 6963732 commit 0f10ce1

File tree

3 files changed

+60
-7
lines changed

3 files changed

+60
-7
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ declare_type(PyMethod_Type, method, PyMethodObject);
153153
declare_type(PyCode_Type, code, PyCodeObject);
154154
declare_type(PyFrame_Type, frame, PyFrameObject);
155155
declare_type(PyTraceBack_Type, traceback, PyTracebackObject);
156+
declare_type(_PyWeakref_RefType, ReferenceType, PyWeakReference);
156157
// Below types use the same object structure as others, and thus
157158
// POLYGLOT_DECLARE_TYPE should not be called again
158159
initialize_type(PySuper_Type, super, _object);
@@ -162,6 +163,8 @@ initialize_type(PyBool_Type, bool, _longobject);
162163
initialize_type(_PyNotImplemented_Type, NotImplementedType, _object);
163164
initialize_type(PyDictProxy_Type, mappingproxy, _object);
164165
initialize_type(PyEllipsis_Type, ellipsis, _object);
166+
initialize_type(_PyWeakref_ProxyType, ProxyType, PyWeakReference);
167+
initialize_type(_PyWeakref_CallableProxyType, CallableProxyType, PyWeakReference);
165168

166169
POLYGLOT_DECLARE_TYPE(PyThreadState);
167170

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (c) 2019, 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+
PyTypeObject _PyWeakref_RefType = PY_TRUFFLE_TYPE("weakref", &PyType_Type, Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE, sizeof(PyWeakReference));
44+
PyTypeObject _PyWeakref_ProxyType = PY_TRUFFLE_TYPE("weakproxy", &PyType_Type, Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, sizeof(PyWeakReference));
45+
PyTypeObject _PyWeakref_CallableProxyType = PY_TRUFFLE_TYPE("weakcallableproxy", &PyType_Type, Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, sizeof(PyWeakReference));

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/PythonCextBuiltins.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,13 @@ Object run(Object obj,
291291
@GenerateNodeFactory
292292
@TypeSystemReference(PythonArithmeticTypes.class)
293293
abstract static class PyTruffle_Type extends NativeBuiltin {
294+
295+
private static final String[] LOOKUP_MODULES = new String[]{
296+
PythonCextBuiltins.PYTHON_CEXT,
297+
"_weakref",
298+
"builtins"
299+
};
300+
294301
@Specialization
295302
@TruffleBoundary
296303
Object doI(String typeName) {
@@ -300,13 +307,11 @@ Object doI(String typeName) {
300307
return core.lookupType(type);
301308
}
302309
}
303-
Object attribute = core.lookupBuiltinModule(PythonCextBuiltins.PYTHON_CEXT).getAttribute(typeName);
304-
if (attribute != PNone.NO_VALUE) {
305-
return attribute;
306-
}
307-
attribute = core.lookupBuiltinModule("builtins").getAttribute(typeName);
308-
if (attribute != PNone.NO_VALUE) {
309-
return attribute;
310+
for (String module : LOOKUP_MODULES) {
311+
Object attribute = core.lookupBuiltinModule(module).getAttribute(typeName);
312+
if (attribute != PNone.NO_VALUE) {
313+
return attribute;
314+
}
310315
}
311316
throw raise(PythonErrorType.KeyError, "'%s'", typeName);
312317
}

0 commit comments

Comments
 (0)