Ejemplo n.º 1
0
        self.assertEquals(CPyMarshal.ReadDouble(data), 2.2e22)

        Marshal.FreeHGlobal(data)

    def testWriteByte(self):
        data = Marshal.AllocHGlobal(1)

        CPyMarshal.WriteByte(data, 0)
        self.assertEquals(Marshal.ReadByte(data), 0, "wrong")

        CPyMarshal.WriteByte(data, 255)
        self.assertEquals(Marshal.ReadByte(data), 255, "wrong")

        Marshal.FreeHGlobal(data)

    def testReadByte(self):
        data = Marshal.AllocHGlobal(1)

        Marshal.WriteByte(data, 0)
        self.assertEquals(CPyMarshal.ReadByte(data), 0, "wrong")

        Marshal.WriteByte(data, 255)
        self.assertEquals(CPyMarshal.ReadByte(data), 255, "wrong")

        Marshal.FreeHGlobal(data)


suite = makesuite(CPyMarshalTest_32)
if __name__ == '__main__':
    run(suite)
        self.assertEquals(resultPtr, mapper._Py_NotImplementedStruct, "wrong")
        self.assertEquals(mapper.RefCount(resultPtr), 2, "did not incref")
        self.assertEquals(mapper.Retrieve(resultPtr), NotImplemented,
                          "not mapped properly")


class PythonMapper_Py_OptimizeFlag_Test(TestCase):
    @WithMapper
    def testFills(self, mapper, addToCleanUp):
        # TODO: if we set a lower value, numpy will crash inside arr_add_docstring
        # I consider docstrings to be low-priority-enough that it's OK to fudge this
        # for now. also, fixing it would be hard ;).
        flagPtr = Marshal.AllocHGlobal(Marshal.SizeOf(Int32))
        addToCleanUp(lambda: Marshal.FreeHGlobal(flagPtr))
        mapper.RegisterData("Py_OptimizeFlag", flagPtr)

        self.assertEquals(CPyMarshal.ReadInt(flagPtr), 2)


suite = makesuite(
    PythonMapper_CreateDestroy_Test,
    PythonMapper_References_Test,
    PythonMapper_GetFuncPtr_NonApi_Test,
    PythonMapper_NoneTest,
    PythonMapper_NotImplementedTest,
    PythonMapper_Py_OptimizeFlag_Test,
)

if __name__ == '__main__':
    run(suite)
Ejemplo n.º 3
0
            mapper.Store('x'), mapper.Store('y'), mapper.Store('z'))
        self.assertEquals(mapper.Retrieve(slicePtr), slice('x', 'y', 'z'))

        slicePtr = mapper.PySlice_New(IntPtr.Zero, IntPtr.Zero, IntPtr.Zero)
        self.assertEquals(mapper.Retrieve(slicePtr), slice(None, None, None))


    @WithMapper
    def testCreateEllipsis(self, mapper, addToCleanUp):
        ellipsisTypePtr = Marshal.AllocHGlobal(Marshal.SizeOf(PyTypeObject))
        addToCleanUp(lambda: Marshal.FreeHGlobal(ellipsisTypePtr))

        ellipsisPtr = Marshal.AllocHGlobal(Marshal.SizeOf(PyObject))
        addToCleanUp(lambda: Marshal.FreeHGlobal(ellipsisPtr))

        mapper.RegisterData("PyEllipsis_Type", ellipsisTypePtr)
        mapper.RegisterData("_Py_EllipsisObject", ellipsisPtr)
        
        self.assertEquals(CPyMarshal.ReadPtrField(ellipsisPtr, PyObject, "ob_type"), mapper.PyEllipsis_Type)
        self.assertEquals(CPyMarshal.ReadIntField(ellipsisPtr, PyObject, "ob_refcnt"), 1)
        
        self.assertEquals(mapper.Store(Ellipsis), ellipsisPtr)
        self.assertEquals(mapper.RefCount(ellipsisPtr), 2)


suite = makesuite(
    SliceTest,
)
if __name__ == '__main__':
    run(suite)
Ejemplo n.º 4
0
        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)
            
        



suite = makesuite(MethodTest)
if __name__ == '__main__':
    run(suite)
Ejemplo n.º 5
0
        path = os.path.join(testDir, "test")

        class C(object):
            __str__ = lambda s: "str"
            __repr__ = lambda s: "repr"

        f = open(path, "w")
        try:
            fPtr = mapper.Store(f)
            cPtr = mapper.Store(C())
            self.assertEquals(mapper.IC_PyFile_WriteObject(cPtr, fPtr, 0), 0)
            self.assertEquals(mapper.IC_PyFile_WriteObject(cPtr, fPtr, 1), 0)
            mapper.DecRef(fPtr)
            self.assertEquals(mapper.IC_PyFile_WriteObject(cPtr, mapper.Store(object()), 0), -1)
            self.assertMapperHasError(mapper, TypeError)
        finally:
            f.close()

        f = open(path)
        try:
            result = f.read()
            self.assertEquals(result, "reprstr")
        finally:
            f.close()


suite = makesuite(PyFile_Type_Test, PyFileAPIFunctions)

if __name__ == "__main__":
    run(suite)
Ejemplo n.º 6
0
            map.CheckBridgePtrs(True)
            del obj1
            del obj2
            return map, ref1, ref2
        map, ref1, ref2 = do()
        gcwait()
        
        self.assertEquals(ref1.IsAlive, False, "failed to GC")
        self.assertEquals(ref2.IsAlive, False, "failed to GC")
    
    
    def testMapOverBridgePtrs(self):
        map, ptr1, obj1, _ = self.getVars()
        __, ptr2, obj2, ___ = self.getVars()
        map.BridgeAssociate(ptr1, obj1)
        map.BridgeAssociate(ptr2, obj2)
        
        ptrs = []
        def MapFunc(ptr):
            ptrs.append(ptr)
        map.MapOverBridgePtrs(PtrFunc(MapFunc))
        self.assertEquals(len(ptrs), 2)
        self.assertEquals(set(ptrs), set([ptr1, ptr2]))
        
        

suite = makesuite(InterestingPtrMapTest)
if __name__ == '__main__':
    run(suite)
Ejemplo n.º 7
0
        mapper.Dispose()
        deallocTypes()

    def test_PyObject_NewVar(self):
        allocs = []
        allocator = GetAllocatingTestAllocator(allocs, [])
        mapper = PythonMapper(allocator)
        deallocTypes = CreateTypes(mapper)

        typeObjSize = Marshal.SizeOf(PyTypeObject())
        typePtr = Marshal.AllocHGlobal(typeObjSize)
        CPyMarshal.Zero(typePtr, typeObjSize)
        CPyMarshal.WriteIntField(typePtr, PyTypeObject, "tp_basicsize", 31337)
        CPyMarshal.WriteIntField(typePtr, PyTypeObject, "tp_itemsize", 1337)

        del allocs[:]
        objPtr = mapper._PyObject_NewVar(typePtr, 123)
        self.assertEquals(allocs, [(objPtr, 31337 + (1337 * 123))])
        self.assertEquals(CPyMarshal.ReadPtrField(objPtr, PyObject, "ob_type"), typePtr)
        self.assertEquals(CPyMarshal.ReadIntField(objPtr, PyObject, "ob_refcnt"), 1)
        self.assertEquals(mapper.HasPtr(objPtr), False)

        mapper.Dispose()
        deallocTypes()


suite = makesuite(ObjectFunctionsTest, PyBaseObject_Type_Test, NewInitFunctionsTest)

if __name__ == "__main__":
    run(suite)
Ejemplo n.º 8
0
            wait()

            mapper.PyEval_RestoreThread(pe_token)  # 12
            signal()
            wait()

            mapper.PyGILState_Release(0)  # 14
            signal()
            wait()

            self.assertFalse(lock.TryAcquire())  # 16
            signal()

        t = Thread(ThreadStart(AnotherThread))
        t.Start()
        OneThread()
        t.Join()

        mapper.EnsureGIL()


suite = makesuite(
    PyThread_functions_Test,
    PyThreadExceptionTest,
    PyThreadStateTest,
    PyEvalGILThreadTest,
)

if __name__ == '__main__':
    run(suite)
            text = read(dst, '%s.Generated.cs' % name)
            self.assertNotEquals(text.find(expected), -1, "generated: >>>%s<<<" % text)
        
        assertFinds('CodeSnippets', EXPECTED_SNIPPETS)

        shutil.rmtree(src)
        shutil.rmtree(dst)


SNIPPETS_FILES = {
    'FOO.py': 'some random pile of code',
    'BAR.py': 'some random pile of code, with "double" quotes',
}

EXPECTED_SNIPPETS = """
namespace Ironclad
{
    internal class CodeSnippets
    {
        public const string BAR = @"some random pile of code, with ""double"" quotes";

        public const string FOO = @"some random pile of code";
    }
}
"""


suite = makesuite(GenerateCodeSnippetsTest)
if __name__ == '__main__':
    run(suite)
Ejemplo n.º 10
0
                    result = -1
                self.assertEquals(mapper.PySequence_Contains(mapper.Store(seq), mapper.Store(val)), result)
                self.assertMapperHasError(mapper, error)

    @WithMapper
    def testPySequence_Tuple_withTuple(self, mapper, _):
        tuplePtr = mapper.Store((1, 2, 3))
        self.assertEquals(mapper.PySequence_Tuple(tuplePtr), tuplePtr)
        self.assertEquals(mapper.RefCount(tuplePtr), 2)
        
    
    @WithMapper
    def testPySequence_Tuple_notTuple(self, mapper, _):
        listPtr = mapper.Store([4, 5, 6])
        tuplePtr = mapper.PySequence_Tuple(listPtr)
        self.assertEquals(mapper.Retrieve(tuplePtr), (4, 5, 6))
    
    
    @WithMapper
    def testPySequence_Tuple_notSequence(self, mapper, _):
        self.assertEquals(mapper.PySequence_Tuple(mapper.Store(123)), IntPtr.Zero)
        self.assertMapperHasError(mapper, TypeError)


suite = makesuite(
    SequenceFunctionsTest,
)

if __name__ == '__main__':
    run(suite)
Ejemplo n.º 11
0
extern _jumptable

section .code

global _FUNC2
global _FUNC3
global _FUNC4
_FUNC2:
    jmp [_jumptable+0]
_FUNC3:
    jmp [_jumptable+4]
_FUNC4:
    jmp [_jumptable+8]
"""

EXPECT_HEADER = """\
void FUNC3(void);
int FUNC4(void);
"""

EXPECT_OUTPUT = {
    'STUBINIT': EXPECT_STUBINIT,
    'HEADER': EXPECT_HEADER,
    'JUMPS': EXPECT_JUMPS,
}

suite = makesuite(StubGeneratorTest, )

if __name__ == '__main__':
    run(suite)
Ejemplo n.º 12
0
        mapper = PythonMapper(allocator)
        deallocTypes = CreateTypes(mapper)

        typeObjSize = Marshal.SizeOf(PyTypeObject)
        typePtr = Marshal.AllocHGlobal(typeObjSize)
        CPyMarshal.Zero(typePtr, typeObjSize)
        CPyMarshal.WriteIntField(typePtr, PyTypeObject, "tp_basicsize", 31337)
        CPyMarshal.WriteIntField(typePtr, PyTypeObject, "tp_itemsize", 1337)

        del allocs[:]
        objPtr = mapper._PyObject_NewVar(typePtr, 123)
        self.assertEquals(allocs, [(objPtr, 31337 + (1337 * 123))])
        self.assertEquals(CPyMarshal.ReadPtrField(objPtr, PyObject, 'ob_type'),
                          typePtr)
        self.assertEquals(
            CPyMarshal.ReadIntField(objPtr, PyObject, 'ob_refcnt'), 1)
        self.assertEquals(mapper.HasPtr(objPtr), False)

        mapper.Dispose()
        deallocTypes()


suite = makesuite(
    ObjectFunctionsTest,
    PyBaseObject_Type_Test,
    NewInitFunctionsTest,
)

if __name__ == '__main__':
    run(suite)
Ejemplo n.º 13
0
    def testPyIter_Next_ExplodingIterator(self, mapper, _):
        class BorkedException(Exception):
            pass

        def GetNext():
            raise BorkedException("Release the hounds!")

        explodingIterator = (GetNext() for _ in range(3))

        iterPtr = mapper.Store(explodingIterator)
        self.assertEquals(mapper.PyIter_Next(iterPtr), IntPtr.Zero,
                          "bad return")
        self.assertNotEquals(mapper.LastException, None,
                             "failed to set exception")

        def Raise():
            raise mapper.LastException

        try:
            Raise()
        except BorkedException, e:
            self.assertEquals(str(e), "Release the hounds!",
                              "unexpected message")
        else:
            self.fail("wrong exception")


suite = makesuite(IterationTest, )
if __name__ == '__main__':
    run(suite)
Ejemplo n.º 14
0
def GetFailedImportTestSuite(name, e):
    class FailedImportTest(unittest.TestCase):
        def testFailedImport(self):
            raise Exception("could not import %s:\n%s" % (name, e))
    return makesuite(FailedImportTest)
Ejemplo n.º 15
0
        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()
        # safe to call Dispose twice

    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 testLoadBuiltinModule(self):
        sr = StubReference(os.path.join("tests", "data", "fakepython.dll"))
        sr.LoadBuiltinModule(
            'somecrazymodule')  # if func not found and callable, error
        sr.Dispose()


suite = makesuite(StubReferenceTest)
if __name__ == '__main__':
    run(suite)
Ejemplo n.º 16
0
    def assertFindsType(self, name):
        class MyPM(PythonApi):
            def fillmethod(self, address):
                WritePyTypeObject(address)
        setattr(MyPM, "Register_" + name, getattr(MyPM, "fillmethod"))
        self.assertDataSetterSetsAndRemembers(MyPM, name, Marshal.SizeOf(PyTypeObject), TestWrotePyTypeObject)


    def testFindsTypes(self):
        for _type in TYPES:
            self.assertFindsType(_type)
        

    def testUninitialisedTypesAreNull(self):
        pa = PythonApi()
        for _type in TYPES:
            self.assertEquals(getattr(pa, _type), IntPtr.Zero, "unexpected")


    def testAddressGetterFailsCleanly(self):
        self.assertEquals(PythonApi().GetFuncPtr("_nonsenx%vQ#*7&"), IntPtr.Zero)


suite = makesuite(
    PythonApiTest,
)

if __name__ == '__main__':
    run(suite)
Ejemplo n.º 17
0
        public override void
        Register_PyBar_Type(IntPtr ptr)
        {
            CPyMarshal.Zero(ptr, Marshal.SizeOf(typeof(PyTypeObject)));
            CPyMarshal.WriteIntField(ptr, typeof(PyTypeObject), "ob_refcnt", 1);
            this.NumberSetupMethod(ptr);
            CPyMarshal.WritePtrField(ptr, typeof(PyTypeObject), "tp_init", this.GetFuncPtr("SomeOtherInitMethod"));
            string name = (string)Builtin.getattr(this.scratchContext, TypeCache.Bar, "__name__");
            CPyMarshal.WriteCStringField(ptr, typeof(PyTypeObject), "tp_name", name);
            this.map.Associate(ptr, TypeCache.Bar);
        }

        public override void
        Register_PyBaz_Type(IntPtr ptr)
        {
            CPyMarshal.Zero(ptr, Marshal.SizeOf(typeof(PyTypeObject)));
            CPyMarshal.WriteIntField(ptr, typeof(PyTypeObject), "ob_refcnt", 1);

            string name = (string)Builtin.getattr(this.scratchContext, TypeCache.Baz, "__name__");
            CPyMarshal.WriteCStringField(ptr, typeof(PyTypeObject), "tp_name", name);
            this.map.Associate(ptr, TypeCache.Baz);
        }
    }
}
"""


suite = makesuite(GenerateMapperTest)
if __name__ == '__main__':
    run(suite)
Ejemplo n.º 18
0
                    error = type(e)
                    result = -1
                self.assertEquals(
                    mapper.PySequence_Contains(mapper.Store(seq),
                                               mapper.Store(val)), result)
                self.assertMapperHasError(mapper, error)

    @WithMapper
    def testPySequence_Tuple_withTuple(self, mapper, _):
        tuplePtr = mapper.Store((1, 2, 3))
        self.assertEquals(mapper.PySequence_Tuple(tuplePtr), tuplePtr)
        self.assertEquals(mapper.RefCount(tuplePtr), 2)

    @WithMapper
    def testPySequence_Tuple_notTuple(self, mapper, _):
        listPtr = mapper.Store([4, 5, 6])
        tuplePtr = mapper.PySequence_Tuple(listPtr)
        self.assertEquals(mapper.Retrieve(tuplePtr), (4, 5, 6))

    @WithMapper
    def testPySequence_Tuple_notSequence(self, mapper, _):
        self.assertEquals(mapper.PySequence_Tuple(mapper.Store(123)),
                          IntPtr.Zero)
        self.assertMapperHasError(mapper, TypeError)


suite = makesuite(SequenceFunctionsTest, )

if __name__ == '__main__':
    run(suite)
Ejemplo n.º 19
0
            self.assertEquals(_int, int(value), "converted wrong")
            self.assertMapperHasError(mapper, None)
            mapper.DecRef(ptr)
        
        badvalues = ['foo', object, object(), "123.45"]
        for value in badvalues:
            ptr = mapper.Store(value)
            self.assertEquals(mapper.PyNumber_Int(ptr), IntPtr.Zero)
            
            error = None
            try:
                int(value)
            except Exception, e:
                error = type(e)
            self.assertMapperHasError(mapper, error)
            mapper.DecRef(ptr)
        
    

suite = makesuite(
    PyBool_Test,
    PyInt_Test,
    PyLong_Test,
    PyFloat_Test,
    PyComplex_Test,
    PyNumber_Test,
)

if __name__ == '__main__':
    run(suite)
Ejemplo n.º 20
0
        self.assertRaises(KeyError, allocator.Free, ptr3)

    def testRealloc(self):
        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

        i = 1
        while not DoTest(i):
            i = i + 1
            if i > 5:
                self.fail(
                    "failed to convince allocator to reallocate into a new block"
                )


suite = makesuite(HGlobalAllocatorTest)

if __name__ == '__main__':
    run(suite)
Ejemplo n.º 21
0
        sr.Dispose()
        self.assertEquals(Unmanaged.GetModuleHandle("python26.dll"), IntPtr.Zero,
                          "library not unmapped on dispose")

        sr.Dispose()
        # safe to call Dispose twice
        
        
    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 testLoadBuiltinModule(self):
        sr = StubReference(os.path.join("tests", "data", "fakepython.dll"))
        sr.LoadBuiltinModule('somecrazymodule') # if func not found and callable, error
        sr.Dispose()
        



suite = makesuite(StubReferenceTest)
if __name__ == '__main__':
    run(suite)

Ejemplo n.º 22
0
    @WithMapper
    def testPyRun_StringFlags_Locals(self, mapper, _):
        globals_ = {"foo": "bar"}
        locals_ = {"baz": "qux"}
        resultPtr = mapper.PyRun_StringFlags(
            "baz = 123\nqux = foo",
            int(EvalToken.Py_file_input),
            mapper.Store(globals_),
            mapper.Store(locals_),
            IntPtr.Zero,
        )
        self.assertEquals(resultPtr, mapper._Py_NoneStruct)
        self.assertEquals(globals_["foo"], "bar")
        self.assertEquals(locals_["baz"], 123)
        self.assertEquals(locals_["qux"], "bar")

    @WithMapper
    def testPyRun_StringFlags_Error(self, mapper, _):
        resultPtr = mapper.PyRun_StringFlags(
            "raise ValueError('amoral')", int(EvalToken.Py_file_input), mapper.Store({}), IntPtr.Zero, IntPtr.Zero
        )
        self.assertEquals(resultPtr, IntPtr.Zero)
        self.assertMapperHasError(mapper, ValueError)


suite = makesuite(ExecTest)

if __name__ == "__main__":
    run(suite)
Ejemplo n.º 23
0
def GetFailedImportTestSuite(name, e):
    class FailedImportTest(unittest.TestCase):
        def testFailedImport(self):
            raise Exception("could not import %s:\n%s" % (name, e))

    return makesuite(FailedImportTest)
        self.assertEquals(mapper.Retrieve(resultPtr), NotImplemented, "not mapped properly")



class PythonMapper_Py_OptimizeFlag_Test(TestCase):

    @WithMapper
    def testFills(self, mapper, addToCleanUp):
        # TODO: if we set a lower value, numpy will crash inside arr_add_docstring
        # I consider docstrings to be low-priority-enough that it's OK to fudge this
        # for now. also, fixing it would be hard ;).
        flagPtr = Marshal.AllocHGlobal(Marshal.SizeOf(Int32()))
        addToCleanUp(lambda: Marshal.FreeHGlobal(flagPtr))
        mapper.RegisterData("Py_OptimizeFlag", flagPtr)
        
        self.assertEquals(CPyMarshal.ReadInt(flagPtr), 2)



suite = makesuite(
    PythonMapper_CreateDestroy_Test,
    PythonMapper_References_Test,
    PythonMapper_GetFuncPtr_NonApi_Test,
    PythonMapper_NoneTest,
    PythonMapper_NotImplementedTest,
    PythonMapper_Py_OptimizeFlag_Test,
)

if __name__ == '__main__':
    run(suite)
Ejemplo n.º 25
0
        # check that it gets inherited (or not)
        mapper.Retrieve(basePtr)
        mapper.Retrieve(typePtr)
        CPyMarshal.WriteIntField(typePtr, PyTypeObject, "tp_flags", int(Py_TPFLAGS.HAVE_CLASS))
        # end rigmarole
        
        CPyMarshal.WritePtrField(typePtr, PyTypeObject, "tp_base", basePtr)
        for field in INHERIT_FIELDS + DONT_INHERIT_FIELDS:
            CPyMarshal.WritePtrField(typePtr, PyTypeObject, field, NO_VALUE)
            CPyMarshal.WritePtrField(basePtr, PyTypeObject, field, SOME_VALUE)
        
        mapper.PyType_Ready(typePtr)
        for field in INHERIT_FIELDS:
            self.assertEquals(CPyMarshal.ReadPtrField(typePtr, PyTypeObject, field), SOME_VALUE)
        for field in DONT_INHERIT_FIELDS:
            self.assertEquals(CPyMarshal.ReadPtrField(typePtr, PyTypeObject, field), NO_VALUE)
        


suite = makesuite(
    Types_Test,
    OldStyle_Test,
    PyType_GenericNew_Test,
    IC_PyType_New_Test,
    PyType_GenericAlloc_Test,
    PyType_Ready_InheritTest,
)

if __name__ == '__main__':
    run(suite)
Ejemplo n.º 26
0
    def testPyList_GetSlice_error(self, mapper, _):
        self.assertEquals(mapper.PyList_GetSlice(mapper.Store(object()), 1, 2), IntPtr.Zero)
        self.assertMapperHasError(mapper, TypeError)
        
        
    @WithMapper
    def testPyList_GetItem(self, mapper, _):
        listPtr = mapper.Store([1, 2, 3])
        for i in range(3):
            result = mapper.Retrieve(mapper.PyList_GetItem(listPtr, i))
            self.assertEquals(result, i + 1)
        
        notListPtr = mapper.Store(object())
        self.assertEquals(mapper.PyList_GetItem(notListPtr, 0), IntPtr.Zero)
        self.assertMapperHasError(mapper, TypeError)
    
    
    @WithMapper
    def testPyList_AsTuple(self, mapper, _):
        tPtr = mapper.PyList_AsTuple(mapper.Store([1, 2, 3]))
        self.assertEquals(mapper.Retrieve(tPtr), (1, 2, 3))
        
        
suite = makesuite(
    PyList_Type_Test,
    ListFunctionsTest,
)

if __name__ == '__main__':
    run(suite)
Ejemplo n.º 27
0
    
    @WithMapper
    def testPyEval_GetBuiltins(self, mapper, _):
        builtinsPtr = mapper.PyEval_GetBuiltins()
        import __builtin__
        self.assertEquals(mapper.Retrieve(builtinsPtr), __builtin__.__dict__)
        
        
        
class SysTest(TestCase):
    
    @WithMapper
    def testPySys_GetObject(self, mapper, _):
        modulesPtr = mapper.PySys_GetObject('modules')
        modules = mapper.Retrieve(modulesPtr)
        self.assertEquals(modules is sys.modules, True)


suite = makesuite(
    Py_InitModule4_SetupTest,
    Py_InitModule4_Test,
    PyModule_Functions_Test,
    ImportTest,
    NastyImportDetailsTest,
    BuiltinsTest, 
    SysTest, 
)

if __name__ == '__main__':
    run(suite)
Ejemplo n.º 28
0
    def testBigInteger(self):
        for cls in (NumberI, NumberL, NumberF):
            result = NumberMaker.MakeBigInteger(cls())
            self.assertEquals(isinstance(result, long), True)
            self.assertEquals(result, NUMBER_VALUE)

        self.assertRaises(TypeError, lambda: NumberMaker.MakeBigInteger(object()))

    def testUnsignedBigInteger(self):
        class NumberNeg(object):
            def __int__(self):
                return -1
        self.assertRaises(TypeError, lambda: NumberMaker.MakeUnsignedBigInteger(NumberNeg()))

        result = NumberMaker.MakeBigInteger(NumberF())
        self.assertEquals(isinstance(result, long), True)
        self.assertEquals(result, NUMBER_VALUE)

    def testFloat(self):
        for cls in (NumberI, NumberL, NumberF):
            result = NumberMaker.MakeFloat(cls())
            self.assertEquals(isinstance(result, float), True)
            self.assertEquals(result, NUMBER_VALUE)

        self.assertRaises(TypeError, lambda: NumberMaker.MakeFloat(object()))



suite = makesuite(NumberMakerTest)
if __name__ == '__main__':
    run(suite)
Ejemplo n.º 29
0
from tests.utils.runtest import makesuite, run
from tests.utils.testcase import TestCase

from tools.utils.dllreader import DllReader


class DllReaderTest(TestCase):

    def testInit(self):
        sm = DllReader('tests/data/exportsymbols.dll')
        self.assertEquals(sm.functions, ['Func', 'Funk', 'Jazz'])
        self.assertEquals(sm.data, ['Alphabetised', 'AnotherExportedSymbol', 'ExportedSymbol'])



suite = makesuite(
    DllReaderTest,
)

if __name__ == '__main__':
    run(suite)
Ejemplo n.º 30
0
            mapper.PyEval_RestoreThread(x)              # 10
            signal(); wait()
            
            mapper.PyEval_RestoreThread(pe_token)       # 12
            signal(); wait()
            
            mapper.PyGILState_Release(0)                # 14
            signal(); wait()
            
            self.assertFalse(lock.TryAcquire())         # 16
            signal()
            
        t = Thread(ThreadStart(AnotherThread))
        t.Start()
        OneThread()
        t.Join()
    
        mapper.EnsureGIL()



suite = makesuite(
    PyThread_functions_Test,
    PyThreadExceptionTest,
    PyThreadStateTest,
    PyEvalGILThreadTest,
)

if __name__ == '__main__':
    run(suite)
Ejemplo n.º 31
0
    
    def testPyMem_Free_NonNull(self):
        frees = []
        mapper = PythonMapper(GetAllocatingTestAllocator([], frees))
        
        ptr = mapper.PyMem_Malloc(123)
        mapper.PyMem_Free(ptr)
        self.assertEquals(frees, [ptr], "did not free")
        mapper.Dispose()
    

    def testPyMem_Free_Null(self):
        frees = []
        mapper = PythonMapper(GetAllocatingTestAllocator([], frees))
        
        mapper.PyMem_Free(IntPtr.Zero)
        self.assertEquals(frees, [], "freed inappropriately")
        mapper.Dispose()


suite = makesuite(
    GetMallocTest('PyMem_Malloc'),
    GetMallocTest('PyObject_Malloc'),
    GetReallocTest('PyMem_Malloc', 'PyMem_Realloc'),
    GetReallocTest('PyObject_Malloc', 'PyObject_Realloc'),
    PyMem_Free_Test, # PyObject_Free is a different matter
)

if __name__ == '__main__':
    run(suite)
Ejemplo n.º 32
0
class FunctionTest(TestCase):
    @WithMapper
    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)

    @WithMapper
    def testStoreType(self, mapper, _):
        self.assertEquals(mapper.Retrieve(mapper.PyFunction_Type),
                          FunctionType)
        self.assertEquals(
            CPyMarshal.ReadIntField(mapper.PyFunction_Type, PyTypeObject,
                                    'tp_basicsize'),
            Marshal.SizeOf(PyFunctionObject()))


suite = makesuite(FunctionTest)
if __name__ == '__main__':
    run(suite)
Ejemplo n.º 33
0
                type(_int) in (int, long), True, "returned inappropriate type")
            self.assertEquals(_int, int(value), "converted wrong")
            self.assertMapperHasError(mapper, None)
            mapper.DecRef(ptr)

        badvalues = ['foo', object, object(), "123.45"]
        for value in badvalues:
            ptr = mapper.Store(value)
            self.assertEquals(mapper.PyNumber_Int(ptr), IntPtr.Zero)

            error = None
            try:
                int(value)
            except Exception, e:
                error = type(e)
            self.assertMapperHasError(mapper, error)
            mapper.DecRef(ptr)


suite = makesuite(
    PyBool_Test,
    PyInt_Test,
    PyLong_Test,
    PyFloat_Test,
    PyComplex_Test,
    PyNumber_Test,
)

if __name__ == '__main__':
    run(suite)
Ejemplo n.º 34
0
        for notmapping in notmappings:
            ptr = mapper.Store(notmapping)
            self.assertEquals(mapper.PyMapping_Check(ptr), 0)
            self.assertEquals(mapper.LastException, None)
            mapper.DecRef(ptr)

    
    @WithMapper
    def testPyMapping_GetItemString(self, mapper, _):
        data = {'foo': 'bar'}
        class Mapping(object):
            def __getitem__(self, key):
                return data[key]
        
        mptr = mapper.Store(Mapping())
        fooresult = mapper.PyMapping_GetItemString(mptr, 'foo')
        self.assertEquals(mapper.Retrieve(fooresult), 'bar')
        
        self.assertEquals(mapper.PyMapping_GetItemString(mptr, 'baz'), IntPtr.Zero)
        self.assertMapperHasError(mapper, KeyError)
        
        


suite = makesuite(
    MappingTest,
)

if __name__ == '__main__':
    run(suite)
            text = read(dst, '%s.Generated.cs' % name)
            self.assertNotEquals(text.find(expected), -1,
                                 "generated: >>>%s<<<" % text)

        assertFinds('CodeSnippets', EXPECTED_SNIPPETS)

        shutil.rmtree(src)
        shutil.rmtree(dst)


SNIPPETS_FILES = {
    'FOO.py': 'some random pile of code',
    'BAR.py': 'some random pile of code, with "double" quotes',
}

EXPECTED_SNIPPETS = """
namespace Ironclad
{
    internal class CodeSnippets
    {
        public const string BAR = @"some random pile of code, with ""double"" quotes";

        public const string FOO = @"some random pile of code";
    }
}
"""

suite = makesuite(GenerateCodeSnippetsTest)
if __name__ == '__main__':
    run(suite)
Ejemplo n.º 36
0
        # check that it gets inherited (or not)
        mapper.Retrieve(basePtr)
        mapper.Retrieve(typePtr)
        CPyMarshal.WriteIntField(typePtr, PyTypeObject, "tp_flags", int(Py_TPFLAGS.HAVE_CLASS))
        # end rigmarole
        
        CPyMarshal.WritePtrField(typePtr, PyTypeObject, "tp_base", basePtr)
        for field in INHERIT_FIELDS + DONT_INHERIT_FIELDS:
            CPyMarshal.WritePtrField(typePtr, PyTypeObject, field, NO_VALUE)
            CPyMarshal.WritePtrField(basePtr, PyTypeObject, field, SOME_VALUE)
        
        mapper.PyType_Ready(typePtr)
        for field in INHERIT_FIELDS:
            self.assertEquals(CPyMarshal.ReadPtrField(typePtr, PyTypeObject, field), SOME_VALUE)
        for field in DONT_INHERIT_FIELDS:
            self.assertEquals(CPyMarshal.ReadPtrField(typePtr, PyTypeObject, field), NO_VALUE)
        


suite = makesuite(
    Types_Test,
    OldStyle_Test,
    PyType_GenericNew_Test,
    IC_PyType_New_Test,
    PyType_GenericAlloc_Test,
    PyType_Ready_InheritTest,
)

if __name__ == '__main__':
    run(suite)
Ejemplo n.º 37
0

    def testMultiThreaded(self):
        lock = Lock()
        
        def TestCanAcquire():
            self.assertEquals(lock.Acquire(), 1)
            self.assertEquals(lock.IsAcquired, True)
            lock.Release()
        t = Thread(ThreadStart(TestCanAcquire))
        t.Start()
        t.Join()
        
        lock.Acquire()
        
        def TestCannotAcquire():
            self.assertEquals(lock.TryAcquire(), False)
            self.assertEquals(lock.IsAcquired, False)
        t = Thread(ThreadStart(TestCannotAcquire))
        t.Start()
        t.Join()
        
        lock.Release()
        

suite = makesuite(
    LockTest,
)
if __name__ == '__main__':
    run(suite)
Ejemplo n.º 38
0
        valuePtrPtr = CPyMarshal.Offset(keyPtrPtr, CPyMarshal.PtrSize)
        addDealloc(lambda: Marshal.FreeHGlobal(posPtr))
        CPyMarshal.WriteInt(posPtr, 0)

        d = dict(a=1)
        dPtr = mapper.Store(d)
        mapper.EnsureGIL()
        mapper.PyDict_Next(dPtr, posPtr, keyPtrPtr, valuePtrPtr)

        keyPtr = CPyMarshal.ReadPtr(keyPtrPtr)
        valuePtr = CPyMarshal.ReadPtr(valuePtrPtr)

        # grab extra references to retard spoilage
        mapper.IncRef(keyPtr)
        mapper.IncRef(valuePtr)

        mapper.ReleaseGIL()

        # check refcount has dropped back to 1
        self.assertEquals(mapper.RefCount(keyPtr), 1)
        self.assertEquals(mapper.RefCount(valuePtr), 1)


suite = makesuite(
    DictTest,
    PyDict_Next_Test,
)

if __name__ == '__main__':
    run(suite)
Ejemplo n.º 39
0
        
        d = dict(a=1)
        dPtr = mapper.Store(d)
        mapper.EnsureGIL()
        mapper.PyDict_Next(dPtr, posPtr, keyPtrPtr, valuePtrPtr)
        
        keyPtr = CPyMarshal.ReadPtr(keyPtrPtr)
        valuePtr = CPyMarshal.ReadPtr(valuePtrPtr)
        
        # grab extra references to retard spoilage
        mapper.IncRef(keyPtr)
        mapper.IncRef(valuePtr)
        
        mapper.ReleaseGIL()
        
        # check refcount has dropped back to 1
        self.assertEquals(mapper.RefCount(keyPtr), 1)
        self.assertEquals(mapper.RefCount(valuePtr), 1)





suite = makesuite(
    DictTest,
    PyDict_Next_Test,
)

if __name__ == '__main__':
    run(suite)
Ejemplo n.º 40
0
                                              TestWrotePtr)

    def assertFindsType(self, name):
        class MyPM(PythonApi):
            def fillmethod(self, address):
                WritePyTypeObject(address)

        setattr(MyPM, "Register_" + name, getattr(MyPM, "fillmethod"))
        self.assertDataSetterSetsAndRemembers(MyPM, name,
                                              Marshal.SizeOf(PyTypeObject()),
                                              TestWrotePyTypeObject)

    def testFindsTypes(self):
        for _type in TYPES:
            self.assertFindsType(_type)

    def testUninitialisedTypesAreNull(self):
        pa = PythonApi()
        for _type in TYPES:
            self.assertEquals(getattr(pa, _type), IntPtr.Zero, "unexpected")

    def testAddressGetterFailsCleanly(self):
        self.assertEquals(PythonApi().GetFuncPtr("_nonsenx%vQ#*7&"),
                          IntPtr.Zero)


suite = makesuite(PythonApiTest, )

if __name__ == '__main__':
    run(suite)
Ejemplo n.º 41
0
    @WithMapper
    def testPyTuple_GetSlice(self, mapper, _):
        def TestSlice(originalTuplePtr, start, stop):
            newTuplePtr = mapper.PyTuple_GetSlice(originalTuplePtr, start, stop)
            self.assertMapperHasError(mapper, None)
            self.assertEquals(mapper.Retrieve(newTuplePtr), mapper.Retrieve(originalTuplePtr)[start:stop], "bad slice")
            mapper.DecRef(newTuplePtr)
        
        tuplePtr = mapper.Store((0, 1, 2, 3, 4, 5, 6, 7))
        slices = (
            (3, 4), (5, 0), (5, 200), (999, 1000)
        )
        for (start, stop) in slices:
            TestSlice(tuplePtr, start, stop)
            
         
    @WithMapper
    def testPyTuple_GetSlice_error(self, mapper, _):
        self.assertEquals(mapper.PyTuple_GetSlice(mapper.Store(object()), 1, 2), IntPtr.Zero)
        self.assertMapperHasError(mapper, TypeError)



suite = makesuite(
    PyTuple_Type_Test,
    TupleTest,
)

if __name__ == '__main__':
    run(suite)
Ejemplo n.º 42
0
        return 0L

    def __float__(self):
        return 0.0001


class BugTest(TestCase):
    def testDisplayhook(self):
        self.assertEquals(
            hasattr(sys, '__displayhook__'), False,
            "ironclad.py and PythonMapper.MessWithSys may no longer need to set sys.__displayhook__ = sys.displayhook"
        )

    def testLogWorksNow(self):
        math.log(Number())
        math.log10(Number())

    def testUIntLen(self):
        class C(object):
            def __len__(self):
                return UInt32(123)

        self.assertEquals(
            len(C()), 123,
            "uint len bug is back (are you using ipy 2.0 instead of 2.0.1?)")


suite = makesuite(BugTest)
if __name__ == '__main__':
    run(suite)
Ejemplo n.º 43
0
        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)


suite = makesuite(MethodTest)
if __name__ == '__main__':
    run(suite)
Ejemplo n.º 44
0
        self.assertEquals(lock.CountAcquired, 0)

    def testMultiThreaded(self):
        lock = Lock()

        def TestCanAcquire():
            self.assertEquals(lock.Acquire(), 1)
            self.assertEquals(lock.IsAcquired, True)
            lock.Release()

        t = Thread(ThreadStart(TestCanAcquire))
        t.Start()
        t.Join()

        lock.Acquire()

        def TestCannotAcquire():
            self.assertEquals(lock.TryAcquire(), False)
            self.assertEquals(lock.IsAcquired, False)

        t = Thread(ThreadStart(TestCannotAcquire))
        t.Start()
        t.Join()

        lock.Release()


suite = makesuite(LockTest)
if __name__ == "__main__":
    run(suite)
Ejemplo n.º 45
0
class PyMem_Free_Test(TestCase):
    def testPyMem_Free_NonNull(self):
        frees = []
        mapper = PythonMapper(GetAllocatingTestAllocator([], frees))

        ptr = mapper.PyMem_Malloc(123)
        mapper.PyMem_Free(ptr)
        self.assertEquals(frees, [ptr], "did not free")
        mapper.Dispose()

    def testPyMem_Free_Null(self):
        frees = []
        mapper = PythonMapper(GetAllocatingTestAllocator([], frees))

        mapper.PyMem_Free(IntPtr.Zero)
        self.assertEquals(frees, [], "freed inappropriately")
        mapper.Dispose()


suite = makesuite(
    GetMallocTest('PyMem_Malloc'),
    GetMallocTest('PyObject_Malloc'),
    GetReallocTest('PyMem_Malloc', 'PyMem_Realloc'),
    GetReallocTest('PyObject_Malloc', 'PyObject_Realloc'),
    PyMem_Free_Test,  # PyObject_Free is a different matter
)

if __name__ == '__main__':
    run(suite)
Ejemplo n.º 46
0
    def testWriteByte(self):
        data = Marshal.AllocHGlobal(1)
        
        CPyMarshal.WriteByte(data, 0)
        self.assertEquals(Marshal.ReadByte(data), 0, "wrong")

        CPyMarshal.WriteByte(data, 255)
        self.assertEquals(Marshal.ReadByte(data), 255, "wrong")
        
        Marshal.FreeHGlobal(data)


    def testReadByte(self):
        data = Marshal.AllocHGlobal(1)
        
        Marshal.WriteByte(data, 0)
        self.assertEquals(CPyMarshal.ReadByte(data), 0, "wrong")

        Marshal.WriteByte(data, 255)
        self.assertEquals(CPyMarshal.ReadByte(data), 255, "wrong")
        
        Marshal.FreeHGlobal(data)




suite = makesuite(CPyMarshalTest_32)
if __name__ == '__main__':
    run(suite)

Ejemplo n.º 47
0
            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()
        # safe to call twice
    
    
    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")
    

suite = makesuite(PydImporterTest)
if __name__ == '__main__':
    run(suite)
Ejemplo n.º 48
0
import sys
import math
from tests.utils.runtest import makesuite, run
from tests.utils.testcase import TestCase

from System import UInt32

class Number(object):
    def __long__(self):
        return 0L
    def __float__(self):
        return 0.0001

class BugTest(TestCase):

    def testLogWorksNow(self):
        math.log(Number())
        math.log10(Number())

    def testUIntLen(self):
        class C(object):
            def __len__(self):
                return UInt32(123)
        self.assertEquals(len(C()), 123, "uint len bug is back (are you using ipy 2.0 instead of 2.0.1?)")

suite = makesuite(BugTest)
if __name__ == '__main__':
    run(suite)
Ejemplo n.º 49
0
from Ironclad.Structs import PyFunctionObject, PyTypeObject

attrs = (
    'func_code', 'func_globals', 'func_defaults', 'func_closure', 'func_doc',
    'func_name', 'func_dict', 'func_weakreflist', 'func_module'
)

class FunctionTest(TestCase):

    @WithMapper
    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 = Marshal.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)
    
    @WithMapper
    def testStoreType(self, mapper, _):
        self.assertEquals(mapper.Retrieve(mapper.PyFunction_Type), FunctionType)
        self.assertEquals(CPyMarshal.ReadIntField(mapper.PyFunction_Type, PyTypeObject, 'tp_basicsize'), Marshal.SizeOf(PyFunctionObject))

suite = makesuite(FunctionTest)
if __name__ == '__main__':
    run(suite)
Ejemplo n.º 50
0
from tests.utils.runtest import makesuite, run
from tests.utils.testcase import TestCase

from tools.utils.dllreader import DllReader


class DllReaderTest(TestCase):

    def testInit(self):
        sm = DllReader('tests/data/exportsymbols.dll')
        self.assertEquals(sm.functions, ['Func', 'Funk', 'Jazz'])
        self.assertEquals(sm.data, ['Alphabetised', 'AnotherExportedSymbol', 'ExportedSymbol'])



suite = makesuite(
    DllReaderTest,
)

if __name__ == '__main__':
    run(suite)
Ejemplo n.º 51
0
        self.assertRaises(KeyError, allocator.Free, ptr3)
        
        
    def testRealloc(self):
        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
        
        i = 1
        while not DoTest(i):
            i = i + 1
            if i > 5:
                self.fail("failed to convince allocator to reallocate into a new block")
        
    

suite = makesuite(HGlobalAllocatorTest)

if __name__ == '__main__':
    run(suite)
Ejemplo n.º 52
0
                                      mapper.Store('z'))
        self.assertEquals(mapper.Retrieve(slicePtr), slice('x', 'y', 'z'))

        slicePtr = mapper.PySlice_New(IntPtr.Zero, IntPtr.Zero, IntPtr.Zero)
        self.assertEquals(mapper.Retrieve(slicePtr), slice(None, None, None))

    @WithMapper
    def testCreateEllipsis(self, mapper, addToCleanUp):
        ellipsisTypePtr = Marshal.AllocHGlobal(Marshal.SizeOf(PyTypeObject))
        addToCleanUp(lambda: Marshal.FreeHGlobal(ellipsisTypePtr))

        ellipsisPtr = Marshal.AllocHGlobal(Marshal.SizeOf(PyObject))
        addToCleanUp(lambda: Marshal.FreeHGlobal(ellipsisPtr))

        mapper.RegisterData("PyEllipsis_Type", ellipsisTypePtr)
        mapper.RegisterData("_Py_EllipsisObject", ellipsisPtr)

        self.assertEquals(
            CPyMarshal.ReadPtrField(ellipsisPtr, PyObject, "ob_type"),
            mapper.PyEllipsis_Type)
        self.assertEquals(
            CPyMarshal.ReadIntField(ellipsisPtr, PyObject, "ob_refcnt"), 1)

        self.assertEquals(mapper.Store(Ellipsis), ellipsisPtr)
        self.assertEquals(mapper.RefCount(ellipsisPtr), 2)


suite = makesuite(SliceTest, )
if __name__ == '__main__':
    run(suite)
Ejemplo n.º 53
0
        self.assertEquals(mapper.PyIter_Next(notIterPtr), IntPtr.Zero, "bad return")
        self.assertMapperHasError(mapper, TypeError)


    @WithMapper
    def testPyIter_Next_ExplodingIterator(self, mapper, _):
        class BorkedException(Exception):
            pass
        def GetNext():
            raise BorkedException("Release the hounds!")
        explodingIterator = (GetNext() for _ in range(3))
        
        iterPtr = mapper.Store(explodingIterator)
        self.assertEquals(mapper.PyIter_Next(iterPtr), IntPtr.Zero, "bad return")
        self.assertNotEquals(mapper.LastException, None, "failed to set exception")
        
        def Raise():
            raise mapper.LastException
        try:
            Raise()
        except BorkedException, e:
            self.assertEquals(str(e), "Release the hounds!", "unexpected message")
        else:
            self.fail("wrong exception")

suite = makesuite(
    IterationTest,
)
if __name__ == '__main__':
    run(suite)
Ejemplo n.º 54
0
        f = open(path, 'w')
        try:
            fPtr = mapper.Store(f)
            cPtr = mapper.Store(C())
            self.assertEquals(mapper.IC_PyFile_WriteObject(cPtr, fPtr, 0), 0)
            self.assertEquals(mapper.IC_PyFile_WriteObject(cPtr, fPtr, 1), 0)
            mapper.DecRef(fPtr)
            self.assertEquals(
                mapper.IC_PyFile_WriteObject(cPtr, mapper.Store(object()), 0),
                -1)
            self.assertMapperHasError(mapper, TypeError)
        finally:
            f.close()

        f = open(path)
        try:
            result = f.read()
            self.assertEquals(result, 'reprstr')
        finally:
            f.close()


suite = makesuite(
    PyFile_Type_Test,
    PyFileAPIFunctions,
)

if __name__ == '__main__':
    run(suite)