Skip to content

Commit 0ba1376

Browse files
committed
[GR-17148] Migrate to Truffle Env API changes.
PullRequest: graalpython/583
2 parents 10367bd + 2359eaf commit 0ba1376

File tree

9 files changed

+120
-38
lines changed

9 files changed

+120
-38
lines changed

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

+10-17
Original file line numberDiff line numberDiff line change
@@ -97,23 +97,6 @@ Py_ssize_t PyLong_AsSsize_t(PyObject *obj) {
9797
return UPCALL_CEXT_L(_jls_PyLong_AsPrimitive, native_to_java(obj), 1, sizeof(Py_ssize_t));
9898
}
9999

100-
PyObject * PyLong_FromVoidPtr(void *p) {
101-
#if SIZEOF_VOID_P <= SIZEOF_LONG
102-
return PyLong_FromUnsignedLongLong((unsigned long)p);
103-
#else
104-
105-
#if SIZEOF_LONG_LONG < SIZEOF_VOID_P
106-
# error "PyLong_FromVoidPtr: sizeof(long long) < sizeof(void*)"
107-
#endif
108-
return PyLong_FromUnsignedLongLong((unsigned long long)(uintptr_t)p);
109-
#endif /* SIZEOF_VOID_P <= SIZEOF_LONG */
110-
}
111-
112-
UPCALL_ID(PyLong_AsVoidPtr);
113-
void * PyLong_AsVoidPtr(PyObject *obj){
114-
return (void *)UPCALL_CEXT_PTR(_jls_PyLong_AsVoidPtr, native_to_java(obj));
115-
}
116-
117100
UPCALL_ID(PyLong_FromLongLong);
118101
PyObject * PyLong_FromLong(long n) {
119102
return UPCALL_CEXT_O(_jls_PyLong_FromLongLong, n, 1);
@@ -131,6 +114,16 @@ PyObject * PyLong_FromUnsignedLongLong(unsigned long long n) {
131114
return UPCALL_CEXT_O(_jls_PyLong_FromLongLong, n, 0);
132115
}
133116

117+
PyObject * PyLong_FromVoidPtr(void *p) {
118+
// directly do the upcall to avoid a cast to primitive
119+
return UPCALL_CEXT_O(_jls_PyLong_FromLongLong, p, 0);
120+
}
121+
122+
UPCALL_ID(PyLong_AsVoidPtr);
123+
void * PyLong_AsVoidPtr(PyObject *obj){
124+
return (void *)UPCALL_CEXT_PTR(_jls_PyLong_AsVoidPtr, native_to_java(obj));
125+
}
126+
134127
UPCALL_ID(_PyLong_Sign);
135128
int _PyLong_Sign(PyObject *vv) {
136129
return UPCALL_CEXT_I(_jls__PyLong_Sign, native_to_java(vv));

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,12 @@ static PyObject* wrap_lenfunc(lenfunc f, PyObject* a) {
170170
return PyLong_FromSsize_t(f(a));
171171
}
172172

173-
static Py_hash_t wrap_hashfunc(hashfunc f, PyObject* a) {
174-
return PyLong_FromSsize_t(f(a));
173+
static PyObject* wrap_hashfunc(hashfunc f, PyObject* a) {
174+
Py_hash_t res = f(a);
175+
if (res == -1 && PyErr_Occurred()) {
176+
return NULL;
177+
}
178+
return PyLong_FromSsize_t(res);
175179
}
176180

177181
static PyObject* wrap_reverse_binop(binaryfunc f, PyObject* a, PyObject* b) {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ def test_internal_languages_dont_eval():
282282
try:
283283
polyglot.eval(language="nfi", string="default")
284284
except NotImplementedError as e:
285-
assert "internal language" in str(e)
285+
assert "No language for id nfi found" in str(e)
286286

287287
assert polyglot.eval(language="python", string="21 * 2") == 42
288288

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ private Object loadDynamicModuleWithSpec(String name, String path, InteropLibrar
200200
String basename = name.substring(name.lastIndexOf('.') + 1);
201201
TruffleObject sulongLibrary;
202202
try {
203-
CallTarget callTarget = env.parse(Source.newBuilder(LLVM_LANGUAGE, env.getTruffleFile(path)).build());
203+
CallTarget callTarget = env.parseInternal(Source.newBuilder(LLVM_LANGUAGE, env.getTruffleFile(path)).build());
204204
sulongLibrary = (TruffleObject) callTarget.call();
205205
} catch (SecurityException | IOException e) {
206206
throw raise(ImportError, "cannot load %s: %m", path, e);
@@ -250,7 +250,7 @@ private void ensureCapiWasLoaded() {
250250
if (!PythonOptions.getOption(ctxt, PythonOptions.ExposeInternalSources)) {
251251
capiSrcBuilder.internal(true);
252252
}
253-
capi = ctxt.getEnv().parse(capiSrcBuilder.build()).call();
253+
capi = ctxt.getEnv().parseInternal(capiSrcBuilder.build()).call();
254254
} catch (SecurityException | IOException e) {
255255
throw raise(PythonErrorType.ImportError, "cannot load capi from " + capiFile.getAbsoluteFile().getPath());
256256
}

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

+13-13
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public abstract static class ImportNode extends PythonBuiltinNode {
119119
@TruffleBoundary
120120
public Object importSymbol(String name) {
121121
Env env = getContext().getEnv();
122-
if (!env.isPolyglotAccessAllowed()) {
122+
if (!env.isPolyglotBindingsAccessAllowed()) {
123123
throw raise(PythonErrorType.NotImplementedError, "polyglot access is not allowed");
124124
}
125125
Object object = env.importSymbol(name);
@@ -137,7 +137,7 @@ abstract static class EvalInteropNode extends PythonBuiltinNode {
137137
@Specialization
138138
Object evalString(@SuppressWarnings("unused") PNone path, String value, String langOrMimeType) {
139139
Env env = getContext().getEnv();
140-
if (!env.isPolyglotAccessAllowed()) {
140+
if (!env.isPolyglotEvalAllowed()) {
141141
throw raise(PythonErrorType.NotImplementedError, "polyglot access is not allowed");
142142
}
143143
try {
@@ -148,14 +148,14 @@ Object evalString(@SuppressWarnings("unused") PNone path, String value, String l
148148
if (mimeType) {
149149
newBuilder = newBuilder.mimeType(langOrMimeType);
150150
}
151-
return env.parse(newBuilder.build()).call();
151+
return env.parsePublic(newBuilder.build()).call();
152152
} catch (RuntimeException e) {
153153
throw raise(NotImplementedError, e);
154154
}
155155
}
156156

157157
private void raiseIfInternal(Env env, String lang) {
158-
LanguageInfo languageInfo = env.getLanguages().get(lang);
158+
LanguageInfo languageInfo = env.getPublicLanguages().get(lang);
159159
if (languageInfo != null && languageInfo.isInternal()) {
160160
throw raise(NotImplementedError, "access to internal language %s is not permitted", lang);
161161
}
@@ -165,7 +165,7 @@ private void raiseIfInternal(Env env, String lang) {
165165
@Specialization
166166
Object evalFile(String path, @SuppressWarnings("unused") PNone string, String langOrMimeType) {
167167
Env env = getContext().getEnv();
168-
if (!env.isPolyglotAccessAllowed()) {
168+
if (!env.isPolyglotEvalAllowed()) {
169169
throw raise(PythonErrorType.NotImplementedError, "polyglot access is not allowed");
170170
}
171171
try {
@@ -176,7 +176,7 @@ Object evalFile(String path, @SuppressWarnings("unused") PNone string, String la
176176
if (mimeType) {
177177
newBuilder = newBuilder.mimeType(langOrMimeType);
178178
}
179-
return getContext().getEnv().parse(newBuilder.name(path).build()).call();
179+
return getContext().getEnv().parsePublic(newBuilder.name(path).build()).call();
180180
} catch (IOException e) {
181181
throw raise(OSError, "%s", e);
182182
} catch (RuntimeException e) {
@@ -188,11 +188,11 @@ Object evalFile(String path, @SuppressWarnings("unused") PNone string, String la
188188
@Specialization
189189
Object evalFile(String path, @SuppressWarnings("unused") PNone string, @SuppressWarnings("unused") PNone lang) {
190190
Env env = getContext().getEnv();
191-
if (!env.isPolyglotAccessAllowed()) {
191+
if (!env.isPolyglotEvalAllowed()) {
192192
throw raise(PythonErrorType.NotImplementedError, "polyglot access is not allowed");
193193
}
194194
try {
195-
return getContext().getEnv().parse(Source.newBuilder(PythonLanguage.ID, env.getTruffleFile(path)).name(path).build()).call();
195+
return getContext().getEnv().parsePublic(Source.newBuilder(PythonLanguage.ID, env.getTruffleFile(path)).name(path).build()).call();
196196
} catch (IOException e) {
197197
throw raise(OSError, "%s", e);
198198
} catch (RuntimeException e) {
@@ -214,7 +214,7 @@ Object evalWithoutContent(Object path, Object string, Object lang) {
214214

215215
@TruffleBoundary(transferToInterpreterOnException = false)
216216
private static String findLanguageByMimeType(Env env, String mimeType) {
217-
Map<String, LanguageInfo> languages = env.getLanguages();
217+
Map<String, LanguageInfo> languages = env.getPublicLanguages();
218218
for (String language : languages.keySet()) {
219219
for (String registeredMimeType : languages.get(language).getMimeTypes()) {
220220
if (mimeType.equals(registeredMimeType)) {
@@ -240,7 +240,7 @@ public abstract static class ExportSymbolNode extends PythonBuiltinNode {
240240
@TruffleBoundary
241241
public Object exportSymbolKeyValue(String name, Object value) {
242242
Env env = getContext().getEnv();
243-
if (!env.isPolyglotAccessAllowed()) {
243+
if (!env.isPolyglotBindingsAccessAllowed()) {
244244
throw raise(PythonErrorType.NotImplementedError, "polyglot access is not allowed");
245245
}
246246
env.exportSymbol(name, value);
@@ -268,7 +268,7 @@ public Object exportSymbolAmbiguous(Object arg1, String arg2) {
268268
@TruffleBoundary
269269
public Object exportSymbol(PFunction fun, @SuppressWarnings("unused") PNone name) {
270270
Env env = getContext().getEnv();
271-
if (!env.isPolyglotAccessAllowed()) {
271+
if (!env.isPolyglotBindingsAccessAllowed()) {
272272
throw raise(PythonErrorType.NotImplementedError, "polyglot access is not allowed");
273273
}
274274
env.exportSymbol(fun.getName(), fun);
@@ -279,7 +279,7 @@ public Object exportSymbol(PFunction fun, @SuppressWarnings("unused") PNone name
279279
@TruffleBoundary
280280
public Object exportSymbol(PBuiltinFunction fun, @SuppressWarnings("unused") PNone name) {
281281
Env env = getContext().getEnv();
282-
if (!env.isPolyglotAccessAllowed()) {
282+
if (!env.isPolyglotBindingsAccessAllowed()) {
283283
throw raise(PythonErrorType.NotImplementedError, "polyglot access is not allowed");
284284
}
285285
env.exportSymbol(fun.getName(), fun);
@@ -322,7 +322,7 @@ protected static boolean isModule(Object o) {
322322
@TruffleBoundary
323323
private void export(String name, Object obj) {
324324
Env env = getContext().getEnv();
325-
if (!env.isPolyglotAccessAllowed()) {
325+
if (!env.isPolyglotBindingsAccessAllowed()) {
326326
throw raise(PythonErrorType.NotImplementedError, "polyglot access is not allowed");
327327
}
328328
env.exportSymbol(name, obj);

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ Object useSRE(@SuppressWarnings("unused") String code,
105105
@TruffleBoundary
106106
Object run(String code,
107107
@SuppressWarnings("unused") @CachedContext(PythonLanguage.class) PythonContext context) {
108-
return getContext().getEnv().parse(Source.newBuilder("regex", code, "build-regex-engine").build()).call();
108+
return getContext().getEnv().parseInternal(Source.newBuilder("regex", code, "build-regex-engine").build()).call();
109109
}
110110
}
111111

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/PythonNativeVoidPtr.java

+8
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import com.oracle.graal.python.builtins.objects.PythonAbstractObject;
4545
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
4646
import com.oracle.graal.python.builtins.objects.type.LazyPythonClass;
47+
import com.oracle.truffle.api.CompilerAsserts;
4748
import com.oracle.truffle.api.interop.TruffleObject;
4849
import com.oracle.truffle.api.library.ExportLibrary;
4950
import com.oracle.truffle.api.library.ExportMessage;
@@ -65,4 +66,11 @@ public int compareTo(Object o) {
6566
public LazyPythonClass getLazyPythonClass() {
6667
return PythonBuiltinClassType.PInt;
6768
}
69+
70+
@Override
71+
public String toString() {
72+
CompilerAsserts.neverPartOfCompilation();
73+
return String.format("PythonNativeVoidPtr(%s)", object);
74+
}
75+
6876
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/ints/IntBuiltins.java

+77
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@
100100
import com.oracle.truffle.api.dsl.Specialization;
101101
import com.oracle.truffle.api.dsl.TypeSystemReference;
102102
import com.oracle.truffle.api.frame.VirtualFrame;
103+
import com.oracle.truffle.api.interop.InteropLibrary;
104+
import com.oracle.truffle.api.interop.UnsupportedMessageException;
105+
import com.oracle.truffle.api.library.CachedLibrary;
103106
import com.oracle.truffle.api.profiles.BranchProfile;
104107
import com.oracle.truffle.api.profiles.ConditionProfile;
105108

@@ -1407,6 +1410,62 @@ boolean eqPiPi(PInt a, PInt b) {
14071410
return a.equals(b);
14081411
}
14091412

1413+
@Specialization
1414+
boolean eqVoidPtrLong(PythonNativeVoidPtr a, long b,
1415+
@Shared("lib") @CachedLibrary(limit = "1") InteropLibrary lib) {
1416+
if (lib.isPointer(a.object)) {
1417+
try {
1418+
long ptrVal = lib.asPointer(a.object);
1419+
// pointers are considered unsigned
1420+
return ptrVal >= 0L && ptrVal == b;
1421+
} catch (UnsupportedMessageException e) {
1422+
// fall through
1423+
}
1424+
}
1425+
return doHash(a.object, b);
1426+
}
1427+
1428+
@Specialization
1429+
boolean eqLongVoidPtr(long a, PythonNativeVoidPtr b,
1430+
@Shared("lib") @CachedLibrary(limit = "1") InteropLibrary lib) {
1431+
return eqVoidPtrLong(b, a, lib);
1432+
}
1433+
1434+
@Specialization
1435+
@TruffleBoundary
1436+
boolean eqVoidPtrPInt(PythonNativeVoidPtr a, PInt b,
1437+
@Shared("lib") @CachedLibrary(limit = "1") InteropLibrary lib) {
1438+
if (lib.isPointer(a.object)) {
1439+
try {
1440+
long ptrVal = lib.asPointer(a.object);
1441+
if (ptrVal < 0) {
1442+
// pointers are considered unsigned
1443+
BigInteger bi = BigInteger.valueOf(ptrVal).add(BigInteger.ONE.shiftLeft(64));
1444+
return bi.equals(b.getValue());
1445+
}
1446+
return BigInteger.valueOf(ptrVal).equals(b.getValue());
1447+
} catch (UnsupportedMessageException e) {
1448+
// fall through
1449+
}
1450+
}
1451+
try {
1452+
return a.object.hashCode() == b.longValueExact();
1453+
} catch (ArithmeticException e) {
1454+
return false;
1455+
}
1456+
}
1457+
1458+
@Specialization
1459+
boolean eqPIntVoidPtr(PInt a, PythonNativeVoidPtr b,
1460+
@Shared("lib") @CachedLibrary(limit = "1") InteropLibrary lib) {
1461+
return eqVoidPtrPInt(b, a, lib);
1462+
}
1463+
1464+
@TruffleBoundary
1465+
private static boolean doHash(Object object, long b) {
1466+
return object.hashCode() == b;
1467+
}
1468+
14101469
@SuppressWarnings("unused")
14111470
@Fallback
14121471
PNotImplemented eq(Object a, Object b) {
@@ -2184,6 +2243,24 @@ public String doL(long self) {
21842243
public String doPInt(PInt self) {
21852244
return self.toString();
21862245
}
2246+
2247+
@Specialization
2248+
public String doNativeVoidPtr(PythonNativeVoidPtr self,
2249+
@CachedLibrary(limit = "1") InteropLibrary lib) {
2250+
if (lib.isPointer(self.object)) {
2251+
try {
2252+
return Long.toString(lib.asPointer(self.object));
2253+
} catch (UnsupportedMessageException e) {
2254+
// fall through
2255+
}
2256+
}
2257+
return doHash(self.object);
2258+
}
2259+
2260+
@TruffleBoundary
2261+
private static String doHash(Object object) {
2262+
return Integer.toString(object.hashCode());
2263+
}
21872264
}
21882265

21892266
@Builtin(name = SpecialMethodNames.__REPR__, minNumOfPositionalArgs = 1)

mx.graalpython/suite.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@
4444
},
4545
{
4646
"name": "sulong",
47-
"version": "97c9fe405b077d7addd5e4f82290bb3b345b1727",
47+
"version": "41f7f821549b7f661910b2a755e0a5616335a63f",
4848
"subdir": True,
4949
"urls": [
5050
{"url": "https://github.com/oracle/graal", "kind": "git"},
5151
]
5252
},
5353
{
5454
"name": "regex",
55-
"version": "97c9fe405b077d7addd5e4f82290bb3b345b1727",
55+
"version": "41f7f821549b7f661910b2a755e0a5616335a63f",
5656
"subdir": True,
5757
"urls": [
5858
{"url": "https://github.com/oracle/graal", "kind": "git"},

0 commit comments

Comments
 (0)