Exemple #1
0
    def testPyType_Ready(self, mapper, addToCleanUp):
        typePtr = Marshal.AllocHGlobal(Marshal.SizeOf(PyTypeObject()))
        CPyMarshal.Zero(typePtr, Marshal.SizeOf(PyTypeObject()))
        addToCleanUp(lambda: Marshal.FreeHGlobal(typePtr))

        self.assertEquals(mapper.PyType_Ready(typePtr), 0, "wrong")
        self.assertEquals(
            CPyMarshal.ReadPtrField(typePtr, PyTypeObject, "ob_type"),
            mapper.PyType_Type, "failed to fill in missing ob_type")
        self.assertEquals(
            CPyMarshal.ReadPtrField(typePtr, PyTypeObject, "tp_base"),
            mapper.PyBaseObject_Type, "failed to fill in missing tp_base")
        tp_dict = mapper.Retrieve(
            CPyMarshal.ReadPtrField(typePtr, PyTypeObject, "tp_dict"))
        self.assertEquals(mapper.Retrieve(typePtr).__dict__, tp_dict)

        typeFlags = CPyMarshal.ReadIntField(typePtr, PyTypeObject, "tp_flags")
        self.assertEquals(typeFlags & UInt32(Py_TPFLAGS.READY),
                          UInt32(Py_TPFLAGS.READY), "did not ready type")
        self.assertEquals(
            typeFlags & UInt32(Py_TPFLAGS.HAVE_CLASS),
            UInt32(Py_TPFLAGS.HAVE_CLASS),
            "we always set this flag, for no better reason than 'it makes ctypes kinda work'"
        )

        CPyMarshal.WritePtrField(typePtr, PyTypeObject, "ob_type", IntPtr.Zero)
        self.assertEquals(mapper.PyType_Ready(typePtr), 0, "wrong")
        self.assertEquals(
            CPyMarshal.ReadPtrField(typePtr, PyTypeObject, "ob_type"),
            IntPtr.Zero, "unexpectedly and unnecessarily rereadied type")
Exemple #2
0
 def create(name, size):
     block = Marshal.AllocHGlobal(size)
     if name == 'PyFile_Type':
         CPyMarshal.Zero(block, size);
         CPyMarshal.WritePtrField(block, PyTypeObject, 'tp_dealloc', mapper.GetFuncPtr('IC_file_dealloc'))
     mapper.RegisterData(name, block)
     blocks.append(block)
Exemple #3
0
 def testPyType_IsSubtype_NullPtrs(self, mapper, CallLater):
     type_size = Marshal.SizeOf(PyTypeObject)
     ptr = Marshal.AllocHGlobal(type_size)
     CallLater(lambda: Marshal.FreeHGlobal(ptr))
     CPyMarshal.Zero(ptr, type_size)
     
     self.assertTrue(mapper.PyType_IsSubtype(ptr, mapper.PyBaseObject_Type))
     self.assertTrue(mapper.PyType_IsSubtype(ptr, ptr))
Exemple #4
0
    def testWritePtrField(self):
        data = Marshal.AllocHGlobal(Marshal.SizeOf(PyObject()))
        CPyMarshal.Zero(data, Marshal.SizeOf(PyObject()))

        CPyMarshal.WritePtrField(data, PyObject, "ob_type", IntPtr(12345))
        dataStruct = PtrToStructure(data, PyObject)
        self.assertEquals(dataStruct.ob_type, IntPtr(12345), "failed to write")

        Marshal.FreeHGlobal(data)
Exemple #5
0
    def testWriteDoubleField(self):
        data = Marshal.AllocHGlobal(Marshal.SizeOf(PyFloatObject()))
        CPyMarshal.Zero(data, Marshal.SizeOf(PyFloatObject()))

        CPyMarshal.WriteDoubleField(data, PyFloatObject, "ob_fval", 7.6e-5)
        dataStruct = PtrToStructure(data, PyFloatObject)
        self.assertEquals(dataStruct.ob_fval, 7.6e-5)

        Marshal.FreeHGlobal(data)
Exemple #6
0
    def testWriteIntField(self):
        data = Marshal.AllocHGlobal(Marshal.SizeOf(PyIntObject()))
        CPyMarshal.Zero(data, Marshal.SizeOf(PyIntObject()))

        for value in (Int32.MaxValue, Int32.MinValue):
            CPyMarshal.WriteIntField(data, PyIntObject, "ob_ival", value)
            dataStruct = PtrToStructure(data, PyIntObject)
            self.assertEquals(dataStruct.ob_ival, value, "failed to write")

        Marshal.FreeHGlobal(data)
Exemple #7
0
    def testReadPtrField(self):
        data = Marshal.AllocHGlobal(Marshal.SizeOf(PyTypeObject()))
        CPyMarshal.Zero(data, Marshal.SizeOf(PyTypeObject()))

        CPyMarshal.WritePtrField(data, PyTypeObject, "tp_doc", IntPtr(12345))
        self.assertEquals(
            CPyMarshal.ReadPtrField(data, PyTypeObject, "tp_doc"),
            IntPtr(12345), "failed to read")

        Marshal.FreeHGlobal(data)
Exemple #8
0
    def testReadCStringFieldEmpty(self):
        data = Marshal.AllocHGlobal(Marshal.SizeOf(PyTypeObject()))
        CPyMarshal.Zero(data, Marshal.SizeOf(PyTypeObject()))
        CPyMarshal.WritePtrField(data, PyTypeObject, "tp_doc", IntPtr.Zero)

        self.assertEquals(
            CPyMarshal.ReadCStringField(data, PyTypeObject, "tp_doc"), "",
            "failed to read correctly")

        Marshal.FreeHGlobal(data)
Exemple #9
0
    def testReadDoubleField(self):
        data = Marshal.AllocHGlobal(Marshal.SizeOf(PyFloatObject()))
        CPyMarshal.Zero(data, Marshal.SizeOf(PyFloatObject()))

        CPyMarshal.WriteDoubleField(data, PyFloatObject, "ob_fval", -1.2e34)
        self.assertEquals(
            CPyMarshal.ReadDoubleField(data, PyFloatObject, "ob_fval"),
            -1.2e34)

        Marshal.FreeHGlobal(data)
Exemple #10
0
    def testReadIntField(self):
        data = Marshal.AllocHGlobal(Marshal.SizeOf(PyIntObject()))
        CPyMarshal.Zero(data, Marshal.SizeOf(PyIntObject()))

        for value in (Int32.MaxValue, Int32.MinValue):
            CPyMarshal.WriteIntField(data, PyIntObject, "ob_ival", value)
            self.assertEquals(
                CPyMarshal.ReadIntField(data, PyIntObject, "ob_ival"), value,
                "failed to read")

        Marshal.FreeHGlobal(data)
Exemple #11
0
    def testWriteCStringField(self):
        data = Marshal.AllocHGlobal(Marshal.SizeOf(PyTypeObject()))
        CPyMarshal.Zero(data, Marshal.SizeOf(PyTypeObject()))
        string = "Hey, I am a string. I have tricksy \\escapes\\."
        CPyMarshal.WriteCStringField(data, PyTypeObject, "tp_doc", string)

        self.assertEquals(
            CPyMarshal.ReadCStringField(data, PyTypeObject, "tp_doc"), string,
            "failed to read correctly")
        Marshal.FreeHGlobal(
            CPyMarshal.ReadPtrField(data, PyTypeObject, "tp_doc"))
        Marshal.FreeHGlobal(data)
Exemple #12
0
    def testReadUIntField(self):
        data = Marshal.AllocHGlobal(Marshal.SizeOf(PyTypeObject()))
        CPyMarshal.Zero(data, Marshal.SizeOf(PyTypeObject()))

        for value in (UInt32.MaxValue, UInt32.MinValue):
            CPyMarshal.WriteUIntField(data, PyTypeObject, "tp_version_tag",
                                      value)
            self.assertEquals(
                CPyMarshal.ReadUIntField(data, PyTypeObject, "tp_version_tag"),
                value, "failed to read")

        Marshal.FreeHGlobal(data)
Exemple #13
0
    def testWriteUIntField(self):
        data = Marshal.AllocHGlobal(Marshal.SizeOf(PyTypeObject()))
        CPyMarshal.Zero(data, Marshal.SizeOf(PyTypeObject()))

        for value in (UInt32.MaxValue, UInt32.MinValue):
            CPyMarshal.WriteUIntField(data, PyTypeObject, "tp_version_tag",
                                      value)
            dataStruct = PtrToStructure(data, PyTypeObject)
            self.assertEquals(dataStruct.tp_version_tag, value,
                              "failed to write")

        Marshal.FreeHGlobal(data)
Exemple #14
0
def MakeNumSeqMapMethods(_type, slots):
    size = Marshal.SizeOf(_type())
    ptr = Marshal.AllocHGlobal(size)
    CPyMarshal.Zero(ptr, size)
    deallocs = []
    for (slot, func) in slots.items():
        dgt = NUMSEQMAP_METHODS[slot](func)
        CPyMarshal.WriteFunctionPtrField(ptr, _type, slot, dgt)
        deallocs.append(GC_NotYet(dgt))
        
    def dealloc():
        for f in deallocs:
            f()
        Marshal.FreeHGlobal(ptr)
    return ptr, dealloc
Exemple #15
0
def MakeItemsTablePtr(items):
    if not items:
        return IntPtr.Zero, lambda: None
    itemtype = items[0].__class__
    typesize = Marshal.SizeOf(itemtype())
    size = typesize * (len(items) + 1)
    
    tablePtr = Marshal.AllocHGlobal(size)
    CPyMarshal.Zero(tablePtr, size)
    for i, item in enumerate(items):
        Marshal.StructureToPtr(item, OffsetPtr(tablePtr, typesize * i), False)

    def dealloc():
        Marshal.DestroyStructure(tablePtr, itemtype)
        Marshal.FreeHGlobal(tablePtr)
    return tablePtr, dealloc
Exemple #16
0
 def test_PyObject_New(self):
     allocs = []
     allocator = GetAllocatingTestAllocator(allocs, [])
     mapper = PythonMapper(allocator)
     deallocTypes = CreateTypes(mapper)
     
     typeObjSize = Marshal.SizeOf(PyTypeObject())
     typePtr = Marshal.AllocHGlobal(typeObjSize)
     CPyMarshal.Zero(typePtr, typeObjSize)
     CPyMarshal.WriteIntField(typePtr, PyTypeObject, "tp_basicsize", 31337)
     
     del allocs[:]
     objPtr = mapper._PyObject_New(typePtr)
     self.assertEquals(allocs, [(objPtr, 31337)])
     self.assertEquals(CPyMarshal.ReadPtrField(objPtr, PyObject, 'ob_type'), typePtr)
     self.assertEquals(CPyMarshal.ReadIntField(objPtr, PyObject, 'ob_refcnt'), 1)
     self.assertEquals(mapper.HasPtr(objPtr), False)
     
     mapper.Dispose()
     deallocTypes()
Exemple #17
0
    def testReadFunctionPtrField(self):
        data = Marshal.AllocHGlobal(Marshal.SizeOf(PyTypeObject()))
        CPyMarshal.Zero(data, Marshal.SizeOf(PyTypeObject()))

        calls = []

        def TestFunc(selfPtr, argsPtr, kwargsPtr):
            calls.append((selfPtr, argsPtr, kwargsPtr))
            return 123

        self.testDgt = dgt_int_ptrptrptr(TestFunc)
        CPyMarshal.WriteFunctionPtrField(data, PyTypeObject, "tp_init",
                                         self.testDgt)

        readDgt = CPyMarshal.ReadFunctionPtrField(data, PyTypeObject,
                                                  "tp_init", dgt_int_ptrptrptr)

        args = (IntPtr(111), IntPtr(222), IntPtr(333))
        self.assertEquals(readDgt(*args), 123, "not hooked up")
        self.assertEquals(calls, [args], "not hooked up")
Exemple #18
0
    def testWorksWithoutEmbeddedNulls(self, mapper, addDealloc):
        dataPtrPtr = Marshal.AllocHGlobal(CPyMarshal.PtrSize * 2)
        sizePtr = CPyMarshal.Offset(dataPtrPtr, CPyMarshal.PtrSize)
        addDealloc(lambda: Marshal.FreeHGlobal(dataPtrPtr))

        testStr = "You're fighting Ed the Undying." + self.getStringWithValues(
            1, 256)
        strPtr = mapper.Store(testStr)
        dataPtr = self.dataPtrFromStrPtr(strPtr)
        self.assertEquals(
            mapper.PyString_AsStringAndSize(strPtr, dataPtrPtr, sizePtr), 0)
        self.assertEquals(CPyMarshal.ReadPtr(dataPtrPtr), dataPtr)
        self.assertEquals(CPyMarshal.ReadInt(sizePtr), len(testStr))
        self.assertMapperHasError(mapper, None)

        CPyMarshal.Zero(dataPtrPtr, CPyMarshal.PtrSize * 2)
        self.assertEquals(
            mapper.PyString_AsStringAndSize(strPtr, dataPtrPtr, IntPtr.Zero),
            0)
        self.assertEquals(CPyMarshal.ReadPtr(dataPtrPtr), dataPtr)
        self.assertMapperHasError(mapper, None)
Exemple #19
0
    def testZero(self):
        bufferlen = 200
        zerolen = 173

        data = Marshal.AllocHGlobal(bufferlen)
        this = data
        for _ in xrange(bufferlen):
            CPyMarshal.WriteByte(this, 255)
            this = OffsetPtr(this, 1)

        CPyMarshal.Zero(data, zerolen)

        this = data
        for i in xrange(bufferlen):
            actual = CPyMarshal.ReadByte(this)
            expected = (255, 0)[i < zerolen]
            self.assertEquals(
                actual, expected,
                "wrong value at %d (%d, %d)" % (i, actual, expected))
            this = OffsetPtr(this, 1)

        Marshal.FreeHGlobal(data)
Exemple #20
0
def MakeTypePtr(mapper, params, allocator=None):
    fields = dict(MAKETYPEPTR_DEFAULTS)
    fields.update(GetMapperTypePtrDefaults(mapper))
    fields.update(params)
    
    deallocs = []
    typeSize = Marshal.SizeOf(PyTypeObject())
    if allocator:
        # pretend this was constructed by a C extension, using the mapper's allocator
        # hence mapper should do the deallocation itself
        typePtr = allocator.Alloc(typeSize)
    else:
        typePtr = Marshal.AllocHGlobal(typeSize)
        deallocs.append(lambda: Marshal.FreeHGlobal(typePtr))
    CPyMarshal.Zero(typePtr, typeSize)
    
    for field, value in fields.items():
        deallocs.append(WriteTypeField(typePtr, field, value))

    def dealloc():
        for f in deallocs:
            f()
    return typePtr, dealloc