Exemple #1
0
        public static object Reload(PythonModule module)
        {
            if (module.Filename == null)
            {
                return(Importer.ReloadBuiltin(module));
            }

            CompilerContext cc     = new CompilerContext(module.Filename);
            Parser          parser = Parser.FromFile(module.SystemState, cc);
            Stmt            s      = parser.ParseFileInput();
            PythonModule    pmod   = OutputGenerator.GenerateModule(module.SystemState, cc, s, module.ModuleName, "__" + System.Threading.Interlocked.Increment(ref reloadCounter));

            foreach (KeyValuePair <object, object> attr in module.__dict__)
            {
                if (pmod.__dict__.ContainsObjectKey(attr.Key))
                {
                    continue;
                }
                pmod.__dict__.AddObjectKey(attr.Key, attr.Value);
            }

            module.UpdateForReload(pmod);

            return(module);
        }
Exemple #2
0
        private static PythonModule LoadFromSource(SystemState state, string fullName, string fileName)
        {
            CompilerContext context = new CompilerContext(fileName);
            Parser          parser  = Parser.FromFile(state, context);
            Stmt            s       = parser.ParseFileInput();

            PythonModule pmod = OutputGenerator.GenerateModule(state, context, s, fullName);

            pmod.Filename   = fileName;
            pmod.ModuleName = fullName;

            return(pmod);
        }
Exemple #3
0
        private int RunFileInNewModule(string fileName, string moduleName, bool skipLine, out bool exitRaised)
        {
            CompilerContext context = new CompilerContext(fileName);
            Parser          p       = Parser.FromFile(engineContext.SystemState, context, skipLine, false);
            Stmt            s       = p.ParseFileInput();

            PythonModule mod = OutputGenerator.GenerateModule(engineContext.SystemState, context, s, moduleName);

            Sys.modules[mod.ModuleName] = mod;
            engineContext.ResetModule(mod);
            mod.SetAttr(engineContext, SymbolTable.File, fileName);
            exitRaised = false;

            try {
                mod.Initialize();
            } catch (PythonSystemExit e) {
                exitRaised = true;
                return(e.GetExitCode(engineContext));
            }

            return(0);
        }
Exemple #4
0
        public int RunFile(string fileName)
        {
            Parser p = Parser.FromFile(engineContext.SystemState, context.CopyWithNewSourceFile(fileName));
            Stmt   s = p.ParseFileInput();

            string moduleName = "tmp" + counter++;

            PythonModule mod = OutputGenerator.GenerateModule(engineContext.SystemState, p.CompilerContext, s, moduleName);

            foreach (KeyValuePair <SymbolId, object> name in engineContext.Module.__dict__.SymbolAttributes)
            {
                mod.SetAttr(engineContext, name.Key, name.Value);
            }
            mod.SetAttr(engineContext, SymbolTable.File, fileName);

            try {
                mod.Initialize();
            } catch (PythonSystemExit e) {
                return(e.GetExitCode(engineContext));
            }

            return(0);
        }