Exemple #1
0
        public static ModuleNotFoundError Create(string message)
        {
            var exc = PyTypeObject.DefaultNew <ModuleNotFoundError>(ModuleNotFoundErrorClass.Instance);

            exc.Message = message;
            return(exc);
        }
Exemple #2
0
        public bool IsSubtypeOf(PyTypeObject type)
        {
            if (this == type)
            {
                return(true);
            }

            PyTypeObject tp_base = this.tp_base.TryRead();

            if (tp_base != null && tp_base.IsSubtypeOf(type))
            {
                return(true);
            }

            PyTupleObject tp_bases = this.tp_bases.TryRead();

            if (tp_bases != null)
            {
                var bases = tp_bases.ReadElements().OfType <PyTypeObject>();
                if (bases.Any(t => t.IsSubtypeOf(type)))
                {
                    return(true);
                }
            }

            return(false);
        }
Exemple #3
0
 /// <summary>
 /// Create a new Python exception based on a message
 /// </summary>
 /// <param name="message">The exception message</param>
 public PyException(string message)
 {
     __class__ = PyExceptionClass.Instance;
     PyTypeObject.DefaultNewPyObject(this, PyExceptionClass.Instance);
     tb      = null;
     Message = message;
 }
Exemple #4
0
        /// <summary>
        /// returns the Root of the UITree.
        /// </summary>
        /// <param name="MemoryReader"></param>
        /// <returns></returns>
        static public UITreeNode UIRoot(
            IPythonMemoryReader MemoryReader)
        {
            var CandidateAddresses = PyTypeObject.EnumeratePossibleAddressesOfInstancesOfPythonTypeFilteredByObType(MemoryReader, "UIRoot");

            var CandidatesWithChildrenCount = new List <KeyValuePair <UITreeNode, int> >();

            foreach (var CandidateAddress in CandidateAddresses)
            {
                var Candidate = new UITreeNode(CandidateAddress, MemoryReader);

                int CandidateChildrenCount = 0;

                {
                    var CandidateChildren = Candidate.EnumerateChildrenTransitive(MemoryReader);

                    if (null != CandidateChildren)
                    {
                        CandidateChildrenCount = CandidateChildren.Count();
                    }
                }

                CandidatesWithChildrenCount.Add(new KeyValuePair <UITreeNode, int>(Candidate, CandidateChildrenCount));
            }

            //	return the candidate the most children nodes where found in.
            return
                (CandidatesWithChildrenCount
                 .OrderByDescending((CandidateWithChildrenCount) => CandidateWithChildrenCount.Value)
                 .FirstOrDefault()
                 .Key);
        }
Exemple #5
0
        public void InvokingWrappedMethod()
        {
            var wrapper  = new WrappedCodeObject("hello_void", typeof(TestPythonClass).GetMethod("hello_void"), this);
            var instance = PyTypeObject.DefaultNew <TestPythonObject>(TestPythonClass.Instance);
            var method   = instance.__getattribute__("hello_void") as IPyCallable;

            method.Call(null, null, new object[0]);
        }
Exemple #6
0
 public bool IsInstanceOf(PyTypeObject type)
 {
     if (this == type)
     {
         return(true);
     }
     return(ob_type.Read().IsSubtypeOf(type));
 }
Exemple #7
0
        public TObject Allocate <TObject>(long extraBytes = 0)
            where TObject : PyObject
        {
            ulong   ptr = Allocate(StructProxy.SizeOf <TObject>(_process) + extraBytes);
            TObject obj = DataProxy.Create <TObject>(_process, ptr, polymorphic: false);

            obj.ob_refcnt.Write(1);

            PyTypeObject pyType = PyObject.GetPyType <TObject>(_process);

            obj.ob_type.Write(pyType);

            return(obj);
        }
Exemple #8
0
            public ProxyTypes(DkmProcess process)
            {
                var langVer = process.GetPythonRuntimeInfo().LanguageVersion;

                var proxyTypes = typeof(PyObject).Assembly.GetTypes().Where(t => typeof(PyObject).IsAssignableFrom(t) && !t.IsAbstract);

                foreach (var proxyType in proxyTypes)
                {
                    string typeVarName = null;

                    PyTypeAttribute[] pyTypeAttrs = (PyTypeAttribute[])Attribute.GetCustomAttributes(proxyType, typeof(PyTypeAttribute), inherit: false);
                    if (pyTypeAttrs.Length == 0)
                    {
                        typeVarName = ComputeVariableName(proxyType);
                    }
                    else
                    {
                        foreach (PyTypeAttribute pyTypeAttr in pyTypeAttrs)
                        {
                            if (pyTypeAttr.MinVersion != PythonLanguageVersion.None && langVer < pyTypeAttr.MinVersion)
                            {
                                continue;
                            }
                            if (pyTypeAttr.MaxVersion != PythonLanguageVersion.None && langVer > pyTypeAttr.MaxVersion)
                            {
                                continue;
                            }

                            typeVarName = pyTypeAttr.VariableName ?? ComputeVariableName(proxyType);
                            break;
                        }

                        if (typeVarName == null)
                        {
                            continue;
                        }
                    }

                    PyTypeObject pyType    = PyTypeObject.FromNativeGlobalVariable(process, typeVarName);
                    ProxyInfo    proxyInfo = new ProxyInfo(proxyType);
                    ProxyInfoFromPyTypePtr.Add(pyType.Address, proxyInfo);

                    PyTypeFromType.Add(proxyType, pyType);
                }
            }
Exemple #9
0
        protected void CheckPyType <TObject>() where TObject : PyObject
        {
            // Check whether this is a freshly allocated object and skip the type check if so.
            if (ob_refcnt.Read() == 0)
            {
                return;
            }

            T            proxyTypes   = Process.GetOrCreateDataItem(() => new ProxyTypes(Process));
            var          expectedType = proxyTypes.PyTypeFromType[typeof(TObject)];
            PyTypeObject ob_type      = this.ob_type.Read();

            if (!ob_type.IsSubtypeOf(expectedType))
            {
                Debug.Fail("Expected object of type " + expectedType.tp_name.Read().ReadUnicode() + " but got a " + ob_type.tp_name.Read().ReadUnicode() + " instead.");
                throw new InvalidOperationException();
            }
        }
Exemple #10
0
        public object ReadValue(PyObject obj)
        {
            PyTypeObject typeObject = obj.LoadType(PyMemoryReader);
            object       value      = null;

            switch (typeObject.tp_name_Val)
            {
            case "str":
                value = new PyStr(obj.BaseAddress, MemoryReader);
                break;

            case "float":
                value = new PyFloat(obj.BaseAddress, MemoryReader);
                break;

            case "int":
                value = new PyInt(obj.BaseAddress, MemoryReader);
                break;

            case "bool":
                value = new PyBool(obj.BaseAddress, MemoryReader);
                break;

            case "unicode":
                value = new PyUnicode(obj.BaseAddress, MemoryReader);
                break;

            case "list":
                value = new PyList(obj.BaseAddress, MemoryReader);
                break;

            default:
                break;
            }

            if (value != null)
            {
                return(value);
            }

            return(obj.BaseAddress.ToString("x"));
        }
Exemple #11
0
        private static ProxyInfo?FindProxyInfoForPyType(DkmProcess process, ulong typePtr)
        {
            if (typePtr == 0)
            {
                return(null);
            }

            var map = process.GetOrCreateDataItem(() => new ProxyTypes(process)).ProxyInfoFromPyTypePtr;

            if (map.TryGetValue(typePtr, out ProxyInfo proxyInfo))
            {
                return(proxyInfo);
            }

            // If we didn't get a direct match, look at tp_base and tp_bases.
            PyTypeObject typeObject    = new PyTypeObject(process, typePtr);
            var          tp_base       = typeObject.tp_base.Raw.Read();
            var          baseProxyInfo = FindProxyInfoForPyType(process, tp_base);

            if (baseProxyInfo != null)
            {
                return(baseProxyInfo);
            }

            PyTupleObject tp_bases = typeObject.tp_bases.TryRead();

            if (tp_bases != null)
            {
                foreach (var bas in tp_bases.ReadElements())
                {
                    baseProxyInfo = FindProxyInfoForPyType(process, bas.Raw.Read());
                    if (baseProxyInfo != null)
                    {
                        return(baseProxyInfo);
                    }
                }
            }

            return(null);
        }
Exemple #12
0
        public void OutputToFile(string path)
        {
            List <string> data = new List <string>();

            try
            {
                ReadRootNode();

                if (RootNode == null)
                {
                    return;
                }
                else
                {
                    data.Add("UIRoot found at " + RootNode.BaseAddress.ToString("x"));
                    data.Add("");
                }

                var AllNodes =
                    new UITreeNode[] { RootNode }
                .Concat(RootNode.EnumerateChildrenTransitive(PyMemoryReader)).ToArray();

                //	show off the spoils....
                foreach (var Node in AllNodes)
                {
                    data.Add("==================================================================================");
                    data.Add("= UITree Node at " + Node.BaseAddress.ToString("x"));
                    data.Add("==================================================================================");
                    data.Add("");

                    var Dict = Node.Dict;

                    if (null == Dict)
                    {
                        continue;
                    }

                    var DictSlots = Dict.Slots;

                    if (null == DictSlots)
                    {
                        continue;
                    }

                    int itterator = 0;

                    //	show info for each entry in dict that has a String as Key.
                    foreach (var Entry in DictSlots)
                    {
                        var EntryKeyStr = Entry.KeyStr;

                        if (null == EntryKeyStr)
                        {
                            continue;
                        }

                        var          me_value   = Entry.me_value;
                        PyObject     obj        = new PyObject(me_value.Value, MemoryReader);
                        PyTypeObject typeObject = obj.LoadType(PyMemoryReader);

                        data.Add
                        (
                            itterator +
                            " : " +
                            Entry.BaseAddress.ToString("x") +
                            " + Entry[\"" +
                            EntryKeyStr +
                            "\"].Value(" +
                            GetTypeName(me_value) +
                            ") = " +
                            (me_value.HasValue ? GetStringValueFromObject(obj) : "null")
                        );

                        if (GetTypeName(me_value) == "list")
                        {
                            uint[] items = ((PyList)ReadValue(me_value)).Items;

                            if (items == null)
                            {
                                continue;
                            }

                            foreach (uint address in items)
                            {
                                obj = new PyObject(address, MemoryReader);
                                String type = GetTypeName(obj);

                                data.Add("+ List Item: (" + type + ") = " + GetStringValueFromObject(obj));
                            }
                        }

                        itterator += 1;
                    }

                    data.Add("");
                }

                System.IO.File.WriteAllLines(path, data.ToArray());
            }
            finally
            {
            }
        }
Exemple #13
0
        public string GetTypeName(PyObject obj)
        {
            PyTypeObject typeObject = obj.LoadType(PyMemoryReader);

            return(typeObject.tp_name_Val);
        }
Exemple #14
0
 public PyIOBase(PyTypeObject fromType, Handle resourceHandle, Stream nativeStream)
     : base(fromType)
 {
     _resourceHandle = resourceHandle;
     _nativeStream   = nativeStream;
 }
Exemple #15
0
 public PyTextIOBase(PyTypeObject fromType, Handle resourceHandle, Stream nativeStream)
     : base(fromType, resourceHandle, nativeStream)
 {
 }