Ejemplo n.º 1
0
        internal ReflectedPackage MakePackage(SystemState state, string fullName, string name)
        {
            ReflectedPackage rp = new ReflectedPackage();
            object           mod;
            PythonModule     pmod;

            if (!Importer.TryGetExistingModule(state, name, out mod))
            {
                // no collisions (yet), create a new module for the package.
                pmod = new PythonModule(name, new Dict(), state);
                pmod.PackageImported = true;
            }
            else
            {
                // there's already a module by this name.  We'll just
                // set the InnerModule but not make it visible until
                // the user does an import (and we set PackageImported).
                pmod = mod as PythonModule;
                System.Diagnostics.Debug.Assert(pmod != null);
            }

            rp.fullName      = fullName;
            pmod.InnerModule = rp;
            __dict__[SymbolTable.StringToId(name)] = pmod;
            return(rp);
        }
Ejemplo n.º 2
0
        public bool LoadAssembly(SystemState state, Assembly assem, bool lazy)
        {
            bool loaded;

            if (loadedAssemblies.TryGetValue(assem, out loaded) && lazy)
            {
                return(false);
            }

            if (!loaded)
            {
                foreach (PythonModuleAttribute pma in assem.GetCustomAttributes(typeof(PythonModuleAttribute), false))
                {
                    builtins.Add(pma.name, pma.type);
                }
            }

            foreach (Type type in assem.GetExportedTypes())
            {
                //  Skip nested types. They get loaded during parent type initalization.
                //
                if (type.IsNested)
                {
                    continue;
                }

                // save all the namespaces, types will be lazily initialized
                // on demand in GetAttr
                ReflectedPackage pkg = GetOrMakeTopPackage(state, assem, type.Namespace);

                if (!loaded)
                {
                    if (String.IsNullOrEmpty(type.Namespace))
                    {
                        // but we need to save top-level types immediately
                        ComObject.AddType(type.GUID, type);

                        pkg.SaveType(type);
                    }
                }
                else
                {
                    // doing a non-lazy reload...  force all types to get loaded
                    pkg.LoadAllTypes();
                }
            }

            // Assembly was loaded
            loadedAssemblies[assem] = true;

            return(true);
        }
Ejemplo n.º 3
0
        private ReflectedPackage GetOrMakeTopPackage(SystemState state, Assembly assm, string ns)
        {
            ReflectedPackage ret = this;

            if (ns != null)
            {
                string[] pieces = ns.Split('.');
                for (int i = 0; i < pieces.Length; i++)
                {
                    if (!ret.packageAssemblies.Contains(assm))
                    {
                        ret.packageAssemblies.Add(assm);
                    }
                    ret = ret.GetOrMakePackage(state, String.Join(".", pieces, 0, i + 1), pieces[i]);
                }
            }

            if (!ret.packageAssemblies.Contains(assm))
            {
                ret.packageAssemblies.Add(assm);
            }
            return(ret);
        }
        private ReflectedPackage MakePackage(SystemState state, string fullName, string name, bool isolated)
        {
            ReflectedPackage rp = new ReflectedPackage();
            object mod;
            PythonModule pmod;

            if (isolated || !Importer.TryGetExistingModule(state, name, out mod)) {
                // no collisions (yet), create a new module for the package.
                pmod = new PythonModule(name, new Dict(), state);
                pmod.PackageImported = true;
            } else {
                // there's already a module by this name.  We'll just
                // set the InnerModule but not make it visible until
                // the user does an import (and we set PackageImported).
                pmod = mod as PythonModule;
                System.Diagnostics.Debug.Assert(pmod != null);
            }

            rp.fullName = fullName;
            pmod.InnerModule = rp;
            __dict__[SymbolTable.StringToId(name)] = pmod;
            return rp;
        }