def testShrink(self): allocs = [] frees = [] mapper = PythonMapper(GetAllocatingTestAllocator(allocs, frees)) deallocTypes = CreateTypes(mapper) del allocs[:] oldLength = 365 newLength = 20 ptrPtr = Marshal.AllocHGlobal(Marshal.SizeOf(IntPtr)) try: strPtr = mapper.PyString_FromStringAndSize(IntPtr.Zero, oldLength) Marshal.WriteIntPtr(ptrPtr, strPtr) baseSize = Marshal.SizeOf(PyStringObject) self.assertEquals(allocs, [(strPtr, oldLength + baseSize)], "allocated wrong") self.assertEquals(mapper._PyString_Resize(ptrPtr, newLength), 0, "bad return on success") self.assertHasStringType(strPtr, mapper) self.assertStringObjectHasLength(strPtr, newLength) self.assertEquals(allocs, [(strPtr, oldLength + baseSize)], "unexpected extra alloc") self.assertEquals(frees, [], "unexpected frees") finally: mapper.Dispose() Marshal.FreeHGlobal(ptrPtr) deallocTypes()
def testCreateStringWithData(self): allocs = [] mapper = PythonMapper(GetAllocatingTestAllocator(allocs, [])) deallocTypes = CreateTypes(mapper) del allocs[:] try: testString = "we also run the shovel racket" + self.getStringWithValues( 0, 256) testBytes = self.byteArrayFromString(testString) testData = self.ptrFromByteArray(testBytes) testLength = len(testString) strPtr = mapper.PyString_FromStringAndSize(testData, testLength) baseSize = Marshal.SizeOf(PyStringObject) self.assertEquals(allocs, [(strPtr, testLength + baseSize)], "allocated wrong") self.assertHasStringType(strPtr, mapper) self.assertStringObjectHasLength(strPtr, testLength) self.assertStringObjectHasDataBytes(strPtr, testBytes) self.assertEquals(mapper.Retrieve(strPtr), testString, "failed to read string data") finally: mapper.Dispose() deallocTypes()
def testCreateEmptyString(self): allocs = [] mapper = PythonMapper(GetAllocatingTestAllocator(allocs, [])) deallocTypes = CreateTypes(mapper) del allocs[:] try: testString = "we run the grease racket in this town" + self.getStringWithValues( 0, 256) testLength = len(testString) strPtr = mapper.PyString_FromStringAndSize(IntPtr.Zero, testLength) baseSize = Marshal.SizeOf(PyStringObject) self.assertEquals(allocs, [(strPtr, testLength + baseSize)], "allocated wrong") self.assertStringObjectHasLength(strPtr, testLength) self.assertHasStringType(strPtr, mapper) testBytes = self.byteArrayFromString(testString) self.fillStringDataWithBytes(strPtr, testBytes) resultStr = mapper.Retrieve(strPtr) self.assertEquals(resultStr, testString, "failed to read string data") strPtr2 = mapper.Store(resultStr) self.assertEquals(strPtr2, strPtr, "did not remember already had this string") self.assertEquals(mapper.RefCount(strPtr), 2, "did not incref on store") finally: mapper.Dispose() deallocTypes()
def testGrow(self): allocs = [] frees = [] mapper = PythonMapper(GetAllocatingTestAllocator(allocs, frees)) deallocTypes = CreateTypes(mapper) del allocs[:] oldLength = 20 testString = "slings and arrows" + self.getStringWithValues(0, 256) newLength = len(testString) oldStrPtr = mapper.PyString_FromStringAndSize(IntPtr.Zero, oldLength) ptrPtr = Marshal.AllocHGlobal(Marshal.SizeOf(IntPtr)) try: Marshal.WriteIntPtr(ptrPtr, oldStrPtr) newStrPtr = IntPtr.Zero baseSize = Marshal.SizeOf(PyStringObject) self.assertEquals(allocs, [(oldStrPtr, oldLength + baseSize)], "allocated wrong") self.assertEquals(mapper._PyString_Resize(ptrPtr, newLength), 0, "bad return on success") newStrPtr = Marshal.ReadIntPtr(ptrPtr) expectedAllocs = [(oldStrPtr, oldLength + baseSize), (newStrPtr, newLength + baseSize)] self.assertEquals(allocs, expectedAllocs, "allocated wrong") self.assertEquals(frees, [oldStrPtr], "did not free unused memory") self.assertHasStringType(newStrPtr, mapper) self.assertStringObjectHasLength(newStrPtr, newLength) testBytes = self.byteArrayFromString(testString) self.fillStringDataWithBytes(newStrPtr, testBytes) self.assertEquals(mapper.Retrieve(newStrPtr), testString, "failed to read string data") if oldStrPtr != newStrPtr: # this would otherwise fail (very, very rarely) self.assertEquals(oldStrPtr in frees, True) finally: mapper.Dispose() Marshal.FreeHGlobal(ptrPtr) deallocTypes()
def testErrorHandling(self): allocs = [] frees = [] mapper = PythonMapper(GetAllocatingTestAllocator(allocs, frees)) deallocTypes = CreateTypes(mapper) del allocs[:] ptrPtr = Marshal.AllocHGlobal(Marshal.SizeOf(IntPtr)) try: data = mapper.PyString_FromStringAndSize(IntPtr.Zero, 365) Marshal.WriteIntPtr(ptrPtr, data) baseSize = Marshal.SizeOf(PyStringObject) self.assertEquals(allocs, [(data, 365 + baseSize)], "allocated wrong") self.assertEquals(mapper._PyString_Resize(ptrPtr, 2000000000), -1, "bad return on error") self.assertEquals(type(mapper.LastException), MemoryError, "wrong exception type") self.assertTrue(data in frees, "did not deallocate") finally: mapper.Dispose() Marshal.FreeHGlobal(ptrPtr) deallocTypes()