コード例 #1
0
ファイル: Importer.cs プロジェクト: weimingtom/IronPythonMod
        private 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 |= CallerContextFlags.ShowCls;
                return(((ICallerContext)mod).SystemState.ClrModule);
            }
            Type ty;

            if (mod.SystemState.TopPackage.Builtins.TryGetValue(name, out ty))
            {
                if (typeof(CustomFieldIdDict).IsAssignableFrom(ty))
                {
                    CustomFieldIdDict dict = (CustomFieldIdDict)ty.GetConstructor(Type.EmptyTypes).Invoke(new object[0]);
                    InitializeModule  init = (InitializeModule)Delegate.CreateDelegate(
                        typeof(InitializeModule), dict, "Initialize");
                    return(InitializeModule(name, new PythonModule(name, dict, mod.SystemState, init, CallerContextFlags.None)));
                }
                else
                {
                    return(MakePythonModule(mod, name, (ReflectedType)Ops.GetDynamicTypeFromType(ty)));
                }
            }
            return(null);
        }
コード例 #2
0
ファイル: Importer.cs プロジェクト: weimingtom/IronPythonMod
        internal static PythonModule ReloadBuiltin(PythonModule module)
        {
            Type ty;

            if (module.SystemState.TopPackage.Builtins.TryGetValue(module.ModuleName, out ty))
            {
                if (typeof(CustomFieldIdDict).IsAssignableFrom(ty))
                {
                    CustomFieldIdDict dict = (CustomFieldIdDict)ty.GetConstructor(Type.EmptyTypes).Invoke(new object[0]);
                    //@todo share logic to copy old values in when not already there from reload
                    module.__dict__ = dict;
                    module.Initialize();
                    return(module);
                }
                else
                {
                    ReflectedType type = (ReflectedType)Ops.GetDynamicTypeFromType(ty);
                    type.Initialize();
                    foreach (string attrName in type.GetAttrNames(module))
                    {
                        SymbolId id = SymbolTable.StringToId(attrName);
                        module.__dict__[id] = type.GetAttr(module, null, id);
                    }
                    module.Initialize();
                    return(module);
                }
            }
            throw new NotImplementedException();
        }
コード例 #3
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);
        }
コード例 #4
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);
        }
コード例 #5
0
 public static int ExecuteCompiled(CustomFieldIdDict compiled, InitializeModule init, string fullName)
 {
     return(ExecuteCompiled(compiled, init, fullName, null));
 }