コード例 #1
0
        /// <summary>
        /// Creates PythonSuperType with a MRO list that starts with the next class in line in classType's MRO,
        /// or searches classType's MRO for <see cref="typeToFind"/>, then builds an MRO list for
        /// the remaining classes after <see cref="typeToFind"/>.
        /// </summary>
        /// <param name="classType"></param>
        /// <param name="typeToFind"></param>
        /// <returns></returns>
        internal static PythonSuperType Create(IPythonClassType classType, IPythonType typeToFind = null)
        {
            var mro = classType?.Mro ?? Array.Empty <IPythonType>();

            if (classType == null || mro.Count == 0)
            {
                return(null);
            }

            // skip doing work if the newStartType is the first element in the callers mro
            if (typeToFind?.Equals(classType.Mro.FirstOrDefault()) == false)
            {
                var mroList = classType.Mro.ToList();
                var start   = mroList.FindIndex(0, t => t.Equals(typeToFind));
                if (start >= 0)
                {
                    mro = mroList.GetRange(start, mro.Count - start).ToArray();
                }
                else
                {
                    return(null);  // typeToFind wasn't in the mro
                }
            }

            var nextClassInLine = mro?.FirstOrDefault();
            var builtins        = classType.DeclaringModule.Interpreter.ModuleResolution.BuiltinsModule;

            // Skip the first element, super's search starts at the next element in the mro for both super() and super(cls, typeToFind)
            return(nextClassInLine != null ? new PythonSuperType(mro.Skip(1).ToArray(), builtins) : null);
        }