Esempio n. 1
0
        public static PythonType POINTER(CodeContext /*!*/ context, [NotNull] string name)
        {
            PythonType       res  = MakePointer(context, name, new PythonDictionary());
            PythonContext    pc   = context.LanguageContext;
            PythonDictionary dict = (PythonDictionary)pc.GetModuleState(_pointerTypeCacheKey);

            lock (dict) {
                dict[Builtin.id(res)] = res;
            }

            return(res);
        }
Esempio n. 2
0
        /// <summary>
        /// Returns the dialects from the code context.
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        private static DialectRegistry GetDialects(CodeContext /*!*/ context)
        {
            PythonContext ctx = PythonContext.GetContext(context);

            if (!ctx.HasModuleState(_dialectRegistryKey))
            {
                ctx.SetModuleState(_dialectRegistryKey,
                                   new DialectRegistry());
            }

            return((DialectRegistry)ctx.GetModuleState(_dialectRegistryKey));
        }
Esempio n. 3
0
        /// <summary>
        /// Returns a new type which represents a pointer given the existing type.
        /// </summary>
        public static PythonType POINTER(CodeContext /*!*/ context, PythonType type)
        {
            PythonContext    pc   = context.LanguageContext;
            PythonDictionary dict = (PythonDictionary)pc.GetModuleState(_pointerTypeCacheKey);

            lock (dict) {
                if (!dict.TryGetValue(type, out object res))
                {
                    string name;
                    if (type == null)
                    {
                        name = "c_void_p";
                    }
                    else
                    {
                        name = "LP_" + type.Name;
                    }

                    dict[type] = res = MakePointer(context, name, PythonOps.MakeDictFromItems(new object[] { type, "_type_" }));
                }

                return(res as PythonType);
            }
        }
Esempio n. 4
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);
            }
        }