class TestCase(unittest.TestCase): def test(self): doctest.testmod(comtypes.test.test_comserver, optionflags=doctest.ELLIPSIS) # The following functions are never called, they only contain doctests: if sys.version_info >= (3, 0): def ShowEvents(self): ''' >>> from comtypes.client import CreateObject, ShowEvents >>> >>> o = CreateObject("TestComServerLib.TestComServer") >>> con = ShowEvents(o) # event found: ITestComServerEvents_EvalStarted # event found: ITestComServerEvents_EvalCompleted >>> result = o.eval("10. / 4") Event ITestComServerEvents_EvalStarted(None, '10. / 4') Event ITestComServerEvents_EvalCompleted(None, '10. / 4', VARIANT(vt=0x5, 2.5)) >>> result 2.5 >>> ''' else: def ShowEvents(self): ''' >>> from comtypes.client import CreateObject, ShowEvents >>> >>> o = CreateObject("TestComServerLib.TestComServer") >>> con = ShowEvents(o) # event found: ITestComServerEvents_EvalStarted # event found: ITestComServerEvents_EvalCompleted >>> result = o.eval("10. / 4") Event ITestComServerEvents_EvalStarted(None, u'10. / 4') Event ITestComServerEvents_EvalCompleted(None, u'10. / 4', VARIANT(vt=0x5, 2.5)) >>> result 2.5 >>> ''' # The following test, if enabled, works but the testsuit # crashes elsewhere. Is there s problem with SAFEARRAYs? if is_resource_enabled("CRASHES"): def Fails(self): ''' >>> from comtypes.client import CreateObject, ShowEvents >>> >>> o = CreateObject("TestComServerLib.TestComServer") >>> con = ShowEvents(o) # event found: ITestComServerEvents_EvalStarted # event found: ITestComServerEvents_EvalCompleted >>> result = o.eval("['32'] * 2") Event ITestComServerEvents_EvalStarted(None, u"['32'] * 2") Event ITestComServerEvents_EvalCompleted(None, u"['32'] * 2", VARIANT(vt=0x200c, (u'32', u'32'))) >>> result (u'32', u'32') >>> ''' def GetEvents(): """
from comtypes.gen.TestComServerLib import MYCOLOR t = _midlSAFEARRAY(MYCOLOR) self.failUnless(t is _midlSAFEARRAY(MYCOLOR)) sa = t.from_param([MYCOLOR(0, 0, 0), MYCOLOR(1, 2, 3)]) self.failUnlessEqual([(x.red, x.green, x.blue) for x in sa[0]], [(0.0, 0.0, 0.0), (1.0, 2.0, 3.0)]) def doit(): t.from_param([MYCOLOR(0, 0, 0), MYCOLOR(1, 2, 3)]) bytes = find_memleak(doit) self.failIf(bytes, "Leaks %d bytes" % bytes) if is_resource_enabled("pythoncom"): try: import pythoncom except ImportError: # pywin32 not installed... pass else: # pywin32 is available. The pythoncom dll contains two handy # exported functions that allow to create a VARIANT from a Python # object, also a function that unpacks a VARIANT into a Python # object. # # This allows us to create und unpack SAFEARRAY instances # contained in VARIANTs, and check for consistency with the # comtypes code.
class TestInproc(unittest.TestCase): def create_object(self): return CreateObject("TestComServerLib.TestComServer", clsctx=comtypes.CLSCTX_INPROC_SERVER) def _find_memleak(self, func): bytes = find_memleak(func) self.failIf(bytes, "Leaks %d bytes" % bytes) def test_mixedinout(self): o = self.create_object() self.failUnlessEqual(o.MixedInOut(2, 4), (3, 5)) def test_getname(self): from ctypes import byref, pointer from comtypes import BSTR # This tests a tricky bug, introduced with this patch: # http://www.python.org/sf/1643874 # # Returning a BSTR as an [out] parameter from a server # implementation must transfer the ownership to the caller. # When this is not done, the BSTR instance is SysFreeString'd # too early, and the memory is reused. obj = self.create_object() pb = pointer(BSTR()) # Get the BSTR from the server: obj._ITestComServer__com__get_name(pb) # Retrieve the value, but keep the pointer to the BSTR alive: name = pb[0] # Create sme BSTR's to reuse the memory in case it has been freed: for i in range(10): BSTR("f" * len(name)) # Make sure the pointer is still valid: self.failUnlessEqual(pb[0], name) if is_resource_enabled("memleaks"): def test_get_id(self): obj = self.create_object() self._find_memleak(lambda: obj.id) def test_get_name(self): obj = self.create_object() self._find_memleak(lambda: obj.name) def test_set_name(self): obj = self.create_object() def func(): obj.name = u"abcde" self._find_memleak(func) def test_SetName(self): obj = self.create_object() def func(): obj.SetName(u"abcde") self._find_memleak(func) def test_eval(self): obj = self.create_object() def func(): return obj.eval("(1, 2, 3)") self.failUnlessEqual(func(), (1, 2, 3)) self._find_memleak(func) def test_get_typeinfo(self): obj = self.create_object() def func(): obj.GetTypeInfo(0) obj.GetTypeInfoCount() obj.QueryInterface(comtypes.IUnknown) self._find_memleak(func)
self.failUnlessEqual(func(), (1, 2, 3)) self._find_memleak(func) def test_get_typeinfo(self): obj = self.create_object() def func(): obj.GetTypeInfo(0) obj.GetTypeInfoCount() obj.QueryInterface(comtypes.IUnknown) self._find_memleak(func) if is_resource_enabled("ui"): class TestLocalServer(TestInproc): def create_object(self): return CreateObject("TestComServerLib.TestComServer", clsctx=comtypes.CLSCTX_LOCAL_SERVER) try: from win32com.client import Dispatch except ImportError: pass else: class TestInproc_win32com(TestInproc): def create_object(self):
t = _midlSAFEARRAY(MYCOLOR) self.failUnless(t is _midlSAFEARRAY(MYCOLOR)) sa = t.from_param([MYCOLOR(0, 0, 0), MYCOLOR(1, 2, 3)]) self.failUnlessEqual([(x.red, x.green, x.blue) for x in sa[0]], [(0.0, 0.0, 0.0), (1.0, 2.0, 3.0)]) def doit(): t.from_param([MYCOLOR(0, 0, 0), MYCOLOR(1, 2, 3)]) bytes = find_memleak(doit) self.failIf(bytes, "Leaks %d bytes" % bytes) if is_resource_enabled("pythoncom"): try: import pythoncom except ImportError: # pywin32 not installed... pass else: # pywin32 is available. The pythoncom dll contains two handy # exported functions that allow to create a VARIANT from a Python # object, also a function that unpacks a VARIANT into a Python # object. # # This allows us to create und unpack SAFEARRAY instances # contained in VARIANTs, and check for consistency with the # comtypes code.
def test_eval(self): obj = self.create_object() def func(): return obj.eval("(1, 2, 3)") self.failUnlessEqual(func(), (1, 2, 3)) self._find_memleak(func) def test_get_typeinfo(self): obj = self.create_object() def func(): obj.GetTypeInfo(0) obj.GetTypeInfoCount() obj.QueryInterface(comtypes.IUnknown) self._find_memleak(func) if is_resource_enabled("ui"): class TestLocalServer(TestInproc): def create_object(self): return CreateObject("TestComServerLib.TestComServer", clsctx = comtypes.CLSCTX_LOCAL_SERVER) try: from win32com.client import Dispatch except ImportError: pass else: class TestInproc_win32com(TestInproc): def create_object(self): return Dispatch("TestComServerLib.TestComServer") # These tests make no sense with win32com, override to disable them:
class Test(unittest.TestCase): if is_resource_enabled("pythoncom"): def test_win32com(self): # EnsureDispatch is case-sensitive from win32com.client.gencache import EnsureDispatch d = EnsureDispatch("TestDispServerLib.TestDispServer") self.assertEqual(d.eval("3.14"), 3.14) self.assertEqual(d.eval("1 + 2"), 3) self.assertEqual(d.eval("[1 + 2, 'foo', None]"), (3, 'foo', None)) self.assertEqual(d.eval2("3.14"), 3.14) self.assertEqual(d.eval2("1 + 2"), 3) self.assertEqual(d.eval2("[1 + 2, 'foo', None]"), (3, 'foo', None)) d.eval( "__import__('comtypes.client').client.CreateObject('Scripting.Dictionary')" ) server_id = d.eval("id(self)") self.assertEqual(d.id, server_id) self.assertEqual(d.name, "spam, spam, spam") d.SetName("foo bar") self.assertEqual(d.name, "foo bar") d.name = "blah" self.assertEqual(d.name, "blah") def test_win32com_dyndispatch(self): # dynamic Dispatch is case-IN-sensitive from win32com.client.dynamic import Dispatch d = Dispatch("TestDispServerLib.TestDispServer") self.assertEqual(d.eval("3.14"), 3.14) self.assertEqual(d.eval("1 + 2"), 3) self.assertEqual(d.eval("[1 + 2, 'foo', None]"), (3, 'foo', None)) self.assertEqual(d.eval2("3.14"), 3.14) self.assertEqual(d.eval2("1 + 2"), 3) self.assertEqual(d.eval2("[1 + 2, 'foo', None]"), (3, 'foo', None)) d.eval( "__import__('comtypes.client').client.CreateObject('Scripting.Dictionary')" ) self.assertEqual(d.EVAL("3.14"), 3.14) self.assertEqual(d.EVAL("1 + 2"), 3) self.assertEqual(d.EVAL("[1 + 2, 'foo', None]"), (3, 'foo', None)) self.assertEqual(d.EVAL2("3.14"), 3.14) self.assertEqual(d.EVAL2("1 + 2"), 3) self.assertEqual(d.EVAL2("[1 + 2, 'foo', None]"), (3, 'foo', None)) server_id = d.eval("id(self)") self.assertEqual(d.id, server_id) self.assertEqual(d.ID, server_id) self.assertEqual(d.Name, "spam, spam, spam") self.assertEqual(d.nAME, "spam, spam, spam") d.SetName("foo bar") self.assertEqual(d.Name, "foo bar") # fails. Why? ## d.name = "blah" ## self.assertEqual(d.Name, "blah") def test_comtypes(self): from comtypes.client import CreateObject d = CreateObject("TestDispServerLib.TestDispServer") self.assertEqual(d.eval("3.14"), 3.14) self.assertEqual(d.eval("1 + 2"), 3) self.assertEqual(d.eval("[1 + 2, 'foo', None]"), (3, 'foo', None)) self.assertEqual(d.eval2("3.14"), 3.14) self.assertEqual(d.eval2("1 + 2"), 3) self.assertEqual(d.eval2("[1 + 2, 'foo', None]"), (3, 'foo', None)) d.eval( "__import__('comtypes.client').client.CreateObject('Scripting.Dictionary')" ) self.assertEqual(d.EVAL("3.14"), 3.14) self.assertEqual(d.EVAL("1 + 2"), 3) self.assertEqual(d.EVAL("[1 + 2, 'foo', None]"), (3, 'foo', None)) self.assertEqual(d.EVAL2("3.14"), 3.14) self.assertEqual(d.EVAL2("1 + 2"), 3) self.assertEqual(d.EVAL2("[1 + 2, 'foo', None]"), (3, 'foo', None)) server_id = d.eval("id(self)") self.assertEqual(d.id, server_id) self.assertEqual(d.ID, server_id) self.assertEqual(d.Name, "spam, spam, spam") self.assertEqual(d.nAME, "spam, spam, spam") d.SetName("foo bar") self.assertEqual(d.Name, "foo bar") d.name = "blah" self.assertEqual(d.Name, "blah") def test_withjscript(self): import os jscript = os.path.join(os.path.dirname(__file__), "test_jscript.js") errcode = os.system("cscript -nologo %s" % jscript) self.assertEqual(errcode, 0)