|
47 | 47 | import com.oracle.graal.python.builtins.objects.list.PList;
|
48 | 48 | import com.oracle.graal.python.builtins.objects.traceback.PTraceback;
|
49 | 49 | import com.oracle.graal.python.builtins.objects.tuple.PTuple;
|
| 50 | +import com.oracle.graal.python.builtins.objects.tuple.TupleBuiltins.GetItemNode; |
| 51 | +import com.oracle.graal.python.lib.PyObjectGetAttr; |
| 52 | +import com.oracle.graal.python.lib.PyObjectReprAsObjectNode; |
| 53 | +import com.oracle.graal.python.lib.PyObjectStrAsObjectNode; |
50 | 54 | import com.oracle.graal.python.nodes.ErrorMessages;
|
51 | 55 | import com.oracle.graal.python.nodes.PGuards;
|
52 | 56 | import com.oracle.graal.python.nodes.PRaiseNode;
|
| 57 | +import static com.oracle.graal.python.nodes.SpecialAttributeNames.__NAME__; |
| 58 | +import static com.oracle.graal.python.nodes.SpecialMethodNames.__REPR__; |
| 59 | +import static com.oracle.graal.python.nodes.SpecialMethodNames.__STR__; |
53 | 60 | import com.oracle.graal.python.nodes.argument.ReadArgumentNode;
|
54 | 61 | import com.oracle.graal.python.nodes.expression.CastToListExpressionNode.CastToListNode;
|
55 | 62 | import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
|
|
61 | 68 | import com.oracle.graal.python.nodes.object.SetDictNode;
|
62 | 69 | import com.oracle.graal.python.nodes.util.CannotCastException;
|
63 | 70 | import com.oracle.graal.python.nodes.util.CastToJavaBooleanNode;
|
| 71 | +import com.oracle.graal.python.nodes.util.CastToJavaStringNode; |
64 | 72 | import com.oracle.graal.python.runtime.exception.PythonErrorType;
|
65 | 73 | import com.oracle.graal.python.runtime.formatting.ErrorMessageFormatter;
|
| 74 | +import com.oracle.graal.python.util.PythonUtils; |
66 | 75 | import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
|
67 | 76 | import com.oracle.truffle.api.dsl.Cached;
|
68 | 77 | import com.oracle.truffle.api.dsl.Fallback;
|
@@ -318,4 +327,92 @@ Object reduce(VirtualFrame frame, PBaseException self,
|
318 | 327 | return factory().createTuple(new Object[]{clazz, args, dict});
|
319 | 328 | }
|
320 | 329 | }
|
| 330 | + |
| 331 | + @Builtin(name = __REPR__, minNumOfPositionalArgs = 1) |
| 332 | + @GenerateNodeFactory |
| 333 | + public abstract static class ReprNode extends PythonUnaryBuiltinNode { |
| 334 | + @Specialization(guards = "argsLen(frame, self, lenNode, argsNode) == 0") |
| 335 | + Object reprNoArgs(@SuppressWarnings("unused") VirtualFrame frame, PBaseException self, |
| 336 | + @SuppressWarnings("unused") @Cached SequenceStorageNodes.LenNode lenNode, |
| 337 | + @SuppressWarnings("unused") @Cached ArgsNode argsNode, |
| 338 | + @Cached GetClassNode getClassNode, |
| 339 | + @Cached PyObjectGetAttr getAttrNode, |
| 340 | + @Cached CastToJavaStringNode castStringNode) { |
| 341 | + Object type = getClassNode.execute(self); |
| 342 | + StringBuilder sb = new StringBuilder(); |
| 343 | + PythonUtils.append(sb, castStringNode.execute(getAttrNode.execute(frame, type, __NAME__))); |
| 344 | + PythonUtils.append(sb, "()"); |
| 345 | + return PythonUtils.sbToString(sb); |
| 346 | + } |
| 347 | + |
| 348 | + @Specialization(guards = "argsLen(frame, self, lenNode, argsNode) == 1") |
| 349 | + Object reprArgs1(VirtualFrame frame, PBaseException self, |
| 350 | + @SuppressWarnings("unused") @Cached SequenceStorageNodes.LenNode lenNode, |
| 351 | + @SuppressWarnings("unused") @Cached ArgsNode argsNode, |
| 352 | + @Cached GetClassNode getClassNode, |
| 353 | + @Cached PyObjectGetAttr getAttrNode, |
| 354 | + @Cached GetItemNode getItemNode, |
| 355 | + @Cached PyObjectReprAsObjectNode reprNode, |
| 356 | + @Cached CastToJavaStringNode castStringNode) { |
| 357 | + Object type = getClassNode.execute(self); |
| 358 | + StringBuilder sb = new StringBuilder(); |
| 359 | + PythonUtils.append(sb, castStringNode.execute(getAttrNode.execute(frame, type, __NAME__))); |
| 360 | + PythonUtils.append(sb, "("); |
| 361 | + PythonUtils.append(sb, (String) reprNode.execute(frame, getItemNode.execute(frame, self.getArgs(), 0))); |
| 362 | + PythonUtils.append(sb, ")"); |
| 363 | + return PythonUtils.sbToString(sb); |
| 364 | + } |
| 365 | + |
| 366 | + @Specialization(guards = "argsLen(frame, self, lenNode, argsNode) > 1") |
| 367 | + Object reprArgs(VirtualFrame frame, PBaseException self, |
| 368 | + @SuppressWarnings("unused") @Cached SequenceStorageNodes.LenNode lenNode, |
| 369 | + @SuppressWarnings("unused") @Cached ArgsNode argsNode, |
| 370 | + @Cached GetClassNode getClassNode, |
| 371 | + @Cached PyObjectGetAttr getAttrNode, |
| 372 | + @Cached com.oracle.graal.python.builtins.objects.tuple.TupleBuiltins.ReprNode reprNode, |
| 373 | + @Cached CastToJavaStringNode castStringNode) { |
| 374 | + Object type = getClassNode.execute(self); |
| 375 | + StringBuilder sb = new StringBuilder(); |
| 376 | + PythonUtils.append(sb, castStringNode.execute(getAttrNode.execute(frame, type, __NAME__))); |
| 377 | + PythonUtils.append(sb, (String) reprNode.execute(frame, self.getArgs())); |
| 378 | + return PythonUtils.sbToString(sb); |
| 379 | + } |
| 380 | + |
| 381 | + protected int argsLen(VirtualFrame frame, PBaseException self, SequenceStorageNodes.LenNode lenNode, ArgsNode argsNode) { |
| 382 | + return lenNode.execute(((PTuple) argsNode.executeObject(frame, self, PNone.NO_VALUE)).getSequenceStorage()); |
| 383 | + } |
| 384 | + } |
| 385 | + |
| 386 | + @Builtin(name = __STR__, minNumOfPositionalArgs = 1) |
| 387 | + @GenerateNodeFactory |
| 388 | + public abstract static class StrNode extends PythonUnaryBuiltinNode { |
| 389 | + @SuppressWarnings("unused") |
| 390 | + @Specialization(guards = "argsLen(frame, self, lenNode, argsNode) == 0") |
| 391 | + Object strNoArgs(VirtualFrame frame, PBaseException self, |
| 392 | + @Cached SequenceStorageNodes.LenNode lenNode, |
| 393 | + @Cached ArgsNode argsNode) { |
| 394 | + return PythonUtils.EMPTY_STRING; |
| 395 | + } |
| 396 | + |
| 397 | + @Specialization(guards = "argsLen(frame, self, lenNode, argsNode) == 1") |
| 398 | + Object strArgs1(VirtualFrame frame, PBaseException self, |
| 399 | + @SuppressWarnings("unused") @Cached SequenceStorageNodes.LenNode lenNode, |
| 400 | + @SuppressWarnings("unused") @Cached ArgsNode argsNode, |
| 401 | + @Cached GetItemNode getItemNode, |
| 402 | + @Cached PyObjectStrAsObjectNode strNode) { |
| 403 | + return strNode.execute(frame, getItemNode.execute(frame, self.getArgs(), 0)); |
| 404 | + } |
| 405 | + |
| 406 | + @Specialization(guards = {"argsLen(frame, self, lenNode, argsNode) > 1"}) |
| 407 | + Object strArgs(VirtualFrame frame, PBaseException self, |
| 408 | + @SuppressWarnings("unused") @Cached SequenceStorageNodes.LenNode lenNode, |
| 409 | + @SuppressWarnings("unused") @Cached ArgsNode argsNode, |
| 410 | + @Cached PyObjectStrAsObjectNode strNode) { |
| 411 | + return strNode.execute(frame, self.getArgs()); |
| 412 | + } |
| 413 | + |
| 414 | + protected int argsLen(VirtualFrame frame, PBaseException self, SequenceStorageNodes.LenNode lenNode, ArgsNode argsNode) { |
| 415 | + return lenNode.execute(((PTuple) argsNode.executeObject(frame, self, PNone.NO_VALUE)).getSequenceStorage()); |
| 416 | + } |
| 417 | + } |
321 | 418 | }
|
0 commit comments