Exemplo n.º 1
0
            public object load_module(CodeContext /*!*/ context, string fullname)
            {
                bool             ispackage;
                string           modpath;
                PythonModule     mod;
                PythonContext    pythonContext = context.LanguageContext;
                PythonDictionary dict;
                ScriptCode       script = null;

                byte[] code = GetModuleCode(context, fullname, out ispackage, out modpath);
                if (code == null)
                {
                    return(null);
                }

                mod = pythonContext.CompileModule(modpath, fullname,
                                                  new SourceUnit(pythonContext, new MemoryStreamContentProvider(pythonContext, code, modpath), modpath, SourceCodeKind.File),
                                                  ModuleOptions.None, out script);

                dict = mod.__dict__;
                // we do these here because we don't want CompileModule to initialize the module until we've set
                // up some additional stuff
                dict.Add("__name__", fullname);
                dict.Add("__loader__", this);
                dict.Add("__package__", null);

                if (ispackage)
                {
                    // add __path__ to the module *before* the code
                    // gets executed
                    string subname  = GetSubName(fullname);
                    string fullpath = string.Format("{0}{1}{2}{3}",
                                                    _archive,
                                                    Path.DirectorySeparatorChar,
                                                    _prefix.Length > 0 ? _prefix : string.Empty,
                                                    subname);

                    PythonList pkgpath = PythonOps.MakeList(fullpath);
                    dict.Add("__path__", pkgpath);
                }

                script.Run(mod.Scope);
                return(mod);
            }
Exemplo n.º 2
0
 private static Scope/*!*/ LoadPackageDirectory(PythonContext/*!*/ context, string moduleName, string path) {
     string initPath = Path.Combine(path, "__init__.py");
     
     SourceUnit sourceUnit =  context.CreateFileUnit(initPath, context.DefaultEncoding);
     return context.CompileModule(initPath, moduleName, sourceUnit, ModuleOptions.Initialize).Scope;
 }
Exemplo n.º 3
0
 private static Scope/*!*/ LoadPythonSource(PythonContext/*!*/ context, string/*!*/ name, PythonFile/*!*/ file, string/*!*/ fileName) {
     SourceUnit sourceUnit = context.CreateSnippet(file.read(), String.IsNullOrEmpty(fileName) ? null : fileName, SourceCodeKind.File);
     return context.CompileModule(fileName, name, sourceUnit, ModuleOptions.Initialize).Scope;
 }