Example #1
0
        public static void warn_explicit(CodeContext context, object message, PythonType category, string filename, int lineno, string module = null, PythonDictionary registry = null, object module_globals = null)
        {
            PythonContext    pContext = context.LanguageContext;
            PythonDictionary fields   = (PythonDictionary)pContext.GetModuleState(_keyFields);
            object           warnings = pContext.GetWarningsModule();

            PythonExceptions.BaseException msg;
            string text; // message text

            if (string.IsNullOrEmpty(module))
            {
                module = (filename == null || filename == "") ? "<unknown>" : filename;
                if (module.EndsWith(".py"))
                {
                    module = module.Substring(0, module.Length - 3);
                }
            }
            if (registry == null)
            {
                registry = new PythonDictionary();
            }
            if (PythonOps.IsInstance(message, PythonExceptions.Warning))
            {
                msg      = (PythonExceptions.BaseException)message;
                text     = msg.ToString();
                category = DynamicHelpers.GetPythonType(msg);
            }
            else
            {
                text = message.ToString();
                msg  = PythonExceptions.CreatePythonThrowable(category, message.ToString());
            }

            PythonTuple key = PythonTuple.MakeTuple(text, category, lineno);

            if (registry.ContainsKey(key))
            {
                return;
            }

            string      action      = Converter.ConvertToString(fields[_keyDefaultAction]);
            PythonTuple last_filter = null;
            bool        loop_break  = false;

            List filters = (List)fields[_keyFilters];

            if (warnings != null)
            {
                filters = PythonOps.GetBoundAttr(context, warnings, "filters") as List;
                if (filters == null)
                {
                    throw PythonOps.ValueError("_warnings.filters must be a list");
                }
            }

            foreach (PythonTuple filter in filters)
            {
                last_filter = filter;
                action      = (string)filter._data[0];
                PythonRegex.RE_Pattern fMsg = (PythonRegex.RE_Pattern)filter._data[1];
                PythonType             fCat = (PythonType)filter._data[2];
                PythonRegex.RE_Pattern fMod = (PythonRegex.RE_Pattern)filter._data[3];
                int fLno;
                if (filter._data[4] is int)
                {
                    fLno = (int)filter._data[4];
                }
                else
                {
                    fLno = (Extensible <int>)filter._data[4];
                }

                if ((fMsg == null || fMsg.match(text) != null) &&
                    category.IsSubclassOf(fCat) &&
                    (fMod == null || fMod.match(module) != null) &&
                    (fLno == 0 || fLno == lineno))
                {
                    loop_break = true;
                    break;
                }
            }
            if (!loop_break)
            {
                action = Converter.ConvertToString(fields[_keyDefaultAction]);
            }

            switch (action)
            {
            case "ignore":
                registry.Add(key, 1);
                return;

            case "error":
                throw msg.GetClrException();

            case "once":
                registry.Add(key, 1);
                PythonTuple      onceKey  = PythonTuple.MakeTuple(text, category);
                PythonDictionary once_reg = (PythonDictionary)fields[_keyOnceRegistry];
                if (once_reg.ContainsKey(onceKey))
                {
                    return;
                }
                once_reg.Add(key, 1);
                break;

            case "always":
                break;

            case "module":
                registry.Add(key, 1);
                PythonTuple altKey = PythonTuple.MakeTuple(text, category, 0);
                if (registry.ContainsKey(altKey))
                {
                    return;
                }
                registry.Add(altKey, 1);
                break;

            case "default":
                registry.Add(key, 1);
                break;

            default:
                throw PythonOps.RuntimeError("Unrecognized action ({0}) in warnings.filters:\n {1}", action, last_filter);
            }

            if (warnings != null)
            {
                object show_fxn = PythonOps.GetBoundAttr(context, warnings, "showwarning");
                if (show_fxn != null)
                {
                    PythonCalls.Call(
                        context,
                        show_fxn,
                        msg, category, filename, lineno, null, null);
                }
                else
                {
                    showwarning(context, msg, category, filename, lineno, null, null);
                }
            }
            else
            {
                showwarning(context, msg, category, filename, lineno, null, null);
            }
        }
Example #2
0
        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;
        }