Skip to content

Commit 7583cb4

Browse files
committed
Properly implement eq and repr for PythonNativeVoidPtr.
1 parent 94efdfa commit 7583cb4

File tree

3 files changed

+96
-17
lines changed

3 files changed

+96
-17
lines changed

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

Lines changed: 10 additions & 17 deletions
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/src/com/oracle/graal/python/builtins/objects/cext/PythonNativeVoidPtr.java

Lines changed: 8 additions & 0 deletions
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

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@
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.TruffleObject;
105+
import com.oracle.truffle.api.interop.UnsupportedMessageException;
106+
import com.oracle.truffle.api.library.CachedLibrary;
103107
import com.oracle.truffle.api.profiles.BranchProfile;
104108
import com.oracle.truffle.api.profiles.ConditionProfile;
105109

@@ -1407,6 +1411,62 @@ boolean eqPiPi(PInt a, PInt b) {
14071411
return a.equals(b);
14081412
}
14091413

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

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

0 commit comments

Comments
 (0)