Exemplo n.º 1
0
        public static object __new__(CodeContext /*!*/ context, [NotNull] PythonType cls, string name, PythonTuple bases, PythonDictionary /*!*/ dict)
        {
            if (dict == null)
            {
                throw PythonOps.TypeError("dict must be a dictionary");
            }
            else if (cls != TypeCache.OldClass)
            {
                throw PythonOps.TypeError("{0} is not a subtype of classobj", cls.Name);
            }

            if (!dict.ContainsKey("__module__"))
            {
                object moduleValue;
                if (context.TryGetGlobalVariable("__name__", out moduleValue))
                {
                    dict["__module__"] = moduleValue;
                }
            }

            foreach (object o in bases)
            {
                if (o is PythonType)
                {
                    return(PythonOps.MakeClass(context, name, bases._data, String.Empty, dict));
                }
            }

            return(new OldClass(name, bases, dict, String.Empty));
        }
Exemplo n.º 2
0
        public Argument(PythonBoss pyBoss, long address, PythonDictionary spec, Process process, int depth, Arguments parent, string namePrefix)
        {
            Address = address;
            this.process = process;
            _pyBoss = pyBoss;
            _parent = parent;
            NamePrefix = namePrefix;

            // Parse the spec for this argument
            // stackspec: [{"name": "socket",
            //		      "size": 4,
            //		      "type": None,
            //		      "fuzz": NOFUZZ,
            //            "type_args": None},]

            Fuzz = (bool)spec.get("fuzz");
            Name = (string)spec.get("name");
            _argumentType = (object)spec.get("type");
            if ( spec.ContainsKey("type_args") )
            {
                _typeArgs = spec.get("type_args");
            }

            // Validate required fields
            if (Name == null)
                throw new Exception("ERROR: Argument specification must include 'name' attribute. Failed when parsing name prefix '" + namePrefix + "'.");
            else if (Fuzz == null)
                throw new Exception("ERROR: Argument specification must include 'fuzz' attribute. Failed when parsing type '" + namePrefix + Name + "'.");
            else if (spec.get("size") == null)
                throw new Exception("ERROR: Argument specification must include 'size' attribute. Failed when parsing type '" + namePrefix + Name + "'.");

            if (spec.get("size") is string)
            {
                object sizeArgument = null;
                if (parent.TryGetMemberSearchUp((string)spec.get("size"), out sizeArgument))
                    Size = ((Argument)sizeArgument).ToInt();
                else
                    throw new Exception("ERROR: Unable to load size for type '" + Name + "' from parent member named '" + (string)spec.get("size") + "'. Please make sure this field exists in the parent.");
            }
            else if (spec.get("size") is int)
            {
                Size = (int)spec.get("size");
            }
            else
            {
                throw new Exception("ERROR: Unable to load size for type '" + Name + "'. The size must be of type 'int' or type 'string'. Size is type: '" + spec.get("size").ToString() + "'" );
            }

            // Read the data
            try
            {
                Data = MemoryFunctions.ReadMemory(process.ProcessDotNet, address, (uint)Size);
            }
            catch (Exception e)
            {
                Data = null;
            }

            PointerTarget = null;
        }
Exemplo n.º 3
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);
        }
Exemplo n.º 4
0
        public RegisterArgument(PythonBoss pyBoss, Context context, PythonDictionary spec, Process process, Arguments parent, string namePrefix)
        {
            NamePrefix   = namePrefix;
            this.process = process;

            // Parse the spec for this argument
            // regspec: [{"name": "socket",
            //		      "register": "rcx",
            //		      "type": None,
            //		      "fuzz": NOFUZZ},]

            Register      = ((string)spec.get("register")).ToLower();
            Fuzz          = (bool)spec.get("fuzz");
            Name          = (string)spec.get("name");
            _argumentType = (object)spec.get("type");
            if (spec.ContainsKey("type_args"))
            {
                _typeArgs = spec.get("type_args");
            }

            // Validate required fields
            if (Name == null)
            {
                throw new Exception("ERROR: Argument specification must include 'name' attribute. Failed when parsing name prefix '" + namePrefix + "'.");
            }
            else if (Fuzz == null)
            {
                throw new Exception("ERROR: Argument specification must include 'fuzz' attribute. Failed when parsing type '" + namePrefix + Name + "'.");
            }

            this.process = process;
            _pyBoss      = pyBoss;
            _parent      = parent;

            // Read the data
            var tmpData = context.GetMember(Register);

            if (tmpData is UInt32)
            {
                Data = BitConverter.GetBytes((UInt32)tmpData);
            }
            else if (tmpData is UInt64)
            {
                Data = BitConverter.GetBytes((UInt64)tmpData);
            }
            else
            {
                throw new Exception("Register argument type definition problem. The register must be of type 'int' or 'long'. The is likely an engine bug. Argument name: " + Name + ". The unsupported register type is: " + tmpData.ToString());
            }
            Size = Data.Length;

            PointerTarget = null;
        }
Exemplo n.º 5
0
        private void SetDict(CodeContext /*!*/ context, object value)
        {
            PythonDictionary dict = value as PythonDictionary;

            if (dict == null)
            {
                throw PythonOps.TypeError("__dict__ must be set to a dictionary");
            }
            if (HasFinalizer() && !_class.HasFinalizer)
            {
                if (!dict.ContainsKey("__del__"))
                {
                    ClearFinalizer();
                }
            }
            else if (dict.ContainsKey("__del__"))
            {
                AddFinalizer(context);
            }

            _dict = dict;
        }
Exemplo n.º 6
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);
            }
        }
Exemplo n.º 7
0
        public Argument(PythonBoss pyBoss, long address, PythonDictionary spec, Process process, int depth, Arguments parent, string namePrefix)
        {
            Address      = address;
            this.process = process;
            _pyBoss      = pyBoss;
            _parent      = parent;
            NamePrefix   = namePrefix;


            // Parse the spec for this argument
            // stackspec: [{"name": "socket",
            //		      "size": 4,
            //		      "type": None,
            //		      "fuzz": NOFUZZ,
            //            "type_args": None},]

            Fuzz          = (bool)spec.get("fuzz");
            Name          = (string)spec.get("name");
            _argumentType = (object)spec.get("type");
            if (spec.ContainsKey("type_args"))
            {
                _typeArgs = spec.get("type_args");
            }


            // Validate required fields
            if (Name == null)
            {
                throw new Exception("ERROR: Argument specification must include 'name' attribute. Failed when parsing name prefix '" + namePrefix + "'.");
            }
            else if (Fuzz == null)
            {
                throw new Exception("ERROR: Argument specification must include 'fuzz' attribute. Failed when parsing type '" + namePrefix + Name + "'.");
            }
            else if (spec.get("size") == null)
            {
                throw new Exception("ERROR: Argument specification must include 'size' attribute. Failed when parsing type '" + namePrefix + Name + "'.");
            }


            if (spec.get("size") is string)
            {
                object sizeArgument = null;
                if (parent.TryGetMemberSearchUp((string)spec.get("size"), out sizeArgument))
                {
                    Size = ((Argument)sizeArgument).ToInt();
                }
                else
                {
                    throw new Exception("ERROR: Unable to load size for type '" + Name + "' from parent member named '" + (string)spec.get("size") + "'. Please make sure this field exists in the parent.");
                }
            }
            else if (spec.get("size") is int)
            {
                Size = (int)spec.get("size");
            }
            else
            {
                throw new Exception("ERROR: Unable to load size for type '" + Name + "'. The size must be of type 'int' or type 'string'. Size is type: '" + spec.get("size").ToString() + "'");
            }

            // Read the data
            try
            {
                Data = MemoryFunctions.ReadMemory(process.ProcessDotNet, address, (uint)Size);
            }
            catch (Exception e)
            {
                Data = null;
            }


            PointerTarget = null;
        }
Exemplo n.º 8
0
        public static object __new__(CodeContext/*!*/ context, [NotNull]PythonType cls, string name, PythonTuple bases, PythonDictionary/*!*/ dict) {
            if (dict == null) {
                throw PythonOps.TypeError("dict must be a dictionary");
            } else if (cls != TypeCache.OldClass) {
                throw PythonOps.TypeError("{0} is not a subtype of classobj", cls.Name);
            }

            if (!dict.ContainsKey("__module__")) {
                object moduleValue;
                if (context.TryGetGlobalVariable("__name__", out moduleValue)) {
                    dict["__module__"] = moduleValue;
                }
            }

            foreach (object o in bases) {
                if (o is PythonType) {
                    return PythonOps.MakeClass(context, name, bases._data, String.Empty, dict);
                }
            }

            return new OldClass(name, bases, dict, String.Empty);
        }
Exemplo n.º 9
0
            public object load_module(CodeContext /*!*/ context, string fullname)
            {
                fullname = MakeValidPath(fullname, resolved_subpath);

                string code = null;
                GenericModuleCodeType moduleType;
                bool             ispackage    = false;
                string           modpath      = null;
                string           fullFileName = null;
                PythonModule     mod;
                PythonDictionary dict = null;

                // Go through available import types by search-order
                foreach (var order in _search_order)
                {
                    string tempCode = this.resolver.GetScriptSource(fullname.Replace(".", "/") + order.Key);

                    if (tempCode != null)
                    {
                        moduleType   = order.Value;
                        code         = tempCode;
                        modpath      = fullname;
                        fullFileName = fullname.Replace(".", "/") + order.Key;

                        if ((order.Value & GenericModuleCodeType.Package) == GenericModuleCodeType.Package)
                        {
                            ispackage = true;
                        }

                        break;
                    }
                }

                // of no code was loaded
                if (code == null)
                {
                    return(null);
                }

                ScriptCode scriptCode = null;

                mod = context.LanguageContext.CompileModule(fullFileName, fullname,
                                                            new SourceUnit(context.LanguageContext, new SourceStringContentProvider(code), modpath, SourceCodeKind.File),
                                                            ModuleOptions.None, out scriptCode);

                dict = mod.Get__dict__();

                // Set values before execute script
                dict.Add("__name__", fullname.Split('.').Last());
                dict.Add("__loader__", this);
                dict.Add("__package__", null);

                if (ispackage)
                {
                    // Add path
                    string fullpath = RESOLVER_PATH_NAME + "." + string.Format(fullname.Replace("/", "."));

                    //_rel_path = fullpath;
                    paths.Add(fullpath);

                    List pkgpath = PythonOps.MakeList(fullpath);

                    if (dict.ContainsKey("__path__"))
                    {
                        dict["__path__"] = pkgpath;
                    }
                    else
                    {
                        dict.Add("__path__", pkgpath);
                    }
                }
                else
                {
                    StringBuilder packageName  = new StringBuilder();
                    string[]      packageParts = fullname.Split(new char[] { '/' });
                    for (int i = 0; i < packageParts.Length - 1; i++)
                    {
                        if (i > 0)
                        {
                            packageName.Append(".");
                        }

                        packageName.Append(packageParts[i]);
                    }

                    dict["__package__"] = packageName.ToString();
                }

                scriptCode.Run(mod.Scope);
                return(mod);
            }