testPippo.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import sys
  2. import unittest
  3. import pythoncom
  4. from win32com.client import Dispatch
  5. from win32com.client.gencache import EnsureDispatch
  6. class PippoTester(unittest.TestCase):
  7. def setUp(self):
  8. from win32com.test.util import RegisterPythonServer
  9. from win32com.test import pippo_server
  10. RegisterPythonServer(pippo_server.__file__, "Python.Test.Pippo")
  11. # create it.
  12. self.object = Dispatch("Python.Test.Pippo")
  13. def testLeaks(self):
  14. try:
  15. gtrc = sys.gettotalrefcount
  16. except AttributeError:
  17. print("Please run this with python_d for leak tests")
  18. gtrc = lambda: 0
  19. # note creating self.object() should have consumed our "one time" leaks
  20. self.object.Method1()
  21. start = gtrc()
  22. for i in range(1000):
  23. object = Dispatch("Python.Test.Pippo")
  24. object.Method1()
  25. object = None
  26. end = gtrc()
  27. if end-start > 5:
  28. self.fail("We lost %d references!" % (end-start,))
  29. def testResults(self):
  30. rc, out1 = self.object.Method2(123, 111)
  31. self.assertEqual(rc, 123)
  32. self.assertEqual(out1, 222)
  33. def testPythonArrays(self):
  34. self._testArray([-3, -2, -1, 0, 1, 2, 3])
  35. self._testArray([-3.14, -2, -.1, 0., 1.1, 2.5, 3])
  36. def testNumpyArrays(self):
  37. try:
  38. import numpy
  39. except:
  40. print("Numpy test not possible because numpy module failed to import")
  41. return
  42. self._testArray(numpy.array([-3, -2, -1, 0, 1, 2, 3]))
  43. self._testArray(numpy.array([-3.14, -2, -.1, 0., 1.1, 2.5, 3]))
  44. def testByteArrays(self):
  45. if 'bytes' in dir(__builtins__):
  46. # Use eval to avoid compilation error in Python 2.
  47. self._testArray(eval("b'abcdef'"))
  48. self._testArray(eval("bytearray(b'abcdef')"))
  49. def _testArray(self, inArray):
  50. outArray = self.object.Method3(inArray)
  51. self.assertEqual(list(outArray), list(inArray))
  52. def testLeaksGencache(self):
  53. try:
  54. gtrc = sys.gettotalrefcount
  55. except AttributeError:
  56. print("Please run this with python_d for leak tests")
  57. gtrc = lambda: 0
  58. # note creating self.object() should have consumed our "one time" leaks
  59. object = EnsureDispatch("Python.Test.Pippo")
  60. start = gtrc()
  61. for i in range(1000):
  62. object = EnsureDispatch("Python.Test.Pippo")
  63. object.Method1()
  64. object = None
  65. end = gtrc()
  66. if end-start > 10:
  67. self.fail("We lost %d references!" % (end-start,))
  68. if __name__=='__main__':
  69. unittest.main()