[Python-checkins] r62377 - in ctypes/trunk/comtypes/comtypes/test: TestDispServer.idl TestDispServer.py TestDispServer.tlb test_dispinterface.py
thomas.heller
python-checkins at python.org
Fri Apr 18 20:56:36 CEST 2008
Author: thomas.heller
Date: Fri Apr 18 20:56:35 2008
New Revision: 62377
Log:
Tests for dispinterface implementation.
Added:
ctypes/trunk/comtypes/comtypes/test/TestDispServer.idl (contents, props changed)
ctypes/trunk/comtypes/comtypes/test/TestDispServer.py (contents, props changed)
ctypes/trunk/comtypes/comtypes/test/TestDispServer.tlb (contents, props changed)
ctypes/trunk/comtypes/comtypes/test/test_dispinterface.py (contents, props changed)
Added: ctypes/trunk/comtypes/comtypes/test/TestDispServer.idl
==============================================================================
--- (empty file)
+++ ctypes/trunk/comtypes/comtypes/test/TestDispServer.idl Fri Apr 18 20:56:35 2008
@@ -0,0 +1,73 @@
+/*
+ 2882fa40-2d69-4880-8073-e81fa29e1785
+ 7ae4b0e3-5d92-4ab1-b5d0-2a95c1c3ba73
+ f557bf87-3e3f-4c73-9bc1-7d633d83714b
+*/
+
+import "oaidl.idl";
+import "ocidl.idl";
+
+[
+ uuid(3b3b2a10-7fef-4bcc-90fe-43a221162b1b),
+ helpstring("A custom event interface")
+ ]
+dispinterface DTestDispServerEvents {
+ properties:
+
+ methods:
+ [id(10)]
+ void EvalStarted([in] BSTR what);
+
+ [id(11)]
+ void EvalCompleted([in] BSTR what, [in] VARIANT result);
+};
+
+[
+ uuid(d44d11ba-aa1f-4e93-8f5a-8fa0a4715241),
+ helpstring("DTestDispServer interface")
+ ]
+dispinterface DTestDispServer {
+ properties:
+ [propget, id(10), helpstring("the id of the server")]
+ UINT id;
+
+ [id(11), helpstring("the name of the server")]
+ BSTR name;
+
+ methods:
+
+ [id(12), helpstring("a method that receives an BSTR [in] parameter")]
+ void SetName([in] BSTR name);
+
+ [id(13), helpstring("evaluate an expression and return the result")]
+ VARIANT eval([in] BSTR what);
+
+ [id(14), helpstring("evaluate an expression and return the result")]
+ VARIANT eval2([in] BSTR what);
+
+ /* Some methods that use defaultvalues */
+ [id(100)]
+ void do_cy([in, defaultvalue(32.78)] CURRENCY *value);
+
+ [id(101)]
+ void do_date([in, defaultvalue(32)] DATE *value);
+};
+
+[
+ uuid(6baa1c79-4ba0-47f2-9ad7-d2ffb1c0f3e3),
+ version(1.0),
+ helpstring("TestDispServer 1.0 Type library")
+ ]
+library TestDispServerLib
+{
+ importlib("stdole2.tlb");
+
+ [
+ uuid(bb2aba53-9d42-435b-acc3-ae2c274517b0),
+ helpstring("TestDispServer class object")
+ ]
+ coclass TestDispServer {
+ [default] dispinterface DTestDispServer;
+ [default, source] dispinterface DTestDispServerEvents;
+ };
+};
Added: ctypes/trunk/comtypes/comtypes/test/TestDispServer.py
==============================================================================
--- (empty file)
+++ ctypes/trunk/comtypes/comtypes/test/TestDispServer.py Fri Apr 18 20:56:35 2008
@@ -0,0 +1,98 @@
+import sys, os
+import logging
+logging.basicConfig()
+##logging.basicConfig(level=logging.DEBUG)
+##logger = logging.getLogger(__name__)
+
+sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), r"..\..")))
+
+import ctypes
+import comtypes
+from comtypes.hresult import *
+import comtypes.client
+import comtypes.errorinfo
+import comtypes.server
+import comtypes.server.connectionpoints
+import comtypes.typeinfo
+
+################################################################
+
+# Create the wrapper in the comtypes.gen package, it will be named
+# TestComServerLib; the name is derived from the 'library ' statement
+# in the IDL file
+if not hasattr(sys, "frozen"):
+ # pathname of the type library file
+ tlbfile = os.path.join(os.path.dirname(__file__), "TestDispServer.tlb")
+ # if running as frozen app (dll or exe), the wrapper should be in
+ # the library archive, so we don't need to generate it.
+ comtypes.client.GetModule(tlbfile)
+
+# Import the wrapper
+from comtypes.gen import TestDispServerLib
+
+################################################################
+
+# Implement the CoClass. Use the coclass from the wrapper as base
+# class, and use DualDispMixin as base class which provides default
+# implementations of IDispatch, IProvideClassInfo, IProvideClassInfo2
+# interfaces. ISupportErrorInfo is implemented by the COMObject base
+# class.
+class TestDispServer(
+ TestDispServerLib.TestDispServer, # the coclass from the typelib wrapper
+ comtypes.server.connectionpoints.ConnectableObjectMixin,
+ ):
+
+ # The default interface from the typelib MUST be the first
+ # interface, other interfaces can follow
+
+ _com_interfaces_ = TestDispServerLib.TestDispServer._com_interfaces_ + \
+ [comtypes.typeinfo.IProvideClassInfo2,
+ comtypes.errorinfo.ISupportErrorInfo,
+ comtypes.connectionpoints.IConnectionPointContainer,
+ ]
+
+ # registry entries
+ _reg_threading_ = "Both"
+ _reg_progid_ = "TestDispServerLib.TestDispServer.1"
+ _reg_novers_progid_ = "TestDispServerLib.TestDispServer"
+ _reg_desc_ = "comtypes COM server sample for testing"
+ _reg_clsctx_ = comtypes.CLSCTX_INPROC_SERVER | comtypes.CLSCTX_LOCAL_SERVER
+
+ ################################
+ # DTestDispServer methods
+
+ def DTestDispServer_eval(self, this, expr, presult):
+ self.Fire_Event(0, "EvalStarted", expr)
+ presult[0].value = eval(expr)
+ self.Fire_Event(0, "EvalCompleted", expr, presult[0].value)
+ return S_OK
+
+ def DTestDispServer_eval2(self, expr):
+ self.Fire_Event(0, "EvalStarted", expr)
+ result = eval(expr)
+ self.Fire_Event(0, "EvalCompleted", expr, result)
+ return result
+
+ def DTestDispServer__get_id(self, this, pid):
+ pid[0] = id(self)
+ return S_OK
+
+ _name = u"spam, spam, spam"
+
+ # Implementation of the DTestDispServer::Name propget
+ def DTestDispServer__get_name(self, this, pname):
+ pname[0].value = self._name
+ return S_OK
+
+ # Implementation of the DTestDispServer::Name propput
+ def DTestDispServer__set_name(self, this, name):
+ self._name = name
+ return S_OK
+
+ # Implementation of the DTestDispServer::SetName method
+ def DTestDispServer_sEtNaMe(self, name):
+ self._name = name
+
+if __name__ == "__main__":
+ from comtypes.server.register import UseCommandLine
+ UseCommandLine(TestDispServer)
Added: ctypes/trunk/comtypes/comtypes/test/TestDispServer.tlb
==============================================================================
Binary file. No diff available.
Added: ctypes/trunk/comtypes/comtypes/test/test_dispinterface.py
==============================================================================
--- (empty file)
+++ ctypes/trunk/comtypes/comtypes/test/test_dispinterface.py Fri Apr 18 20:56:35 2008
@@ -0,0 +1,120 @@
+import unittest
+
+from comtypes.server.register import register#, unregister
+
+################################################################
+import comtypes.test.TestDispServer
+register(comtypes.test.TestDispServer.TestDispServer)
+
+class Test(unittest.TestCase):
+
+ def test_win32com(self):
+ from win32com.client 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('MSScriptControl.ScriptControl')")
+
+ 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
+## d.name = "blah"
+## self.assertEqual(d.Name, "blah")
+
+ def test_win32com_dyndispatch(self):
+ 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('MSScriptControl.ScriptControl')")
+
+ 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
+## 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('MSScriptControl.ScriptControl')")
+
+ 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
+## d.name = "blah"
+## self.assertEqual(d.Name, "blah")
+
+if __name__ == "__main__":
+ unittest.main()
More information about the Python-checkins
mailing list