示例#1
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));
            }
        }
示例#2
0
 public static int Compare(BigInteger x, [NotNull] Extensible <double> y)
 {
     return(-((int)DoubleOps.Compare(y.Value, x)));
 }
示例#3
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) ||
                x is OldInstance &&
                PythonTypeOps.TryInvokeUnaryOperator(context, x, "__int__", 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.GetOldName(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.GetOldName(result));
                }
            }

            if (x is OldInstance)
            {
                throw PythonOps.AttributeError("{0} instance has no attribute '__trunc__'",
                                               ((OldInstance)x)._class.Name);
            }
            else
            {
                throw PythonOps.TypeError("long() argument must be a string or a number, not '{0}'",
                                          DynamicHelpers.GetPythonType(x).Name);
            }
        }
示例#4
0
 public static object __getnewargs__(CodeContext context, double self)
 {
     return(PythonTuple.MakeTuple(DoubleOps.__new__(context, TypeCache.Double, self)));
 }
示例#5
0
 public static int Compare(BigInteger x, double y)
 {
     return(-((int)DoubleOps.Compare(y, x)));
 }
示例#6
0
 public static int __hash__(float x)
 {
     return(DoubleOps.__hash__(((double)x)));
 }
示例#7
0
 public static string __format__(CodeContext /*!*/ context, float self, [NotNull] string /*!*/ formatSpec)
 {
     return(DoubleOps.__format__(context, self, formatSpec));
 }
示例#8
0
 public static float Power(float x, float y)
 {
     return((float)DoubleOps.Power(x, y));
 }
示例#9
0
 public static float Mod(float x, float y)
 {
     return((float)DoubleOps.Mod(x, y));
 }
示例#10
0
 private static string FormatComplexValue(CodeContext /*!*/ context, double x)
 => DoubleOps.Repr(context, x, trailingZeroAfterWholeFloat: false);