Summary description for ConstantValue.
Пример #1
0
 private static BigInteger ParseBigIntegerSign(string s, int radix)
 {
     try {
         return(LiteralParser.ParseBigIntegerSign(s, radix));
     } catch (ArgumentException e) {
         throw Ops.ValueError(e.Message);
     }
 }
Пример #2
0
        public static object Make(PythonType cls, string s, int radix)
        {
            ValidateType(cls);

            try {
                return(LiteralParser.ParseIntegerSign(s, radix));
            } catch (ArgumentException e) {
                throw Ops.ValueError(e.Message);
            }
        }
Пример #3
0
        public static object Make(PythonType cls, object o)
        {
            if (cls == FloatType)
            {
                if (o is string)
                {
                    try {
                        return(LiteralParser.ParseFloat((string)o));
                    } catch (FormatException) {
                        throw Ops.ValueError("invalid literal for float()");
                    }
                }
                Conversion conv;
                object     d = Converter.TryConvertToDouble(o, out conv);
                if (conv != Conversion.None)
                {
                    return(d);
                }

                if (o is Complex64)
                {
                    throw Ops.TypeError("can't convert complex to float; use abs(z)");
                }

                d = Ops.Call(Ops.GetAttr(DefaultContext.Default, o, SymbolTable.ConvertToFloat));
                if (d is double)
                {
                    return(d);
                }
                throw Ops.TypeError("__float__ returned non-float (type %s)", Ops.GetDynamicType(d));
            }
            else
            {
                return(cls.ctor.Call(cls, o));
            }
        }
Пример #4
0
        public static object Make(
            PythonType cls,
            [DefaultParameterValueAttribute(null)] object real,
            [DefaultParameterValueAttribute(null)] object imag
            )
        {
            Conversion conv;
            Complex64  real2, imag2;

            real2 = imag2 = new Complex64();

            if (imag != null)
            {
                if (real is string)
                {
                    throw Ops.TypeError("complex() can't take second arg if first is a string");
                }
                if (imag is string)
                {
                    throw Ops.TypeError("complex() second arg can't be a string");
                }
                imag2 = Converter.TryConvertToComplex64(imag, out conv);
                if (conv == Conversion.None)
                {
                    throw Ops.TypeError("complex() argument must be a string or a number");
                }
            }

            if (real != null)
            {
                if (real is string)
                {
                    real2 = LiteralParser.ParseComplex64((string)real);
                }
                else if (real is Complex64)
                {
                    if (imag == null && cls == ComplexType)
                    {
                        return(real);
                    }
                    else
                    {
                        real2 = (Complex64)real;
                    }
                }
                else
                {
                    real2 = Converter.TryConvertToComplex64(real, out conv);
                    if (conv == Conversion.None)
                    {
                        throw Ops.TypeError("complex() argument must be a string or a number");
                    }
                }
            }

            Complex64 c = real2 + imag2 * Complex64.MakeImaginary(1);

            if (cls == ComplexType)
            {
                return(new Complex64(c.real, c.imag));
            }
            else
            {
                return(cls.ctor.Call(cls, c.real, c.imag));
            }
        }