public static void warn(CodeContext context, object message, [DefaultParameterValue(null)] PythonType category, [DefaultParameterValue(1)] int stacklevel) { PythonContext pContext = PythonContext.GetContext(context); List argv = pContext.GetSystemStateValue("argv") as List; PythonDictionary dict = pContext.GetSystemStateValue("__dict__") as PythonDictionary; if (PythonOps.IsInstance(message, PythonExceptions.Warning)) { category = DynamicHelpers.GetPythonType(message); } if (category == null) { category = PythonExceptions.UserWarning; } if (!category.IsSubclassOf(PythonExceptions.Warning)) { throw PythonOps.ValueError("category is not a subclass of Warning"); } // default behavior without sys._getframe PythonDictionary globals = Builtin.globals(context) as PythonDictionary; int lineno = 1; string module; string filename; if (globals != null && globals.ContainsKey("__name__")) { module = (string)globals.get("__name__"); } else { module = "<string>"; } filename = globals.get("__file__") as string; if (filename == null || filename == "") { if (module == "__main__") { if (argv != null && argv.Count > 0) { filename = argv[0] as string; } else { // interpreter lacks sys.argv filename = "__main__"; } } if (filename == null || filename == "") { filename = module; } } PythonDictionary registry = (PythonDictionary)globals.setdefault("__warningregistry__", new PythonDictionary()); warn_explicit(context, message, category, filename, lineno, module, registry, globals); }
internal static void showwarning(CodeContext context, object message, PythonType category, string filename, int lineno, object file = null, string line = null) { string text = formatwarning(message, category, filename, lineno, line); try { if (file == null) { PythonContext pContext = context.LanguageContext; PythonFile stderr = pContext.GetSystemStateValue("stderr") as PythonFile; if (stderr != null) { stderr.write(text); } else { // use CLR stderr if python's is unavailable Console.Error.Write(text); } } else { if (file is PythonFile) { ((PythonFile)file).write(text); } else if (file is TextWriter) { ((TextWriter)file).Write(text); } // unrecognized file type - warning is lost } } catch (IOException) { // invalid file - warning is lost } }
private void InitializeEnvironmentVariables() { #if !SILVERLIGHT if (!Options.IgnoreEnvironmentVariables) { string warnings = Environment.GetEnvironmentVariable("IRONPYTHONWARNINGS"); object o = PythonContext.GetSystemStateValue("warnoptions"); if (o == null) { o = new List(); PythonContext.SetSystemStateValue("warnoptions", o); } List warnoptions = o as List; if (warnoptions != null && !string.IsNullOrEmpty(warnings)) { string[] warns = warnings.Split(','); foreach (string warn in warns) { warnoptions.Add(warn); } } } #endif }
protected override void Initialize() { Debug.Assert(Language != null); base.Initialize(); Console.Output = new OutputWriter(PythonContext, false); Console.ErrorOutput = new OutputWriter(PythonContext, true); // TODO: must precede path initialization! (??? - test test_importpkg.py) int pathIndex = PythonContext.PythonOptions.SearchPaths.Count; Language.DomainManager.LoadAssembly(typeof(string).Assembly); Language.DomainManager.LoadAssembly(typeof(System.Diagnostics.Debug).Assembly); InitializePath(ref pathIndex); InitializeEnvironmentVariables(); InitializeModules(); InitializeExtensionDLLs(); // ensure the warnings module loads var warnOptions = PythonContext.GetSystemStateValue("warnoptions") as PythonList; if (warnOptions?.Count > 0) { PythonContext.GetWarningsModule(); } ImportSite(); // Equivalent to -i command line option // Check if IRONPYTHONINSPECT was set before execution string inspectLine = Environment.GetEnvironmentVariable("IRONPYTHONINSPECT"); if (inspectLine != null) { Options.Introspection = true; } // If running in console mode (including with -c), the current working directory should be // the first entry in sys.path. If running a script file, however, the CWD should not be added; // instead, the script's containg folder should be added. string fullPath = "."; // this is a valid path resolving to current working dir. Pinky-swear. if (Options.Command == null && Options.FileName != null) { if (Options.FileName == "-") { Options.FileName = "<stdin>"; } else { if (Directory.Exists(Options.FileName)) { Options.FileName = Path.Combine(Options.FileName, "__main__.py"); } if (!File.Exists(Options.FileName)) { Console.WriteLine( String.Format( System.Globalization.CultureInfo.InvariantCulture, "File {0} does not exist.", Options.FileName), Style.Error); Environment.Exit(1); } fullPath = Path.GetDirectoryName( Language.DomainManager.Platform.GetFullPath(Options.FileName) ); } } PythonContext.InsertIntoPath(0, fullPath); PythonContext.MainThread = Thread.CurrentThread; }