示例#1
0
        public static object ReloadModule(CodeContext /*!*/ context, PythonModule /*!*/ module)
        {
            PythonContext pc = context.LanguageContext;

            // We created the module and it only contains Python code. If the user changes
            // __file__ we'll reload from that file.
            string fileName = module.GetFile() as string;

            // built-in module:
            if (fileName == null)
            {
                ReloadBuiltinModule(context, module);
                return(module);
            }

            string name = module.GetName();

            if (name != null)
            {
                PythonList path = null;
                // find the parent module and get it's __path__ property
                int dotIndex = name.LastIndexOf('.');
                if (dotIndex != -1)
                {
                    PythonModule parentModule;
                    path = GetParentPathAndModule(context, name.Substring(0, dotIndex), out parentModule);
                }

                object reloaded;
                if (TryLoadMetaPathModule(context, module.GetName() as string, path, out reloaded) && reloaded != null)
                {
                    return(module);
                }

                PythonList sysPath;
                if (context.LanguageContext.TryGetSystemPath(out sysPath))
                {
                    object ret = ImportFromPathHook(context, name, name, sysPath, null);
                    if (ret != null)
                    {
                        return(ret);
                    }
                }
            }

            if (!pc.DomainManager.Platform.FileExists(fileName))
            {
                throw PythonOps.SystemError("module source file not found");
            }

            var sourceUnit = pc.CreateFileUnit(fileName, pc.DefaultEncoding, SourceCodeKind.File);

            pc.GetScriptCode(sourceUnit, name, ModuleOptions.None, Compiler.CompilationMode.Lookup).Run(module.Scope);
            return(module);
        }