コード例 #1
0
        /// <summary>
        /// Determines if the type associated with the first MetaObject is a subclass of the
        /// type associated with the second MetaObject.
        /// </summary>
        internal static bool IsSubclassOf(DynamicMetaObject /*!*/ xType, DynamicMetaObject /*!*/ yType)
        {
            PythonType x = MetaPythonObject.GetPythonType(xType);
            PythonType y = MetaPythonObject.GetPythonType(yType);

            return(x.IsSubclassOf(y));
        }
コード例 #2
0
ファイル: _warnings.cs プロジェクト: rudimk/dlr-dotnet
        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);
        }
コード例 #3
0
ファイル: array.cs プロジェクト: zgpglee/ironpython3
        private static array ArrayReconstructor(CodeContext context, [NotNull]PythonType cls, [NotNull]string typecode, int mformat_code, [NotNull]Bytes items) {
            if (typecode.Length != 1)
                throw PythonOps.TypeError("expected character, got {0}", PythonTypeOps.GetName(typecode));
            if (!typecodes.Contains(typecode))
                throw PythonOps.ValueError("bad typecode (must be b, B, u, h, H, i, I, l, L, q, Q, f or d)");

            var actualTypeCode = MachineFormatToTypeCode(mformat_code, out bool isBigEndian, out string? encoding);

            var arrayType = DynamicHelpers.GetPythonTypeFromType(typeof(array));

            if (!cls.IsSubclassOf(arrayType)) {
                throw PythonOps.TypeError($"{cls} is not a subtype of array.array");
            }

            array res;
            if (cls == arrayType) {
                res = new array(actualTypeCode);
            } else if (cls.CreateInstance(context, actualTypeCode) is array arr) {
                res = arr;
            } else {
                throw PythonOps.TypeError($"{cls} is not a subtype of array.array");
            }

            if (encoding == null) {
                res.frombytes(items);
                if (isBigEndian) res.byteswap();
            } else {
                res.fromunicode(context, StringOps.RawDecode(context, items, encoding, null));
            }
            return res;
コード例 #4
0
 private static bool ShouldInvokeInit(PythonType cls, PythonType newObjectType, int argCnt)
 {
     // don't run __init__ if it's not a subclass of ourselves,
     // or if this is the user doing type(x), or if it's a standard
     // .NET type which doesn't have an __init__ method (this is a perf optimization)
     return((!cls.IsSystemType || cls.IsPythonType) &&
            newObjectType.IsSubclassOf(cls) &&
            (cls != TypeCache.PythonType || argCnt > 1));
 }
コード例 #5
0
ファイル: _warnings.cs プロジェクト: profMagija/ironpython3
        public static void warn(CodeContext context, object message, PythonType category = null, int stacklevel = 1)
        {
            PythonContext pContext = context.LanguageContext;
            List          argv     = pContext.GetSystemStateValue("argv") as List;

            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");
            }

            TraceBackFrame   caller = null;
            PythonDictionary globals;
            int lineno;

            if (context.LanguageContext.PythonOptions.Frames)
            {
                try {
                    caller = SysModule._getframeImpl(context, stacklevel - 1);
                } catch (ValueErrorException) { }
            }
            if (caller == null)
            {
                globals = Builtin.globals(context) as PythonDictionary;
                lineno  = 1;
            }
            else
            {
                globals = caller.f_globals;
                lineno  = (int)caller.f_lineno;
            }

            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);
        }
コード例 #6
0
ファイル: _warnings.cs プロジェクト: profMagija/ironpython3
        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);
            }
        }