Ejemplo n.º 1
0
        public static object __new__(CodeContext context, PythonType cls, object x)
        {
            Extensible <string> es;

            if (x is string)
            {
                return(ReturnObject(context, cls, ParseBigIntegerSign((string)x, 10)));
            }
            else if ((es = x as Extensible <string>) != null)
            {
                object value;
                if (PythonTypeOps.TryInvokeUnaryOperator(context, x, "__long__", out value))
                {
                    return(ReturnObject(context, cls, (BigInteger)value));
                }

                return(ReturnObject(context, cls, ParseBigIntegerSign(es.Value, 10)));
            }
            if (x is double)
            {
                return(ReturnObject(context, cls, DoubleOps.__long__((double)x)));
            }
            if (x is int)
            {
                return(ReturnObject(context, cls, (BigInteger)(int)x));
            }
            if (x is BigInteger)
            {
                return(ReturnObject(context, cls, x));
            }

            if (x is Complex)
            {
                throw PythonOps.TypeError("can't convert complex to long; use long(abs(z))");
            }

            if (x is decimal)
            {
                return(ReturnObject(context, cls, (BigInteger)(decimal)x));
            }

            object     result;
            int        intRes;
            BigInteger bigintRes;

            if (PythonTypeOps.TryInvokeUnaryOperator(context, x, "__long__", out result) &&
                !Object.ReferenceEquals(result, NotImplementedType.Value))
            {
                if (result is int || result is BigInteger ||
                    result is Extensible <int> || result is Extensible <BigInteger> )
                {
                    return(ReturnObject(context, cls, result));
                }
                else
                {
                    throw PythonOps.TypeError("__long__ returned non-long (type {0})", PythonTypeOps.GetName(result));
                }
            }
            else if (PythonOps.TryGetBoundAttr(context, x, "__trunc__", out result))
            {
                result = PythonOps.CallWithContext(context, result);
                if (Converter.TryConvertToInt32(result, out intRes))
                {
                    return(ReturnObject(context, cls, (BigInteger)intRes));
                }
                else if (Converter.TryConvertToBigInteger(result, out bigintRes))
                {
                    return(ReturnObject(context, cls, bigintRes));
                }
                else
                {
                    throw PythonOps.TypeError("__trunc__ returned non-Integral (type {0})", PythonTypeOps.GetName(result));
                }
            }

            throw PythonOps.TypeError("long() argument must be a string or a number, not '{0}'",
                                      DynamicHelpers.GetPythonType(x).Name);
        }
Ejemplo n.º 2
0
        public static object __new__(CodeContext /*!*/ context, PythonType cls, object x)
        {
            object value = null;

            if (x is string)
            {
                value = ParseFloat((string)x);
            }
            else if (x is Extensible <string> )
            {
                if (!PythonTypeOps.TryInvokeUnaryOperator(context, x, "__float__", out value))
                {
                    value = ParseFloat(((Extensible <string>)x).Value);
                }
            }
            else if (x is char)
            {
                value = ParseFloat(ScriptingRuntimeHelpers.CharToString((char)x));
            }
            else if (x is Complex)
            {
                throw PythonOps.TypeError("can't convert complex to float; use abs(z)");
            }
            else
            {
                object d = PythonOps.CallWithContext(context, PythonOps.GetBoundAttr(context, x, "__float__"));
                if (d is double)
                {
                    value = d;
                }
                else if (d is Extensible <double> )
                {
                    value = ((Extensible <double>)d).Value;
                }
                else
                {
                    throw PythonOps.TypeError("__float__ returned non-float (type {0})", PythonTypeOps.GetName(d));
                }
            }

            if (cls == TypeCache.Double)
            {
                return(value);
            }
            else
            {
                return(cls.CreateInstance(context, value));
            }
        }
Ejemplo n.º 3
0
        private static object FastNew(CodeContext /*!*/ context, object o)
        {
            Extensible <BigInteger> el;

            if (o is string)
            {
                return(__new__(null, (string)o, 10));
            }
            if (o is double)
            {
                return(DoubleOps.__int__((double)o));
            }
            if (o is int)
            {
                return(o);
            }
            if (o is bool)
            {
                return(((bool)o) ? 1 : 0);
            }
            if (o is BigInteger)
            {
                int res;
                if (((BigInteger)o).AsInt32(out res))
                {
                    return(ScriptingRuntimeHelpers.Int32ToObject(res));
                }
                return(o);
            }

            if ((el = o as Extensible <BigInteger>) != null)
            {
                int res;
                if (el.Value.AsInt32(out res))
                {
                    return(ScriptingRuntimeHelpers.Int32ToObject(res));
                }
                return(el.Value);
            }

            if (o is float)
            {
                return(DoubleOps.__int__((double)(float)o));
            }

            if (o is Complex)
            {
                throw PythonOps.TypeError("can't convert complex to int; use int(abs(z))");
            }

            if (o is Int64)
            {
                Int64 val = (Int64)o;
                if (Int32.MinValue <= val && val <= Int32.MaxValue)
                {
                    return((Int32)val);
                }
                else
                {
                    return((BigInteger)val);
                }
            }
            else if (o is UInt32)
            {
                UInt32 val = (UInt32)o;
                if (val <= Int32.MaxValue)
                {
                    return((Int32)val);
                }
                else
                {
                    return((BigInteger)val);
                }
            }
            else if (o is UInt64)
            {
                UInt64 val = (UInt64)o;
                if (val <= Int32.MaxValue)
                {
                    return((Int32)val);
                }
                else
                {
                    return((BigInteger)val);
                }
            }
            else if (o is Decimal)
            {
                Decimal val = (Decimal)o;
                if (Int32.MinValue <= val && val <= Int32.MaxValue)
                {
                    return((Int32)val);
                }
                else
                {
                    return((BigInteger)val);
                }
            }
            else if (o is Enum)
            {
                return(((IConvertible)o).ToInt32(null));
            }

            Extensible <string> es = o as Extensible <string>;

            if (es != null)
            {
                // __int__ takes precedence, call it if it's available...
                object value;
                if (PythonTypeOps.TryInvokeUnaryOperator(DefaultContext.Default, es, "__int__", out value))
                {
                    return(value);
                }

                // otherwise call __new__ on the string value
                return(__new__(null, es.Value, 10));
            }

            object     result;
            int        intRes;
            BigInteger bigintRes;

            if (PythonTypeOps.TryInvokeUnaryOperator(context, o, "__int__", out result) &&
                !Object.ReferenceEquals(result, NotImplementedType.Value))
            {
                if (result is int || result is BigInteger ||
                    result is Extensible <int> || result is Extensible <BigInteger> )
                {
                    return(result);
                }
                else
                {
                    throw PythonOps.TypeError("__int__ returned non-Integral (type {0})", PythonTypeOps.GetOldName(result));
                }
            }
            else if (PythonOps.TryGetBoundAttr(context, o, "__trunc__", out result))
            {
                result = PythonOps.CallWithContext(context, result);
                if (result is int || result is BigInteger ||
                    result is Extensible <int> || result is Extensible <BigInteger> )
                {
                    return(result);
                }
                else if (Converter.TryConvertToInt32(result, out intRes))
                {
                    return(intRes);
                }
                else if (Converter.TryConvertToBigInteger(result, out bigintRes))
                {
                    return(bigintRes);
                }
                else
                {
                    throw PythonOps.TypeError("__trunc__ returned non-Integral (type {0})", PythonTypeOps.GetOldName(result));
                }
            }

            if (o is OldInstance)
            {
                throw PythonOps.AttributeError("{0} instance has no attribute '__trunc__'", PythonTypeOps.GetOldName((OldInstance)o));
            }
            else
            {
                throw PythonOps.TypeError("int() argument must be a string or a number, not '{0}'", PythonTypeOps.GetName(o));
            }
        }
Ejemplo n.º 4
0
 static void Warn(CodeContext context, object result)
 {
     PythonOps.Warn(context, PythonExceptions.DeprecationWarning, $"__int__ returned non-int (type {PythonTypeOps.GetName(result)}).  The ability to return an instance of a strict subclass of int is deprecated, and may be removed in a future version of Python.");
 }
Ejemplo n.º 5
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));
Ejemplo n.º 6
0
 public static string SimpleRepr(object self)
 {
     return(String.Format("<{0} object at {1}>",
                          PythonTypeOps.GetName(self),
                          PythonOps.HexId(self)));
 }
Ejemplo n.º 7
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));
                    }
Ejemplo n.º 8
0
        internal static void CheckInitArgs(CodeContext context, IDictionary <object, object> dict, object[] args, object self)
        {
            if (((args != null && args.Length > 0) || (dict != null && dict.Count > 0)))
            {
                PythonType pt            = DynamicHelpers.GetPythonType(self);
                bool       hasObjectInit = pt.HasObjectInit(context);
                bool       hasObjectNew  = pt.HasObjectNew(context);

                // NoneType seems to get some special treatment (None.__init__('abc') works)
                if (hasObjectNew && self != null)
                {
                    throw PythonOps.TypeError("object.__init__() takes no parameters");
                }
                else if ((!hasObjectNew && !hasObjectInit) || self == null)
                {
                    PythonOps.Warn(context, PythonExceptions.DeprecationWarning, "object.__init__() takes no parameters for type {0}", PythonTypeOps.GetName(self));
                }
            }
        }
Ejemplo n.º 9
0
        public static string __format__(CodeContext /*!*/ context, object self, [NotNull] string /*!*/ formatSpec)
        {
            if (formatSpec != string.Empty)
            {
                throw PythonOps.TypeError("unsupported format string passed to {0}.__format__", PythonTypeOps.GetName(self));
            }

            return(PythonOps.ToString(context, self));
        }
Ejemplo n.º 10
0
            static bool TryInvokeFloat(CodeContext context, object o, out object result)
            {
                if (PythonTypeOps.TryInvokeUnaryOperator(context, o, "__float__", out result))
                {
                    switch (result)
                    {
                    case double _:
                        return(true);

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

                    default:
                        throw PythonOps.TypeError("__float__ returned non-float (type {0})", PythonTypeOps.GetName(result));
                    }
Ejemplo n.º 11
0
        public static object __new__(CodeContext context, PythonType cls, [Optional] object?real, [Optional] object?imag)
        {
            if (real == null)
            {
                throw PythonOps.TypeError($"complex() first argument must be a string or a number, not '{PythonTypeOps.GetName(real)}'");
            }
            if (imag == null)
            {
                throw PythonOps.TypeError($"complex() second argument must be a number, not '{PythonTypeOps.GetName(real)}'");
            }

            Complex imag2;

            if (imag is Missing)
            {
                imag2 = Complex.Zero;
            }
            else
            {
                if (real is string)
                {
                    throw PythonOps.TypeError("complex() can't take second arg if first is a string");
                }
                if (imag is string)
                {
                    throw PythonOps.TypeError("complex() second arg can't be a string");
                }
                if (!Converter.TryConvertToComplex(imag, out imag2))
                {
                    throw PythonOps.TypeError($"complex() second argument must be a number, not '{PythonTypeOps.GetName(real)}'");
                }
            }

            Complex real2;

            if (real is Missing)
            {
                real2 = Complex.Zero;
            }
            else if (real is string)
            {
                real2 = LiteralParser.ParseComplex((string)real);
            }
            else if (real is Extensible <string> )
            {
                real2 = LiteralParser.ParseComplex(((Extensible <string>)real).Value);
            }
            else if (real is Complex)
            {
                if (imag is Missing && cls == TypeCache.Complex)
                {
                    return(real);
                }
                else
                {
                    real2 = (Complex)real;
                }
            }
            else if (!Converter.TryConvertToComplex(real, out real2))
            {
                throw PythonOps.TypeError($"complex() first argument must be a string or a number, not '{PythonTypeOps.GetName(real)}'");
            }

            double real3 = real2.Real - imag2.Imaginary;
            double imag3 = real2.Imaginary + imag2.Real;

            if (cls == TypeCache.Complex)
            {
                return(new Complex(real3, imag3));
            }
            else
            {
                return(cls.CreateInstance(context, real3, imag3));
            }
        }
Ejemplo n.º 12
0
 internal static IList <byte> CoerceBytes(object obj)
 {
     if (!(obj is IList <byte> ret))
     {
         throw PythonOps.TypeError("a bytes-like object is required, not '{0}'", PythonTypeOps.GetName(obj));
     }
     return(ret);
 }