|
47 | 47 | import static com.oracle.graal.python.builtins.objects.cext.common.CExtContext.isClassOrStaticMethod;
|
48 | 48 | import static com.oracle.graal.python.nodes.ErrorMessages.BAD_ARG_TO_INTERNAL_FUNC_S;
|
49 | 49 | import static com.oracle.graal.python.nodes.ErrorMessages.BAD_ARG_TO_INTERNAL_FUNC_WAS_S_P;
|
| 50 | +import static com.oracle.graal.python.nodes.ErrorMessages.BASE_MUST_BE; |
50 | 51 | import static com.oracle.graal.python.nodes.ErrorMessages.CANNOT_CONVERT_P_OBJ_TO_S;
|
51 | 52 | import static com.oracle.graal.python.nodes.ErrorMessages.HASH_MISMATCH;
|
52 | 53 | import static com.oracle.graal.python.nodes.ErrorMessages.LIST_INDEX_OUT_OF_RANGE;
|
|
58 | 59 | import static com.oracle.graal.python.nodes.SpecialMethodNames.ITEMS;
|
59 | 60 | import static com.oracle.graal.python.nodes.SpecialMethodNames.KEYS;
|
60 | 61 | import static com.oracle.graal.python.nodes.SpecialMethodNames.VALUES;
|
61 |
| -import static com.oracle.graal.python.nodes.SpecialMethodNames.__NEW__; |
| 62 | +import static com.oracle.graal.python.nodes.SpecialMethodNames.__FLOAT__; |
62 | 63 | import static com.oracle.graal.python.nodes.SpecialMethodNames.__ITER__;
|
| 64 | +import static com.oracle.graal.python.nodes.SpecialMethodNames.__NEW__; |
63 | 65 | import static com.oracle.graal.python.runtime.exception.PythonErrorType.OverflowError;
|
64 | 66 | import static com.oracle.graal.python.util.PythonUtils.EMPTY_BYTE_ARRAY;
|
65 | 67 | import static com.oracle.graal.python.util.PythonUtils.EMPTY_OBJECT_ARRAY;
|
|
94 | 96 | import com.oracle.graal.python.builtins.modules.BuiltinConstructors.FrozenSetNode;
|
95 | 97 | import com.oracle.graal.python.builtins.modules.BuiltinConstructors.MappingproxyNode;
|
96 | 98 | import com.oracle.graal.python.builtins.modules.BuiltinConstructors.StrNode;
|
| 99 | +import com.oracle.graal.python.builtins.modules.BuiltinFunctions.AbsNode; |
| 100 | +import com.oracle.graal.python.builtins.modules.BuiltinFunctions.BinNode; |
| 101 | +import com.oracle.graal.python.builtins.modules.BuiltinFunctions.DivModNode; |
| 102 | +import com.oracle.graal.python.builtins.modules.BuiltinFunctions.HexNode; |
| 103 | +import com.oracle.graal.python.builtins.modules.BuiltinFunctions.OctNode; |
97 | 104 | import com.oracle.graal.python.builtins.modules.PythonCextBuiltinsFactory.CreateFunctionNodeGen;
|
98 | 105 | import com.oracle.graal.python.builtins.objects.PNone;
|
99 | 106 | import com.oracle.graal.python.builtins.objects.PNotImplemented;
|
|
284 | 291 | import com.oracle.graal.python.nodes.PRaiseNode;
|
285 | 292 | import com.oracle.graal.python.nodes.SpecialAttributeNames;
|
286 | 293 | import com.oracle.graal.python.nodes.SpecialMethodNames;
|
287 |
| -import static com.oracle.graal.python.nodes.SpecialMethodNames.__FLOAT__; |
288 | 294 | import com.oracle.graal.python.nodes.WriteUnraisableNode;
|
289 | 295 | import com.oracle.graal.python.nodes.argument.CreateArgumentsNode.CreateAndCheckArgumentsNode;
|
290 | 296 | import com.oracle.graal.python.nodes.argument.keywords.ExpandKeywordStarargsNode;
|
@@ -2304,7 +2310,7 @@ public Object signNative(VirtualFrame frame, Object obj,
|
2304 | 2310 | }
|
2305 | 2311 |
|
2306 | 2312 | @Specialization(guards = {"!isInteger(obj)", "!isPInt(obj)", "!isPIntSubtype(frame, obj,getClassNode,isSubtypeNode)"})
|
2307 |
| - public Object sign(VirtualFrame frame, Object obj, |
| 2313 | + public Object sign(@SuppressWarnings("unused") VirtualFrame frame, @SuppressWarnings("unused") Object obj, |
2308 | 2314 | @SuppressWarnings("unused") @Cached GetClassNode getClassNode,
|
2309 | 2315 | @SuppressWarnings("unused") @Cached IsSubtypeNode isSubtypeNode) {
|
2310 | 2316 | // assert(PyLong_Check(v));
|
@@ -2505,6 +2511,181 @@ public PComplex asDouble(double r, double i) {
|
2505 | 2511 | }
|
2506 | 2512 | }
|
2507 | 2513 |
|
| 2514 | + ///////////// number ///////////// |
| 2515 | + |
| 2516 | + @Builtin(name = "PyNumber_Check", minNumOfPositionalArgs = 1) |
| 2517 | + @TypeSystemReference(PythonTypes.class) |
| 2518 | + @GenerateNodeFactory |
| 2519 | + abstract static class PyNumberCheckNode extends PythonUnaryBuiltinNode { |
| 2520 | + @Specialization |
| 2521 | + Object check(VirtualFrame frame, Object obj, |
| 2522 | + @Cached com.oracle.graal.python.lib.PyNumberCheckNode checkNode, |
| 2523 | + @Cached TransformExceptionToNativeNode transformExceptionToNativeNode, |
| 2524 | + @Cached GetNativeNullNode getNativeNullNode) { |
| 2525 | + try { |
| 2526 | + return checkNode.execute(frame, obj); |
| 2527 | + } catch (PException e) { |
| 2528 | + transformExceptionToNativeNode.execute(e); |
| 2529 | + return getNativeNullNode.execute(); |
| 2530 | + } |
| 2531 | + } |
| 2532 | + } |
| 2533 | + |
| 2534 | + @Builtin(name = "PyNumber_Index", minNumOfPositionalArgs = 1) |
| 2535 | + @GenerateNodeFactory |
| 2536 | + abstract static class PyNumberIndexNode extends PythonUnaryBuiltinNode { |
| 2537 | + @Specialization |
| 2538 | + Object index(VirtualFrame frame, Object obj, |
| 2539 | + @Cached com.oracle.graal.python.lib.PyNumberIndexNode indexNode, |
| 2540 | + @Cached TransformExceptionToNativeNode transformExceptionToNativeNode, |
| 2541 | + @Cached GetNativeNullNode getNativeNullNode) { |
| 2542 | + try { |
| 2543 | + return indexNode.execute(frame, obj); |
| 2544 | + } catch (PException e) { |
| 2545 | + transformExceptionToNativeNode.execute(e); |
| 2546 | + return getNativeNullNode.execute(); |
| 2547 | + } |
| 2548 | + } |
| 2549 | + } |
| 2550 | + |
| 2551 | + @Builtin(name = "PyNumber_Long", minNumOfPositionalArgs = 1) |
| 2552 | + @GenerateNodeFactory |
| 2553 | + abstract static class PyNumberLongNode extends PythonUnaryBuiltinNode { |
| 2554 | + |
| 2555 | + @Specialization |
| 2556 | + int nlong(int i) { |
| 2557 | + return i; |
| 2558 | + } |
| 2559 | + |
| 2560 | + @Specialization |
| 2561 | + long nlong(long i) { |
| 2562 | + return i; |
| 2563 | + } |
| 2564 | + |
| 2565 | + @Fallback |
| 2566 | + Object nlong(VirtualFrame frame, Object obj, |
| 2567 | + @Cached com.oracle.graal.python.builtins.modules.BuiltinConstructors.IntNode intNode, |
| 2568 | + @Cached TransformExceptionToNativeNode transformExceptionToNativeNode, |
| 2569 | + @Cached GetNativeNullNode getNativeNullNode) { |
| 2570 | + try { |
| 2571 | + return intNode.executeWith(frame, obj, PNone.NO_VALUE); |
| 2572 | + } catch (PException e) { |
| 2573 | + transformExceptionToNativeNode.execute(e); |
| 2574 | + return getNativeNullNode.execute(); |
| 2575 | + } |
| 2576 | + } |
| 2577 | + } |
| 2578 | + |
| 2579 | + @Builtin(name = "PyNumber_Absolute", minNumOfPositionalArgs = 1) |
| 2580 | + @GenerateNodeFactory |
| 2581 | + abstract static class PyNumberAbsoluteNode extends PythonUnaryBuiltinNode { |
| 2582 | + @Specialization |
| 2583 | + Object abs(VirtualFrame frame, Object obj, |
| 2584 | + @Cached AbsNode absNode, |
| 2585 | + @Cached TransformExceptionToNativeNode transformExceptionToNativeNode, |
| 2586 | + @Cached GetNativeNullNode getNativeNullNode) { |
| 2587 | + try { |
| 2588 | + return absNode.execute(frame, obj); |
| 2589 | + } catch (PException e) { |
| 2590 | + transformExceptionToNativeNode.execute(e); |
| 2591 | + return getNativeNullNode.execute(); |
| 2592 | + } |
| 2593 | + } |
| 2594 | + } |
| 2595 | + |
| 2596 | + @Builtin(name = "PyNumber_Divmod", minNumOfPositionalArgs = 2) |
| 2597 | + @GenerateNodeFactory |
| 2598 | + abstract static class PyNumberDivmodeNode extends PythonBinaryBuiltinNode { |
| 2599 | + @Specialization |
| 2600 | + Object div(VirtualFrame frame, Object a, Object b, |
| 2601 | + @Cached DivModNode divNode, |
| 2602 | + @Cached TransformExceptionToNativeNode transformExceptionToNativeNode, |
| 2603 | + @Cached GetNativeNullNode getNativeNullNode) { |
| 2604 | + try { |
| 2605 | + return divNode.execute(frame, a, b); |
| 2606 | + } catch (PException e) { |
| 2607 | + transformExceptionToNativeNode.execute(e); |
| 2608 | + return getNativeNullNode.execute(); |
| 2609 | + } |
| 2610 | + } |
| 2611 | + } |
| 2612 | + |
| 2613 | + @Builtin(name = "PyNumber_ToBase", minNumOfPositionalArgs = 2) |
| 2614 | + @GenerateNodeFactory |
| 2615 | + abstract static class PyNumberToBaseNode extends PythonBinaryBuiltinNode { |
| 2616 | + @Specialization(guards = "base == 2") |
| 2617 | + Object toBase(VirtualFrame frame, Object n, @SuppressWarnings("unused") int base, |
| 2618 | + @Cached com.oracle.graal.python.lib.PyNumberIndexNode indexNode, |
| 2619 | + @Cached BinNode binNode, |
| 2620 | + @Cached TransformExceptionToNativeNode transformExceptionToNativeNode, |
| 2621 | + @Cached GetNativeNullNode getNativeNullNode) { |
| 2622 | + try { |
| 2623 | + Object i = indexNode.execute(frame, n); |
| 2624 | + return binNode.execute(frame, i); |
| 2625 | + } catch (PException e) { |
| 2626 | + transformExceptionToNativeNode.execute(e); |
| 2627 | + return getNativeNullNode.execute(); |
| 2628 | + } |
| 2629 | + } |
| 2630 | + |
| 2631 | + @Specialization(guards = "base == 8") |
| 2632 | + Object toBase(VirtualFrame frame, Object n, @SuppressWarnings("unused") int base, |
| 2633 | + @Cached OctNode octNode, |
| 2634 | + @Cached TransformExceptionToNativeNode transformExceptionToNativeNode, |
| 2635 | + @Cached GetNativeNullNode getNativeNullNode) { |
| 2636 | + try { |
| 2637 | + return octNode.execute(frame, n); |
| 2638 | + } catch (PException e) { |
| 2639 | + transformExceptionToNativeNode.execute(e); |
| 2640 | + return getNativeNullNode.execute(); |
| 2641 | + } |
| 2642 | + } |
| 2643 | + |
| 2644 | + @Specialization(guards = "base == 10") |
| 2645 | + Object toBase(VirtualFrame frame, Object n, @SuppressWarnings("unused") int base, |
| 2646 | + @Cached com.oracle.graal.python.lib.PyNumberIndexNode indexNode, |
| 2647 | + @Cached StrNode strNode, |
| 2648 | + @Cached TransformExceptionToNativeNode transformExceptionToNativeNode, |
| 2649 | + @Cached GetNativeNullNode getNativeNullNode) { |
| 2650 | + try { |
| 2651 | + Object i = indexNode.execute(frame, n); |
| 2652 | + if (i instanceof Boolean) { |
| 2653 | + i = ((boolean) i) ? 1 : 0; |
| 2654 | + } |
| 2655 | + return strNode.executeWith(frame, i); |
| 2656 | + } catch (PException e) { |
| 2657 | + transformExceptionToNativeNode.execute(e); |
| 2658 | + return getNativeNullNode.execute(); |
| 2659 | + } |
| 2660 | + } |
| 2661 | + |
| 2662 | + @Specialization(guards = "base == 16") |
| 2663 | + Object toBase(VirtualFrame frame, Object n, @SuppressWarnings("unused") int base, |
| 2664 | + @Cached PyNumberIndexNode indexNode, |
| 2665 | + @Cached HexNode hexNode, |
| 2666 | + @Cached TransformExceptionToNativeNode transformExceptionToNativeNode, |
| 2667 | + @Cached GetNativeNullNode getNativeNullNode) { |
| 2668 | + try { |
| 2669 | + Object i = indexNode.execute(frame, n); |
| 2670 | + return hexNode.execute(frame, i); |
| 2671 | + } catch (PException e) { |
| 2672 | + transformExceptionToNativeNode.execute(e); |
| 2673 | + return getNativeNullNode.execute(); |
| 2674 | + } |
| 2675 | + } |
| 2676 | + |
| 2677 | + @Specialization(guards = "!checkBase(base)") |
| 2678 | + Object toBase(VirtualFrame frame, @SuppressWarnings("unused") Object n, @SuppressWarnings("unused") int base, |
| 2679 | + @Cached GetNativeNullNode getNativeNullNode, |
| 2680 | + @Cached PRaiseNativeNode raiseNativeNode) { |
| 2681 | + return raiseNativeNode.raise(frame, getNativeNullNode.execute(), SystemError, BASE_MUST_BE); |
| 2682 | + } |
| 2683 | + |
| 2684 | + protected boolean checkBase(int base) { |
| 2685 | + return base == 2 || base == 8 || base == 10 || base == 16; |
| 2686 | + } |
| 2687 | + } |
| 2688 | + |
2508 | 2689 | /**
|
2509 | 2690 | * This is used in the ExternalFunctionNode below, so all arguments passed from Python code into
|
2510 | 2691 | * a C function are automatically unwrapped if they are wrapped. This function is also called
|
|
0 commit comments