Ejemplo n.º 1
0
        internal static PythonDictionary GetInvertedRegistry(CodeContext /*!*/ context)
        {
            EnsureModuleInitialized(context);

            return((PythonDictionary)PythonContext.GetContext(context).GetModuleState(_invertedRegistryKey));
        }
Ejemplo n.º 2
0
 private static int GetStackSize(CodeContext /*!*/ context)
 {
     return((int)PythonContext.GetContext(context).GetModuleState(_stackSizeKey));
 }
Ejemplo n.º 3
0
 private static long GetLockCount(CodeContext /*!*/ context)
 {
     return((long)PythonContext.GetContext(context).GetModuleState(_lockCountKey));
 }
Ejemplo n.º 4
0
        public static object iconcat(CodeContext /*!*/ context, object a, object b)
        {
            TestBothSequence(a, b);

            return(PythonContext.GetContext(context).Operation(PythonOperationKind.InPlaceAdd, a, b));
        }
Ejemplo n.º 5
0
 public static object Call(CodeContext /*!*/ context, Delegate @delegate, [ParamDictionary] IDictionary <object, object> dict, params object[] args)
 {
     return(PythonContext.GetContext(context).CallWithKeywords(@delegate, args, dict));
 }
Ejemplo n.º 6
0
 public static object irshift(CodeContext /*!*/ context, object a, object b)
 {
     return(PythonContext.GetContext(context).Operation(PythonOperationKind.InPlaceRightShift, a, b));
 }
Ejemplo n.º 7
0
 public static object itruediv(CodeContext /*!*/ context, object a, object b)
 {
     return(PythonContext.GetContext(context).Operation(PythonOperationKind.InPlaceTrueDivide, a, b));
 }
Ejemplo n.º 8
0
 public static object ne(CodeContext /*!*/ context, object a, object b)
 {
     return(PythonContext.GetContext(context).Operation(PythonOperationKind.NotEqual, a, b));
 }
Ejemplo n.º 9
0
 public static object gt(CodeContext /*!*/ context, object a, object b)
 {
     return(PythonContext.GetContext(context).Operation(PythonOperationKind.GreaterThan, a, b));
 }
Ejemplo n.º 10
0
 public static void setrecursionlimit(CodeContext /*!*/ context, int limit)
 {
     PythonContext.GetContext(context).RecursionLimit = limit;
 }
Ejemplo n.º 11
0
 public static int getrecursionlimit(CodeContext /*!*/ context)
 {
     return(PythonContext.GetContext(context).RecursionLimit);
 }
Ejemplo n.º 12
0
 public static string getdefaultencoding(CodeContext /*!*/ context)
 {
     return(PythonContext.GetContext(context).GetDefaultEncodingName());
 }
Ejemplo n.º 13
0
        /// <summary>
        /// Returns the digits for the format spec, no sign is included.
        /// </summary>
        private static string DoubleToFormatString(CodeContext /*!*/ context, double self, StringFormatSpec /*!*/ spec)
        {
            self = Math.Abs(self);
            const int DefaultPrecision = 6;
            int       precision        = spec.Precision ?? DefaultPrecision;

            string digits;

            switch (spec.Type)
            {
            case '%': {
                string fmt = "0." + new string('0', precision) + "%";
                if (spec.ThousandsComma)
                {
                    fmt = "#," + fmt;
                }
                digits = self.ToString(fmt, CultureInfo.InvariantCulture);
                break;
            }

            case 'f':
            case 'F': {
                string fmt = "0." + new string('0', precision);
                if (spec.ThousandsComma)
                {
                    fmt = "#," + fmt;
                }
                digits = self.ToString(fmt, CultureInfo.InvariantCulture);
                break;
            }

            case 'e':
            case 'E': {
                string fmt = "0." + new string('0', precision) + spec.Type + "+00";
                if (spec.ThousandsComma)
                {
                    fmt = "#," + fmt;
                }
                digits = self.ToString(fmt, CultureInfo.InvariantCulture);
                break;
            }

            case '\0':
            case null:
                if (spec.Precision != null)
                {
                    // precision applies to the combined digits before and after the decimal point
                    // so we first need find out how many digits we have before...
                    int    digitCnt = 1;
                    double cur      = self;
                    while (cur >= 10)
                    {
                        cur /= 10;
                        digitCnt++;
                    }

                    // Use exponents if we don't have enough room for all the digits before.  If we
                    // only have as single digit avoid exponents.
                    if (digitCnt > spec.Precision.Value && digitCnt != 1)
                    {
                        // first round off the decimal value
                        self = MathUtils.RoundAwayFromZero(self, 0);

                        // then remove any insignificant digits
                        double pow = Math.Pow(10, digitCnt - Math.Max(spec.Precision.Value, 1));
                        self = self - (self % pow);

                        // finally format w/ the requested precision
                        string fmt = "0.0" + new string('#', spec.Precision.Value);

                        digits = self.ToString(fmt + "e+00", CultureInfo.InvariantCulture);
                    }
                    else
                    {
                        // we're including all the numbers to the right of the decimal we can, we explicitly
                        // round to match CPython's behavior
                        int decimalPoints = Math.Max(spec.Precision.Value - digitCnt, 0);

                        self   = MathUtils.RoundAwayFromZero(self, decimalPoints);
                        digits = self.ToString("0.0" + new string('#', decimalPoints));
                    }
                }
                else
                {
                    // just the default formatting
                    if (IncludeExponent(self))
                    {
                        digits = self.ToString("0.#e+00", CultureInfo.InvariantCulture);
                    }
                    else if (spec.ThousandsComma)
                    {
                        digits = self.ToString("#,0.0###", CultureInfo.InvariantCulture);
                    }
                    else
                    {
                        digits = self.ToString("0.0###", CultureInfo.InvariantCulture);
                    }
                }
                break;

            case 'n':
            case 'g':
            case 'G': {
                // precision applies to the combined digits before and after the decimal point
                // so we first need find out how many digits we have before...
                int    digitCnt = 1;
                double cur      = self;
                while (cur >= 10)
                {
                    cur /= 10;
                    digitCnt++;
                }

                // Use exponents if we don't have enough room for all the digits before.  If we
                // only have as single digit avoid exponents.
                if (digitCnt > precision && digitCnt != 1)
                {
                    // first round off the decimal value
                    self = MathUtils.RoundAwayFromZero(self, 0);

                    // then remove any insignificant digits
                    double pow  = Math.Pow(10, digitCnt - Math.Max(precision, 1));
                    double rest = self / pow;
                    self = self - self % pow;
                    if ((rest % 1) >= .5)
                    {
                        // round up
                        self += pow;
                    }

                    string fmt;
                    if (spec.Type == 'n' && PythonContext.GetContext(context).NumericCulture != PythonContext.CCulture)
                    {
                        // we've already figured out, we don't have any digits for decimal points, so just format as a number + exponent
                        fmt = "0";
                    }
                    else if (spec.Precision > 1 || digitCnt > 6)
                    {
                        // include the requested precision to the right of the decimal
                        fmt = "0.#" + new string('#', precision);
                    }
                    else
                    {
                        // zero precision, no decimal
                        fmt = "0";
                    }
                    if (spec.ThousandsComma)
                    {
                        fmt = "#," + fmt;
                    }

                    digits = self.ToString(fmt + (spec.Type == 'G' ? "E+00" : "e+00"), CultureInfo.InvariantCulture);
                }
                else
                {
                    // we're including all the numbers to the right of the decimal we can, we explicitly
                    // round to match CPython's behavior
                    if (self < 1)
                    {
                        // no implicit 0
                        digitCnt--;
                    }
                    int decimalPoints = Math.Max(precision - digitCnt, 0);

                    self = MathUtils.RoundAwayFromZero(self, decimalPoints);

                    if (spec.Type == 'n' && PythonContext.GetContext(context).NumericCulture != PythonContext.CCulture)
                    {
                        if (digitCnt != precision && (self % 1) != 0)
                        {
                            digits = self.ToString("#,0.0" + new string('#', decimalPoints));
                        }
                        else
                        {
                            // leave out the decimal if the precision == # of digits or we have a whole number
                            digits = self.ToString("#,0");
                        }
                    }
                    else
                    {
                        if (digitCnt != precision && (self % 1) != 0)
                        {
                            digits = self.ToString("0.0" + new string('#', decimalPoints));
                        }
                        else
                        {
                            // leave out the decimal if the precision == # of digits or we have a whole number
                            digits = self.ToString("0");
                        }
                    }
                }
            }
            break;

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

            return(digits);
        }
Ejemplo n.º 14
0
        internal static PythonDictionary GetExtensionCache(CodeContext /*!*/ context)
        {
            EnsureModuleInitialized(context);

            return((PythonDictionary)PythonContext.GetContext(context).GetModuleState(_extensionCacheKey));
        }
Ejemplo n.º 15
0
 public static object iand(CodeContext /*!*/ context, object a, object b)
 {
     return(PythonContext.GetContext(context).Operation(PythonOperationKind.InPlaceBitwiseAnd, a, b));
 }
Ejemplo n.º 16
0
 public static object add(CodeContext /*!*/ context, object a, object b)
 {
     return(PythonContext.GetContext(context).Operation(PythonOperationKind.Add, a, b));
 }
Ejemplo n.º 17
0
 public static object ipow(CodeContext /*!*/ context, object a, object b)
 {
     return(PythonContext.GetContext(context).Operation(PythonOperationKind.InPlacePower, a, b));
 }
Ejemplo n.º 18
0
 public static object floordiv(CodeContext /*!*/ context, object a, object b)
 {
     return(PythonContext.GetContext(context).Operation(PythonOperationKind.FloorDivide, a, b));
 }
Ejemplo n.º 19
0
 public static object isub(CodeContext /*!*/ context, object a, object b)
 {
     return(PythonContext.GetContext(context).Operation(PythonOperationKind.InPlaceSubtract, a, b));
 }
Ejemplo n.º 20
0
 public static object mul(CodeContext /*!*/ context, object a, object b)
 {
     return(PythonContext.GetContext(context).Operation(PythonOperationKind.Multiply, a, b));
 }
Ejemplo n.º 21
0
 public static object ixor(CodeContext /*!*/ context, object a, object b)
 {
     return(PythonContext.GetContext(context).Operation(PythonOperationKind.InPlaceExclusiveOr, a, b));
 }
Ejemplo n.º 22
0
 public static object or_(CodeContext /*!*/ context, object a, object b)
 {
     return(PythonContext.GetContext(context).Operation(PythonOperationKind.BitwiseOr, a, b));
 }
Ejemplo n.º 23
0
 public static object Call(CodeContext /*!*/ context, Delegate @delegate, params object[] args)
 {
     return(PythonContext.GetContext(context).CallSplat(@delegate, args));
 }
Ejemplo n.º 24
0
 public static bool contains(CodeContext /*!*/ context, object a, object b)
 {
     return(PythonContext.GetContext(context).Contains(b, a));
 }
Ejemplo n.º 25
0
 public static int field_size_limit(CodeContext /*!*/ context)
 {
     return((int)PythonContext.GetContext(context).
            GetModuleState(_fieldSizeLimitKey));
 }
Ejemplo n.º 26
0
 public static void delitem(CodeContext /*!*/ context, object a, object b)
 {
     PythonContext.GetContext(context).DelIndex(a, b);
 }
Ejemplo n.º 27
0
 private static void SetStackSize(CodeContext /*!*/ context, int stackSize)
 {
     PythonContext.GetContext(context).SetModuleState(_stackSizeKey, stackSize);
 }
Ejemplo n.º 28
0
 public static void setitem(CodeContext /*!*/ context, object a, object b, object c)
 {
     PythonContext.GetContext(context).SetIndex(a, b, c);
 }
Ejemplo n.º 29
0
 private static void SetLockCount(CodeContext /*!*/ context, long lockCount)
 {
     PythonContext.GetContext(context).SetModuleState(_lockCountKey, lockCount);
 }
Ejemplo n.º 30
0
        internal static PythonDictionary GetDispatchTable(CodeContext /*!*/ context)
        {
            EnsureModuleInitialized(context);

            return((PythonDictionary)PythonContext.GetContext(context).GetModuleState(_dispatchTableKey));
        }