Пример #1
0
        public static object DefaultNew(CodeContext context, PythonType type\u00F8, params object[] args\u00F8)
        {
            if (type\u00F8 == null)
            {
                throw PythonOps.TypeError(
                          "__new__ expected type object, got {0}",
                          PythonOps.Repr(context, DynamicHelpers.GetPythonType(type\u00F8))
                          );
            }

            CheckNewArgs(context, null, args\u00F8, type\u00F8);

            return(type\u00F8.CreateInstance(context));
        }
Пример #2
0
        public static object NonDefaultNewKW(CodeContext context, PythonType type\u00F8, [ParamDictionary] IDictionary <object, object> kwargs\u00F8, params object[] args\u00F8)
        {
            if (type\u00F8 == null)
            {
                throw PythonOps.TypeError("__new__ expected type object, got {0}", PythonOps.Repr(context, DynamicHelpers.GetPythonType(type\u00F8)));
            }
            if (args\u00F8 == null)
            {
                args\u00F8 = new object[1];
            }

            string [] names;
            GetKeywordArgs(kwargs\u00F8, args\u00F8, out args\u00F8, out names);
            return(type\u00F8.CreateInstance(context, args\u00F8, names));
        }
Пример #3
0
        public static string __repr__(CodeContext /*!*/ context, Dictionary <K, V> self)
        {
            List <object> infinite = PythonOps.GetAndCheckInfinite(self);

            if (infinite == null)
            {
                return("{...}");
            }

            int index = infinite.Count;

            infinite.Add(self);
            try {
                StringBuilder res = new StringBuilder();
                res.Append("Dictionary[");
                res.Append(DynamicHelpers.GetPythonTypeFromType(typeof(K)).Name);
                res.Append(", ");
                res.Append(DynamicHelpers.GetPythonTypeFromType(typeof(V)).Name);
                res.Append("](");
                if (self.Count > 0)
                {
                    res.Append("{");
                    string comma = "";
                    foreach (KeyValuePair <K, V> obj in self)
                    {
                        res.Append(comma);
                        res.Append(PythonOps.Repr(context, obj.Key));
                        res.Append(" : ");
                        res.Append(PythonOps.Repr(context, obj.Value));
                        comma = ", ";
                    }
                    res.Append("}");
                }

                res.Append(")");
                return(res.ToString());
            } finally {
                Debug.Assert(index == infinite.Count - 1);
                infinite.RemoveAt(index);
            }
        }
Пример #4
0
        internal static object NewFloat(CodeContext /*!*/ context, PythonType type, object x)
        {
            string str;
            bool   replaceUnicode = true;

            if (x is string s)
            {
                str = s;
            }
            else if (x is char c)
            {
                str = ScriptingRuntimeHelpers.CharToString(c);
            }
            else if (TryToFloat(context, x, out var d))
            {
                return(d);
            }
            else if (x is Extensible <string> es)
            {
                str = es.Value;
            }
            else if (x is IBufferProtocol bufferProtocol)
            {
                using IPythonBuffer buf = bufferProtocol.GetBufferNoThrow()
                                          ?? throw PythonOps.TypeErrorForBadInstance($"{type.Name}() argument must be a string or a number, not '{{0}}'", x);
                str            = buf.AsReadOnlySpan().MakeString();
                replaceUnicode = false;
            }
            else
            {
                throw PythonOps.TypeErrorForBadInstance($"{type.Name}() argument must be a string or a number, not '{{0}}'", x);
            }

            if (LiteralParser.TryParseFloat(str, out var res, replaceUnicode: replaceUnicode))
            {
                return(res);
            }

            throw PythonOps.ValueError($"could not convert string to {type.Name}: {PythonOps.Repr(context, x)}");
        }
Пример #5
0
        public static string __repr__(CodeContext /*!*/ context, List <T> self)
        {
            List <object> infinite = PythonOps.GetAndCheckInfinite(self);

            if (infinite == null)
            {
                return("[...]");
            }

            int index = infinite.Count;

            infinite.Add(self);
            try {
                StringBuilder res = new StringBuilder();
                res.Append("List[");
                res.Append(DynamicHelpers.GetPythonTypeFromType(typeof(T)).Name);
                res.Append("](");
                if (self.Count > 0)
                {
                    res.Append("[");
                    string comma = "";
                    foreach (T obj in self)
                    {
                        res.Append(comma);
                        res.Append(PythonOps.Repr(context, obj));
                        comma = ", ";
                    }
                    res.Append("]");
                }

                res.Append(")");
                return(res.ToString());
            } finally {
                System.Diagnostics.Debug.Assert(index == infinite.Count - 1);
                infinite.RemoveAt(index);
            }
        }
Пример #6
0
 /// <summary>
 /// Returns a friendly string representation of the object.
 /// </summary>
 public static string __str__(CodeContext /*!*/ context, object o)
 {
     return(PythonOps.Repr(context, o));
 }