|
31 | 31 | import static com.oracle.graal.python.nodes.SpecialAttributeNames.__DICT__;
|
32 | 32 | import static com.oracle.graal.python.nodes.SpecialAttributeNames.__FUNC__;
|
33 | 33 | import static com.oracle.graal.python.nodes.SpecialAttributeNames.__KWDEFAULTS__;
|
| 34 | +import static com.oracle.graal.python.nodes.SpecialAttributeNames.__NAME__; |
| 35 | +import static com.oracle.graal.python.nodes.SpecialAttributeNames.__QUALNAME__; |
34 | 36 | import static com.oracle.graal.python.nodes.SpecialMethodNames.__GETATTRIBUTE__;
|
35 | 37 | import static com.oracle.graal.python.nodes.SpecialMethodNames.__GET__;
|
36 | 38 | import static com.oracle.graal.python.nodes.SpecialMethodNames.__REDUCE__;
|
|
44 | 46 | import com.oracle.graal.python.builtins.PythonBuiltinClassType;
|
45 | 47 | import com.oracle.graal.python.builtins.PythonBuiltins;
|
46 | 48 | import com.oracle.graal.python.builtins.objects.PNone;
|
47 |
| -import com.oracle.graal.python.builtins.objects.PythonAbstractObject; |
48 | 49 | import com.oracle.graal.python.builtins.objects.function.PKeyword;
|
| 50 | +import com.oracle.graal.python.builtins.objects.module.PythonModule; |
49 | 51 | import com.oracle.graal.python.builtins.objects.object.ObjectBuiltins;
|
50 | 52 | import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
|
51 |
| -import com.oracle.graal.python.builtins.objects.type.TypeNodes.GetNameNode; |
| 53 | +import com.oracle.graal.python.builtins.objects.type.TypeNodes; |
52 | 54 | import com.oracle.graal.python.nodes.ErrorMessages;
|
53 |
| -import com.oracle.graal.python.nodes.SpecialAttributeNames; |
54 | 55 | import com.oracle.graal.python.nodes.attributes.GetAttributeNode;
|
55 | 56 | import com.oracle.graal.python.nodes.builtins.FunctionNodes.GetDefaultsNode;
|
56 | 57 | import com.oracle.graal.python.nodes.builtins.FunctionNodes.GetKeywordDefaultsNode;
|
57 | 58 | import com.oracle.graal.python.nodes.call.special.LookupAndCallBinaryNode;
|
| 59 | +import com.oracle.graal.python.nodes.call.special.LookupAndCallUnaryNode; |
58 | 60 | import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
|
59 | 61 | import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
|
60 | 62 | import com.oracle.graal.python.nodes.function.builtins.PythonTernaryBuiltinNode;
|
61 | 63 | import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
|
62 | 64 | import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
|
| 65 | +import com.oracle.graal.python.nodes.util.CannotCastException; |
| 66 | +import com.oracle.graal.python.nodes.util.CastToJavaStringNode; |
63 | 67 | import com.oracle.graal.python.runtime.exception.PException;
|
64 | 68 | import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
|
65 | 69 | import com.oracle.truffle.api.dsl.Cached;
|
@@ -130,23 +134,31 @@ protected Object doIt(VirtualFrame frame, PMethod self, Object key,
|
130 | 134 | @Builtin(name = __REPR__, minNumOfPositionalArgs = 1)
|
131 | 135 | @GenerateNodeFactory
|
132 | 136 | public abstract static class ReprNode extends PythonUnaryBuiltinNode {
|
133 |
| - @Specialization(limit = "3") |
134 |
| - Object reprMethod(VirtualFrame frame, PMethod self, |
135 |
| - @CachedLibrary("self.getSelf()") PythonObjectLibrary lib, |
136 |
| - @Cached("createGetAttributeNode()") GetAttributeNode getNameAttrNode, |
137 |
| - @Cached GetNameNode getTypeNameNode) { |
138 |
| - String typeName = getTypeNameNode.execute(lib.getLazyPythonClass(self.getSelf())); |
139 |
| - return strFormat("<bound method %s of %s object at 0x%x>", getNameAttrNode.executeObject(frame, self.getFunction()), typeName, PythonAbstractObject.systemHashCode(self.getSelf())); |
| 137 | + @Specialization |
| 138 | + Object reprMethod(VirtualFrame frame, PMethod method, |
| 139 | + @Cached("create(__REPR__)") LookupAndCallUnaryNode callReprNode, |
| 140 | + @Cached CastToJavaStringNode toJavaStringNode, |
| 141 | + @CachedLibrary(limit = "1") PythonObjectLibrary pol) { |
| 142 | + Object self = method.getSelf(); |
| 143 | + Object func = method.getFunction(); |
| 144 | + String defname = "?"; |
| 145 | + |
| 146 | + Object funcName = pol.lookupAttribute(func, __QUALNAME__); |
| 147 | + if (funcName == PNone.NO_VALUE) { |
| 148 | + funcName = pol.lookupAttribute(func, __NAME__); |
| 149 | + } |
| 150 | + |
| 151 | + try { |
| 152 | + return strFormat("<bound method %s of %s>", toJavaStringNode.execute(funcName), callReprNode.executeObject(frame, self)); |
| 153 | + } catch (CannotCastException e) { |
| 154 | + return strFormat("<bound method %s of %s>", defname, callReprNode.executeObject(frame, self)); |
| 155 | + } |
140 | 156 | }
|
141 | 157 |
|
142 | 158 | @TruffleBoundary
|
143 | 159 | private static String strFormat(String fmt, Object... objects) {
|
144 | 160 | return String.format(fmt, objects);
|
145 | 161 | }
|
146 |
| - |
147 |
| - protected static GetAttributeNode createGetAttributeNode() { |
148 |
| - return GetAttributeNode.create(SpecialAttributeNames.__QUALNAME__, null); |
149 |
| - } |
150 | 162 | }
|
151 | 163 |
|
152 | 164 | @Builtin(name = __DEFAULTS__, minNumOfPositionalArgs = 1, isGetter = true)
|
@@ -194,4 +206,52 @@ PMethod doGeneric(@SuppressWarnings("unused") PMethod self, Object obj, @Suppres
|
194 | 206 | return factory().createMethod(obj, self.getFunction());
|
195 | 207 | }
|
196 | 208 | }
|
| 209 | + |
| 210 | + @Builtin(name = __NAME__, minNumOfPositionalArgs = 1, isGetter = true) |
| 211 | + @GenerateNodeFactory |
| 212 | + public abstract static class MethodName extends PythonUnaryBuiltinNode { |
| 213 | + @Specialization |
| 214 | + Object getName(VirtualFrame frame, PMethod method, |
| 215 | + @Cached("create(__NAME__)") GetAttributeNode getNameAttrNode) { |
| 216 | + return getNameAttrNode.executeObject(frame, method.getFunction()); |
| 217 | + } |
| 218 | + } |
| 219 | + |
| 220 | + @Builtin(name = __QUALNAME__, minNumOfPositionalArgs = 1, isGetter = true) |
| 221 | + @GenerateNodeFactory |
| 222 | + public abstract static class MethodQualName extends PythonUnaryBuiltinNode { |
| 223 | + @Specialization(limit = "3") |
| 224 | + Object getQualName(VirtualFrame frame, PMethod method, |
| 225 | + @Cached("create(__NAME__)") GetAttributeNode getNameAttrNode, |
| 226 | + @Cached("create(__QUALNAME__)") GetAttributeNode getQualNameAttrNode, |
| 227 | + @Cached TypeNodes.IsTypeNode isTypeNode, |
| 228 | + @Cached CastToJavaStringNode castToJavaStringNode, |
| 229 | + @CachedLibrary("method.getSelf()") PythonObjectLibrary pol) { |
| 230 | + Object self = method.getSelf(); |
| 231 | + String methodName; |
| 232 | + try { |
| 233 | + methodName = castToJavaStringNode.execute(getNameAttrNode.executeObject(frame, method)); |
| 234 | + } catch (CannotCastException e) { |
| 235 | + throw raise(PythonBuiltinClassType.TypeError, ErrorMessages.IS_NOT_A, __NAME__, "unicode object"); |
| 236 | + } |
| 237 | + if (self == null || self instanceof PythonModule) { |
| 238 | + return methodName; |
| 239 | + } |
| 240 | + |
| 241 | + Object type = isTypeNode.execute(self) ? self : pol.getLazyPythonClass(self); |
| 242 | + String typeQualName; |
| 243 | + try { |
| 244 | + typeQualName = castToJavaStringNode.execute(getQualNameAttrNode.executeObject(frame, type)); |
| 245 | + } catch (CannotCastException e) { |
| 246 | + throw raise(PythonBuiltinClassType.TypeError, ErrorMessages.IS_NOT_A, __QUALNAME__, "unicode object"); |
| 247 | + } |
| 248 | + |
| 249 | + return getQualNameGeneric(typeQualName, methodName); |
| 250 | + } |
| 251 | + |
| 252 | + @TruffleBoundary |
| 253 | + private static Object getQualNameGeneric(String typeQualName, String name) { |
| 254 | + return String.format("%s.%s", typeQualName, name); |
| 255 | + } |
| 256 | + } |
197 | 257 | }
|
0 commit comments