Exemple #1
0
        Register_PyString_Type(IntPtr address)
        {
            // not quite trivial to autogenerate
            CPyMarshal.Zero(address, Marshal.SizeOf(typeof(PyTypeObject)));
            CPyMarshal.WriteIntField(address, typeof(PyTypeObject), "ob_refcnt", 1);
            CPyMarshal.WriteIntField(address, typeof(PyTypeObject), "tp_basicsize", Marshal.SizeOf(typeof(PyStringObject)) - 1);
            CPyMarshal.WriteIntField(address, typeof(PyTypeObject), "tp_itemsize", 1);
            CPyMarshal.WriteCStringField(address, typeof(PyTypeObject), "tp_name", "str");
            CPyMarshal.WritePtrField(address, typeof(PyTypeObject), "tp_str", this.GetFuncPtr("IC_PyString_Str"));
            CPyMarshal.WritePtrField(address, typeof(PyTypeObject), "tp_repr", this.GetFuncPtr("PyObject_Repr"));

            uint   sqSize = (uint)Marshal.SizeOf(typeof(PySequenceMethods));
            IntPtr sqPtr  = this.allocator.Alloc(sqSize);

            CPyMarshal.Zero(sqPtr, sqSize);
            CPyMarshal.WritePtrField(sqPtr, typeof(PySequenceMethods), "sq_concat", this.GetFuncPtr("IC_PyString_Concat_Core"));
            CPyMarshal.WritePtrField(address, typeof(PyTypeObject), "tp_as_sequence", sqPtr);

            uint   bfSize = (uint)Marshal.SizeOf(typeof(PyBufferProcs));
            IntPtr bfPtr  = this.allocator.Alloc(bfSize);

            CPyMarshal.Zero(bfPtr, bfSize);
            CPyMarshal.WritePtrField(bfPtr, typeof(PyBufferProcs), "bf_getreadbuffer", this.GetFuncPtr("IC_str_getreadbuffer"));
            CPyMarshal.WritePtrField(bfPtr, typeof(PyBufferProcs), "bf_getwritebuffer", this.GetFuncPtr("IC_str_getwritebuffer"));
            CPyMarshal.WritePtrField(bfPtr, typeof(PyBufferProcs), "bf_getsegcount", this.GetFuncPtr("IC_str_getsegcount"));
            CPyMarshal.WritePtrField(bfPtr, typeof(PyBufferProcs), "bf_getcharbuffer", this.GetFuncPtr("IC_str_getreadbuffer"));
            CPyMarshal.WritePtrField(address, typeof(PyTypeObject), "tp_as_buffer", bfPtr);

            CPyMarshal.WriteIntField(address, typeof(PyTypeObject), "tp_flags", (Int32)Py_TPFLAGS.HAVE_GETCHARBUFFER);

            this.map.Associate(address, TypeCache.String);
        }
        PyList_New(int length)
        {
            if (length == 0)
            {
                return(this.IC_PyList_New_Zero());
            }

            PyListObject list = new PyListObject();

            list.ob_refcnt = 1;
            list.ob_type   = this.PyList_Type;
            list.ob_size   = length;
            list.allocated = length;

            int bytes = length * CPyMarshal.PtrSize;

            list.ob_item = this.allocator.Alloc((uint)bytes);
            CPyMarshal.Zero(list.ob_item, bytes);

            IntPtr listPtr = this.allocator.Alloc((uint)Marshal.SizeOf(typeof(PyListObject)));

            Marshal.StructureToPtr(list, listPtr, false);
            this.incompleteObjects[listPtr] = UnmanagedDataMarker.PyListObject;
            return(listPtr);
        }
Exemple #3
0
        _PyObject_New(IntPtr typePtr)
        {
            uint   tp_basicsize = CPyMarshal.ReadUIntField(typePtr, typeof(PyTypeObject), "tp_basicsize");
            IntPtr objPtr       = this.allocator.Alloc(tp_basicsize);

            CPyMarshal.Zero(objPtr, tp_basicsize);
            return(this.PyObject_Init(objPtr, typePtr));
        }
Exemple #4
0
        public ThreadState(PythonMapper mapper)
        {
            this.mapper = mapper;
            uint size = (uint)Marshal.SizeOf(typeof(PyThreadState));

            this.ptr = this.mapper.PyMem_Malloc(size); // leaka leaka leaka
            CPyMarshal.Zero(this.ptr, size);
        }
Exemple #5
0
 Register_PyBool_Type(IntPtr address)
 {
     // not quite trivial to autogenerate
     CPyMarshal.Zero(address, Marshal.SizeOf(typeof(PyTypeObject)));
     CPyMarshal.WriteIntField(address, typeof(PyTypeObject), "ob_refcnt", 1);
     CPyMarshal.WritePtrField(address, typeof(PyTypeObject), "tp_base", this.PyInt_Type);
     CPyMarshal.WriteCStringField(address, typeof(PyTypeObject), "tp_name", "bool");
     this.map.Associate(address, TypeCache.Boolean);
 }
Exemple #6
0
        _PyObject_NewVar(IntPtr typePtr, int nitems)
        {
            int    tp_basicsize = CPyMarshal.ReadIntField(typePtr, typeof(PyTypeObject), "tp_basicsize");
            int    tp_itemsize  = CPyMarshal.ReadIntField(typePtr, typeof(PyTypeObject), "tp_itemsize");
            uint   size         = (uint)(tp_basicsize + nitems * tp_itemsize);
            IntPtr objPtr       = this.allocator.Alloc(size);

            CPyMarshal.Zero(objPtr, size);
            return(this.PyObject_Init(objPtr, typePtr));
        }
Exemple #7
0
        Register_PyEllipsis_Type(IntPtr address)
        {
            // not quite trivial to autogenerate
            // (but surely there's a better way to get the Ellipsis object...)
            CPyMarshal.Zero(address, Marshal.SizeOf(typeof(PyTypeObject)));
            CPyMarshal.WriteIntField(address, typeof(PyTypeObject), "ob_refcnt", 1);
            CPyMarshal.WriteCStringField(address, typeof(PyTypeObject), "tp_name", "ellipsis");
            object ellipsisType = PythonCalls.Call(Builtin.type, new object[] { PythonOps.Ellipsis });

            this.map.Associate(address, ellipsisType);
        }
        StoreTyped(PythonFunction func)
        {
            uint   size = (uint)Marshal.SizeOf(typeof(PyFunctionObject));
            IntPtr ptr  = this.allocator.Alloc(size);

            CPyMarshal.Zero(ptr, size);
            CPyMarshal.WriteIntField(ptr, typeof(PyIntObject), "ob_refcnt", 1);
            CPyMarshal.WritePtrField(ptr, typeof(PyIntObject), "ob_type", this.PyFunction_Type);
            this.map.Associate(ptr, func);
            return(ptr);
        }
Exemple #9
0
        StoreTyped(PythonFile obj)
        {
            IntPtr ptr = this.allocator.Alloc((uint)Marshal.SizeOf(typeof(PyFileObject)));

            CPyMarshal.Zero(ptr, Marshal.SizeOf(typeof(PyFileObject)));
            CPyMarshal.WriteIntField(ptr, typeof(PyObject), "ob_refcnt", 1);
            CPyMarshal.WritePtrField(ptr, typeof(PyObject), "ob_type", this.PyFile_Type);
            CPyMarshal.WriteIntField(ptr, typeof(PyFileObject), "f_fp", -2);
            CPyMarshal.WritePtrField(ptr, typeof(PyFileObject), "f_name", this.Store(obj.name));
            CPyMarshal.WritePtrField(ptr, typeof(PyFileObject), "f_mode", this.Store(obj.mode));
            this.map.Associate(ptr, obj);
            return(ptr);
        }
Exemple #10
0
        AddNumberMethodsWithoutIndex(IntPtr typePtr)
        {
            uint   nmSize = (uint)Marshal.SizeOf(typeof(PyNumberMethods));
            IntPtr nmPtr  = this.allocator.Alloc(nmSize);

            CPyMarshal.Zero(nmPtr, nmSize);

            CPyMarshal.WritePtrField(nmPtr, typeof(PyNumberMethods), "nb_int", this.GetFuncPtr("PyNumber_Int"));
            CPyMarshal.WritePtrField(nmPtr, typeof(PyNumberMethods), "nb_long", this.GetFuncPtr("PyNumber_Long"));
            CPyMarshal.WritePtrField(nmPtr, typeof(PyNumberMethods), "nb_float", this.GetFuncPtr("PyNumber_Float"));
            CPyMarshal.WritePtrField(nmPtr, typeof(PyNumberMethods), "nb_multiply", this.GetFuncPtr("PyNumber_Multiply"));

            CPyMarshal.WritePtrField(typePtr, typeof(PyTypeObject), "tp_as_number", nmPtr);
        }
        StoreTyped(Method meth)
        {
            uint   size    = (uint)Marshal.SizeOf(typeof(PyMethodObject));
            IntPtr methPtr = this.allocator.Alloc(size);

            CPyMarshal.Zero(methPtr, size);

            CPyMarshal.WriteIntField(methPtr, typeof(PyMethodObject), "ob_refcnt", 1);
            CPyMarshal.WritePtrField(methPtr, typeof(PyMethodObject), "ob_type", this.PyMethod_Type);
            CPyMarshal.WritePtrField(methPtr, typeof(PyMethodObject), "im_func", this.Store(meth.im_func));
            CPyMarshal.WritePtrField(methPtr, typeof(PyMethodObject), "im_self", this.Store(meth.im_self));
            CPyMarshal.WritePtrField(methPtr, typeof(PyMethodObject), "im_class", this.Store(meth.im_class));

            this.map.Associate(methPtr, meth);
            return(methPtr);
        }
        StoreTyped(OldInstance inst)
        {
            uint   size = (uint)Marshal.SizeOf(typeof(PyInstanceObject));
            IntPtr ptr  = this.allocator.Alloc(size);

            CPyMarshal.Zero(ptr, size);

            CPyMarshal.WriteIntField(ptr, typeof(PyObject), "ob_refcnt", 1);
            CPyMarshal.WritePtrField(ptr, typeof(PyObject), "ob_type", this.PyInstance_Type);

            CPyMarshal.WritePtrField(ptr, typeof(PyInstanceObject), "in_class",
                                     this.Store(Builtin.getattr(this.scratchContext, inst, "__class__")));
            CPyMarshal.WritePtrField(ptr, typeof(PyInstanceObject), "in_dict",
                                     this.Store(Builtin.getattr(this.scratchContext, inst, "__dict__")));

            this.map.Associate(ptr, inst);
            return(ptr);
        }
        StoreTyped(OldClass cls)
        {
            uint   size = (uint)Marshal.SizeOf(typeof(PyClassObject));
            IntPtr ptr  = this.allocator.Alloc(size);

            CPyMarshal.Zero(ptr, size);

            CPyMarshal.WriteIntField(ptr, typeof(PyObject), "ob_refcnt", 2); // leak classes deliberately
            CPyMarshal.WritePtrField(ptr, typeof(PyObject), "ob_type", this.PyClass_Type);

            CPyMarshal.WritePtrField(ptr, typeof(PyClassObject), "cl_bases",
                                     this.Store(Builtin.getattr(this.scratchContext, cls, "__bases__")));
            CPyMarshal.WritePtrField(ptr, typeof(PyClassObject), "cl_dict",
                                     this.Store(Builtin.getattr(this.scratchContext, cls, "__dict__")));
            CPyMarshal.WritePtrField(ptr, typeof(PyClassObject), "cl_name",
                                     this.Store(Builtin.getattr(this.scratchContext, cls, "__name__")));

            this.map.Associate(ptr, cls);
            return(ptr);
        }
        CreateTuple(int size)
        {
            PyTupleObject tuple = new PyTupleObject();

            tuple.ob_refcnt = 1;
            tuple.ob_type   = this.PyTuple_Type;
            tuple.ob_size   = size;

            int    baseSize  = Marshal.SizeOf(typeof(PyTupleObject));
            int    extraSize = (CPyMarshal.PtrSize * (size - 1));
            IntPtr tuplePtr  = this.allocator.Alloc((uint)(baseSize + extraSize));

            Marshal.StructureToPtr(tuple, tuplePtr, false);

            IntPtr itemsPtr = CPyMarshal.Offset(
                tuplePtr, Marshal.OffsetOf(typeof(PyTupleObject), "ob_item"));

            CPyMarshal.Zero(itemsPtr, CPyMarshal.PtrSize * size);
            return(tuplePtr);
        }
        PyType_GenericAlloc(IntPtr typePtr, int nItems)
        {
            int size = CPyMarshal.ReadIntField(typePtr, typeof(PyTypeObject), "tp_basicsize");

            if (nItems > 0)
            {
                int itemsize = CPyMarshal.ReadIntField(typePtr, typeof(PyTypeObject), "tp_itemsize");
                size += (nItems * itemsize);
            }

            IntPtr newInstance = this.allocator.Alloc((uint)size);

            CPyMarshal.Zero(newInstance, size);
            CPyMarshal.WriteUIntField(newInstance, typeof(PyObject), "ob_refcnt", 1);
            CPyMarshal.WritePtrField(newInstance, typeof(PyObject), "ob_type", typePtr);

            if (nItems > 0)
            {
                CPyMarshal.WriteIntField(newInstance, typeof(PyVarObject), "ob_size", nItems);
            }

            return(newInstance);
        }
        StoreTyped(PythonType _type)
        {
            uint   typeSize = (uint)Marshal.SizeOf(typeof(PyTypeObject));
            IntPtr typePtr  = this.allocator.Alloc(typeSize);

            CPyMarshal.Zero(typePtr, typeSize);

            CPyMarshal.WriteIntField(typePtr, typeof(PyTypeObject), "ob_refcnt", 2);

            object ob_type = PythonCalls.Call(this.scratchContext, Builtin.type, new object[] { _type });

            CPyMarshal.WritePtrField(typePtr, typeof(PyTypeObject), "ob_type", this.Store(ob_type));

            string tp_name = (string)_type.__getattribute__(this.scratchContext, "__name__");

            CPyMarshal.WritePtrField(typePtr, typeof(PyTypeObject), "tp_name", this.Store(tp_name));

            PythonTuple tp_bases = (PythonTuple)_type.__getattribute__(this.scratchContext, "__bases__");
            object      tp_base  = tp_bases[0];

            CPyMarshal.WritePtrField(typePtr, typeof(PyTypeObject), "tp_base", this.Store(tp_base));
            if (tp_bases.__len__() > 1)
            {
                CPyMarshal.WritePtrField(typePtr, typeof(PyTypeObject), "tp_bases", this.Store(tp_bases));
            }

            this.scratchModule.Get__dict__()["_ironclad_bases"]     = tp_bases;
            this.scratchModule.Get__dict__()["_ironclad_metaclass"] = ob_type;
            this.ExecInModule(CodeSnippets.CLASS_STUB_CODE, this.scratchModule);
            this.classStubs[typePtr] = this.scratchModule.Get__dict__()["_ironclad_class_stub"];

            this.actualisableTypes[typePtr] = new ActualiseDelegate(this.ActualiseArbitraryObject);
            this.map.Associate(typePtr, _type);
            this.PyType_Ready(typePtr);
            return(typePtr);
        }