Ejemplo n.º 1
0
        GenerateClass(IntPtr typePtr)
        {
            ClassBuilder cb       = new ClassBuilder(typePtr);
            PythonTuple  tp_bases = this.ExtractBases(typePtr);

            foreach (object _base in tp_bases)
            {
                this.UpdateMethodTableObj(cb.methodTable, _base);
            }

            IntPtr ob_typePtr = CPyMarshal.ReadPtrField(typePtr, typeof(PyObject), "ob_type");

            this.IncRef(ob_typePtr);
            object ob_type = this.Retrieve(ob_typePtr);

            this.scratchModule.Get__dict__()["_ironclad_metaclass"] = ob_type;
            this.scratchModule.Get__dict__()["_ironclad_bases"]     = tp_bases;
            this.ExecInModule(cb.code.ToString(), this.scratchModule);
            object klass      = this.scratchModule.Get__dict__()["_ironclad_class"];
            object klass_stub = this.scratchModule.Get__dict__()["_ironclad_class_stub"];

            this.classStubs[typePtr] = klass_stub;
            Builtin.setattr(this.scratchContext, klass, "_dispatcher", new Dispatcher(this, cb.methodTable));
            object typeDict = Builtin.getattr(this.scratchContext, klass, "__dict__");

            CPyMarshal.WritePtrField(typePtr, typeof(PyTypeObject), "tp_dict", this.Store(typeDict));
            return(klass);
        }
Ejemplo n.º 2
0
 IC_PyList_Append_Empty(IntPtr listPtr, ref PyListObject listStruct, IntPtr itemPtr)
 {
     listStruct.ob_size   = 1;
     listStruct.allocated = 1;
     listStruct.ob_item   = this.allocator.Alloc(CPyMarshal.PtrSize);
     CPyMarshal.WritePtr(listStruct.ob_item, itemPtr);
     Marshal.StructureToPtr(listStruct, listPtr, false);
 }
Ejemplo n.º 3
0
 EnsureGIL()
 {
     this.tempObjects.Push(new List <IntPtr>());
     if (this.GIL.Acquire() == 1)
     {
         CPyMarshal.WritePtr(this._PyThreadState_Current, this.threadState.Ptr);
     }
 }
Ejemplo n.º 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);
        }
Ejemplo n.º 5
0
 GenerateRichcmpMethods()
 {
     if (CPyMarshal.ReadPtrField(this.ptr, typeof(PyTypeObject), "tp_richcompare") != IntPtr.Zero)
     {
         this.code.Append(String.Format(CodeSnippets.RICHCMP_METHOD_TEMPLATE, tablePrefix));
         this.ConnectTypeField("tp_richcompare", typeof(dgt_ptr_ptrptrint));
     }
 }
Ejemplo n.º 6
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));
        }
Ejemplo n.º 7
0
 ConnectTypeField(string fieldName, Type dgtType)
 {
     if (CPyMarshal.ReadPtrField(this.ptr, typeof(PyTypeObject), fieldName) != IntPtr.Zero)
     {
         Delegate dgt = CPyMarshal.ReadFunctionPtrField(this.ptr, typeof(PyTypeObject), fieldName, dgtType);
         this.methodTable[this.tablePrefix + fieldName] = dgt;
     }
 }
Ejemplo n.º 8
0
 IC_str_getsegcount(IntPtr strPtr, IntPtr lenPtr)
 {
     if (lenPtr != IntPtr.Zero)
     {
         CPyMarshal.WriteInt(lenPtr, this.PyString_Size(strPtr));
     }
     return(1);
 }
Ejemplo n.º 9
0
        IC_PyBaseObject_Dealloc(IntPtr objPtr)
        {
            IntPtr       objType = CPyMarshal.ReadPtrField(objPtr, typeof(PyObject), "ob_type");
            dgt_void_ptr freeDgt = (dgt_void_ptr)
                                   CPyMarshal.ReadFunctionPtrField(
                objType, typeof(PyTypeObject), "tp_free", typeof(dgt_void_ptr));

            freeDgt(objPtr);
        }
Ejemplo n.º 10
0
        StoreTyped(BigInteger value)
        {
            IntPtr ptr = this.allocator.Alloc((uint)Marshal.SizeOf(typeof(PyObject)));

            CPyMarshal.WriteIntField(ptr, typeof(PyObject), "ob_refcnt", 1);
            CPyMarshal.WritePtrField(ptr, typeof(PyObject), "ob_type", this.PyLong_Type);
            this.map.Associate(ptr, value);
            return(ptr);
        }
Ejemplo n.º 11
0
        StoreObject(object obj)
        {
            IntPtr ptr = this.allocator.Alloc((uint)Marshal.SizeOf(typeof(PyObject)));

            CPyMarshal.WriteIntField(ptr, typeof(PyObject), "ob_refcnt", 1);
            CPyMarshal.WritePtrField(ptr, typeof(PyObject), "ob_type", this.PyBaseObject_Type);
            this.map.Associate(ptr, obj);
            return(ptr);
        }
Ejemplo n.º 12
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);
 }
Ejemplo n.º 13
0
 Register_PyFile_Type(IntPtr address)
 {
     // not worth autogenerating
     // we're using the cpy file type by default, with methods patched in C
     // to redirect into C# when ipy files turn up
     CPyMarshal.WriteIntField(address, typeof(PyTypeObject), "ob_refcnt", 1);
     CPyMarshal.WriteCStringField(address, typeof(PyTypeObject), "tp_name", "file");
     this.map.Associate(address, TypeCache.PythonFile);
 }
Ejemplo n.º 14
0
        CreatePyStringWithBytes(byte[] bytes)
        {
            IntPtr strPtr = this.AllocPyString(bytes.Length);
            IntPtr bufPtr = CPyMarshal.Offset(
                strPtr, Marshal.OffsetOf(typeof(PyStringObject), "ob_sval"));

            Marshal.Copy(bytes, 0, bufPtr, bytes.Length);
            return(strPtr);
        }
Ejemplo n.º 15
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));
        }
Ejemplo n.º 16
0
        StoreTyped(double value)
        {
            IntPtr ptr = this.allocator.Alloc((uint)Marshal.SizeOf(typeof(PyFloatObject)));

            CPyMarshal.WriteIntField(ptr, typeof(PyFloatObject), "ob_refcnt", 1);
            CPyMarshal.WritePtrField(ptr, typeof(PyFloatObject), "ob_type", this.PyFloat_Type);
            CPyMarshal.WriteDoubleField(ptr, typeof(PyFloatObject), "ob_fval", value);
            this.map.Associate(ptr, value);
            return(ptr);
        }
Ejemplo n.º 17
0
        StoreTyped(PythonExceptions.BaseException exc)
        {
            IntPtr ptr = this.allocator.Alloc((uint)Marshal.SizeOf(typeof(PyObject)));

            CPyMarshal.WriteIntField(ptr, typeof(PyObject), "ob_refcnt", 1);
            object type_ = PythonCalls.Call(Builtin.type, new object[] { exc });

            CPyMarshal.WritePtrField(ptr, typeof(PyObject), "ob_type", this.Store(type_));
            this.map.Associate(ptr, exc);
            return(ptr);
        }
Ejemplo n.º 18
0
        IC__PyString_Resize_NoGrow(IntPtr strPtr, int newSize)
        {
            CPyMarshal.WriteIntField(strPtr, typeof(PyStringObject), "ob_size", newSize);
            IntPtr bufPtr = CPyMarshal.Offset(
                strPtr, Marshal.OffsetOf(typeof(PyStringObject), "ob_sval"));
            IntPtr terminatorPtr = CPyMarshal.Offset(
                bufPtr, newSize);

            CPyMarshal.WriteByte(terminatorPtr, 0);
            return(0);
        }
Ejemplo n.º 19
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);
        }
Ejemplo n.º 20
0
        ReadCStringField(IntPtr addr, Type type, string field)
        {
            IntPtr strPtrAddr = CPyMarshal.GetField(addr, type, field);
            IntPtr readAddr   = CPyMarshal.ReadPtr(strPtrAddr);

            if (readAddr != IntPtr.Zero)
            {
                return(Marshal.PtrToStringAnsi(readAddr));
            }
            return("");
        }
Ejemplo n.º 21
0
        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);
        }
Ejemplo n.º 22
0
        AddNumberMethodsWithIndex(IntPtr typePtr)
        {
            this.AddNumberMethodsWithoutIndex(typePtr);
            IntPtr nmPtr = CPyMarshal.ReadPtrField(typePtr, typeof(PyTypeObject), "tp_as_number");

            CPyMarshal.WritePtrField(nmPtr, typeof(PyNumberMethods), "nb_index", this.GetFuncPtr("PyNumber_Index"));

            Py_TPFLAGS flags = (Py_TPFLAGS)CPyMarshal.ReadIntField(typePtr, typeof(PyTypeObject), "tp_flags");

            flags |= Py_TPFLAGS.HAVE_INDEX;
            CPyMarshal.WriteIntField(typePtr, typeof(PyTypeObject), "tp_flags", (Int32)flags);
        }
Ejemplo n.º 23
0
        IC_PySlice_Dealloc(IntPtr slicePtr)
        {
            this.DecRef(CPyMarshal.ReadPtrField(slicePtr, typeof(PySliceObject), "start"));
            this.DecRef(CPyMarshal.ReadPtrField(slicePtr, typeof(PySliceObject), "stop"));
            this.DecRef(CPyMarshal.ReadPtrField(slicePtr, typeof(PySliceObject), "step"));

            dgt_void_ptr freeDgt = (dgt_void_ptr)
                                   CPyMarshal.ReadFunctionPtrField(
                this.PySlice_Type, typeof(PyTypeObject), "tp_free", typeof(dgt_void_ptr));

            freeDgt(slicePtr);
        }
Ejemplo n.º 24
0
        PyString_InternFromString(IntPtr stringData)
        {
            IntPtr newStrPtr    = PyString_FromString(stringData);
            IntPtr newStrPtrPtr = this.allocator.Alloc((uint)Marshal.SizeOf(typeof(IntPtr)));

            CPyMarshal.WritePtr(newStrPtrPtr, newStrPtr);
            this.PyString_InternInPlace(newStrPtrPtr);
            IntPtr newNewStrPtr = CPyMarshal.ReadPtr(newStrPtrPtr);

            this.allocator.Free(newStrPtrPtr);
            return(newNewStrPtr);
        }
Ejemplo n.º 25
0
        StoreTyped(Slice slice)
        {
            IntPtr ptr = this.allocator.Alloc((uint)Marshal.SizeOf(typeof(PySliceObject)));

            CPyMarshal.WriteIntField(ptr, typeof(PySliceObject), "ob_refcnt", 1);
            CPyMarshal.WritePtrField(ptr, typeof(PySliceObject), "ob_type", this.PySlice_Type);
            CPyMarshal.WritePtrField(ptr, typeof(PySliceObject), "start", this.Store(slice.start));
            CPyMarshal.WritePtrField(ptr, typeof(PySliceObject), "stop", this.Store(slice.stop));
            CPyMarshal.WritePtrField(ptr, typeof(PySliceObject), "step", this.Store(slice.step));
            this.map.Associate(ptr, slice);
            return(ptr);
        }
Ejemplo n.º 26
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);
        }
Ejemplo n.º 27
0
        StoreTyped(Complex value)
        {
            IntPtr ptr = this.allocator.Alloc((uint)Marshal.SizeOf(typeof(PyComplexObject)));

            CPyMarshal.WriteIntField(ptr, typeof(PyComplexObject), "ob_refcnt", 1);
            CPyMarshal.WritePtrField(ptr, typeof(PyComplexObject), "ob_type", this.PyComplex_Type);
            IntPtr cpxptr = CPyMarshal.GetField(ptr, typeof(PyComplexObject), "cval");

            CPyMarshal.WriteDoubleField(cpxptr, typeof(Py_complex), "real", value.Real);
            CPyMarshal.WriteDoubleField(cpxptr, typeof(Py_complex), "imag", value.Imaginary);
            this.map.Associate(ptr, value);
            return(ptr);
        }
Ejemplo n.º 28
0
        IC_PyMethod_Dealloc(IntPtr objPtr)
        {
            this.DecRef(CPyMarshal.ReadPtrField(objPtr, typeof(PyMethodObject), "im_func"));
            this.DecRef(CPyMarshal.ReadPtrField(objPtr, typeof(PyMethodObject), "im_self"));
            this.DecRef(CPyMarshal.ReadPtrField(objPtr, typeof(PyMethodObject), "im_class"));

            IntPtr       objType = CPyMarshal.ReadPtrField(objPtr, typeof(PyObject), "ob_type");
            dgt_void_ptr freeDgt = (dgt_void_ptr)
                                   CPyMarshal.ReadFunctionPtrField(
                objType, typeof(PyTypeObject), "tp_free", typeof(dgt_void_ptr));

            freeDgt(objPtr);
        }
Ejemplo n.º 29
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);
        }
Ejemplo n.º 30
0
        InheritIntField(IntPtr typePtr, string name)
        {
            int fieldVal = CPyMarshal.ReadIntField(typePtr, typeof(PyTypeObject), name);

            if (fieldVal == 0)
            {
                IntPtr basePtr = CPyMarshal.ReadPtrField(typePtr, typeof(PyTypeObject), "tp_base");
                if (basePtr != IntPtr.Zero)
                {
                    CPyMarshal.WriteIntField(typePtr, typeof(PyTypeObject), name,
                                             CPyMarshal.ReadIntField(basePtr, typeof(PyTypeObject), name));
                }
            }
        }