Ejemplo n.º 1
0
 protected override object Calculate(object l, object r)
 {
     return(ScriptingRuntimeHelpers.Int32ToObject((Int32)l >> (Int32)r));
 }
Ejemplo n.º 2
0
 private static object GreaterThenResult(int?comparisonResult)
 {
     return((comparisonResult.HasValue) ? ScriptingRuntimeHelpers.BooleanToObject(comparisonResult.Value > 0) : null);
 }
Ejemplo n.º 3
0
        private static object CharmapDecodeWorker(string input, string errors, IDictionary <object, object> map, bool isDecode)
        {
            if (input.Length == 0)
            {
                return(PythonTuple.MakeTuple(String.Empty, 0));
            }

            StringBuilder res = new StringBuilder();

            for (int i = 0; i < input.Length; i++)
            {
                object val;

                if (map == null)
                {
                    res.Append(input[i]);
                    continue;
                }

                object charObj = ScriptingRuntimeHelpers.Int32ToObject((int)input[i]);

                if (!map.TryGetValue(charObj, out val))
                {
                    if (errors == "strict" && isDecode)
                    {
                        throw PythonOps.UnicodeDecodeError("failed to find key in mapping");
                    }
                    else if (!isDecode)
                    {
                        throw PythonOps.UnicodeEncodeError("failed to find key in mapping");
                    }
                    res.Append("\ufffd");
                }
                else if (val == null)
                {
                    if (errors == "strict" && isDecode)
                    {
                        throw PythonOps.UnicodeDecodeError("'charmap' codec can't decode characters at index {0} because charmap maps to None", i);
                    }
                    else if (!isDecode)
                    {
                        throw PythonOps.UnicodeEncodeError("charmap", input[i], i,
                                                           "'charmap' codec can't encode characters at index {0} because charmap maps to None", i);
                    }
                    res.Append("\ufffd");
                }
                else if (val is string)
                {
                    res.Append((string)val);
                }
                else if (val is int)
                {
                    res.Append((char)(int)val);
                }
                else
                {
                    throw PythonOps.TypeError("charmap must be an int, str, or None");
                }
            }
            return(PythonTuple.MakeTuple(res.ToString(), res.Length));
        }
Ejemplo n.º 4
0
 public object /*!*/ ToObject()
 {
     return((object)_bignum ?? ScriptingRuntimeHelpers.Int32ToObject(_fixnum));
 }
Ejemplo n.º 5
0
        public static string /*!*/ __format__(CodeContext /*!*/ context, BigInteger /*!*/ self, [NotNull] string /*!*/ formatSpec)
        {
            StringFormatSpec spec = StringFormatSpec.FromString(formatSpec);

            if (spec.Precision != null)
            {
                throw PythonOps.ValueError("Precision not allowed in integer format specifier");
            }

            BigInteger val = self;

            if (self < 0)
            {
                val = -self;
            }
            string digits;

            switch (spec.Type)
            {
            case 'n':
                CultureInfo culture = PythonContext.GetContext(context).NumericCulture;

                if (culture == CultureInfo.InvariantCulture)
                {
                    // invariant culture maps to CPython's C culture, which doesn't
                    // include any formatting info.
                    goto case 'd';
                }

                digits = ToCultureString(val, PythonContext.GetContext(context).NumericCulture);
                break;

#if !FEATURE_NUMERICS
            case null:
            case 'd':
                digits = val.ToString();
                break;

            case '%':
                if (val == BigInteger.Zero)
                {
                    digits = "0.000000%";
                }
                else
                {
                    digits = val.ToString() + "00.000000%";
                }
                break;

            case 'e': digits = ToExponent(val, true, 6, 7); break;

            case 'E': digits = ToExponent(val, false, 6, 7); break;

            case 'f':
                if (val != BigInteger.Zero)
                {
                    digits = val.ToString() + ".000000";
                }
                else
                {
                    digits = "0.000000";
                }
                break;

            case 'F':
                if (val != BigInteger.Zero)
                {
                    digits = val.ToString() + ".000000";
                }
                else
                {
                    digits = "0.000000";
                }
                break;

            case 'g':
                if (val >= 1000000)
                {
                    digits = ToExponent(val, true, 0, 6);
                }
                else
                {
                    digits = val.ToString();
                }
                break;

            case 'G':
                if (val >= 1000000)
                {
                    digits = ToExponent(val, false, 0, 6);
                }
                else
                {
                    digits = val.ToString();
                }
                break;
#else
            case null:
            case 'd':
                if (spec.ThousandsComma)
                {
                    digits = val.ToString("#,0", CultureInfo.InvariantCulture);
                }
                else
                {
                    digits = val.ToString("D", CultureInfo.InvariantCulture);
                }
                break;

            case '%':
                if (spec.ThousandsComma)
                {
                    digits = val.ToString("#,0.000000%", CultureInfo.InvariantCulture);
                }
                else
                {
                    digits = val.ToString("0.000000%", CultureInfo.InvariantCulture);
                }
                break;

            case 'e':
                if (spec.ThousandsComma)
                {
                    digits = val.ToString("#,0.000000e+00", CultureInfo.InvariantCulture);
                }
                else
                {
                    digits = val.ToString("0.000000e+00", CultureInfo.InvariantCulture);
                }
                break;

            case 'E':
                if (spec.ThousandsComma)
                {
                    digits = val.ToString("#,0.000000E+00", CultureInfo.InvariantCulture);
                }
                else
                {
                    digits = val.ToString("0.000000E+00", CultureInfo.InvariantCulture);
                }
                break;

            case 'f':
            case 'F':
                if (spec.ThousandsComma)
                {
                    digits = val.ToString("#,########0.000000", CultureInfo.InvariantCulture);
                }
                else
                {
                    digits = val.ToString("#########0.000000", CultureInfo.InvariantCulture);
                }
                break;

            case 'g':
                if (val >= 1000000)
                {
                    digits = val.ToString("0.#####e+00", CultureInfo.InvariantCulture);
                }
                else if (spec.ThousandsComma)
                {
                    digits = val.ToString("#,0", CultureInfo.InvariantCulture);
                }
                else
                {
                    digits = val.ToString(CultureInfo.InvariantCulture);
                }
                break;

            case 'G':
                if (val >= 1000000)
                {
                    digits = val.ToString("0.#####E+00", CultureInfo.InvariantCulture);
                }
                else if (spec.ThousandsComma)
                {
                    digits = val.ToString("#,0", CultureInfo.InvariantCulture);
                }
                else
                {
                    digits = val.ToString(CultureInfo.InvariantCulture);
                }
                break;
#endif
            case 'X':
                digits = AbsToHex(val, false);
                break;

            case 'x':
                digits = AbsToHex(val, true);
                break;

            case 'o':     // octal
                digits = ToOctal(val, true);
                break;

            case 'b':     // binary
                digits = ToBinary(val, false, true);
                break;

            case 'c':     // single char
                int iVal;
                if (spec.Sign != null)
                {
                    throw PythonOps.ValueError("Sign not allowed with integer format specifier 'c'");
                }
                else if (!self.AsInt32(out iVal))
                {
                    throw PythonOps.OverflowError("long int too large to convert to int");
                }
                else if (iVal < 0 || iVal > 0xFF)
                {
                    throw PythonOps.OverflowError("%c arg not in range(0x10000)");
                }

                digits = ScriptingRuntimeHelpers.CharToString((char)iVal);
                break;

            default:
                throw PythonOps.ValueError("Unknown format code '{0}'", spec.Type.ToString());
            }

            Debug.Assert(digits[0] != '-');

            return(spec.AlignNumericText(digits, self.IsZero(), self.IsPositive()));
        }
Ejemplo n.º 6
0
 public void Push(int value)
 {
     Data[StackIndex++] = ScriptingRuntimeHelpers.Int32ToObject(value);
 }
Ejemplo n.º 7
0
        public static string __format__(CodeContext /*!*/ context, int self, [NotNull] string /*!*/ formatSpec)
        {
            StringFormatSpec spec = StringFormatSpec.FromString(formatSpec);

            if (spec.Precision != null)
            {
                throw PythonOps.ValueError("Precision not allowed in integer format specifier");
            }

            string digits;
            int    width = 0;

            switch (spec.Type)
            {
            case 'n':
                CultureInfo culture = PythonContext.GetContext(context).NumericCulture;

                if (culture == CultureInfo.InvariantCulture)
                {
                    // invariant culture maps to CPython's C culture, which doesn't
                    // include any formatting info.
                    goto case 'd';
                }
                width = spec.Width ?? 0;

                // If we're padding with leading zeros and we might be inserting
                // culture sensitive number group separators. (i.e. commas)
                // So use FormattingHelper.ToCultureString for that support.
                if (spec.Fill.HasValue && spec.Fill.Value == '0' && width > 1)
                {
                    digits = FormattingHelper.ToCultureString(self, culture.NumberFormat, spec);
                }
                else
                {
                    digits = self.ToString("N0", culture);
                }
                break;

            case null:
            case 'd':
                if (spec.ThousandsComma)
                {
                    width = spec.Width ?? 0;

                    // If we're inserting commas, and we're padding with leading zeros.
                    // AlignNumericText won't know where to place the commas,
                    // so use FormattingHelper.ToCultureString for that support.
                    if (spec.Fill.HasValue && spec.Fill.Value == '0' && width > 1)
                    {
                        digits = FormattingHelper.ToCultureString(self, FormattingHelper.InvariantCommaNumberInfo, spec);
                    }
                    else
                    {
                        digits = self.ToString("#,0", CultureInfo.InvariantCulture);
                    }
                }
                else
                {
                    digits = self.ToString("D", CultureInfo.InvariantCulture);
                }
                break;

            case '%':
                if (spec.ThousandsComma)
                {
                    digits = self.ToString("#,0.000000%", CultureInfo.InvariantCulture);
                }
                else
                {
                    digits = self.ToString("0.000000%", CultureInfo.InvariantCulture);
                }
                break;

            case 'e':
                if (spec.ThousandsComma)
                {
                    digits = self.ToString("#,0.000000e+00", CultureInfo.InvariantCulture);
                }
                else
                {
                    digits = self.ToString("0.000000e+00", CultureInfo.InvariantCulture);
                }
                break;

            case 'E':
                if (spec.ThousandsComma)
                {
                    digits = self.ToString("#,0.000000E+00", CultureInfo.InvariantCulture);
                }
                else
                {
                    digits = self.ToString("0.000000E+00", CultureInfo.InvariantCulture);
                }
                break;

            case 'f':
            case 'F':
                if (spec.ThousandsComma)
                {
                    digits = self.ToString("#,########0.000000", CultureInfo.InvariantCulture);
                }
                else
                {
                    digits = self.ToString("#########0.000000", CultureInfo.InvariantCulture);
                }
                break;

            case 'g':
                if (self >= 1000000 || self <= -1000000)
                {
                    digits = self.ToString("0.#####e+00", CultureInfo.InvariantCulture);
                }
                else if (spec.ThousandsComma)
                {
                    // Handle the common case in 'd'.
                    goto case 'd';
                }
                else
                {
                    digits = self.ToString(CultureInfo.InvariantCulture);
                }
                break;

            case 'G':
                if (self >= 1000000 || self <= -1000000)
                {
                    digits = self.ToString("0.#####E+00", CultureInfo.InvariantCulture);
                }
                else if (spec.ThousandsComma)
                {
                    // Handle the common case in 'd'.
                    goto case 'd';
                }
                else
                {
                    digits = self.ToString(CultureInfo.InvariantCulture);
                }
                break;

            case 'X':
                digits = ToHex(self, false);
                break;

            case 'x':
                digits = ToHex(self, true);
                break;

            case 'o':     // octal
                digits = ToOctal(self, true);
                break;

            case 'b':     // binary
                digits = ToBinary(self, false);
                break;

            case 'c':     // single char
                if (spec.Sign != null)
                {
                    throw PythonOps.ValueError("Sign not allowed with integer format specifier 'c'");
                }

                if (self < 0 || self > 0xFF)
                {
                    throw PythonOps.OverflowError("%c arg not in range(0x10000)");
                }

                digits = ScriptingRuntimeHelpers.CharToString((char)self);
                break;

            default:
                throw PythonOps.ValueError("Unknown format code '{0}'", spec.Type.ToString());
            }

            if (self < 0 && digits[0] == '-')
            {
                digits = digits.Substring(1);
            }

            return(spec.AlignNumericText(digits, self == 0, self > 0));
        }
Ejemplo n.º 8
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.º 9
0
 public override int Run(InterpretedFrame frame)
 {
     // unfortunately Type.IsInstanceOfType() is 35-times slower than "is T" so we use generic code:
     frame.Push(ScriptingRuntimeHelpers.BooleanToObject(frame.Pop() is T));
     return(+1);
 }