Пример #1
0
        public static PythonModule/*!*/ __new__(CodeContext/*!*/ context, PythonType/*!*/ cls, params object[]/*!*/ args\u00F8) {
            PythonModule res;
            if (cls == TypeCache.Module) {
                res = new PythonModule();
            } else if (cls.IsSubclassOf(TypeCache.Module)) {
                res = (PythonModule)cls.CreateInstance(context);
            } else {
                throw PythonOps.TypeError("{0} is not a subtype of module", cls.Name);
            }

            return res;
        }
Пример #2
0
        public static Scope/*!*/ __new__(CodeContext/*!*/ context, PythonType/*!*/ cls, params object[]/*!*/ args\u00F8) {
            Scope res;
            if (cls == TypeCache.Module) {
                res = new Scope(PythonDictionary.MakeSymbolDictionary());
            } else if (cls.IsSubclassOf(TypeCache.Module)) {
                res = (Scope)cls.CreateInstance(context);
            } else {
                throw PythonOps.TypeError("{0} is not a subtype of module", cls.Name);
            }

            PythonContext.GetContext(context).CreateModule(null, res, null, ModuleOptions.None);
            res.Clear();
            return res;
        }
Пример #3
0
        private PythonType CheckGetArgs(CodeContext context, object instance, PythonType owner) {
            if (owner == null) {
                if (instance == null) throw PythonOps.TypeError("__get__(None, None) is invalid");
                owner = DynamicHelpers.GetPythonType(instance);
            } else {
                if (!owner.IsSubclassOf(DynamicHelpers.GetPythonTypeFromType(_func.DeclaringType))) {
                    throw PythonOps.TypeError("descriptor {0} for type {1} doesn't apply to type {2}",
                        PythonOps.Repr(context, _func.Name),
                        PythonOps.Repr(context, DynamicHelpers.GetPythonTypeFromType(_func.DeclaringType).Name),
                        PythonOps.Repr(context, owner.Name));
                }
            }
            if (instance != null)
                BuiltinMethodDescriptor.CheckSelfWorker(context, instance, _func);

            return owner;
        }
Пример #4
0
        public static void warn_explicit(CodeContext context, object message, PythonType category, string filename, int lineno, [DefaultParameterValue(null)]string module, [DefaultParameterValue(null)]PythonDictionary registry, [DefaultParameterValue(null)]object module_globals) {
            PythonContext pContext = PythonContext.GetContext(context);
            PythonDictionary fields = (PythonDictionary)pContext.GetModuleState(_keyFields);
            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;
            foreach (PythonTuple filter in (List)fields[_keyFilters]) {
                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);
            }

            object warnings = pContext.GetWarningsModule();
            if (warnings != null) {
                PythonCalls.Call(
                    context,
                    PythonOps.GetBoundAttr(context, warnings, "showwarning"),
                    msg, category, filename, lineno, null, null);
            } else {
                showwarning(context, msg, category, filename, lineno, null, null);
            }
        }
Пример #5
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);
 }
Пример #6
0
        private bool SubclassImpl(PythonType sub) {
            if (UnderlyingSystemType.IsInterface) {
                // interfaces aren't in bases, and therefore IsSubclassOf doesn't do this check.
                if (UnderlyingSystemType.IsAssignableFrom(sub.UnderlyingSystemType)) {
                    return true;
                }
            }

            return sub.IsSubclassOf(this);
        }