Skip to content

Commit 98bfcef

Browse files
committed
[GR-52132] Fix ctypes issues observed running PyBoy
PullRequest: graalpython/3193
2 parents 4cdda86 + f909ea2 commit 98bfcef

File tree

6 files changed

+29
-13
lines changed

6 files changed

+29
-13
lines changed

graalpython/com.oracle.graal.python.processor/src/com/oracle/graal/python/processor/CApiBuiltinsProcessor.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ public Object visitVariable(VariableTree variableTree, Trees trees) {
9898
private Trees trees;
9999

100100
private String getFieldInitializer(VariableElement theField) {
101+
if (trees == null) {
102+
return "";
103+
}
101104
if (argDescriptorToInitializer.isEmpty()) {
102105
// lazily initialize all arg descriptors in a single scan
103106
var codeScanner = new ArgDescriptorsTreeScanner();
@@ -115,7 +118,13 @@ private String getFieldInitializer(VariableElement theField) {
115118
@Override
116119
public synchronized void init(ProcessingEnvironment pe) {
117120
super.init(pe);
118-
this.trees = Trees.instance(pe);
121+
try {
122+
this.trees = Trees.instance(pe);
123+
} catch (Throwable t) {
124+
// ECJ does not support this, so we skip the some processing of C API builtins
125+
pe.getMessager().printWarning("The compiler does not support source tree parsing during annotation processing. Regeneration of Python C API builtins will be incorrect.");
126+
this.trees = null;
127+
}
119128
}
120129

121130
private String getCSignature(VariableElement obj) {
@@ -425,6 +434,9 @@ private void compareFunction(String name, VariableElement ret1, VariableElement
425434
}
426435

427436
private void compareFunction(String name, VariableElement ret1, String ret2, VariableElement[] args1, String[] args2) {
437+
if (trees == null) {
438+
return; // This isn't correct without parsing
439+
}
428440
if (!isSimilarType(getCSignature(ret1), ret2)) {
429441
processingEnv.getMessager().printError("duplicate entry for " + name + ", different return " + ret1 + " vs. " + ret2);
430442
}
@@ -890,11 +902,17 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
890902
return true;
891903
}
892904
try {
893-
generateCApiSource(allBuiltins, constants, fields, structs);
894-
generateCApiHeader(javaBuiltins, methodFlags);
905+
if (trees != null) {
906+
// needs jdk.compiler
907+
generateCApiSource(allBuiltins, constants, fields, structs);
908+
generateCApiHeader(javaBuiltins, methodFlags);
909+
}
895910
generateBuiltinRegistry(javaBuiltins);
896911
generateCApiAsserts(allBuiltins);
897-
checkImports(allBuiltins);
912+
if (trees != null) {
913+
// needs jdk.compiler
914+
checkImports(allBuiltins);
915+
}
898916
} catch (IOException e) {
899917
processingEnv.getMessager().printError(e.getMessage());
900918
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/ctypes/PyCFuncPtrBuiltins.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2021, 2024, 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
@@ -184,7 +184,7 @@ static Object simple(Object type, @SuppressWarnings("unused") Object[] args, @Su
184184
return pyCDataNewNode.execute(inliningTarget, type, dict);
185185
}
186186

187-
@Specialization(guards = {"args.length <= 1", "isTuple(args)"})
187+
@Specialization(guards = {"args.length == 1", "isTuple(args)"})
188188
static Object fromNativeLibrary(VirtualFrame frame, Object type, Object[] args, @SuppressWarnings("unused") PKeyword[] kwds,
189189
@Cached PyCFuncPtrFromDllNode pyCFuncPtrFromDllNode) {
190190
return pyCFuncPtrFromDllNode.execute(frame, type, args);
@@ -234,7 +234,7 @@ static Object callback(VirtualFrame frame, Object type, Object[] args, @Suppress
234234
return self;
235235
}
236236

237-
@Specialization(guards = {"args.length != 1", "!isPTuple(args)", "isLong(this, args, longCheckNode)"}, limit = "1")
237+
@Specialization(guards = {"args.length > 1", "!isPTuple(args)", "isLong(this, args, longCheckNode)"}, limit = "1")
238238
static Object error(@SuppressWarnings("unused") Object type, @SuppressWarnings("unused") Object[] args, @SuppressWarnings("unused") PKeyword[] kwds,
239239
@SuppressWarnings("unused") @Bind("this") Node inliningTarget,
240240
@SuppressWarnings("unused") @Exclusive @Cached PyLongCheckNode longCheckNode,

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/ctypes/PyCPointerTypeBuiltins.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2021, 2024, 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
@@ -197,7 +197,7 @@ static PyCArgObject byref(Node inliningTarget, Object obj,
197197
parg.tag = 'P';
198198
parg.pffi_type = ffi_type_pointer;
199199
parg.obj = cdata;
200-
parg.valuePointer = cdata.b_ptr;
200+
parg.valuePointer = cdata.b_ptr.createReference();
201201
return parg;
202202
}
203203

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2023, 2024, 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
@@ -72,8 +72,6 @@ private NativePointer() {
7272
/**
7373
* Returns an object representing a {@code NULL} pointer. This may also be used if
7474
* {@link PythonContext#isNativeAccessAllowed()} is {@code false}.
75-
*
76-
* @return
7775
*/
7876
public static NativePointer createNull() {
7977
return new NativePointer();

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/type/TypeBuiltins.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1435,6 +1435,7 @@ static Object set(Object self, Object value,
14351435
@Builtin(name = J___DIR__, minNumOfPositionalArgs = 1, doc = "__dir__ for type objects\n\n\tThis includes all attributes of klass and all of the base\n\tclasses recursively.")
14361436
@GenerateNodeFactory
14371437
public abstract static class DirNode extends PythonUnaryBuiltinNode {
1438+
@Override
14381439
public abstract PSet execute(VirtualFrame frame, Object klass);
14391440

14401441
@Specialization

mx.graalpython/suite.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,6 @@
459459
"GRAALPYTHON_PROCESSOR",
460460
"truffle:TRUFFLE_DSL_PROCESSOR"
461461
],
462-
"forceJavac": True, # GRAALPYTHON_PROCESSOR is not compatible with ECJ
463462
"workingSets": "Truffle,Python",
464463
"testProject": True,
465464
"javaProperties": {

0 commit comments

Comments
 (0)