def testStoreMethod(self, mapper, _): meth = MethodType('foo', 'bar', 'baz') methPtr = mapper.Store(meth) stored = PtrToStructure(methPtr, PyMethodObject) self.assertEquals(stored.ob_refcnt, 1) self.assertEquals(stored.ob_type, mapper.PyMethod_Type) self.assertEquals(stored.im_weakreflist, IntPtr.Zero) attrs = { 'im_func': 'foo', 'im_self': 'bar', 'im_class': 'baz', } attrPtrs = [] for (attr, expected) in attrs.items(): attrPtr = getattr(stored, attr) self.assertEquals(mapper.RefCount(attrPtr), 1) mapper.IncRef(attrPtr) attrPtrs.append(attrPtr) value = mapper.Retrieve(attrPtr) self.assertEquals(value, expected) mapper.DecRef(methPtr) for attrPtr in attrPtrs: self.assertEquals(mapper.RefCount(attrPtr), 1)
def testPyList_New_NonZeroLength(self): allocs = [] mapper = PythonMapper(GetAllocatingTestAllocator(allocs, [])) deallocTypes = CreateTypes(mapper) del allocs[:] SIZE = 27 listPtr = mapper.PyList_New(SIZE) listStruct = 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()
def testWriteDouble(self): data = Marshal.AllocHGlobal(CPyMarshal.DoubleSize) CPyMarshal.WriteDouble(data, 2.2e22) doubleStruct = PtrToStructure(data, DoubleStruct) self.assertEquals(doubleStruct.value, 2.2e22) Marshal.FreeHGlobal(data)
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)
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)
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)
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)
def assertStringObjectHasLength(self, strPtr, length): stringObject = PtrToStructure(strPtr, PyStringObject) self.assertEquals(stringObject.ob_refcnt, 1, "unexpected refcount") self.assertEquals(stringObject.ob_size, length, "unexpected ob_size") self.assertEquals(stringObject.ob_shash, -1, "unexpected currently-useless-field") self.assertEquals(stringObject.ob_sstate, 0, "unexpected currently-useless-field") strDataPtr = self.dataPtrFromStrPtr(strPtr) terminatorPtr = OffsetPtr(strDataPtr, length) self.assertEquals(Marshal.ReadByte(terminatorPtr), 0, "string not terminated")
def testStoreFunction(self, mapper, _): # note: can't be bothered to set any fields for now, # because they're not actually needed at the moment def f(): pass fPtr = mapper.Store(f) stored = PtrToStructure(fPtr, PyFunctionObject) self.assertEquals(stored.ob_refcnt, 1) self.assertEquals(stored.ob_type, mapper.PyFunction_Type) for attr in attrs: self.assertEquals(getattr(stored, attr), IntPtr.Zero)
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 = 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()
def test_PyTuple_Resize(self): allocs = [] mapper = PythonMapper(GetAllocatingTestAllocator(allocs, [])) tuplePtrPtr = Marshal.AllocHGlobal(CPyMarshal.PtrSize) oldTuplePtr = mapper.PyTuple_New(1) del allocs[:] CPyMarshal.WritePtr(tuplePtrPtr, oldTuplePtr) self.assertEquals(mapper._PyTuple_Resize(tuplePtrPtr, 100), 0) newTuplePtr = CPyMarshal.ReadPtr(tuplePtrPtr) expectedSize = Marshal.SizeOf(PyTupleObject()) + (CPyMarshal.PtrSize * (99)) self.assertEquals(allocs, [(newTuplePtr, expectedSize)]) tupleStruct = PtrToStructure(newTuplePtr, PyTupleObject) self.assertEquals(tupleStruct.ob_refcnt, 1) self.assertEquals(tupleStruct.ob_type, mapper.PyTuple_Type) self.assertEquals(tupleStruct.ob_size, 100) mapper.Dispose() Marshal.FreeHGlobal(tuplePtrPtr)
def assertPyTuple_New_Works(self, length): allocs = [] mapper = PythonMapper(GetAllocatingTestAllocator(allocs, [])) typeBlock = Marshal.AllocHGlobal(Marshal.SizeOf(PyTypeObject())) mapper.RegisterData("PyTuple_Type", typeBlock) tuplePtr = mapper.PyTuple_New(length) expectedSize = Marshal.SizeOf(PyTupleObject()) + (CPyMarshal.PtrSize * (length - 1)) self.assertEquals(allocs, [(tuplePtr, expectedSize)], "bad alloc") tupleStruct = PtrToStructure(tuplePtr, PyTupleObject) self.assertEquals(tupleStruct.ob_refcnt, 1, "bad refcount") self.assertEquals(tupleStruct.ob_type, mapper.PyTuple_Type, "bad type") self.assertEquals(tupleStruct.ob_size, length, "bad size") self.assertEquals(mapper.PyTuple_Size(tuplePtr), length, "should still work with uninitialised tuple imo") dataPtr = OffsetPtr(tuplePtr, Marshal.OffsetOf(PyTupleObject, "ob_item")) itemPtrs = [] for i in range(length): self.assertEquals(CPyMarshal.ReadPtr(dataPtr), IntPtr.Zero, "item memory not zeroed") itemPtr = mapper.Store(i + 100) CPyMarshal.WritePtr(dataPtr, itemPtr) itemPtrs.append(itemPtr) dataPtr = OffsetPtr(dataPtr, CPyMarshal.PtrSize) immutableTuple = mapper.Retrieve(tuplePtr) self.assertEquals(immutableTuple, tuple(i + 100 for i in range(length)), "broken") tuplePtr2 = mapper.Store(immutableTuple) self.assertEquals(tuplePtr2, tuplePtr, "didn't realise already had this object stored") self.assertEquals(mapper.RefCount(tuplePtr), 2, "didn't incref") mapper.Dispose() Marshal.FreeHGlobal(typeBlock)
def testFillNotImplemented(self, mapper, _): niPtr = mapper._Py_NotImplementedStruct niStruct = PtrToStructure(niPtr, PyObject) self.assertEquals(niStruct.ob_refcnt, 1, "bad refcount") self.assertEquals(niStruct.ob_type, mapper.PyNotImplemented_Type, "unexpected type")
def testFillNone(self, mapper, _): nonePtr = mapper._Py_NoneStruct noneStruct = PtrToStructure(nonePtr, PyObject) self.assertEquals(noneStruct.ob_refcnt, 1, "bad refcount") self.assertEquals(noneStruct.ob_type, mapper.PyNone_Type, "unexpected type")