[ctypes-commit] ctypes/comtypes/client dynamic.py,NONE,1.1.2.1 __init__.py,1.1.2.10,1.1.2.11
Brought to you by:
theller
From: Thomas H. <th...@us...> - 2006-01-02 19:11:42
|
Update of /cvsroot/ctypes/ctypes/comtypes/client In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv4721 Modified Files: Tag: branch_1_0 __init__.py Added Files: Tag: branch_1_0 dynamic.py Log Message: Sync with upstream version. --- NEW FILE: dynamic.py --- import ctypes import comtypes.automation import comtypes.client def Dispatch(obj): # Wrap an object in a Dispatch instance, exposing methods and properties # via fully dynamic dispatch if isinstance(obj, _Dispatch): return obj if isinstance(obj, ctypes.POINTER(comtypes.automation.IDispatch)): return _Dispatch(obj) return obj class _Dispatch(object): # Expose methods and properties via fully dynamic dispatch def __init__(self, comobj): self._comobj = comobj def __enum(self): e = self._comobj.Invoke(-4) return e.QueryInterface(comtypes.automation.IEnumVARIANT) def __getitem__(self, index): enum = self.__enum() if index > 0: if 0 != enum.Skip(index): raise IndexError, "index out of range" item, fetched = enum.Next(1) if not fetched: raise IndexError, "index out of range" return item def __iter__(self): return _Collection(self.__enum()) ## def __setitem__(self, index, value): ## self._comobj.Invoke(-3, index, value, ## _invkind=comtypes.automation.DISPATCH_PROPERTYPUT|comtypes.automation.DISPATCH_PROPERTYPUTREF) class _Collection(object): def __init__(self, enum): self.enum = enum def next(self): item, fetched = self.enum.Next(1) if fetched: return item raise StopIteration def __iter__(self): return self __all__ = ["Dispatch"] Index: __init__.py =================================================================== RCS file: /cvsroot/ctypes/ctypes/comtypes/client/Attic/__init__.py,v retrieving revision 1.1.2.10 retrieving revision 1.1.2.11 diff -C2 -d -r1.1.2.10 -r1.1.2.11 *** __init__.py 20 Oct 2005 17:16:13 -0000 1.1.2.10 --- __init__.py 2 Jan 2006 19:11:32 -0000 1.1.2.11 *************** *** 23,31 **** --- 23,34 ---- import comtypes + from comtypes.hresult import * import comtypes.automation import comtypes.connectionpoints import comtypes.typeinfo + import comtypes.client.dynamic import logging + logger = logging.getLogger(__name__) __all__ = ["CreateObject", "GetActiveObject", "CoGetObject", *************** *** 38,51 **** # Creates the directory if it doesn't exist - if possible. def _find_gen_dir(): ! if hasattr(sys, "frozen"): try: ! import comtypes.gen except ImportError: - import comtypes module = sys.modules["comtypes.gen"] = new.module("comtypes.gen") comtypes.gen = module return None # determine the place where generated modules live - import comtypes comtypes_path = os.path.join(comtypes.__path__[0], "gen") if not os.path.exists(comtypes_path): --- 41,52 ---- # Creates the directory if it doesn't exist - if possible. def _find_gen_dir(): ! if not os.path.isfile(comtypes.__file__): try: ! from comtypes import gen except ImportError: module = sys.modules["comtypes.gen"] = new.module("comtypes.gen") comtypes.gen = module return None # determine the place where generated modules live comtypes_path = os.path.join(comtypes.__path__[0], "gen") if not os.path.exists(comtypes_path): *************** *** 103,106 **** --- 104,110 ---- (libid, wMajorVerNum, wMinorVerNum, lcid=0) + + Or it can be an object with _reg_libid_ and _reg_version_ + attributes. This function determines the module name from the typelib *************** *** 134,138 **** tlib = comtypes.typeinfo.LoadTypeLibEx(tlib) elif isinstance(tlib, (tuple, list)): ! tlib = comtypes.typeinfo.LoadRegTypeLib(*tlib) # determine the Python module name fullname = _name_module(tlib) --- 138,145 ---- tlib = comtypes.typeinfo.LoadTypeLibEx(tlib) elif isinstance(tlib, (tuple, list)): ! tlib = comtypes.typeinfo.LoadRegTypeLib(comtypes.GUID(tlib[0]), *tlib[1:]) ! elif hasattr(tlib, "_reg_libid_"): ! tlib = comtypes.typeinfo.LoadRegTypeLib(comtypes.GUID(tlib._reg_libid_), ! *tlib._reg_version_) # determine the Python module name fullname = _name_module(tlib) *************** *** 223,239 **** return punk # or should we return None? # find the typelib and the interface name try: pci = punk.QueryInterface(comtypes.typeinfo.IProvideClassInfo) tinfo = pci.GetClassInfo() # TypeInfo for the CoClass # find the interface marked as default ! for index in range(tinfo.GetTypeAttr().cImplTypes): if tinfo.GetImplTypeFlags(index) == 1: break else: ! # should we simply use the first interface now? ! raise TypeError, "No default interface found" href = tinfo.GetRefTypeOfImplType(index) tinfo = tinfo.GetRefTypeInfo(href) except comtypes.COMError: try: pdisp = punk.QueryInterface(comtypes.automation.IDispatch) --- 230,254 ---- return punk # or should we return None? # find the typelib and the interface name + logger.info("wrap(%s)", punk) try: pci = punk.QueryInterface(comtypes.typeinfo.IProvideClassInfo) + logger.info("Does implement IProvideClassInfo") tinfo = pci.GetClassInfo() # TypeInfo for the CoClass # find the interface marked as default ! ta = tinfo.GetTypeAttr() ! for index in range(ta.cImplTypes): if tinfo.GetImplTypeFlags(index) == 1: break else: ! if ta.cImplTypes != 1: ! # Hm, should we use dynamic now? ! raise TypeError, "No default interface found" ! # Only one interface implemented, use that (even if ! # not marked as default). ! index = 0 href = tinfo.GetRefTypeOfImplType(index) tinfo = tinfo.GetRefTypeInfo(href) except comtypes.COMError: + logger.info("Does NOT implement IProvideClassInfo") try: pdisp = punk.QueryInterface(comtypes.automation.IDispatch) *************** *** 241,252 **** # no further chance to find typeinfo, and IDispatch is # more useful than IUnknown. ! return pdisp tinfo = pdisp.GetTypeInfo(0) except comtypes.COMError: ! return punk try: punk.QueryInterface(comtypes.IUnknown, tinfo.GetTypeAttr().guid) except comtypes.COMError: ! return punk itf_name = tinfo.GetDocumentation(-1)[0] # interface name --- 256,271 ---- # no further chance to find typeinfo, and IDispatch is # more useful than IUnknown. ! logger.info("IDispatch without typeinfo, using dynamic") ! return comtypes.client.dynamic.Dispatch(pdisp) ! logger.info("IDispatch with typeinfo") tinfo = pdisp.GetTypeInfo(0) except comtypes.COMError: ! logger.info("Without typeinfo, using dynamic") ! return comtypes.client.dynamic.Dispatch(punk) try: punk.QueryInterface(comtypes.IUnknown, tinfo.GetTypeAttr().guid) except comtypes.COMError: ! logger.info("Does not seem to implement default interface from typeinfo, using dynamic") ! return comtypes.client.dynamic.Dispatch(punk) itf_name = tinfo.GetDocumentation(-1)[0] # interface name *************** *** 257,260 **** --- 276,280 ---- # Python interface class interface = getattr(mod, itf_name) + logger.info("Implements default interface from typeinfo %s", interface) # QI for this interface # XXX *************** *** 269,273 **** # Could the above code, as an optimization, check that QI works, # *before* generating the wraper module? ! return punk.QueryInterface(interface) # Should we do this for POINTER(IUnknown) also? --- 289,295 ---- # Could the above code, as an optimization, check that QI works, # *before* generating the wraper module? ! result = punk.QueryInterface(interface) ! logger.info("Final result is %s", result) ! return result # Should we do this for POINTER(IUnknown) also? *************** *** 316,320 **** # makes sense is to use IProvideClassInfo2 to get the default # source interface. - logger = logging.getLogger("comtypes.events") if interface is None: --- 338,341 ---- *************** *** 346,350 **** dispmap[memid] = mth ! class EventReceiver(comtypes.COMObject): _com_interfaces_ = [interface] --- 367,371 ---- dispmap[memid] = mth ! class DispEventReceiver(comtypes.COMObject): _com_interfaces_ = [interface] *************** *** 357,361 **** return 0 ! rcv = EventReceiver() rcv.dispmap = dispmap else: --- 378,394 ---- return 0 ! def GetTypeInfoCount(self, this, presult): ! if not presult: ! return E_POINTER ! presult[0] = 0 ! return S_OK ! ! def GetTypeInfo(self, this, itinfo, lcid, pptinfo): ! return E_NOTIMPL ! ! def GetIDsOfNames(self, this, riid, rgszNames, cNames, lcid, rgDispId): ! return E_NOTIMPL ! ! rcv = DispEventReceiver() rcv.dispmap = dispmap else: *************** *** 371,378 **** rcv = EventReceiver() ! cpc = source.QueryInterface(comtypes.connectionpoints.IConnectionPointContainer) ! cp = cpc.FindConnectionPoint(ctypes.byref(interface._iid_)) ! logger.debug("Start advise %s", interface) ! cookie = cp.Advise(rcv) def release(ref): --- 404,419 ---- rcv = EventReceiver() ! # XXX All of these (QI, FindConnectionPoint, Advise) can also fail ! # (for buggy objects?), and we should raise an appropriate error ! # then. ! ! try: ! cpc = source.QueryInterface(comtypes.connectionpoints.IConnectionPointContainer) ! cp = cpc.FindConnectionPoint(ctypes.byref(interface._iid_)) ! logger.debug("Start advise %s", interface) ! cookie = cp.Advise(rcv) ! except: ! logger.error("Could not connect to object:", exc_info=True) ! raise def release(ref): *************** *** 458,464 **** --- 499,510 ---- """ clsid = comtypes.GUID.from_progid(progid) + logger.debug("%s -> %s", progid, clsid) if machine is None: + logger.debug("CoCreateInstance(%s, clsctx=%s, interface=%s)", + clsid, clsctx, interface) obj = comtypes.CoCreateInstance(clsid, clsctx=clsctx, interface=interface) else: + logger.debug("CoCreateInstanceEx(%s, clsctx=%s, interface=%s, machine=%s)", + clsid, clsctx, interface, machine) obj = comtypes.CoCreateInstanceEx(clsid, clsctx=clsctx, interface=interface, machine=machine) return _manage(obj, clsid, |