Skip to content

Commit 74c6c82

Browse files
committed
Correctly set '__module__' and '__qualname__' attribute for native types.
1 parent a1ec2f4 commit 74c6c82

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,10 +232,7 @@ int PyType_Ready(PyTypeObject* cls) {
232232
PyDict_SetItemString(native_members, "tp_name", polyglot_from_string(cls->tp_name, SRC_CS));
233233
PyDict_SetItemString(native_members, "tp_doc", polyglot_from_string(cls->tp_doc ? cls->tp_doc : "", SRC_CS));
234234
PyDict_SetItemString(native_members, "tp_basicsize", PyLong_FromSsize_t(cls->tp_basicsize));
235-
const char* lastDot = strrchr(cls->tp_name, '.');
236-
if (lastDot) {
237-
PyDict_SetItemString(native_members, "__module__", polyglot_from_string(lastDot + 1, SRC_CS));
238-
}
235+
const char* class_name = cls->tp_name;
239236
PyTypeObject* javacls = polyglot_invoke(PY_TRUFFLE_CEXT,
240237
"PyType_Ready",
241238
// no conversion of cls here, because we

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

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -487,18 +487,37 @@ Object run(Object typestruct, PythonClass metaClass, PTuple baseClasses, PDict n
487487
bases[i] = (PythonClass) array[i];
488488
}
489489

490-
String name = getStringItem(nativeMembers, "tp_name");
490+
// 'tp_name' contains the fully-qualified name, i.e., 'module.A.B...'
491+
String fqname = getStringItem(nativeMembers, "tp_name");
491492
String doc = getStringItem(nativeMembers, "tp_doc");
492-
String module = getStringItem(nativeMembers, SpecialAttributeNames.__MODULE__);
493-
PythonNativeClass cclass = factory().createNativeClassWrapper(typestruct, metaClass, name, bases);
493+
// the qualified name (i.e. without module name) like 'A.B...'
494+
String qualName = getQualName(fqname);
495+
PythonNativeClass cclass = factory().createNativeClassWrapper(typestruct, metaClass, qualName, bases);
494496
writeNode.execute(cclass, SpecialAttributeNames.__DOC__, doc);
495497
writeNode.execute(cclass, SpecialAttributeNames.__BASICSIZE__, getLongItem(nativeMembers, "tp_basicsize"));
496-
if (module != null) {
497-
writeNode.execute(cclass, SpecialAttributeNames.__MODULE__, module);
498+
String moduleName = getModuleName(fqname);
499+
if (moduleName != null) {
500+
writeNode.execute(cclass, SpecialAttributeNames.__MODULE__, moduleName);
498501
}
499502
return new PythonClassInitNativeWrapper(cclass);
500503
}
501504

505+
private static String getQualName(String fqname) {
506+
int firstDot = fqname.indexOf('.');
507+
if (firstDot != -1) {
508+
return fqname.substring(firstDot + 1);
509+
}
510+
return fqname;
511+
}
512+
513+
private static String getModuleName(String fqname) {
514+
int firstDotIdx = fqname.indexOf('.');
515+
if (firstDotIdx != -1) {
516+
return fqname.substring(0, firstDotIdx);
517+
}
518+
return null;
519+
}
520+
502521
private String getStringItem(PDict nativeMembers, String key) {
503522
Object item = getGetItemNode().execute(nativeMembers.getDictStorage(), key);
504523
if (item instanceof PString) {

0 commit comments

Comments
 (0)