Beispiel #1
0
        public void SetAttr(ICallerContext context, SymbolId name, object value)
        {
            if (name.Id == SymbolTable.ClassId)
            {
                OldClass oc = value as OldClass;
                if (oc == null)
                {
                    throw Ops.TypeError("__class__ must be set to class");
                }
                __class__ = oc;
            }
            else if (name.Id == SymbolTable.DictId)
            {
                IAttributesDictionary dict = value as IAttributesDictionary;
                if (dict == null)
                {
                    throw Ops.TypeError("__dict__ must be set to a dictionary");
                }
                if (HasFinalizer() && !__class__.HasFinalizer)
                {
                    if (!dict.ContainsKey(SymbolTable.Unassign))
                    {
                        ClearFinalizer();
                    }
                }
                else if (dict.ContainsKey(SymbolTable.Unassign))
                {
                    AddFinalizer();
                }

                __dict__ = dict;
            }
            else if (name.Id == SymbolTable.UnassignId)
            {
                if (!HasFinalizer())
                {
                    // user is defining __del__ late bound for the 1st time
                    AddFinalizer();
                }

                __dict__[name] = value;
            }
            else
            {
                __dict__[name] = value;
            }
        }
Beispiel #2
0
        public OldClass(string name, Tuple bases, IDictionary <object, object> dict)
        {
            __bases__ = bases; //!!! validate, maybe even sort
            __name__  = name;

            __dict__ = (IAttributesDictionary)dict;

            if (!__dict__.ContainsKey(SymbolTable.Doc))
            {
                __dict__[SymbolTable.Doc] = null;
            }

            if (__dict__.ContainsKey(SymbolTable.Unassign))
            {
                hasFinalizer = true;
            }

            PromoteFunctionsToMethods();
        }
Beispiel #3
0
 protected virtual void RawDeleteSlot(SymbolId name)
 {
     if (dict.ContainsKey(name))
     {
         dict.Remove(name);
     }
     else
     {
         throw Ops.AttributeError("No attribute {0}.", name);
     }
 }
Beispiel #4
0
        public void BaseDelAttr(ICallerContext context, ISuperDynamicObject self, SymbolId name)
        {
            object slot;

            if (!TryLookupSlot(context, name, out slot))
            {
                IAttributesDictionary d = GetInitializedDict((ISuperDynamicObject)self); //!!! forces dict init
                if (d.ContainsKey(name))
                {
                    d.Remove(name);
                    return;
                }
                else
                {
                    throw Ops.AttributeError("no slot named {0} on {1}", SymbolTable.IdToString(name), this.__name__);
                }
            }
            Ops.DelDescriptor(slot, self);
        }
Beispiel #5
0
        public void DeleteAttr(ICallerContext context, SymbolId name)
        {
            if (name == SymbolTable.Dict)
            {
                throw Ops.TypeError(name.ToString() + " may not be deleted");
            }

            if (dict == null || !dict.ContainsKey(name))
            {
                // We check for SymbolTable.Module as the user code can modify it
                if (name == SymbolTable.Module)
                {
                    throw Ops.TypeError(name.ToString() + " may not be deleted");
                }

                throw Ops.AttributeError("no attribute {0}", name);
            }

            dict.Remove(name);
        }
Beispiel #6
0
        protected UserType(string name, Tuple bases, IDictionary <object, object> dict)
            : base(NewTypeMaker.GetNewType(name, bases, dict))
        {
            List <MethodInfo> ctors = new List <MethodInfo>();

            foreach (MethodInfo mi in type.GetMethods())
            {
                if (mi.Name == ReflectedType.MakeNewName)
                {
                    ctors.Add(mi);
                }
            }

            if (ctors.Count == 0)
            {
                throw new NotImplementedException("no MakeNew found");
            }

            ctor = BuiltinFunction.Make(name, ctors.ToArray(), ctors.ToArray(), FunctionType.Function);

            IAttributesDictionary fastDict = (IAttributesDictionary)dict;

            this.__name__   = name;
            this.__module__ = fastDict[SymbolTable.Module];   // should always be present...

            if (!fastDict.ContainsKey(SymbolTable.Doc))
            {
                fastDict[SymbolTable.Doc] = null;
            }

            InitializeUserType(bases, false);

            this.dict = CreateNamespaceDictionary(dict);

            AddProtocolWrappers();
        }
        public bool TryGetAttr(ICallerContext context, SymbolId name, out object value)
        {
            // We lazily load types on demand.  We try to avoid having to iterate
            // all the types in the assembly on each re-load.
            // If a type is in our dictionary we check a 2ndary dictionary which contains
            //      the number of assemblies we knew about when we reflected over it.  If
            //      this number is less than our current number of assemblies the type is good
            //      as it is to hand out.  If the # of assemblies has changed we need to check
            //      all of our loaded assemblies, and hand out all available types (in case there
            //      are collisions).
            // If we fail to find a name in our dictionary then we will only iterate all of the types
            // if we don't have the full assembly loaded.  This is controllved via our assemblyTypeLoadIndex.



            if (__dict__.TryGetValue(name, out value))
            {
                int level;
                if (!loadLevels.TryGetValue(name, out level) || level >= packageAssemblies.Count)
                {
                    return(true);
                }
            }

            if (assemblyTypeLoadIndex != packageAssemblies.Count)
            {
                value = null;
                string typeName = fullName + "." + SymbolTable.IdToString(name);

                bool fRemovedOld = false;

                // try and find the type name...
                for (int i = 0; i < packageAssemblies.Count; i++)
                {
                    string arityName = typeName + '`';
                    Type[] allTypes  = packageAssemblies[i].GetTypes();

                    for (int j = 0; j < allTypes.Length; j++)
                    {
                        Type t      = allTypes[j];
                        int  nested = t.FullName.IndexOf('+');
                        if (nested != -1)
                        {
                            continue;
                        }

                        object [] attrs = t.GetCustomAttributes(typeof(PythonTypeAttribute), false);
                        if ((attrs.Length > 0 && ((PythonTypeAttribute)attrs[0]).name == typeName) ||
                            t.FullName == typeName ||
                            String.Compare(t.FullName, 0, arityName, 0, arityName.Length) == 0)
                        {
                            if (!fRemovedOld)
                            {
                                // remove the old entry, we replace it w/ the values
                                // we get from the full iteration now.
                                if (__dict__.ContainsKey(name))
                                {
                                    __dict__.Remove(name);
                                }

                                fRemovedOld = true;
                            }
                            ComObject.AddType(t.GUID, t);

                            SaveType(t);
                            value = Ops.GetDynamicTypeFromType(t);
                        }
                    }
                    loadLevels[name] = packageAssemblies.Count;
                }

                if (value != null)
                {
                    return(true);
                }
            }

            // could have been a namespace, try the dictionary one last time...
            if (__dict__.TryGetValue(name, out value))
            {
                return(true);
            }

            value = null;
            return(false);
        }
        private void PopulateDefaultDictionary(IAttributesDictionary fastDict)
        {
            if (!fastDict.ContainsKey(SymbolTable.Doc)) {
                fastDict[SymbolTable.Doc] = null;
            }

            if (HasWeakRef && !fastDict.ContainsKey(SymbolTable.WeakRef)) {
                fastDict[SymbolTable.WeakRef] = new WeakRefWrapper(this);
            }

            if (!fastDict.ContainsKey(SymbolTable.Dict)) {
                fastDict[SymbolTable.Dict] = new DictWrapper(this);
            }
        }
        /// <summary>
        /// Set up the type
        /// </summary>
        /// <param name="resetType">Is an existing type being reset?</param>
        void InitializeUserType(Tuple newBases, IAttributesDictionary newDict, bool resetType)
        {
            newBases = EnsureBaseType(newBases);

            for (int i = 0; i < newBases.Count; i++) {
                for (int j = 0; j < newBases.Count; j++) {
                    if (i != j && newBases[i] == newBases[j]) {
                        throw Ops.TypeError("duplicate base class {0}", ((IPythonType)newBases[i]).Name);
                    }
                }
            }

            if (resetType) {
                // Ensure that we are not switching the CLI type
                Type newType = NewTypeMaker.GetNewType(__name__.ToString(), newBases, (IDictionary<object, object>)dict);
                if (type != newType)
                    throw Ops.TypeErrorForIncompatibleObjectLayout("__bases__ assignment", this, newType);

                foreach (object baseTypeObj in BaseClasses) {
                    if (baseTypeObj is OldClass) continue;
                    DynamicType baseType = baseTypeObj as DynamicType;
                    baseType.RemoveSubclass(this);
                }
            }

            this.bases = newBases;

            // if our dict, or any of our children, have a finalizer, then
            // we have a finalizer.
            bool hasFinalizer = newDict.ContainsKey(SymbolTable.Unassign);

            foreach (object baseTypeObj in BaseClasses) {
                if (baseTypeObj is OldClass) continue;
                DynamicType baseType = baseTypeObj as DynamicType;
                baseType.AddSubclass(this);

                UserType ut = baseType as UserType;
                if (ut != null && ut.HasFinalizer) {
                    hasFinalizer = true;
                }
            }

            HasFinalizer = hasFinalizer;

            if (!resetType)
                Initialize();
        }
Beispiel #10
0
        private int attrs; // actually OldClassAttributes - losing type safety for thread safety

        #endregion Fields

        #region Constructors

        public OldClass(string name, Tuple bases, IDictionary<object, object> dict)
        {
            __bases__ = bases; //!!! validate, maybe even sort
            __name__ = name;

            __dict__ = (IAttributesDictionary)dict;

            if (!__dict__.ContainsKey(SymbolTable.Doc)) {
                __dict__[SymbolTable.Doc] = null;
            }

            if (__dict__.ContainsKey(SymbolTable.Unassign)) {
                HasFinalizer = true;
            }
            if (__dict__.ContainsKey(SymbolTable.SetAttr)) {
                HasSetAttr = true;
            }
        }