Exemplo n.º 1
0
            static bool TryInvokeInt(CodeContext context, object o, out object result)
            {
                if (PythonTypeOps.TryInvokeUnaryOperator(context, o, "__int__", out result))
                {
                    switch (result)
                    {
                    case int _:
                        return(true);

                    case BigInteger bi:
                        if (bi.IsInt32())
                        {
                            result = (int)bi;
                        }
                        return(true);

                    case bool b:
                        Warn(context, result);
                        result = BoolOps.__int__(b);     // Python 3.6: return the int value
                        return(true);

                    case Extensible <int> ei:
                        Warn(context, result);
                        result = ei.Value;     // Python 3.6: return the int value
                        return(true);

                    case Extensible <BigInteger> ebi:
                        Warn(context, result);
                        result = ebi.Value.IsInt32() ? (object)(int)ebi.Value : ebi.Value;     // Python 3.6: return the int value
                        return(true);

                    default:
                        throw PythonOps.TypeError("__int__ returned non-int (type {0})", PythonTypeOps.GetName(result));
                    }
Exemplo n.º 2
0
        private static object FastNew(CodeContext /*!*/ context, object o, int @base = 10)
        {
            object result;

            switch (o)
            {
            case double d:
                return(DoubleOps.__int__(d));

            case bool b:
                return(BoolOps.__int__(b));

            case int _:
                return(o);

            case Extensible <int> ei:
                return(TryInvokeInt(context, o, out var value) ? value : ei.Value);

            case BigInteger val:
                return(val.IsInt32() ? (object)(int)val : o);

            case Extensible <BigInteger> ebi:
                return(TryInvokeInt(context, o, out result) ? result : ebi.Value.IsInt32() ? (object)(int)ebi.Value : ebi.Value);

            case float f:
                return(DoubleOps.__int__(f));

            case long val:
                return(int.MinValue <= val && val <= int.MaxValue ? (object)(int)val : (BigInteger)val);

            case uint val:
                return(val <= int.MaxValue ? (object)(int)val : (BigInteger)val);

            case ulong val:
                return(val <= int.MaxValue ? (object)(int)val : (BigInteger)val);

            case decimal val:
                return(int.MinValue <= val && val <= int.MaxValue ? (object)(int)val : (BigInteger)val);

            case Enum e:
                return(((IConvertible)e).ToInt32(null));

            case string s:
                return(LiteralParser.ParseIntegerSign(s, @base, FindStart(s, @base)));

            case Extensible <string> es:
                return(TryInvokeInt(context, o, out result) ? result : LiteralParser.ParseIntegerSign(es.Value, @base, FindStart(es.Value, @base)));

            default:
                break;
            }

            if (TryInvokeInt(context, o, out result))
            {
                return(result);
            }
            else if (PythonTypeOps.TryInvokeUnaryOperator(context, o, "__trunc__", out result))
            {
                switch (result)
                {
                case int _:
                    return(result);

                case BigInteger bi:
                    return(bi.IsInt32() ? (object)(int)bi : result);

                case bool b:
                    return(BoolOps.__int__(b));    // Python 3.6: return the int value

                case Extensible <int> ei:
                    return(ei.Value);    // Python 3.6: return the int value

                case Extensible <BigInteger> ebi:
                    return(ebi.Value.IsInt32() ? (object)(int)ebi.Value : ebi.Value);    // Python 3.6: return the int value

                default: {
                    if (TryInvokeInt(context, result, out var intResult))
                    {
                        return(intResult);
                    }
                    throw PythonOps.TypeError("__trunc__ returned non-Integral (type {0})", PythonTypeOps.GetName(result));
                }
                }
            }

            throw PythonOps.TypeError("int() argument must be a string, a bytes-like object or a number, not '{0}'", PythonTypeOps.GetName(o));