示例#1
0
    def testDeleteList(self):
        deallocs = []
        mapper = PythonMapper(GetAllocatingTestAllocator([], deallocs))
        deallocTypes = CreateTypes(mapper)
        
        item1 = object()
        item2 = object()
        itemPtr1 = mapper.Store(item1)
        itemPtr2 = mapper.Store(item2)
        
        listPtr = mapper.PyList_New(0)
        
        mapper.PyList_Append(listPtr, itemPtr1)
        mapper.PyList_Append(listPtr, itemPtr2)

        mapper.DecRef(itemPtr1)
        mapper.DecRef(itemPtr2)

        self.assertEquals(len(deallocs), 1, "should have deallocated original data block only at this point")
        dataStore = CPyMarshal.ReadPtrField(listPtr, PyListObject, "ob_item")

        mapper.DecRef(listPtr)
        listDeallocs = deallocs[1:]
        self.assertEquals(len(listDeallocs), 4, "should dealloc list object; data store; both items")
        expectedDeallocs = [listPtr, dataStore, itemPtr1, itemPtr2]
        self.assertEquals(set(listDeallocs), set(expectedDeallocs), "deallocated wrong stuff")
        
        mapper.Dispose()
        deallocTypes()
示例#2
0
 def testPyList_New_NonZeroLength(self):
     allocs = []
     mapper = PythonMapper(GetAllocatingTestAllocator(allocs, []))
     deallocTypes = CreateTypes(mapper)
     del allocs[:]
     
     SIZE = 27
     listPtr = mapper.PyList_New(SIZE)
     
     listStruct = Marshal.PtrToStructure(listPtr, PyListObject)
     self.assertEquals(listStruct.ob_refcnt, 1, "bad refcount")
     self.assertEquals(listStruct.ob_type, mapper.PyList_Type, "bad type")
     self.assertEquals(listStruct.ob_size, SIZE, "bad ob_size")
     self.assertEquals(listStruct.allocated, SIZE, "bad allocated")
     
     dataPtr = listStruct.ob_item
     self.assertNotEquals(dataPtr, IntPtr.Zero, "failed to allocate space for data")
     
     expectedAllocs = [(dataPtr, (SIZE * CPyMarshal.PtrSize)), (listPtr, Marshal.SizeOf(PyListObject))]
     self.assertEquals(set(allocs), set(expectedAllocs), "allocated wrong")
     
     for _ in range(SIZE):
         self.assertEquals(CPyMarshal.ReadPtr(dataPtr), IntPtr.Zero, "failed to zero memory")
         dataPtr = OffsetPtr(dataPtr, CPyMarshal.PtrSize)
     
     mapper.Dispose()
     deallocTypes()
示例#3
0
    def testPyList_Append(self):
        allocs = []
        deallocs = []
        mapper = PythonMapper(GetAllocatingTestAllocator(allocs, deallocs))
        deallocTypes = CreateTypes(mapper)
        
        del allocs[:]
        listPtr = mapper.PyList_New(0)
        self.assertEquals(allocs, [(listPtr, Marshal.SizeOf(PyListObject))], "bad alloc")

        item1 = object()
        item2 = object()
        itemPtr1 = mapper.Store(item1)
        itemPtr2 = mapper.Store(item2)
        
        self.assertEquals(mapper.PyList_Append(listPtr, itemPtr1), 0, "failed to report success")
        self.assertEquals(len(allocs), 4, "didn't allocate memory for data store (list; item1; item2; data store comes 4th)")

        dataPtrAfterFirstAppend = CPyMarshal.ReadPtrField(listPtr, PyListObject, "ob_item")
        self.assertEquals(allocs[3], (dataPtrAfterFirstAppend, CPyMarshal.PtrSize), "allocated wrong amount of memory")
        self.assertEquals(CPyMarshal.ReadPtr(dataPtrAfterFirstAppend), itemPtr1, "failed to fill memory")
        self.assertEquals(mapper.RefCount(itemPtr1), 2, "failed to incref new contents")
        self.assertEquals(mapper.Retrieve(listPtr), [item1], "retrieved wrong list")
        
        # make refcount 1, to prove that references are not lost when reallocing data
        mapper.DecRef(itemPtr1)

        self.assertEquals(mapper.PyList_Append(listPtr, itemPtr2), 0, "failed to report success")
        self.assertEquals(len(allocs), 5, "didn't allocate memory for new, larger data store")
        self.assertEquals(deallocs, [dataPtrAfterFirstAppend])

        dataPtrAfterSecondAppend = CPyMarshal.ReadPtrField(listPtr, PyListObject, "ob_item")
        self.assertEquals(allocs[4], (dataPtrAfterSecondAppend, (CPyMarshal.PtrSize * 2)), 
                          "allocated wrong amount of memory")
        self.assertEquals(CPyMarshal.ReadPtr(dataPtrAfterSecondAppend), itemPtr1, 
                          "failed to keep reference to first item")
        self.assertEquals(CPyMarshal.ReadPtr(OffsetPtr(dataPtrAfterSecondAppend, CPyMarshal.PtrSize)), itemPtr2, 
                          "failed to keep reference to first item")
        self.assertEquals(mapper.RefCount(itemPtr1), 1, "wrong refcount for item existing only in list")
        self.assertEquals(mapper.RefCount(itemPtr2), 2, "wrong refcount newly-added item")
        self.assertEquals(mapper.Retrieve(listPtr), [item1, item2], "retrieved wrong list")
        
        mapper.Dispose()
        deallocTypes()
示例#4
0
    def testPyList_New_ZeroLength(self):
        allocs = []
        mapper = PythonMapper(GetAllocatingTestAllocator(allocs, []))
        deallocTypes = CreateTypes(mapper)
        
        del allocs[:]
        listPtr = mapper.PyList_New(0)
        self.assertEquals(allocs, [(listPtr, Marshal.SizeOf(PyListObject))], "bad alloc")

        listStruct = Marshal.PtrToStructure(listPtr, PyListObject)
        self.assertEquals(listStruct.ob_refcnt, 1, "bad refcount")
        self.assertEquals(listStruct.ob_type, mapper.PyList_Type, "bad type")
        self.assertEquals(listStruct.ob_size, 0, "bad ob_size")
        self.assertEquals(listStruct.ob_item, IntPtr.Zero, "bad data pointer")
        self.assertEquals(listStruct.allocated, 0, "bad allocated")
        self.assertEquals(mapper.Retrieve(listPtr), [], "mapped to wrong object")
        
        mapper.Dispose()
        deallocTypes()