Inheritance: IDynamicMetaObjectProvider, IPythonMembersList
Beispiel #1
0
 public NameEnvironment(PythonModule globals, object locals)
 {
     this.globals = globals;
     if (locals == null) locals = globals.__dict__;
     this.locals = locals;
     this.builtin = TypeCache.Builtin;
 }
 PyModule_New(string name)
 {
     PythonModule module = new PythonModule();
     module.Get__dict__()["__name__"] = name;
     module.Get__dict__()["__doc__"] = "";
     return this.Store(module);
 }
        internal static object ImportBuiltin(PythonModule mod, string name)
        {
            mod.SystemState.TopPackage.Initialize(mod.SystemState);

            if (name.Equals("sys")) return mod.SystemState;
            if (name.Equals("clr")) {
                ((ICallerContext)mod).ContextFlags |= CallerContextAttributes.ShowCls;
                return ((ICallerContext)mod).SystemState.ClrModule;
            }
            Type ty;
            if (mod.SystemState.TopPackage.Builtins.TryGetValue(name, out ty)) {
                // run the type's .cctor before doing any custom reflection on the type.
                // This allows modules to lazily initialize DynamicType's to custom values
                // rather than having them get populated w/ the ReflectedType.  W/o this the
                // cctor runs after we've done a bunch of reflection over the type that doesn't
                // force the cctor to run.
                System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(ty.TypeHandle);

                if (typeof(CompiledModule).IsAssignableFrom(ty)) {
                    return InitializeModule(name, CompiledModule.Load(name, ty, mod.SystemState));
                } else {
                    return MakePythonModule(mod.SystemState, name, (ReflectedType)Ops.GetDynamicTypeFromType(ty));
                }
            }
            return null;
        }
 internal ModuleScope(PythonModule mod, IAttributesDictionary globals, object locals)
 {
     __module__ = mod;
     f_globals = globals;
     f_locals = locals;
     __builtin__ = TypeCache.Builtin;
 }
        Py_InitModule4(string name, IntPtr methodsPtr, string doc, IntPtr selfPtr, int apiver)
        {
            name = this.FixImportName(name);
            
            PythonDictionary methodTable = new PythonDictionary();
            PythonModule module = new PythonModule();
            this.AddModule(name, module);
            this.CreateModulesContaining(name);

            PythonDictionary __dict__ = module.Get__dict__();
            __dict__["__doc__"] = doc;
            __dict__["__name__"] = name;
            string __file__ = this.importFiles.Peek();
            __dict__["__file__"] = __file__;
            List __path__ = new List();
            if (__file__ != null)
            {
                __path__.append(Path.GetDirectoryName(__file__));
            }
            __dict__["__path__"] = __path__;
            __dict__["_dispatcher"] = new Dispatcher(this, methodTable, selfPtr);

            StringBuilder moduleCode = new StringBuilder();
            moduleCode.Append(CodeSnippets.USEFUL_IMPORTS);
            CallableBuilder.GenerateFunctions(moduleCode, methodsPtr, methodTable);
            this.ExecInModule(moduleCode.ToString(), module);
            
            return this.Store(module);
        }
Beispiel #6
0
 public ScriptScope/*!*/ CreateModule(string name, string filename, string docString) {
     var module = new PythonModule();
     _context.PublishModule(name, module);
     module.__init__(name, docString);
     module.__dict__["__file__"] = filename;
     
     return HostingHelpers.CreateScriptScope(_engine, module.Scope);
 }
Beispiel #7
0
        /// <summary>
        /// Creates a new ModuleContext for the specified module.
        /// </summary>
        public ModuleContext(PythonModule/*!*/ module, PythonContext/*!*/ creatingContext) {
            ContractUtils.RequiresNotNull(module, "module");
            ContractUtils.RequiresNotNull(creatingContext, "creatingContext");

            _globals = module.__dict__;
            _pyContext = creatingContext;
            _globalContext = new CodeContext(_globals, this);
            _module = module;
        }
Beispiel #8
0
        /// <summary>
        /// Creates a new ModuleContext which is backed by the specified dictionary.
        /// </summary>
        public ModuleContext(PythonDictionary/*!*/ globals, PythonContext/*!*/ creatingContext) {
            ContractUtils.RequiresNotNull(globals, "globals");
            ContractUtils.RequiresNotNull(creatingContext, "creatingContext");

            _globals = globals;
            _pyContext = creatingContext;
            _globalContext = new CodeContext(globals, this);
            _module = new PythonModule(globals);
            _module.Scope.SetExtension(_pyContext.ContextId, new PythonScopeExtension(_pyContext, _module, this));
        }
Beispiel #9
0
 public BuiltinModule(PythonModule module, ProjectState projectState, bool showClr)
     : base(new LazyDotNetDict(new object[] { module }, projectState, showClr))
 {
     object name;
     if (!module.Get__dict__().TryGetValue("__name__", out name) || !(name is string)) {
         _name = String.Empty;
     } else {
         _name = name as string;
     }
 }
 internal EngineModule(string moduleName, IDictionary<string, object> globalsDict, SystemState systemState)
 {
     Debug.Assert(moduleName != null);
     globals = globalsDict;
     if (globals is IAttributesDictionary)
         globalsAdapter = globals as IAttributesDictionary;
     else
         globalsAdapter = new StringDictionaryAdapterDict(globalsDict);
     PythonModule pythonModule = new PythonModule(moduleName, globalsAdapter, systemState);
     defaultModuleScope = new ModuleScope(pythonModule);
 }
Beispiel #11
0
        public static PythonModule/*!*/ __new__(CodeContext/*!*/ context, PythonType/*!*/ cls, params object[]/*!*/ args\u00F8) {
            PythonModule res;
            if (cls == TypeCache.Module) {
                res = new PythonModule();
            } else if (cls.IsSubclassOf(TypeCache.Module)) {
                res = (PythonModule)cls.CreateInstance(context);
            } else {
                throw PythonOps.TypeError("{0} is not a subtype of module", cls.Name);
            }

            return res;
        }
Beispiel #12
0
        private static object ImportTop(PythonModule mod, string name)
        {
            object ret;

            if (TryGetExistingModule(mod.SystemState, name, out ret))
            {
                PythonModule pm = ret as PythonModule;
                if (pm != null && pm.InnerModule != null)
                {
                    // if we imported before having the assembly
                    // loaded and then loaded the assembly we want
                    // to make the assembly available now.
                    pm.PackageImported = true;
                }

                return(ret);
            }

            ret = ImportBuiltin(mod, name);
            if (ret != null)
            {
                return(ret);
            }

            ret = ImportFromPath(mod.SystemState, name, name, mod.SystemState.path);
            if (ret != null)
            {
                return(ret);
            }

            ret = ImportReflected(mod, name);
            if (ret != null)
            {
                return(ret);
            }

            return(null);
        }
Beispiel #13
0
        public FunctionX(PythonModule globals, string name, CallTargetN target, string[] argNames, object[] defaults, FuncDefType flags)
            :
            base(globals, name, target, argNames, defaults)
        {
            this.flags = flags;
            nparams    = argNames.Length;

            if ((flags & FuncDefType.KeywordDict) != 0)
            {
                extraArgs++;
                nparams--;
                kwDictPos = nparams;
            }

            if ((flags & FuncDefType.ArgList) != 0)
            {
                extraArgs++;
                nparams--;
                argListPos = nparams;
            }

            Debug.Assert(defaults.Length <= nparams);
        }
        internal ReflectedPackage GetOrMakePackage(SystemState state, string fullName, string name)
        {
            object ret;

            if (__dict__.TryGetValue(SymbolTable.StringToId(name), out ret))
            {
                // if it's not a module we'll wipe it out below, eg def System(): pass then
                // import System will result in the namespace being visible.
                PythonModule     pm = ret as PythonModule;
                ReflectedPackage res;
                do
                {
                    res = pm.InnerModule as ReflectedPackage;
                    if (res != null)
                    {
                        return(res);
                    }

                    pm = pm.InnerModule as PythonModule;
                } while (pm != null);
            }

            return(MakePackage(state, fullName, name));
        }
Beispiel #15
0
 public PythonScopeExtension(PythonContext context, Scope scope) : base(scope)
 {
     _module     = new PythonModule(context, scope);
     _modContext = new ModuleContext(_module, context);
 }
Beispiel #16
0
 public static object ReloadModule(CodeContext/*!*/ context, PythonModule/*!*/ module) {
     return ReloadModule(context, module, null);
 }
Beispiel #17
0
 public static object ReloadModule(CodeContext /*!*/ context, PythonModule /*!*/ module)
 {
     return(ReloadModule(context, module, null));
 }
Beispiel #18
0
        /// <summary>
        /// Given the parent module name looks up the __path__ property.
        /// </summary>
        private static List GetParentPathAndModule(CodeContext /*!*/ context, string /*!*/ parentModuleName, out PythonModule parentModule)
        {
            List   path = null;
            object parentModuleObj;

            parentModule = null;

            // Try lookup parent module in the sys.modules
            if (context.LanguageContext.SystemStateModules.TryGetValue(parentModuleName, out parentModuleObj))
            {
                // see if it's a module
                parentModule = parentModuleObj as PythonModule;
                if (parentModule != null)
                {
                    object objPath;
                    // get its path as a List if it's there
                    if (parentModule.__dict__._storage.TryGetPath(out objPath))
                    {
                        path = objPath as List;
                    }
                }
            }
            return(path);
        }
Beispiel #19
0
        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();
        }
Beispiel #20
0
        /// <summary>
        /// Called by the __builtin__.__import__ functions (general importing) and ScriptEngine (for site.py)
        ///
        /// level indiciates whether to perform absolute or relative imports.
        ///     -1 indicates both should be performed
        ///     0 indicates only absolute imports should be performed
        ///     Positive numbers indicate the # of parent directories to search relative to the calling module
        /// </summary>
        public static object ImportModule(CodeContext /*!*/ context, object globals, string /*!*/ modName, bool bottom, int level)
        {
            if (modName.IndexOf(Path.DirectorySeparatorChar) != -1)
            {
                throw PythonOps.ImportError("Import by filename is not supported.", modName);
            }

            string package = null;
            object attribute;

            if (globals is PythonDictionary pyGlobals)
            {
                if (pyGlobals._storage.TryGetPackage(out attribute))
                {
                    package = attribute as string;
                    if (package == null && attribute != null)
                    {
                        throw PythonOps.ValueError("__package__ set to non-string");
                    }
                }
                else
                {
                    package = null;
                    if (level > 0)
                    {
                        // explicit relative import, calculate and store __package__
                        object pathAttr, nameAttr;
                        if (pyGlobals._storage.TryGetName(out nameAttr) && nameAttr is string)
                        {
                            if (pyGlobals._storage.TryGetPath(out pathAttr))
                            {
                                pyGlobals["__package__"] = nameAttr;
                            }
                            else
                            {
                                pyGlobals["__package__"] = ((string)nameAttr).rpartition(".")[0];
                            }
                        }
                    }
                }
            }

            object newmod = null;
            string firstName;
            int    firstDot = modName.IndexOf('.');

            if (firstDot == -1)
            {
                firstName = modName;
            }
            else
            {
                firstName = modName.Substring(0, firstDot);
            }
            string finalName = null;

            if (level != 0)
            {
                // try a relative import

                // if importing a.b.c, import "a" first and then import b.c from a
                string       name; // name of the module we are to import in relation to the current module
                PythonModule parentModule;
                List         path; // path to search
                if (TryGetNameAndPath(context, globals, firstName, level, package, out name, out path, out parentModule))
                {
                    finalName = name;
                    // import relative
                    if (!TryGetExistingOrMetaPathModule(context, name, path, out newmod))
                    {
                        newmod = ImportFromPath(context, firstName, name, path);
                        if (newmod == null)
                        {
                            // add an indirection entry saying this module does not exist
                            // see http://www.python.org/doc/essays/packages.html "Dummy Entries"
                            context.LanguageContext.SystemStateModules[name] = null;
                        }
                        else if (parentModule != null)
                        {
                            parentModule.__dict__[firstName] = newmod;
                        }
                    }
                    else if (firstDot == -1)
                    {
                        // if we imported before having the assembly
                        // loaded and then loaded the assembly we want
                        // to make the assembly available now.

                        if (newmod is NamespaceTracker)
                        {
                            context.ShowCls = true;
                        }
                    }
                }
            }

            if (level <= 0)
            {
                // try an absolute import
                if (newmod == null)
                {
                    object parentPkg;
                    if (!String.IsNullOrEmpty(package) && !context.LanguageContext.SystemStateModules.TryGetValue(package, out parentPkg))
                    {
                        PythonModule warnModule = new PythonModule();
                        warnModule.__dict__["__file__"] = package;
                        warnModule.__dict__["__name__"] = package;
                        ModuleContext modContext = new ModuleContext(warnModule.__dict__, context.LanguageContext);
                        PythonOps.Warn(
                            modContext.GlobalContext,
                            PythonExceptions.RuntimeWarning,
                            "Parent module '{0}' not found while handling absolute import",
                            package);
                    }
                    newmod    = ImportTopAbsolute(context, firstName);
                    finalName = firstName;
                    if (newmod == null)
                    {
                        return(null);
                    }
                }
            }

            // now import the a.b.c etc.  a needs to be included here
            // because the process of importing could have modified
            // sys.modules.
            string[] parts   = modName.Split('.');
            object   next    = newmod;
            string   curName = null;

            for (int i = 0; i < parts.Length; i++)
            {
                curName = i == 0 ? finalName : curName + "." + parts[i];
                object tmpNext;
                if (TryGetExistingModule(context, curName, out tmpNext))
                {
                    next = tmpNext;
                    if (i == 0)
                    {
                        // need to update newmod if we pulled it out of sys.modules
                        // just in case we're in bottom mode.
                        newmod = next;
                    }
                }
                else if (i != 0)
                {
                    // child module isn't loaded yet, import it.
                    next = ImportModuleFrom(context, next, parts, i);
                }
                else
                {
                    // top-level module doesn't exist in sys.modules, probably
                    // came from some weird meta path hook.
                    newmod = next;
                }
            }

            return(bottom ? next : newmod);
        }
Beispiel #21
0
        private static bool TryLoadModule(SystemState state, string fullName, string pathName, out PythonModule ret)
        {
            string binary = pathName + ".exe";
            string source = pathName + ".py";

            PythonModule pmod = null;

            if (ShouldLoadPreCompiledModule(binary, source)) {
                pmod = LoadPreCompiled(state, fullName, binary);
                pmod.Filename = binary;
            } else if (File.Exists(source)) {
                pmod = LoadFromSource(state, fullName, source);
                pmod.Filename = source;
            } else {
                ret = null;
                return false;
            }

            pmod.ModuleName = fullName;

            ret = InitializeModule(fullName, pmod);
            return true;
        }
Beispiel #22
0
 public EngineContext()
 {
     systemState = new SystemState();
     module      = new PythonModule("__main__", new Dict(), systemState);
 }
Beispiel #23
0
        private static bool TryGetNestedModule(PythonModule mod, string name, out object nested)
        {
            if (mod.TryGetAttr(mod, SymbolTable.StringToId(name), out nested)) {
                if (nested is PythonModule) return true;

                // This allows from System.Math import *
                if (nested is ReflectedType) return true;
            }
            return false;
        }
Beispiel #24
0
 /// <summary>
 /// Gateway into importing ... called from Ops.  Performs the initial import of
 /// a module and returns the module.
 /// </summary>
 internal static object Import(PythonModule mod, string fullName, List from)
 {
     object importFunction = FindImportFunction(mod);
     return Ops.CallWithContext(mod, importFunction, fullName, null, null, from);
 }
Beispiel #25
0
        /// <summary>
        /// Interrogates the importing module for __name__ and __path__, which determine
        /// whether the imported module (whose name is 'name') is being imported as nested
        /// module (__path__ is present) or as sibling.
        /// 
        /// For sibling import, the full name of the imported module is parent.sibling
        /// For nested import, the full name of the imported module is parent.module.nested
        /// where parent.module is the mod.__name__
        /// </summary>
        /// <param name="context"></param>
        /// <param name="mod">The module that triggered import</param>
        /// <param name="name">Name of the module to be imported</param>
        /// <param name="full">Output - full name of the module being imported</param>
        /// <param name="path">Path to use to search for "full"</param>
        /// <returns></returns>
        private static bool TryGetNameAndPath(ICallerContext context, PythonModule mod, string name, out string full, out List path)
        {
            // Unless we can find enough information to perform relative import,
            // we are going to import the module whose name we got
            full = name;
            path = null;

            // We need to get __name__ to find the name of the imported module.
            // If absent, fall back to absolute import
            object attribute;
            if (!mod.TryGetAttr(context, SymbolTable.Name, out attribute)) {
                return false;
            }

            // And the __name__ needs to be string
            string modName = attribute as string;
            if (modName == null) {
                return false;
            }

            // If the module has __path__ (and __path__ is list), nested module is being imported
            // otherwise, importing sibling to the importing module

            if (mod.TryGetAttr(context, SymbolTable.Path, out attribute) && (path = attribute as List) != null) {
                // found __path__, importing nested module. The actual name of the nested module
                // is the name of the mod plus the name of the imported module

                full = modName + "." + name;
                return true;
            }

            // importing sibling. The name of the imported module replaces
            // the last element in the importing module name
            int lastDot = modName.LastIndexOf('.');
            if (lastDot < 0) {
                // name doesn't include dot, only absolute import possible
                return false;
            }

            string parentName = modName.Substring(0, lastDot);
            object parentObject;
            // Try lookup parent module in the sys.modules
            if (!mod.SystemState.modules.TryGetValue(parentName, out parentObject)) {
                // parent module not found in sys.modules, fallback to absolute import
                return false;
            }

            PythonModule parentModule;
            if ((parentModule = parentObject as PythonModule) == null) {
                // the sys.module entry is not PythonModule, fallback to absolute import
                return false;
            }

            // The parentModule now needs to have __path__ - list
            if (parentModule.TryGetAttr(context, SymbolTable.Path, out attribute) && (path = attribute as List) != null) {
                // combine the module names
                full = parentName + "." + name;
                return true;
            }

            // not enough information - absolute import
            return false;
        }
Beispiel #26
0
        public InterpFunction(string name, string[] argNames, object[] defaults, Stmt body, PythonModule globals)
        {
            this.name     = name;
            this.argNames = argNames;
            this.defaults = defaults;
            this.body     = body;

            this.globals = globals;
        }
Beispiel #27
0
 public PythonScopeExtension(PythonContext context, PythonModule module, ModuleContext modContext)
     : base(module.Scope)
 {
     _module     = module;
     _modContext = modContext;
 }
Beispiel #28
0
        /// <summary>
        /// Given the parent module name looks up the __path__ property.
        /// </summary>
        private static List GetParentPathAndModule(CodeContext/*!*/ context, string/*!*/ parentModuleName, out PythonModule parentModule) {
            List path = null;
            object parentModuleObj;
            parentModule = null;

            // Try lookup parent module in the sys.modules
            if (PythonContext.GetContext(context).SystemStateModules.TryGetValue(parentModuleName, out parentModuleObj)) {
                // see if it's a module
                parentModule = parentModuleObj as PythonModule;
                if (parentModule != null) {
                    object objPath;
                    // get its path as a List if it's there
                    if (parentModule.__dict__._storage.TryGetPath(out objPath)) {
                        path = objPath as List;
                    }
                }
            }
            return path;
        }
Beispiel #29
0
        // Returning True: such module/file found, and TryLoadXXX should finish.
        private static bool TryLoadPackage(SystemState state, string fullName, string pathName, out PythonModule ret)
        {
            if (!Directory.Exists(pathName)) {
                ret = null;
                return false;
            }

            string binary = Path.Combine(pathName, "__init__.exe");
            string source = Path.Combine(pathName, "__init__.py");

            bool loadBinary = ShouldLoadPreCompiledModule(binary, source);
            bool loadSource = File.Exists(source);

            if (loadBinary || loadSource) {
                PythonModule pmod;
                if (loadBinary) {
                    pmod = LoadPreCompiled(state, fullName, binary);
                    pmod.Filename = binary;
                } else {
                    pmod = LoadFromSource(state, fullName, source);
                    pmod.Filename = source;
                }
                pmod.ModuleName = fullName;
                pmod.SetImportedAttr(DefaultContext.Default, SymbolTable.Path, Ops.MakeList(Path.GetFullPath(pathName)));
                ret = InitializeModule(fullName, pmod);
                return true;
            } else {
                ret = null;
                return false;
            }
        }
Beispiel #30
0
 public FunctionN(PythonModule globals, string name, CallTargetN target, string[] argNames, object[] defaults)
     : base(globals, name, argNames, defaults)
 {
     this.target = target;
 }
Beispiel #31
0
 public void ResetModule(PythonModule mod)
 {
     module = mod;
 }
Beispiel #32
0
 public static Frame MakeFrameForFunction(PythonModule context)
 {
     return(new Frame(context, ((IDictionary <object, object>)context.__dict__), new FieldIdDict()));
 }
Beispiel #33
0
        private static bool TryGetNestedModule(CodeContext/*!*/ context, PythonModule/*!*/ scope, string/*!*/ name, out object nested) {
            Assert.NotNull(context, scope, name);

            if (scope.__dict__.TryGetValue(name, out nested)) {
                if (nested is PythonModule) return true;

                // This allows from System.Math import *
                PythonType dt = nested as PythonType;
                if (dt != null && dt.IsSystemType) {
                    return true;
                }
            }
            return false;
        }
Beispiel #34
0
        private static PythonModule LoadModuleFromSource(SystemState state, string fullName, string fileName)
        {
            PythonModule mod = LoadFromSource(state, fullName, fileName);

            return(InitializeModule(fullName, mod));
        }
Beispiel #35
0
        /// <summary>
        /// Gateway into importing ... called from Ops.  Performs the initial import of
        /// a module and returns the module.
        /// </summary>
        internal static object Import(PythonModule mod, string fullName, List from)
        {
            object importFunction = FindImportFunction(mod);

            return(Ops.CallWithContext(mod, importFunction, fullName, null, null, from));
        }
Beispiel #36
0
        /// <summary>
        /// Interrogates the importing module for __name__ and __path__, which determine
        /// whether the imported module (whose name is 'name') is being imported as nested
        /// module (__path__ is present) or as sibling.
        ///
        /// For sibling import, the full name of the imported module is parent.sibling
        /// For nested import, the full name of the imported module is parent.module.nested
        /// where parent.module is the mod.__name__
        /// </summary>
        /// <param name="context"></param>
        /// <param name="globals">the globals dictionary</param>
        /// <param name="name">Name of the module to be imported</param>
        /// <param name="full">Output - full name of the module being imported</param>
        /// <param name="path">Path to use to search for "full"</param>
        /// <param name="level">the import level for relaive imports</param>
        /// <param name="parentMod">the parent module</param>
        /// <param name="package">the global __package__ value</param>
        /// <returns></returns>
        private static bool TryGetNameAndPath(CodeContext /*!*/ context, object globals, string name, int level, string package, out string full, out List path, out PythonModule parentMod)
        {
            Debug.Assert(level != 0);   // shouldn't be here for absolute imports

            // Unless we can find enough information to perform relative import,
            // we are going to import the module whose name we got
            full      = name;
            path      = null;
            parentMod = null;

            // We need to get __name__ to find the name of the imported module.
            // If absent, fall back to absolute import
            object attribute;

            if (!(globals is PythonDictionary pyGlobals) || !pyGlobals._storage.TryGetName(out attribute))
            {
                return(false);
            }

            // And the __name__ needs to be string
            if (!(attribute is string modName))
            {
                return(false);
            }

            string pn;

            if (package == null)
            {
                // If the module has __path__ (and __path__ is list), nested module is being imported
                // otherwise, importing sibling to the importing module
                if (pyGlobals._storage.TryGetPath(out attribute) && (path = attribute as List) != null)
                {
                    // found __path__, importing nested module. The actual name of the nested module
                    // is the name of the mod plus the name of the imported module
                    if (level == -1)
                    {
                        // absolute import of some module
                        full = modName + "." + name;
                        object parentModule;
                        if (context.LanguageContext.SystemStateModules.TryGetValue(modName, out parentModule))
                        {
                            parentMod = parentModule as PythonModule;
                        }
                    }
                    else if (String.IsNullOrEmpty(name))
                    {
                        // relative import of ancestor
                        full = (StringOps.rsplit(modName, ".", level - 1)[0] as string);
                    }
                    else
                    {
                        // relative import of some ancestors child
                        string parentName = (StringOps.rsplit(modName, ".", level - 1)[0] as string);
                        full = parentName + "." + name;
                        object parentModule;
                        if (context.LanguageContext.SystemStateModules.TryGetValue(parentName, out parentModule))
                        {
                            parentMod = parentModule as PythonModule;
                        }
                    }
                    return(true);
                }

                // importing sibling. The name of the imported module replaces
                // the last element in the importing module name
                int lastDot = modName.LastIndexOf('.');
                if (lastDot == -1)
                {
                    // name doesn't include dot, only absolute import possible
                    if (level > 0)
                    {
                        throw PythonOps.ValueError("Attempted relative import in non-package");
                    }

                    return(false);
                }

                // need to remove more than one name
                int tmpLevel = level;
                while (tmpLevel > 1 && lastDot != -1)
                {
                    lastDot = modName.LastIndexOf('.', lastDot - 1);
                    tmpLevel--;
                }

                if (lastDot == -1)
                {
                    pn = modName;
                }
                else
                {
                    pn = modName.Substring(0, lastDot);
                }
            }
            else
            {
                // __package__ doesn't include module name, so level is - 1.
                pn = GetParentPackageName(level - 1, package.Split('.'));
            }

            path = GetParentPathAndModule(context, pn, out parentMod);
            if (path != null)
            {
                if (String.IsNullOrEmpty(name))
                {
                    full = pn;
                }
                else
                {
                    full = pn + "." + name;
                }
                return(true);
            }

            if (level > 0)
            {
                throw PythonOps.SystemError("Parent module '{0}' not loaded, cannot perform relative import", pn);
            }
            // not enough information - absolute import
            return(false);
        }
Beispiel #37
0
 public MetaModule(PythonModule module, Expression self)
     : base(self, BindingRestrictions.Empty, module)
 {
 }
Beispiel #38
0
        internal static object ReloadModule(CodeContext /*!*/ context, PythonModule /*!*/ module, PythonFile file)
        {
            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.

            // built-in module:
            if (!(module.GetFile() is string fileName))
            {
                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 (context.LanguageContext.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, Compiler.CompilationMode.Lookup).Run(module.Scope);
            return(module);
        }
Beispiel #39
0
        /// <summary>
        /// Called by the __builtin__.__import__ functions (general importing) and ScriptEngine (for site.py)
        /// 
        /// level indiciates whether to perform absolute or relative imports.
        ///     -1 indicates both should be performed
        ///     0 indicates only absolute imports should be performed
        ///     Positive numbers indicate the # of parent directories to search relative to the calling module
        /// </summary>        
        public static object ImportModule(CodeContext/*!*/ context, object globals, string/*!*/ modName, bool bottom, int level) {
            if (modName.IndexOf(Path.DirectorySeparatorChar) != -1) {
                throw PythonOps.ImportError("Import by filename is not supported.", modName);
            }

            string package = null;
            object attribute;
            PythonDictionary pyGlobals = globals as PythonDictionary;
            if (pyGlobals != null) {
                if (pyGlobals._storage.TryGetPackage(out attribute)) {
                    package = attribute as string;
                    if (package == null && attribute != null) {
                        throw PythonOps.ValueError("__package__ set to non-string");
                    }
                } else {
                    package = null;
                    if (level > 0) {
                        // explicit relative import, calculate and store __package__
                        object pathAttr, nameAttr;
                        if (pyGlobals._storage.TryGetName(out nameAttr) && nameAttr is string) {
                            if (pyGlobals._storage.TryGetPath(out pathAttr)) {
                                pyGlobals["__package__"] = nameAttr;
                            } else {
                                pyGlobals["__package__"] = ((string)nameAttr).rpartition(".")[0];
                            }
                        }
                    }
                }
            }

            object newmod = null;
            string[] parts = modName.Split('.');
            string finalName = null;

            if (level != 0) {
                // try a relative import

                // if importing a.b.c, import "a" first and then import b.c from a
                string name;    // name of the module we are to import in relation to the current module
                PythonModule parentModule;
                List path;      // path to search
                if (TryGetNameAndPath(context, globals, parts[0], level, package, out name, out path, out parentModule)) {
                    finalName = name;
                    // import relative
                    if (!TryGetExistingOrMetaPathModule(context, name, path, out newmod)) {
                        newmod = ImportFromPath(context, parts[0], name, path);
                        if (newmod != null && parentModule != null) {
                            parentModule.__dict__[modName] = newmod;
                        }
                    } else if (parts.Length == 1) {
                        // if we imported before having the assembly
                        // loaded and then loaded the assembly we want
                        // to make the assembly available now.

                        if (newmod is NamespaceTracker) {
                            context.ShowCls = true;
                        }
                    }
                }
            }

            if (level <= 0) {
                // try an absolute import
                if (newmod == null) {
                    object parentPkg;
                    if (!String.IsNullOrEmpty(package) && !PythonContext.GetContext(context).SystemStateModules.TryGetValue(package, out parentPkg)) {
                        PythonModule warnModule = new PythonModule();
                        warnModule.__dict__["__file__"] = package;
                        warnModule.__dict__["__name__"] = package;
                        ModuleContext modContext = new ModuleContext(warnModule.__dict__, context.LanguageContext);
                        PythonOps.Warn(
                            modContext.GlobalContext,
                            PythonExceptions.RuntimeWarning,
                            "Parent module '{0}' not found while handling absolute import",
                            package);
                    }

                    newmod = ImportTopAbsolute(context, parts[0]);
                    finalName = parts[0];
                    if (newmod == null) {
                        return null;
                    }
                }
            }

            // now import the a.b.c etc.  a needs to be included here
            // because the process of importing could have modified
            // sys.modules.
            object next = newmod;
            string curName = null;
            for (int i = 0; i < parts.Length; i++) {
                curName = i == 0 ? finalName : curName + "." + parts[i];
                object tmpNext;
                if (TryGetExistingModule(context, curName, out tmpNext)) {
                    next = tmpNext;
                    if (i == 0) {
                        // need to update newmod if we pulled it out of sys.modules
                        // just in case we're in bottom mode.
                        newmod = next;
                    }
                } else if (i != 0) {
                    // child module isn't loaded yet, import it.
                    next = ImportModuleFrom(context, next, parts[i]);
                } else {
                    // top-level module doesn't exist in sys.modules, probably
                    // came from some weird meta path hook.
                    newmod = next;
                }
            }

            return bottom ? next : newmod;
        }
Beispiel #40
0
        /// <summary>
        /// Gateway into importing ... called from Ops.  This is called after
        /// importing the module and is used to return individual items from
        /// the module.  The outer modules dictionary is then updated with the
        /// result.
        /// </summary>
        public static object ImportFrom(CodeContext /*!*/ context, object from, string name)
        {
            PythonModule     scope = from as PythonModule;
            PythonType       pt;
            NamespaceTracker nt;

            if (scope != null)
            {
                object ret;
                if (scope.GetType() == typeof(PythonModule))
                {
                    if (scope.__dict__.TryGetValue(name, out ret))
                    {
                        return(ret);
                    }
                }
                else
                {
                    // subclass of module, it could have overridden __getattr__ or __getattribute__
                    if (PythonOps.TryGetBoundAttr(context, scope, name, out ret))
                    {
                        return(ret);
                    }
                }

                object path;
                List   listPath;
                string stringPath;
                if (scope.__dict__._storage.TryGetPath(out path))
                {
                    if ((listPath = path as List) != null)
                    {
                        return(ImportNestedModule(context, scope, new[] { name }, 0, listPath));
                    }
                    else if ((stringPath = path as string) != null)
                    {
                        return(ImportNestedModule(context, scope, new[] { name }, 0, List.FromArrayNoCopy(stringPath)));
                    }
                }
            }
            else if ((pt = from as PythonType) != null)
            {
                PythonTypeSlot pts;
                object         res;
                if (pt.TryResolveSlot(context, name, out pts) &&
                    pts.TryGetValue(context, null, pt, out res))
                {
                    return(res);
                }
            }
            else if ((nt = from as NamespaceTracker) != null)
            {
                object res = NamespaceTrackerOps.GetCustomMember(context, nt, name);
                if (res != OperationFailed.Value)
                {
                    return(res);
                }
            }
            else
            {
                // This is too lax, for example it allows from module.class import member
                object ret;
                if (PythonOps.TryGetBoundAttr(context, from, name, out ret))
                {
                    return(ret);
                }
            }

            throw PythonOps.ImportError("Cannot import name {0}", name);
        }
Beispiel #41
0
        public override ICollection <MemberDoc> GetMembers(object value)
        {
            List <MemberDoc> res = new List <MemberDoc>();

            PythonModule mod = value as PythonModule;

            if (mod != null)
            {
                foreach (var kvp in mod.__dict__)
                {
                    AddMember(res, kvp, false);
                }
                return(res);
            }

            NamespaceTracker ns = value as NamespaceTracker;

            if (ns != null)
            {
                foreach (var v in ns)
                {
                    AddMember(
                        res,
                        new KeyValuePair <object, object>(
                            v.Key,
                            Importer.MemberTrackerToPython(_context.SharedClsContext, v.Value)
                            ),
                        false
                        );
                }
            }
            else
            {
                OldInstance oi = value as OldInstance;
                if (oi != null)
                {
                    foreach (var member in oi.Dictionary)
                    {
                        AddMember(res, member, false);
                    }

                    AddOldClassMembers(res, oi._class);
                }
                else
                {
                    PythonType pt = value as PythonType;
                    if (pt != null)
                    {
                        foreach (PythonType type in pt.ResolutionOrder)
                        {
                            foreach (var member in type.GetMemberDictionary(_context.SharedContext))
                            {
                                AddMember(res, member, true);
                            }
                        }
                    }
                    else
                    {
                        OldClass oc = value as OldClass;
                        if (oc != null)
                        {
                            AddOldClassMembers(res, oc);
                        }
                        else
                        {
                            pt = DynamicHelpers.GetPythonType(value);
                            foreach (var member in pt.GetMemberDictionary(_context.SharedContext))
                            {
                                AddMember(res, member, true);
                            }
                        }
                    }

                    IPythonObject ipo = value as IPythonObject;
                    if (ipo != null && ipo.Dict != null)
                    {
                        foreach (var member in ipo.Dict)
                        {
                            AddMember(res, member, false);
                        }
                    }
                }
            }

            return(res.ToArray());
        }
Beispiel #42
0
 public DebugProxy(PythonModule module)
 {
     _module = module;
 }
 public PythonScopeExtension(PythonContext context, Scope scope) : base(scope) {
     _module = new PythonModule(context, scope);
     _modContext = new ModuleContext(_module, context);
 }
Beispiel #44
0
        /// <summary>
        /// Interrogates the importing module for __name__ and __path__, which determine
        /// whether the imported module (whose name is 'name') is being imported as nested
        /// module (__path__ is present) or as sibling.
        /// 
        /// For sibling import, the full name of the imported module is parent.sibling
        /// For nested import, the full name of the imported module is parent.module.nested
        /// where parent.module is the mod.__name__
        /// </summary>
        /// <param name="context"></param>
        /// <param name="globals">the globals dictionary</param>
        /// <param name="name">Name of the module to be imported</param>
        /// <param name="full">Output - full name of the module being imported</param>
        /// <param name="path">Path to use to search for "full"</param>
        /// <param name="level">the import level for relaive imports</param>
        /// <param name="parentMod">the parent module</param>
        /// <param name="package">the global __package__ value</param>
        /// <returns></returns>
        private static bool TryGetNameAndPath(CodeContext/*!*/ context, object globals, string name, int level, string package, out string full, out List path, out PythonModule parentMod) {
            Debug.Assert(level != 0);   // shouldn't be here for absolute imports

            // Unless we can find enough information to perform relative import,
            // we are going to import the module whose name we got
            full = name;
            path = null;
            parentMod = null;

            // We need to get __name__ to find the name of the imported module.
            // If absent, fall back to absolute import
            object attribute;

            PythonDictionary pyGlobals = globals as PythonDictionary;
            if (pyGlobals == null || !pyGlobals._storage.TryGetName(out attribute)) {
                return false;
            }

            // And the __name__ needs to be string
            string modName = attribute as string;
            if (modName == null) {
                return false;
            }

            string pn;
            if (package == null) {
                // If the module has __path__ (and __path__ is list), nested module is being imported
                // otherwise, importing sibling to the importing module
                if (pyGlobals._storage.TryGetPath(out attribute) && (path = attribute as List) != null) {
                    // found __path__, importing nested module. The actual name of the nested module
                    // is the name of the mod plus the name of the imported module
                    if (level == -1) {
                        // absolute import of some module
                        full = modName + "." + name;
                        object parentModule;
                        if (PythonContext.GetContext(context).SystemStateModules.TryGetValue(modName, out parentModule)) {
                            parentMod = parentModule as PythonModule;
                        }
                    } else if (String.IsNullOrEmpty(name)) {
                        // relative import of ancestor
                        full = (StringOps.rsplit(modName, ".", level - 1)[0] as string);
                    } else {
                        // relative import of some ancestors child
                        string parentName = (StringOps.rsplit(modName, ".", level - 1)[0] as string);
                        full = parentName + "." + name;
                        object parentModule;
                        if (PythonContext.GetContext(context).SystemStateModules.TryGetValue(parentName, out parentModule)) {
                            parentMod = parentModule as PythonModule;
                        }
                    }
                    return true;
                }

                // importing sibling. The name of the imported module replaces
                // the last element in the importing module name
                string[] names = modName.Split('.');
                if (names.Length == 1) {
                    // name doesn't include dot, only absolute import possible
                    if (level > 0) {
                        throw PythonOps.ValueError("Attempted relative import in non-package");
                    }

                    return false;
                }

                pn = GetParentPackageName(level, names);
            } else {
                // __package__ doesn't include module name, so level is - 1.
                pn = GetParentPackageName(level - 1, package.Split('.'));
            }

            path = GetParentPathAndModule(context, pn, out parentMod);
            if (path != null) {
                if (String.IsNullOrEmpty(name)) {
                    full = pn;
                } else {
                    full = pn + "." + name;
                }
                return true;
            }

            if (level > 0) {
                throw PythonOps.SystemError("Parent module '{0}' not loaded, cannot perform relative import", pn);
            }
            // not enough information - absolute import
            return false;
        }
 public PythonScopeExtension(PythonContext context, PythonModule module, ModuleContext modContext)
     : base(module.Scope) {
     _module = module;
     _modContext = modContext;
 }
Beispiel #46
0
        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;
        }
Beispiel #47
0
 public MetaModule(PythonModule module, Expression self)
     : base(self, BindingRestrictions.Empty, module) {
 }
Beispiel #48
0
        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();
        }
Beispiel #49
0
 public DebugProxy(PythonModule module) {
     _module = module;
 }
Beispiel #50
0
        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());
        }
Beispiel #51
0
 internal static CodeContext/*!*/ CreateDefaultContext(PythonContext/*!*/ context) {
     PythonModule module = new PythonModule(new Scope());
     module.Scope.SetExtension(context.ContextId, module);
     return new CodeContext(module.Scope, context);
 }