Пример #1
0
        private int RunFileWorker(string /*!*/ fileName)
        {
            ScriptCode    compiledCode;
            ModuleOptions modOpt = ModuleOptions.Optimized | ModuleOptions.ModuleBuiltins;

            if (Options.SkipFirstSourceLine)
            {
                modOpt |= ModuleOptions.SkipFirstLine;
            }
            PythonModule module = PythonContext.CompileModule(
                fileName,
                "__main__",
                PythonContext.CreateFileUnit(String.IsNullOrEmpty(fileName) ? null : fileName, PythonContext.DefaultEncoding),
                modOpt,
                out compiledCode);

            PythonContext.PublishModule("__main__", module);
            Scope = module.Scope;

            try {
                compiledCode.Run(Scope);
            } catch (SystemExitException pythonSystemExit) {
                // disable introspection when exited:
                Options.Introspection = false;

                return(GetEffectiveExitCode(pythonSystemExit));
            }

            return(0);
        }
Пример #2
0
        private static PythonModule /*!*/ 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));
        }
Пример #3
0
        private int RunFileWorker(string /*!*/ fileName)
        {
            try {
                // There is no PEP for this case, only http://bugs.python.org/issue1739468
                object importer;
                if (Importer.TryImportMainFromZip(DefaultContext.Default, fileName, out importer))
                {
                    return(0);
                }
                if (importer != null && importer.GetType() != typeof(PythonImport.NullImporter))
                {
                    Console.WriteLine(String.Format("can't find '__main__' module in '{0}'", fileName), Style.Error);
                    return(0);
                }
            } catch (SystemExitException pythonSystemExit) {
                // disable introspection when exited:
                Options.Introspection = false;
                return(GetEffectiveExitCode(pythonSystemExit));
            }

            // classic file
            ScriptCode    compiledCode;
            ModuleOptions modOpt = ModuleOptions.Optimized | ModuleOptions.ModuleBuiltins;

            if (Options.SkipFirstSourceLine)
            {
                modOpt |= ModuleOptions.SkipFirstLine;
            }
            PythonModule module = PythonContext.CompileModule(
                fileName,
                "__main__",
                PythonContext.CreateFileUnit(String.IsNullOrEmpty(fileName) ? null : fileName, PythonContext.DefaultEncoding),
                modOpt,
                out compiledCode);

            PythonContext.PublishModule("__main__", module);
            Scope = module.Scope;

            try {
                compiledCode.Run(Scope);
            } catch (SystemExitException pythonSystemExit) {
                // disable introspection when exited:
                Options.Introspection = false;

                return(GetEffectiveExitCode(pythonSystemExit));
            }

            return(0);
        }
Пример #4
0
        public static object load_source(CodeContext /*!*/ context, string /*!*/ name, string /*!*/ pathname)
        {
            if (name == null)
            {
                throw PythonOps.TypeError("load_source() argument 1 must be string, not None");
            }
            if (pathname == null)
            {
                throw PythonOps.TypeError("load_source() argument 2 must be string, not None");
            }

            // TODO: is this supposed to open PythonFile with Python-specific behavior?
            // we may need to insert additional layer to SourceUnit content provider if so
            PythonContext pc = PythonContext.GetContext(context);

            if (!pc.DomainManager.Platform.FileExists(pathname))
            {
                throw PythonOps.IOError("Couldn't find file: {0}", pathname);
            }

            SourceUnit sourceUnit = pc.CreateFileUnit(pathname, pc.DefaultEncoding, SourceCodeKind.File);

            return(pc.CompileModule(pathname, name, sourceUnit, ModuleOptions.Initialize));
        }