Esempio n. 1
0
        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);
        }