private static object ImportNestedModule(CodeContext /*!*/ context, PythonModule /*!*/ module, string[] parts, int current, List /*!*/ path) { object ret; string name = parts[current]; string fullName = CreateFullName(module.GetName() as string, name); if (TryGetExistingOrMetaPathModule(context, fullName, path, out ret)) { module.__dict__[name] = ret; return(ret); } if (TryGetNestedModule(context, module, parts, current, out ret)) { return(ret); } ImportFromPath(context, name, fullName, path); object importedModule; if (context.LanguageContext.SystemStateModules.TryGetValue(fullName, out importedModule)) { module.__dict__[name] = importedModule; return(importedModule); } throw PythonOps.ImportError("cannot import {0} from {1}", name, module.GetName()); }
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); }
private static object ImportNestedModule(CodeContext /*!*/ context, PythonModule /*!*/ module, ArraySegment <string> parts, PythonList /*!*/ path, string scopeModuleName) { Debug.Assert(parts.Array is not null); Debug.Assert(parts.Count > 0); object ret; int current = parts.Offset + parts.Count - 1; string name = parts.Array[current]; string fullName = CreateFullName(scopeModuleName, parts); if (TryGetExistingOrMetaPathModule(context, fullName, path, out ret)) { module.__dict__[name] = ret; return(ret); } if (TryGetNestedModule(context, module, parts.Array, current, out ret)) { return(ret); } ImportFromPath(context, name, fullName, path); object importedModule; if (context.LanguageContext.SystemStateModules.TryGetValue(fullName, out importedModule)) { module.__dict__[name] = importedModule; return(importedModule); } throw PythonOps.ImportError("cannot import {0} from {1}", name, module.GetName()); }
private static void ReloadBuiltinModule(CodeContext /*!*/ context, PythonModule /*!*/ module) { Assert.NotNull(module); Debug.Assert(module.GetName() is string, "Module is reloadable only if its name is a non-null string"); Type type; string name = (string)module.GetName(); PythonContext pc = context.LanguageContext; if (!pc.BuiltinModules.TryGetValue(name, out type)) { throw PythonOps.ImportError("no module named {0}", module.GetName()); } // should be a built-in module which we can reload. Debug.Assert(((PythonDictionary)module.__dict__)._storage is ModuleDictionaryStorage); ((ModuleDictionaryStorage)module.__dict__._storage).Reload(); }
private static object ImportNestedModule(CodeContext/*!*/ context, PythonModule/*!*/ module, string name, List/*!*/ path) { object ret; string fullName = CreateFullName(module.GetName() as string, name); if (TryGetExistingOrMetaPathModule(context, fullName, path, out ret)) { module.__dict__[name] = ret; return ret; } if (TryGetNestedModule(context, module, name, out ret)) { return ret; } ImportFromPath(context, name, fullName, path); object importedModule; if (PythonContext.GetContext(context).SystemStateModules.TryGetValue(fullName, out importedModule)) { module.__dict__[name] = importedModule; return importedModule; } throw PythonOps.ImportError("cannot import {0} from {1}", name, module.GetName()); }
private static void ReloadBuiltinModule(CodeContext/*!*/ context, PythonModule/*!*/ module) { Assert.NotNull(module); Debug.Assert(module.GetName() is string, "Module is reloadable only if its name is a non-null string"); Type type; string name = (string)module.GetName(); PythonContext pc = PythonContext.GetContext(context); if (!pc.BuiltinModules.TryGetValue(name, out type)) { throw PythonOps.ImportError("no module named {0}", module.GetName()); } // should be a built-in module which we can reload. Debug.Assert(((PythonDictionary)module.__dict__)._storage is ModuleDictionaryStorage); ((ModuleDictionaryStorage)module.__dict__._storage).Reload(); }
internal static object ReloadModule(CodeContext/*!*/ context, PythonModule/*!*/ module, PythonFile file) { PythonContext pc = PythonContext.GetContext(context); // 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() as string; if (name != null) { List 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; } List sysPath; if (PythonContext.GetContext(context).TryGetSystemPath(out sysPath)) { object ret = ImportFromPathHook(context, name, name, sysPath, null); if (ret != null) { return ret; } } } SourceUnit sourceUnit; if (file != null) { sourceUnit = pc.CreateSourceUnit(new PythonFileStreamContentProvider(file), fileName, file.Encoding, SourceCodeKind.File); } else { if (!pc.DomainManager.Platform.FileExists(fileName)) { throw PythonOps.SystemError("module source file not found"); } sourceUnit = pc.CreateFileUnit(fileName, pc.DefaultEncoding, SourceCodeKind.File); } pc.GetScriptCode(sourceUnit, name, ModuleOptions.None).Run(module.Scope); return module; }
private static void ReloadBuiltinModule(CodeContext/*!*/ context, PythonModule/*!*/ module) { Assert.NotNull(module); Debug.Assert(module.GetName() is string, "Module is reloadable only if its name is a non-null string"); Type type; string name = (string)module.GetName(); PythonContext pc = PythonContext.GetContext(context); if (!pc.Builtins.TryGetValue(name, out type)) { throw new NotImplementedException(); } // should be a built-in module which we can reload. Debug.Assert(module.Scope.Dict is PythonDictionary); Debug.Assert(((PythonDictionary)module.Scope.Dict)._storage is ModuleDictionaryStorage); ((ModuleDictionaryStorage)((PythonDictionary)module.Scope.Dict)._storage).Reload(); }
private static object ImportNestedModule(CodeContext/*!*/ context, PythonModule/*!*/ module, string name, List/*!*/ path) { object ret; //NewPythonModule module = PythonContext.GetContext(context).EnsurePythonModule(scope); string fullName = CreateFullName(module.GetName() as string, name); if (TryGetExistingOrMetaPathModule(context, fullName, path, out ret)) { module.Scope.SetVariable(SymbolTable.StringToId(name), ret); return ret; } if (TryGetNestedModule(context, module, name, out ret)) { return ret; } ImportFromPath(context, name, fullName, path); object importedModule; if (PythonContext.GetContext(context).SystemStateModules.TryGetValue(fullName, out importedModule)) { module.Scope.SetVariable(SymbolTable.StringToId(name), importedModule); return importedModule; } throw PythonOps.ImportError("cannot import {0} from {1}", name, module.GetName()); }