def testExtensionTypesAutoActualisable(self): discoveryModes = { "IncRef": lambda f, o: self.assertMaps(mapper, f, o, 5), "Retrieve": lambda f, o: self.assertMaps(mapper, f, o, 4), "DecRef": lambda f, o: self.assertMaps(mapper, f, o, 3), "RefCount": lambda f, o: self.assertMaps(mapper, f, o, 4), } allocator = HGlobalAllocator() mapper = PythonMapper(allocator) deallocTypes = CreateTypes(mapper) # delay deallocs to avoid types with the same addresses causing confusion userTypeDeallocs = [] try: for (mode, TestFunc) in discoveryModes.items(): typePtr, deallocType = MakeTypePtr(mapper, {"tp_name": mode + "Class"}) userTypeDeallocs.append(deallocType) objPtr = allocator.Alloc(Marshal.SizeOf(PyObject)) CPyMarshal.WriteIntField(objPtr, PyObject, "ob_refcnt", 2) CPyMarshal.WritePtrField(objPtr, PyObject, "ob_type", typePtr) discoveryFunc = getattr(mapper, mode) TestFunc(discoveryFunc, objPtr) finally: mapper.Dispose() for deallocFunc in userTypeDeallocs: deallocFunc() deallocTypes()
def testAllocFree(self): allocator = HGlobalAllocator() ptr = allocator.Alloc(REASONABLE_SIZE) self.assertEquals(allocator.Contains(ptr), True) CPyMarshal.WriteInt(ptr, 123) allocator.Free(ptr) self.assertEquals(allocator.Contains(ptr), False) self.assertRaises(KeyError, allocator.Free, ptr)
def testAllocFreeAll(self): allocator = HGlobalAllocator() ptr1 = allocator.Alloc(REASONABLE_SIZE) self.assertEquals(allocator.Contains(ptr1), True) ptr2 = allocator.Alloc(REASONABLE_SIZE) self.assertEquals(allocator.Contains(ptr2), True) ptr3 = allocator.Alloc(REASONABLE_SIZE) self.assertEquals(allocator.Contains(ptr3), True) allocator.Free(ptr1) self.assertEquals(allocator.Contains(ptr1), False) self.assertRaises(KeyError, allocator.Free, ptr1) allocator.FreeAll() self.assertEquals(allocator.Contains(ptr2), False) self.assertEquals(allocator.Contains(ptr3), False) self.assertRaises(KeyError, allocator.Free, ptr2) self.assertRaises(KeyError, allocator.Free, ptr3)
def testDecRefObjectWithZeroRefCountFails(self): allocator = HGlobalAllocator() mapper = PythonMapper(allocator) deallocTypes = CreateTypes(mapper) # need to use same allocator as mapper, otherwise it gets upset on shutdown objPtr = allocator.Alloc(Marshal.SizeOf(PyObject())) CPyMarshal.WriteIntField(objPtr, PyObject, "ob_refcnt", 0) CPyMarshal.WritePtrField(objPtr, PyObject, "ob_type", mapper.PyBaseObject_Type) mapper.StoreBridge(objPtr, object()) self.assertRaisesClr(BadRefCountException, lambda: mapper.DecRef(objPtr)) mapper.Dispose() deallocTypes()
def DoTest(size): allocator = HGlobalAllocator() ptr1 = allocator.Alloc(REASONABLE_SIZE) ptr2 = allocator.Realloc(ptr1, REASONABLE_SIZE * (2**size)) if (ptr1 == ptr2): return False self.assertEquals(allocator.Contains(ptr1), False) self.assertEquals(allocator.Contains(ptr2), True) self.assertRaises(KeyError, allocator.Free, ptr1) allocator.FreeAll() self.assertEquals(allocator.Contains(ptr2), False) self.assertRaises(KeyError, allocator.Free, ptr2) return True
def testStoreBridge(self): allocator = HGlobalAllocator() mapper = PythonMapper(allocator) deallocTypes = CreateTypes(mapper) # need to use same allocator as mapper, otherwise it gets upset on shutdown ptr = allocator.Alloc(Marshal.SizeOf(PyObject)) try: def do(): # see NOTE in interestingptrmaptest obj = object() ref = WeakReference(obj) CPyMarshal.WriteIntField(ptr, PyObject, "ob_refcnt", 1) CPyMarshal.WritePtrField(ptr, PyObject, "ob_type", mapper.PyBaseObject_Type) mapper.StoreBridge(ptr, obj) self.assertEquals(mapper.Retrieve(ptr), obj, "object not stored") self.assertEquals(mapper.Store(obj), ptr, "object not reverse-mapped") mapper.Weaken(obj) CPyMarshal.WriteIntField(ptr, PyObject, "ob_refcnt", 1) mapper.IncRef(ptr) del obj return ref ref = do() gcwait() self.assertEquals(ref.IsAlive, True, "was not strengthened by IncRef") mapper.DecRef(ptr) gcwait() self.assertEquals(ref.IsAlive, False, "was not weakened by DecRef") finally: # need to dealloc ptr ourselves, it doesn't happen automatically # except for objects with Dispatchers mapper.IC_PyBaseObject_Dealloc(ptr) mapper.Dispose() deallocTypes()
def Alloc(self, bytes): ptr = HGlobalAllocator.Alloc(self, bytes) allocsList.append((ptr, bytes)) return ptr