pippo_server.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # A little test server, complete with typelib, we can use for testing.
  2. # Originally submitted with bug:
  3. # [ 753154 ] memory leak wrapping object having _typelib_guid_ attribute
  4. # but modified by mhammond for use as part of the test suite.
  5. import sys, os
  6. import pythoncom
  7. import win32com
  8. import winerror
  9. from win32com.server.util import wrap
  10. class CPippo:
  11. #
  12. # COM declarations
  13. #
  14. _reg_clsid_ = "{1F0F75D6-BD63-41B9-9F88-2D9D2E1AA5C3}"
  15. _reg_desc_ = "Pippo Python test object"
  16. _reg_progid_ = "Python.Test.Pippo"
  17. #_reg_clsctx_ = pythoncom.CLSCTX_LOCAL_SERVER
  18. ###
  19. ### Link to typelib
  20. _typelib_guid_ = '{7783054E-9A20-4584-8C62-6ED2A08F6AC6}'
  21. _typelib_version_ = 1, 0
  22. _com_interfaces_ = ['IPippo']
  23. def __init__(self):
  24. self.MyProp1 = 10
  25. def Method1(self):
  26. return wrap(CPippo())
  27. def Method2(self, in1, inout1):
  28. return in1, inout1 * 2
  29. def Method3(self, in1):
  30. # in1 will be a tuple, not a list.
  31. # Yet, we are not allowed to return a tuple, but need to convert it to a list first. (Bug?)
  32. return list(in1)
  33. def BuildTypelib():
  34. from distutils.dep_util import newer
  35. this_dir = os.path.dirname(__file__)
  36. idl = os.path.abspath(os.path.join(this_dir, "pippo.idl"))
  37. tlb=os.path.splitext(idl)[0] + '.tlb'
  38. if newer(idl, tlb):
  39. print("Compiling %s" % (idl,))
  40. rc = os.system ('midl "%s"' % (idl,))
  41. if rc:
  42. raise RuntimeError("Compiling MIDL failed!")
  43. # Can't work out how to prevent MIDL from generating the stubs.
  44. # just nuke them
  45. for fname in "dlldata.c pippo_i.c pippo_p.c pippo.h".split():
  46. os.remove(os.path.join(this_dir, fname))
  47. print("Registering %s" % (tlb,))
  48. tli=pythoncom.LoadTypeLib(tlb)
  49. pythoncom.RegisterTypeLib(tli,tlb)
  50. def UnregisterTypelib():
  51. k = CPippo
  52. try:
  53. pythoncom.UnRegisterTypeLib(k._typelib_guid_,
  54. k._typelib_version_[0],
  55. k._typelib_version_[1],
  56. 0,
  57. pythoncom.SYS_WIN32)
  58. print("Unregistered typelib")
  59. except pythoncom.error as details:
  60. if details[0]==winerror.TYPE_E_REGISTRYACCESS:
  61. pass
  62. else:
  63. raise
  64. def main(argv=None):
  65. if argv is None: argv = sys.argv[1:]
  66. if '--unregister' in argv:
  67. # Unregister the type-libraries.
  68. UnregisterTypelib()
  69. else:
  70. # Build and register the type-libraries.
  71. BuildTypelib()
  72. import win32com.server.register
  73. win32com.server.register.UseCommandLine(CPippo)
  74. if __name__=='__main__':
  75. main(sys.argv)