Example #1
0
        public static object fromkeys(CodeContext context, PythonType cls, object seq, object value)
        {
            XRange xr = seq as XRange;

            if (xr != null)
            {
                int    n   = xr.__len__();
                object ret = PythonContext.GetContext(context).CallSplat(cls);
                if (ret.GetType() == typeof(PythonDictionary))
                {
                    PythonDictionary dr = ret as PythonDictionary;
                    for (int i = 0; i < n; i++)
                    {
                        dr[xr[i]] = value;
                    }
                }
                else
                {
                    // slow path, user defined dict
                    PythonContext pc = PythonContext.GetContext(context);
                    for (int i = 0; i < n; i++)
                    {
                        pc.SetIndex(ret, xr[i], value);
                    }
                }
                return(ret);
            }
            return(fromkeysAny(context, cls, seq, value));
        }
Example #2
0
        private static object fromkeysAny(CodeContext /*!*/ context, PythonType cls, object o, object value)
        {
            PythonDictionary pyDict;
            object           dict;

            if (cls == TypeCache.Dict)
            {
                string      str;
                ICollection ic = o as ICollection;

                // creating our own dict, try and get the ideal size and add w/o locks
                if (ic != null)
                {
                    pyDict = new PythonDictionary(new CommonDictionaryStorage(ic.Count));
                }
                else if ((str = o as string) != null)
                {
                    pyDict = new PythonDictionary(str.Length);
                }
                else
                {
                    pyDict = new PythonDictionary();
                }

                IEnumerator i = PythonOps.GetEnumerator(o);
                while (i.MoveNext())
                {
                    pyDict._storage.AddNoLock(ref pyDict._storage, i.Current, value);
                }

                return(pyDict);
            }
            else
            {
                // call the user type constructor
                dict   = MakeDict(context, cls);
                pyDict = dict as PythonDictionary;
            }

            if (pyDict != null)
            {
                // then store all the keys with their associated value
                IEnumerator i = PythonOps.GetEnumerator(o);
                while (i.MoveNext())
                {
                    pyDict[i.Current] = value;
                }
            }
            else
            {
                // slow path, cls.__new__ returned a user defined dictionary instead of a PythonDictionary.
                PythonContext pc = PythonContext.GetContext(context);
                IEnumerator   i  = PythonOps.GetEnumerator(o);
                while (i.MoveNext())
                {
                    pc.SetIndex(dict, i.Current, value);
                }
            }

            return(dict);
        }