Skip to content

Commit aa8119d

Browse files
committed
type constructor: special handling of __qualname__
PythonClass: fix interoplib getMetaSimpleName and getMetaQualifiedName messages
1 parent fc27450 commit aa8119d

File tree

3 files changed

+18
-40
lines changed

3 files changed

+18
-40
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2342,6 +2342,7 @@ private PythonClass typeMetaclass(VirtualFrame frame, String name, PTuple bases,
23422342
// copy the dictionary slots over, as CPython does through PyDict_Copy
23432343
// Also check for a __slots__ sequence variable in dict
23442344
Object slots = null;
2345+
boolean qualnameSet = false;
23452346
for (DictEntry entry : nslib.entries(namespace.getDictStorage())) {
23462347
Object key = entry.getKey();
23472348
Object value = entry.getValue();
@@ -2380,11 +2381,22 @@ private PythonClass typeMetaclass(VirtualFrame frame, String name, PTuple bases,
23802381
}
23812382
}
23822383
pythonClass.setAttribute(key, value);
2384+
} else if (SpecialAttributeNames.__QUALNAME__.equals(key)) {
2385+
try {
2386+
pythonClass.setQualName(ensureCastToStringNode().execute(value));
2387+
qualnameSet = true;
2388+
} catch (CannotCastException e) {
2389+
throw raise(PythonBuiltinClassType.TypeError, ErrorMessages.MUST_BE_S_NOT_P, "type __qualname__", "str", value);
2390+
}
23832391
} else {
23842392
pythonClass.setAttribute(key, value);
23852393
}
23862394
}
23872395

2396+
if (!qualnameSet) {
2397+
pythonClass.setQualName(name);
2398+
}
2399+
23882400
// CPython masks the __hash__ method with None when __eq__ is overriden, but __hash__ is
23892401
// not
23902402
Object hashMethod = nslib.getItem(namespace.getDictStorage(), __HASH__);

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

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,9 @@
2626
package com.oracle.graal.python.builtins.objects.type;
2727

2828
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
29-
import com.oracle.graal.python.nodes.SpecialAttributeNames;
3029
import com.oracle.graal.python.nodes.attributes.ReadAttributeFromDynamicObjectNode;
3130
import com.oracle.graal.python.nodes.classes.IsSubtypeNode;
32-
import com.oracle.graal.python.nodes.util.CannotCastException;
33-
import com.oracle.graal.python.nodes.util.CastToJavaStringNode;
3431
import com.oracle.truffle.api.dsl.Cached;
35-
import com.oracle.truffle.api.dsl.Cached.Exclusive;
3632
import com.oracle.truffle.api.dsl.Cached.Shared;
3733
import com.oracle.truffle.api.interop.InteropLibrary;
3834
import com.oracle.truffle.api.interop.UnsupportedMessageException;
@@ -67,32 +63,13 @@ boolean isMetaInstance(Object instance,
6763
}
6864

6965
@ExportMessage
70-
String getMetaSimpleName(
71-
@Exclusive @Cached ReadAttributeFromDynamicObjectNode getName,
72-
@Shared("castStr") @Cached CastToJavaStringNode castStr) {
73-
// n.b.: we're reading directly from the storage here, because this
74-
// method must not have side-effects, so even if there's a __dict__, we
75-
// cannot call its __getitem__
76-
try {
77-
return castStr.execute(getName.execute(getStorage(), SpecialAttributeNames.__NAME__));
78-
} catch (CannotCastException e) {
79-
return "unnamed-class";
80-
81-
}
66+
String getMetaSimpleName() {
67+
return getName();
8268
}
8369

8470
@ExportMessage
85-
String getMetaQualifiedName(
86-
@Exclusive @Cached ReadAttributeFromDynamicObjectNode getName,
87-
@Shared("castStr") @Cached CastToJavaStringNode castStr) {
88-
// n.b.: we're reading directly from the storage here, because this
89-
// method must not have side-effects, so even if there's a __dict__, we
90-
// cannot call its __getitem__
91-
try {
92-
return castStr.execute(getName.execute(getStorage(), SpecialAttributeNames.__QUALNAME__));
93-
} catch (CannotCastException e) {
94-
return "unnamed-class";
95-
}
71+
String getMetaQualifiedName() {
72+
return getQualName();
9673
}
9774

9875
/*

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

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -818,12 +818,7 @@ String getNameType(PythonBuiltinClassType cls, @SuppressWarnings("unused") PNone
818818
}
819819

820820
@Specialization(guards = "isNoValue(value)")
821-
String getNameBuiltin(PythonBuiltinClass cls, @SuppressWarnings("unused") PNone value) {
822-
return cls.getName();
823-
}
824-
825-
@Specialization(guards = {"isNoValue(value)", "!isPythonBuiltinClass(cls)"})
826-
Object getName(PythonClass cls, @SuppressWarnings("unused") PNone value) {
821+
String getNameBuiltin(PythonManagedClass cls, @SuppressWarnings("unused") PNone value) {
827822
return cls.getName();
828823
}
829824

@@ -938,16 +933,10 @@ String getName(PythonBuiltinClassType cls, @SuppressWarnings("unused") PNone val
938933
}
939934

940935
@Specialization(guards = "isNoValue(value)")
941-
String getName(PythonBuiltinClass cls, @SuppressWarnings("unused") PNone value) {
936+
String getName(PythonManagedClass cls, @SuppressWarnings("unused") PNone value) {
942937
return cls.getQualName();
943938
}
944939

945-
@Specialization(guards = {"isNoValue(value)", "!isPythonBuiltinClass(cls)"})
946-
Object getName(PythonClass cls, @SuppressWarnings("unused") PNone value,
947-
@Cached("create()") ReadAttributeFromObjectNode getName) {
948-
return getName.execute(cls, __QUALNAME__);
949-
}
950-
951940
@Specialization(guards = "!isNoValue(value)")
952941
Object setName(@SuppressWarnings("unused") PythonBuiltinClass cls, @SuppressWarnings("unused") Object value) {
953942
throw raise(PythonErrorType.RuntimeError, ErrorMessages.CANT_SET_ATTRIBUTES_OF_TYPE, "built-in/extension 'type'");

0 commit comments

Comments
 (0)