コード例 #1
0
        private static PythonModule DoGenerateModule(SystemState state, CompilerContext context, GlobalSuite gs, string moduleName, string sourceFileName, string outSuffix)
        {
            string fullPath;
            string outDir;
            string fileName;

            if (sourceFileName == "<stdin>")
            {
                fullPath = Environment.CurrentDirectory;
                outDir   = Environment.CurrentDirectory;
                fileName = "__stdin__";
            }
            else
            {
                fullPath = Path.GetFullPath(sourceFileName);
                outDir   = Options.BinariesDirectory == null?Path.GetDirectoryName(fullPath) : Options.BinariesDirectory;

                fileName = Path.GetFileNameWithoutExtension(sourceFileName);
            }

            AssemblyGen ag = new AssemblyGen(moduleName + outSuffix, outDir, fileName + outSuffix + ".exe", true);

            ag.SetPythonSourceFile(fullPath);


            TypeGen tg = GenerateModuleType(moduleName, ag);
            CodeGen cg = GenerateModuleInitialize(context, gs, tg);

            CodeGen main = GenerateModuleEntryPoint(tg, cg, moduleName, null);

            ag.SetEntryPoint(main.MethodInfo, PEFileKinds.ConsoleApplication);

            Type     ret  = tg.FinishType();
            Assembly assm = ag.DumpAndLoad();

            ret = assm.GetType(moduleName);

            CustomFieldIdDict dict = (CustomFieldIdDict)ret.GetConstructor(Type.EmptyTypes).Invoke(new object[0]);
            InitializeModule  init = (InitializeModule)Delegate.CreateDelegate(
                typeof(InitializeModule), dict, "Initialize");

            PythonModule pmod = new PythonModule(moduleName, dict, state, init);

            pmod.InitializeBuiltins();
            return(pmod);
        }
コード例 #2
0
        public static int ExecuteCompiled(CustomFieldIdDict compiled, InitializeModule init, string fullName, string[] references)
        {
            PythonEngine engine = new PythonEngine();

            engine.AddToPath(Environment.CurrentDirectory);

            engine.LoadAssembly(compiled.GetType().Assembly);
            PythonModule module     = new PythonModule(fullName, compiled, engine.engineContext.SystemState);
            string       executable = compiled.GetType().Assembly.Location;

            module.InitializeBuiltins();
            engine.Sys.prefix            = System.IO.Path.GetDirectoryName(executable);
            engine.Sys.executable        = executable;
            engine.Sys.exec_prefix       = engine.Sys.prefix;
            engine.Sys.modules[fullName] = module;

            if (references != null)
            {
                for (int i = 0; i < references.Length; i++)
                {
                    engine.engineContext.SystemState.ClrModule.AddReference(references[i]);
                }
            }

            // first arg is EXE
            List args = new List();

            string[] fullArgs = Environment.GetCommandLineArgs();
            args.Add(Path.GetFullPath(fullArgs[0]));
            for (int i = 1; i < fullArgs.Length; i++)
            {
                args.Add(fullArgs[i]);
            }
            engine.Sys.argv = args;

            try {
                init();
            } catch (PythonSystemExit x) {
                return(x.GetExitCode(engine.engineContext));
            } catch (Exception e) {
                engine.DumpException(e);
                return(-1);
            }
            return(0);
        }