def testMapInitUnmapLibrary(self): self.assertEquals(Unmanaged.GetModuleHandle("python26.dll"), IntPtr.Zero, "library already mapped") sr = StubReference(os.path.join("build", "ironclad", "python26.dll")) self.assertNotEquals(Unmanaged.GetModuleHandle("python26.dll"), IntPtr.Zero, "library not mapped by construction") fpCalls = [] @dgt_getfuncptr def GetFuncPtr(name): fpCalls.append(name) return IntPtr.Zero dataCalls = [] @dgt_registerdata def RegisterData(name, _): dataCalls.append(name) sr.Init(GetFuncPtr, RegisterData) self.assertEquals(len(fpCalls) > 0, True, "did not get any addresses") self.assertEquals(len(dataCalls) > 0, True, "did not set any data") sr.Dispose() self.assertEquals(Unmanaged.GetModuleHandle("python26.dll"), IntPtr.Zero, "library not unmapped on dispose") sr.Dispose()
def testConvertPyFileToDescriptor(self, mapper, _): testDir = tempfile.mkdtemp() outFile = os.path.join(testDir, "test") pyFile = file(outFile, 'w') pyFile.write("I love streams ") pyFile.flush() fd = mapper.ConvertPyFileToDescriptor(pyFile) FILE = Unmanaged._fdopen(fd, "w") testStr = "and file descriptors!" testStrPtr = mapper.Store(testStr) testDataPtr = OffsetPtr(testStrPtr, Marshal.OffsetOf(PyStringObject, "ob_sval")) self.assertTrue( Unmanaged.fwrite(testDataPtr, 1, len(testStr), FILE) > 0, "writing failed") Unmanaged.fflush(FILE) pyFile.close() stream = file(outFile, 'r') output = stream.read() stream.close() self.assertEquals(output, "I love streams and file descriptors!")
def testUnmapsAutomagically(self): sr = StubReference(os.path.join("build", "ironclad", "python26.dll")) self.assertNotEquals(Unmanaged.GetModuleHandle("python26.dll"), IntPtr.Zero, "library not mapped by construction") del sr gcwait() self.assertEquals(Unmanaged.GetModuleHandle("python26.dll"), IntPtr.Zero, "library not unmapped on finalize")
def testUnmapsAutomagically(self): sr = StubReference(DLL_PATH) self.assertNotEquals(Unmanaged.GetModuleHandle(PYTHON_DLL), IntPtr.Zero, "library not mapped by construction") del sr gcwait() self.assertEquals(Unmanaged.GetModuleHandle(PYTHON_DLL), IntPtr.Zero, "library not unmapped on finalize")
def testIC_PyFile_AsFile_Write(self, mapper, addToCleanUp, _): testDir = tempfile.mkdtemp() addToCleanUp(lambda: shutil.rmtree(testDir)) path = os.path.join(testDir, "test") testStr = "meh, string data" testLength = len(testStr) testStrPtr = mapper.Store(testStr) testDataPtr = OffsetPtr(testStrPtr, Marshal.OffsetOf(PyStringObject, "ob_sval")) filePtr = mapper.Store(open(path, "w")) f = mapper.IC_PyFile_AsFile(filePtr) self.assertEquals(Unmanaged.fwrite(testDataPtr, 1, testLength, f), testLength, "didn't work") # nasty test: patch out PyObject_Free # the memory will not be deallocated, but the FILE handle should be calls = [] def Free(ptr): calls.append(ptr) freeDgt = dgt_void_ptr(Free) CPyMarshal.WriteFunctionPtrField(mapper.PyFile_Type, PyTypeObject, "tp_free", freeDgt) mapper.DecRef(filePtr) self.assertEquals(calls, [filePtr], "failed to call tp_free function") mgdF = open(path) result = mgdF.read() self.assertEquals( result, testStr, "failed to write (got >>%s<<) -- deallocing filePtr did not close FILE" % result ) mgdF.close()
def testUnloadsAutomagically(self): pi = PydImporter() pi.Load("tests\\data\\setvalue.pyd") del pi gcwait() self.assertEquals(Unmanaged.GetModuleHandle("setvalue.pyd"), IntPtr.Zero, "failed to unload on dispose")
def testLoadsModuleAndUnloadsOnDispose(self): mapper = PythonMapper(DLL_PATH) try: origcwd = os.getcwd() mapper.LoadModule(os.path.join("tests", "data", "setvalue.pyd"), "some.module") self.assertEquals(os.getcwd(), origcwd, "failed to restore working directory") self.assertNotEquals(Unmanaged.GetModuleHandle("setvalue.pyd"), IntPtr.Zero, "library not mapped by construction") mapper.Dispose() self.assertEquals(Unmanaged.GetModuleHandle("setvalue.pyd"), IntPtr.Zero, "library not unmapped by Dispose") self.assertEquals(Unmanaged.GetModuleHandle(PYTHON_DLL), IntPtr.Zero, "library not unmapped by Dispose") finally: mapper.Dispose()
def testLoadsStubWhenPassedPathAndUnloadsOnDispose(self): mapper = PythonMapper(DLL_PATH) try: self.assertNotEquals(Unmanaged.GetModuleHandle(PYTHON_DLL), IntPtr.Zero, "library not mapped by construction") self.assertNotEquals(PythonMapper._Py_NoneStruct, IntPtr.Zero, "mapping not set up") # weak side-effect test to hopefully prove that ReadyBuiltinTypes has been called self.assertEquals(CPyMarshal.ReadPtrField(mapper.PyLong_Type, PyTypeObject, "tp_base"), mapper.PyBaseObject_Type) mapper.Dispose() self.assertEquals(Unmanaged.GetModuleHandle(PYTHON_DLL), IntPtr.Zero, "library not unmapped by Dispose") finally: mapper.Dispose()
def testIC_PyFile_AsFile_Write(self, mapper, addToCleanUp, _): testDir = tempfile.mkdtemp() addToCleanUp(lambda: shutil.rmtree(testDir)) path = os.path.join(testDir, "test") testStr = "meh, string data" testLength = len(testStr) testStrPtr = mapper.Store(testStr) testDataPtr = OffsetPtr(testStrPtr, Marshal.OffsetOf(PyStringObject, "ob_sval")) filePtr = mapper.Store(open(path, 'w')) f = mapper.IC_PyFile_AsFile(filePtr) self.assertEquals(Unmanaged.fwrite(testDataPtr, 1, testLength, f), testLength, "didn't work") # nasty test: patch out PyObject_Free # the memory will not be deallocated, but the FILE handle should be calls = [] def Free(ptr): calls.append(ptr) freeDgt = dgt_void_ptr(Free) CPyMarshal.WriteFunctionPtrField(mapper.PyFile_Type, PyTypeObject, 'tp_free', freeDgt) mapper.DecRef(filePtr) self.assertEquals(calls, [filePtr], 'failed to call tp_free function') mgdF = open(path) result = mgdF.read() self.assertEquals(result, testStr, "failed to write (got >>%s<<) -- deallocing filePtr did not close FILE" % result) mgdF.close()
def testConvertPyFileToDescriptor(self, mapper, _): testDir = tempfile.mkdtemp() outFile = os.path.join(testDir, "test") pyFile = file(outFile, "w") pyFile.write("I love streams ") pyFile.flush() fd = mapper.ConvertPyFileToDescriptor(pyFile) FILE = Unmanaged._fdopen(fd, "w") testStr = "and file descriptors!" testStrPtr = mapper.Store(testStr) testDataPtr = OffsetPtr(testStrPtr, Marshal.OffsetOf(PyStringObject, "ob_sval")) self.assertTrue(Unmanaged.fwrite(testDataPtr, 1, len(testStr), FILE) > 0, "writing failed") Unmanaged.fflush(FILE) pyFile.close() stream = file(outFile, "r") output = stream.read() stream.close() self.assertEquals(output, "I love streams and file descriptors!")
def testCallsAppropriatelyNamedInitFunctionAndUnloadsWhenDone(self): l = Unmanaged.LoadLibrary("tests\\data\\setvalue.pyd") try: pValue = Unmanaged.GetProcAddress(l, "value") value = CPyMarshal.ReadInt(pValue) self.assertEquals(value, 1, "bad setup") pi = PydImporter() pi.Load("tests\\data\\setvalue.pyd") finally: # lose test reference to setvalue.pyd # only the PydImporter should still have a reference to it Unmanaged.FreeLibrary(l) value = CPyMarshal.ReadInt(pValue) self.assertEquals(value, 2, "PydImporter didn't call correct function") pi.Dispose() self.assertEquals(Unmanaged.GetModuleHandle("setvalue.pyd"), IntPtr.Zero, "failed to unload on dispose") pi.Dispose()
def testIC_PyFile_AsFile(self, mapper, addToCleanUp, stderr_writes): buflen = len(TEST_TEXT) + 10 buf = Marshal.AllocHGlobal(buflen) filePtr = mapper.Store(open(*READ_ARGS)) f = mapper.IC_PyFile_AsFile(filePtr) self.assertEquals(stderr_writes, [('Warning: creating unmanaged FILE* from managed stream. Please use ironclad.open with this extension.',), ('\n',)]) self.assertEquals(Unmanaged.fread(buf, 1, buflen, f), len(TEST_TEXT), "didn't get a real FILE") ptr = buf for c in TEST_TEXT: self.assertEquals(Marshal.ReadByte(ptr), ord(c), "got bad data from FILE") ptr = OffsetPtr(ptr, 1) Marshal.FreeHGlobal(buf)
import clr clr.AddReferenceToFileAndPath("build/ironclad/ironclad.dll") # If we ever completely unload msvcr90.dll, we get weird explosions next time we try to # load it. This is surely my fault, and it would be nice to make it work 'properly', but # for now this hack suffices; it shouldn't be an issue in real use because you shouldn't # be repeatedly creating Mappers. # On platforms where this is not necessary, please implement LoadLibrary such that it # doesn't explode when this file is not found. from Ironclad import Unmanaged Unmanaged.LoadLibrary("tests\data\implicit-load-msvcr90.dll")