Skip to content

Commit e1b5988

Browse files
committed
Implement hasMetaParents and getMetaParents interop API
Fixes #401
1 parent 2e8cad9 commit e1b5988

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

graalpython/com.oracle.graal.python.test.integration/src/com/oracle/graal/python/test/integration/interop/JavaInteropTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,35 @@ def foo(obj):
789789
Value result = foo.execute(recursiveList);
790790
assertEquals(result.as(String.class), "[1, [...]]");
791791
}
792+
793+
@Test
794+
public void testMetaParents() throws IOException {
795+
Source source = Source.newBuilder("python", """
796+
class Foo:
797+
pass
798+
class Bar(Foo):
799+
pass
800+
Bar
801+
""", "input").build();
802+
Value bar = context.eval(source);
803+
assertTrue(bar.isMetaObject());
804+
assertEquals(bar.getMetaSimpleName(), "Bar");
805+
assertTrue(bar.hasMetaParents());
806+
Value barParents = bar.getMetaParents();
807+
assertTrue(barParents.hasArrayElements());
808+
assertEquals(barParents.getArraySize(), 1);
809+
Value foo = barParents.getArrayElement(0);
810+
assertTrue(foo.isMetaObject());
811+
assertEquals(foo.getMetaSimpleName(), "Foo");
812+
assertTrue(foo.hasMetaParents());
813+
Value fooParents = foo.getMetaParents();
814+
assertTrue(fooParents.hasArrayElements());
815+
assertEquals(fooParents.getArraySize(), 1);
816+
Value object = fooParents.getArrayElement(0);
817+
assertTrue(object.isMetaObject());
818+
assertEquals(object.getMetaSimpleName(), "object");
819+
assertFalse(object.hasMetaParents());
820+
}
792821
}
793822

794823
@RunWith(Parameterized.class)

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/PythonAbstractObject.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@
149149
import com.oracle.graal.python.runtime.PythonContext;
150150
import com.oracle.graal.python.runtime.PythonOptions;
151151
import com.oracle.graal.python.runtime.exception.PException;
152+
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
152153
import com.oracle.graal.python.runtime.sequence.storage.SequenceStorage;
153154
import com.oracle.graal.python.util.OverflowException;
154155
import com.oracle.graal.python.util.PythonUtils;
@@ -1498,6 +1499,41 @@ public Object getMetaObject(
14981499
}
14991500
}
15001501

1502+
@ExportMessage
1503+
public boolean hasMetaParents(
1504+
@Bind("$node") Node inliningTarget,
1505+
@Exclusive @Cached TypeNodes.IsTypeNode isTypeNode,
1506+
@Exclusive @Cached TypeNodes.GetBaseClassesNode getBaseClassNode,
1507+
@Exclusive @Cached GilNode gil) {
1508+
boolean mustRelease = gil.acquire();
1509+
try {
1510+
return isTypeNode.execute(inliningTarget, this) && getBaseClassNode.execute(inliningTarget, this).length > 0;
1511+
} finally {
1512+
gil.release(mustRelease);
1513+
}
1514+
}
1515+
1516+
@ExportMessage
1517+
public Object getMetaParents(
1518+
@Bind("$node") Node inliningTarget,
1519+
@Exclusive @Cached PythonObjectFactory factory,
1520+
@Exclusive @Cached TypeNodes.IsTypeNode isTypeNode,
1521+
@Exclusive @Cached TypeNodes.GetBaseClassesNode getBaseClassNode,
1522+
@Exclusive @Cached GilNode gil) throws UnsupportedMessageException {
1523+
boolean mustRelease = gil.acquire();
1524+
try {
1525+
if (isTypeNode.execute(inliningTarget, this)) {
1526+
var bases = getBaseClassNode.execute(inliningTarget, this);
1527+
if (bases.length > 0) {
1528+
return factory.createTuple(bases);
1529+
}
1530+
}
1531+
throw UnsupportedMessageException.create();
1532+
} finally {
1533+
gil.release(mustRelease);
1534+
}
1535+
}
1536+
15011537
@ExportMessage
15021538
public int identityHashCode(
15031539
@Bind("$node") Node inliningTarget,

0 commit comments

Comments
 (0)