[Zope-CVS] CVS: PythonNet/tests/python - leakcheck.py:1.1 stresstest.py:1.1 profile.py:1.2 runtests.py:1.4 test_array.py:1.4 test_class.py:1.7 test_conversion.py:1.5 test_event.py:1.4 test_field.py:1.3 test_module.py:1.3

Brian Lloyd brian at zope.com
Mon Oct 20 23:05:17 EDT 2003


Update of /cvs-repository/PythonNet/tests/python
In directory cvs.zope.org:/tmp/cvs-serv26410/tests/python

Modified Files:
	profile.py runtests.py test_array.py test_class.py 
	test_conversion.py test_event.py test_field.py test_module.py 
Added Files:
	leakcheck.py stresstest.py 
Log Message:
Check in the Great Type Refactoring before I go and break it again. All of 
the tests pass again (well, except for two that were failing before the 
refactoring).

The end result is a nice net reduction of C# code, letting the Python 
runtime handle much more of the work of creating types. 

The refactored version also plays nicely with the Python GC, so the 
Python GC no longer needs to be disabled to run things under Python 
for .NET. :) Also added some very preliminary leak and stress testing 
scripts.



=== Added File PythonNet/tests/python/leakcheck.py ===
"""A utility script to help in basic leak checking."""


def main():

    import sys, gc
    #gc.set_debug(gc.DEBUG_LEAK)

    module = None
    for i in range(50):
        for name in (
            'test_module',
            'test_conversion',
            'test_class',
            'test_interface',
            'test_enum',
            'test_field',
            'test_property',
            'test_indexer',
            'test_event',
            'test_method',
            'test_delegate',
            'test_array',
            ):
            module = __import__(name)
            module.main()

    module = None
    gc.collect()

    for item in gc.get_objects():
        print item, sys.getrefcount(item)



if __name__ == '__main__':
    main()




=== Added File PythonNet/tests/python/stresstest.py ===
"""Basic stress test."""

def main():


    import time
    start = time.clock()

    for i in range(20000):
        print i
        for name in (
            'test_module',
            'test_conversion',
            'test_class',
            'test_interface',
            'test_enum',
            'test_field',
            'test_property',
            'test_indexer',
            'test_event',
            'test_method',
            'test_delegate',
            'test_array',
            ):
            module = __import__(name)
            module.main()

    import pdb; pdb.set_trace()

    stop = time.clock()
    took = str(stop - start)
    print 'Total Time: %s' % took



if __name__ == '__main__':
    main()




=== PythonNet/tests/python/profile.py 1.1 => 1.2 ===
--- PythonNet/tests/python/profile.py:1.1	Tue Sep 30 19:54:13 2003
+++ PythonNet/tests/python/profile.py	Mon Oct 20 23:05:16 2003
@@ -3,10 +3,13 @@
 
 def main():
 
+    import sys, gc
+    gc.set_debug(gc.DEBUG_LEAK)
+
     import time
     start = time.clock()
 
-    for i in range(200):
+    for i in range(50):
         for name in (
             'test_module',
             'test_conversion',
@@ -27,6 +30,11 @@
     stop = time.clock()
     took = str(stop - start)
     print 'Total Time: %s' % took
+
+
+    for item in gc.get_objects():
+        print item, sys.getrefcount(item)
+
 
 
 if __name__ == '__main__':


=== PythonNet/tests/python/runtests.py 1.3 => 1.4 ===
--- PythonNet/tests/python/runtests.py:1.3	Mon Jul 28 22:28:26 2003
+++ PythonNet/tests/python/runtests.py	Mon Oct 20 23:05:16 2003
@@ -1,5 +1,6 @@
 """Run all of the unit tests for this package."""
 
+
 def main():
     import time
 


=== PythonNet/tests/python/test_array.py 1.3 => 1.4 ===
--- PythonNet/tests/python/test_array.py:1.3	Tue Oct  7 22:29:18 2003
+++ PythonNet/tests/python/test_array.py	Mon Oct 20 23:05:16 2003
@@ -1059,6 +1059,236 @@
             raise TypeError, 'iteration over empty array'
 
 
+    def testTupleArrayConversion(self):
+        """Test conversion of tuples to array arguments."""
+        from CLR.Python.Test import ArrayConversionTest
+        from CLR.Python.Test import Spam
+
+        items = []
+        for i in range(10):
+            items.append(Spam(str(i)))
+        items = tuple(items)
+
+        result = ArrayConversionTest.EchoRange(items)
+        self.failUnless(result[0].__class__ == Spam)
+        self.failUnless(len(result) == 10)
+
+
+    def testTupleNestedArrayConversion(self):
+        """Test conversion of tuples to array-of-array arguments."""
+        from CLR.Python.Test import ArrayConversionTest
+        from CLR.Python.Test import Spam
+
+        items = []
+        for i in range(10):
+            subs = []
+            for n in range(10):
+                subs.append(Spam(str(i)))
+            items.append(tuple(subs))
+        items = tuple(items)
+        
+        result = ArrayConversionTest.EchoRangeAA(items)
+        
+        self.failUnless(len(result) == 10)
+        self.failUnless(len(result[0]) == 10)
+        self.failUnless(result[0][0].__class__ == Spam)
+
+
+    def testListArrayConversion(self):
+        """Test conversion of lists to array arguments."""
+        from CLR.Python.Test import ArrayConversionTest
+        from CLR.Python.Test import Spam
+
+        items = []
+        for i in range(10):
+            items.append(Spam(str(i)))
+
+        result = ArrayConversionTest.EchoRange(items)
+        self.failUnless(result[0].__class__ == Spam)
+        self.failUnless(len(result) == 10)
+
+
+    def testListNestedArrayConversion(self):
+        """Test conversion of lists to array-of-array arguments."""
+        from CLR.Python.Test import ArrayConversionTest
+        from CLR.Python.Test import Spam
+
+        items = []
+        for i in range(10):
+            subs = []
+            for n in range(10):
+                subs.append(Spam(str(i)))
+            items.append(subs)
+        
+        result = ArrayConversionTest.EchoRangeAA(items)
+        
+        self.failUnless(len(result) == 10)
+        self.failUnless(len(result[0]) == 10)
+        self.failUnless(result[0][0].__class__ == Spam)
+
+
+    def testSequenceArrayConversion(self):
+        """Test conversion of sequence-like objects to array arguments."""
+        from CLR.Python.Test import ArrayConversionTest
+        from CLR.Python.Test import Spam
+        from UserList import UserList
+
+        items = UserList()
+        for i in range(10):
+            items.append(Spam(str(i)))
+
+        result = ArrayConversionTest.EchoRange(items)
+        self.failUnless(result[0].__class__ == Spam)
+        self.failUnless(len(result) == 10)
+
+
+    def testSequenceNestedArrayConversion(self):
+        """Test conversion of sequences to array-of-array arguments."""
+        from CLR.Python.Test import ArrayConversionTest
+        from CLR.Python.Test import Spam
+        from UserList import UserList
+
+        items = UserList()
+        for i in range(10):
+            subs = UserList()
+            for n in range(10):
+                subs.append(Spam(str(i)))
+            items.append(subs)
+        
+        result = ArrayConversionTest.EchoRangeAA(items)
+        
+        self.failUnless(len(result) == 10)
+        self.failUnless(len(result[0]) == 10)
+        self.failUnless(result[0][0].__class__ == Spam)
+
+
+    def testTupleArrayConversionTypeChecking(self):
+        """Test error handling for tuple conversion to array arguments."""
+        from CLR.Python.Test import ArrayConversionTest
+        from CLR.Python.Test import Spam
+
+        # This should work, because null / None is a valid value in an
+        # array of reference types.
+
+        items = []
+        for i in range(10):
+            items.append(Spam(str(i)))
+        items[1] = None
+        items = tuple(items)
+
+        result = ArrayConversionTest.EchoRange(items)
+
+        self.failUnless(result[0].__class__ == Spam)
+        self.failUnless(result[1] == None)
+        self.failUnless(len(result) == 10)
+
+        def test(items = items):
+            temp = list(items)
+            temp[1] = 1
+
+            result = ArrayConversionTest.EchoRange(tuple(temp))
+
+        self.failUnlessRaises(TypeError, test) 
+
+        def test(items = items):
+            temp = list(items)
+            temp[1] = "spam"
+
+            result = ArrayConversionTest.EchoRange(tuple(temp))
+
+        self.failUnlessRaises(TypeError, test) 
+
+
+    def testListArrayConversionTypeChecking(self):
+        """Test error handling for list conversion to array arguments."""
+        from CLR.Python.Test import ArrayConversionTest
+        from CLR.Python.Test import Spam
+
+        # This should work, because null / None is a valid value in an
+        # array of reference types.
+
+        items = []
+        for i in range(10):
+            items.append(Spam(str(i)))
+        items[1] = None
+
+        result = ArrayConversionTest.EchoRange(items)
+
+        self.failUnless(result[0].__class__ == Spam)
+        self.failUnless(result[1] == None)
+        self.failUnless(len(result) == 10)
+
+        def test(items = items):
+            items[1] = 1
+            result = ArrayConversionTest.EchoRange(items)
+
+        self.failUnlessRaises(TypeError, test) 
+
+        def test(items = items):
+            items[1] = "spam"
+            result = ArrayConversionTest.EchoRange(items)
+
+        self.failUnlessRaises(TypeError, test) 
+
+
+    def testSequenceArrayConversionTypeChecking(self):
+        """Test error handling for sequence conversion to array arguments."""
+        from CLR.Python.Test import ArrayConversionTest
+        from CLR.Python.Test import Spam
+        from UserList import UserList
+
+        # This should work, because null / None is a valid value in an
+        # array of reference types.
+
+        items = UserList()
+        for i in range(10):
+            items.append(Spam(str(i)))
+        items[1] = None
+
+        result = ArrayConversionTest.EchoRange(items)
+
+        self.failUnless(result[0].__class__ == Spam)
+        self.failUnless(result[1] == None)
+        self.failUnless(len(result) == 10)
+
+        def test(items = items):
+            items[1] = 1
+            result = ArrayConversionTest.EchoRange(items)
+
+        self.failUnlessRaises(TypeError, test) 
+
+        def test(items = items):
+            items[1] = "spam"
+            result = ArrayConversionTest.EchoRange(items)
+
+        self.failUnlessRaises(TypeError, test) 
+
+
+    def testMDArrayConversion(self):
+        """Test passing of multi-dimensional array arguments."""
+        from CLR.Python.Test import ArrayConversionTest
+        from CLR.Python.Test import Spam
+        from CLR.System import Array
+
+        # Currently, the runtime does not support automagic conversion of
+        # Python sequences to true multi-dimensional arrays (though it
+        # does support arrays-of-arrays). This test exists mostly as an
+        # example of how a multi-dimensional array can be created and used
+        # with managed code from Python.
+
+        items = Array.CreateInstance(Spam, 5, 5)
+
+        for i in range(5):
+            for n in range(5):
+                items.SetValue(Spam(str((i, n))), (i, n))
+        
+        result = ArrayConversionTest.EchoRangeMD(items)
+        
+        self.failUnless(len(result) == 25)
+        self.failUnless(result[0, 0].__class__ == Spam)
+        self.failUnless(result[0, 0].__class__ == Spam)
+
+
     def testArrayAbuse(self):
         """Test array abuse."""
         _class = Test.PublicArrayTest


=== PythonNet/tests/python/test_class.py 1.6 => 1.7 ===
--- PythonNet/tests/python/test_class.py:1.6	Tue Oct  7 22:29:18 2003
+++ PythonNet/tests/python/test_class.py	Mon Oct 20 23:05:16 2003
@@ -76,6 +76,8 @@
         self.failUnless(table.howMany() == 1)
         self.failUnless(table.Count == 1)
 
+        MyTable = None
+
     # test strange __new__ interactions
     
     # test weird metatype


=== PythonNet/tests/python/test_conversion.py 1.4 => 1.5 ===
--- PythonNet/tests/python/test_conversion.py:1.4	Tue Oct  7 22:29:18 2003
+++ PythonNet/tests/python/test_conversion.py	Mon Oct 20 23:05:16 2003
@@ -663,15 +663,15 @@
 
     def testInterfaceConversion(self):
         """Test interface conversion."""
-        from CLR.Python.Test import Spam
+        from CLR.Python.Test import Spam, ISpam
 
         object = ConversionTest()
 
-        self.failUnless(object.SpamField.__class__.__name__ == "ISpam")
+        self.failUnless(ISpam(object.SpamField).GetValue() == "spam")
         self.failUnless(object.SpamField.GetValue() == "spam")
         
         object.SpamField = Spam("eggs")
-        self.failUnless(object.SpamField.__class__.__name__ == "ISpam")
+        self.failUnless(ISpam(object.SpamField).GetValue() == "eggs")
         self.failUnless(object.SpamField.GetValue() == "eggs")
 
         # need to test spam subclass here.


=== PythonNet/tests/python/test_event.py 1.3 => 1.4 ===
--- PythonNet/tests/python/test_event.py:1.3	Tue Oct  7 22:29:18 2003
+++ PythonNet/tests/python/test_event.py	Mon Oct 20 23:05:16 2003
@@ -266,6 +266,7 @@
         """Test static method handlers."""
         object = EventTest()
         handler = StaticMethodHandler()
+        StaticMethodHandler.value = None
 
         object.PublicEvent += handler.handler
         self.failUnless(handler.value == None)
@@ -284,6 +285,7 @@
         """Test class method handlers."""
         object = EventTest()
         handler = ClassMethodHandler()
+        ClassMethodHandler.value = None
 
         object.PublicEvent += handler.handler
         self.failUnless(handler.value == None)
@@ -318,6 +320,7 @@
     def testManagedStaticMethodHandler(self):
         """Test managed static method handlers."""
         object = EventTest()
+        EventTest.s_value = 0
 
         object.PublicEvent += object.StaticHandler
         self.failUnless(EventTest.s_value == 0)


=== PythonNet/tests/python/test_field.py 1.2 => 1.3 ===
--- PythonNet/tests/python/test_field.py:1.2	Tue Oct  7 22:29:18 2003
+++ PythonNet/tests/python/test_field.py	Mon Oct 20 23:05:16 2003
@@ -383,15 +383,15 @@
 
     def testInterfaceField(self):
         """Test interface fields."""
-        from CLR.Python.Test import Spam
+        from CLR.Python.Test import Spam, ISpam
 
         object = FieldTest()
         
-        self.failUnless(object.SpamField.__class__.__name__ == "ISpam")
+        self.failUnless(ISpam(object.SpamField).GetValue() == "spam")
         self.failUnless(object.SpamField.GetValue() == "spam")
 
         object.SpamField = Spam("eggs")
-        self.failUnless(object.SpamField.__class__.__name__ == "ISpam")
+        self.failUnless(ISpam(object.SpamField).GetValue() == "eggs")
         self.failUnless(object.SpamField.GetValue() == "eggs")
 
 


=== PythonNet/tests/python/test_module.py 1.2 => 1.3 ===
--- PythonNet/tests/python/test_module.py:1.2	Tue Oct  7 22:29:18 2003
+++ PythonNet/tests/python/test_module.py	Mon Oct 20 23:05:16 2003
@@ -20,7 +20,7 @@
 
 
     def isCLRClass(self, object):
-        return type(object).__name__ == 'MetaType' # for now
+        return type(object).__name__ == 'CLR Metatype' # for now
 
 
     def testModuleInterface(self):




More information about the Zope-CVS mailing list