__init__.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. # This module exists to create the "best" dispatch object for a given
  2. # object. If "makepy" support for a given object is detected, it is
  3. # used, otherwise a dynamic dispatch object.
  4. # Note that if the unknown dispatch object then returns a known
  5. # dispatch object, the known class will be used. This contrasts
  6. # with dynamic.Dispatch behaviour, where dynamic objects are always used.
  7. import pythoncom
  8. import dynamic
  9. import gencache
  10. import sys
  11. import pywintypes
  12. _PyIDispatchType = pythoncom.TypeIIDs[pythoncom.IID_IDispatch]
  13. def __WrapDispatch(dispatch, userName = None, resultCLSID = None, typeinfo = None, \
  14. UnicodeToString=None, clsctx = pythoncom.CLSCTX_SERVER,
  15. WrapperClass = None):
  16. """
  17. Helper function to return a makepy generated class for a CLSID if it exists,
  18. otherwise cope by using CDispatch.
  19. """
  20. assert UnicodeToString is None, "this is deprecated and will go away"
  21. if resultCLSID is None:
  22. try:
  23. typeinfo = dispatch.GetTypeInfo()
  24. if typeinfo is not None: # Some objects return NULL, some raise exceptions...
  25. resultCLSID = str(typeinfo.GetTypeAttr()[0])
  26. except (pythoncom.com_error, AttributeError):
  27. pass
  28. if resultCLSID is not None:
  29. import gencache
  30. # Attempt to load generated module support
  31. # This may load the module, and make it available
  32. klass = gencache.GetClassForCLSID(resultCLSID)
  33. if klass is not None:
  34. return klass(dispatch)
  35. # Return a "dynamic" object - best we can do!
  36. if WrapperClass is None: WrapperClass = CDispatch
  37. return dynamic.Dispatch(dispatch, userName, WrapperClass, typeinfo, clsctx=clsctx)
  38. def GetObject(Pathname = None, Class = None, clsctx = None):
  39. """
  40. Mimic VB's GetObject() function.
  41. ob = GetObject(Class = "ProgID") or GetObject(Class = clsid) will
  42. connect to an already running instance of the COM object.
  43. ob = GetObject(r"c:\blah\blah\foo.xls") (aka the COM moniker syntax)
  44. will return a ready to use Python wrapping of the required COM object.
  45. Note: You must specifiy one or the other of these arguments. I know
  46. this isn't pretty, but it is what VB does. Blech. If you don't
  47. I'll throw ValueError at you. :)
  48. This will most likely throw pythoncom.com_error if anything fails.
  49. """
  50. if clsctx is None:
  51. clsctx = pythoncom.CLSCTX_ALL
  52. if (Pathname is None and Class is None) or \
  53. (Pathname is not None and Class is not None):
  54. raise ValueError("You must specify a value for Pathname or Class, but not both.")
  55. if Class is not None:
  56. return GetActiveObject(Class, clsctx)
  57. else:
  58. return Moniker(Pathname, clsctx)
  59. def GetActiveObject(Class, clsctx = pythoncom.CLSCTX_ALL):
  60. """
  61. Python friendly version of GetObject's ProgID/CLSID functionality.
  62. """
  63. resultCLSID = pywintypes.IID(Class)
  64. dispatch = pythoncom.GetActiveObject(resultCLSID)
  65. dispatch = dispatch.QueryInterface(pythoncom.IID_IDispatch)
  66. return __WrapDispatch(dispatch, Class, resultCLSID = resultCLSID, clsctx = clsctx)
  67. def Moniker(Pathname, clsctx = pythoncom.CLSCTX_ALL):
  68. """
  69. Python friendly version of GetObject's moniker functionality.
  70. """
  71. moniker, i, bindCtx = pythoncom.MkParseDisplayName(Pathname)
  72. dispatch = moniker.BindToObject(bindCtx, None, pythoncom.IID_IDispatch)
  73. return __WrapDispatch(dispatch, Pathname, clsctx=clsctx)
  74. def Dispatch(dispatch, userName = None, resultCLSID = None, typeinfo = None, UnicodeToString=None, clsctx = pythoncom.CLSCTX_SERVER):
  75. """Creates a Dispatch based COM object.
  76. """
  77. assert UnicodeToString is None, "this is deprecated and will go away"
  78. dispatch, userName = dynamic._GetGoodDispatchAndUserName(dispatch,userName,clsctx)
  79. return __WrapDispatch(dispatch, userName, resultCLSID, typeinfo, clsctx=clsctx)
  80. def DispatchEx(clsid, machine=None, userName = None, resultCLSID = None, typeinfo = None, UnicodeToString=None, clsctx = None):
  81. """Creates a Dispatch based COM object on a specific machine.
  82. """
  83. assert UnicodeToString is None, "this is deprecated and will go away"
  84. # If InProc is registered, DCOM will use it regardless of the machine name
  85. # (and regardless of the DCOM config for the object.) So unless the user
  86. # specifies otherwise, we exclude inproc apps when a remote machine is used.
  87. if clsctx is None:
  88. clsctx = pythoncom.CLSCTX_SERVER
  89. if machine is not None: clsctx = clsctx & ~pythoncom.CLSCTX_INPROC
  90. if machine is None:
  91. serverInfo = None
  92. else:
  93. serverInfo = (machine,)
  94. if userName is None: userName = clsid
  95. dispatch = pythoncom.CoCreateInstanceEx(clsid, None, clsctx, serverInfo, (pythoncom.IID_IDispatch,))[0]
  96. return Dispatch(dispatch, userName, resultCLSID, typeinfo, clsctx=clsctx)
  97. class CDispatch(dynamic.CDispatch):
  98. """
  99. The dynamic class used as a last resort.
  100. The purpose of this overriding of dynamic.CDispatch is to perpetuate the policy
  101. of using the makepy generated wrapper Python class instead of dynamic.CDispatch
  102. if/when possible.
  103. """
  104. def _wrap_dispatch_(self, ob, userName = None, returnCLSID = None, UnicodeToString=None):
  105. assert UnicodeToString is None, "this is deprecated and will go away"
  106. return Dispatch(ob, userName, returnCLSID,None)
  107. def CastTo(ob, target, typelib = None):
  108. """'Cast' a COM object to another interface"""
  109. # todo - should support target being an IID
  110. mod = None
  111. if typelib is not None: # caller specified target typelib (TypelibSpec). See e.g. selecttlb.EnumTlbs().
  112. mod = gencache.MakeModuleForTypelib(typelib.clsid, typelib.lcid, int(typelib.major, 16), int(typelib.minor, 16))
  113. if not hasattr(mod, target):
  114. raise ValueError("The interface name '%s' does not appear in the " \
  115. "specified library %r" % (target, typelib.ver_desc))
  116. elif hasattr(target, "index"): # string like
  117. # for now, we assume makepy for this to work.
  118. if "CLSID" not in ob.__class__.__dict__:
  119. # Eeek - no makepy support - try and build it.
  120. ob = gencache.EnsureDispatch(ob)
  121. if "CLSID" not in ob.__class__.__dict__:
  122. raise ValueError("Must be a makepy-able object for this to work")
  123. clsid = ob.CLSID
  124. # Lots of hoops to support "demand-build" - ie, generating
  125. # code for an interface first time it is used. We assume the
  126. # interface name exists in the same library as the object.
  127. # This is generally the case - only referenced typelibs may be
  128. # a problem, and we can handle that later. Maybe <wink>
  129. # So get the generated module for the library itself, then
  130. # find the interface CLSID there.
  131. mod = gencache.GetModuleForCLSID(clsid)
  132. # Get the 'root' module.
  133. mod = gencache.GetModuleForTypelib(mod.CLSID, mod.LCID,
  134. mod.MajorVersion, mod.MinorVersion)
  135. # Find the CLSID of the target
  136. target_clsid = mod.NamesToIIDMap.get(target)
  137. if target_clsid is None:
  138. raise ValueError("The interface name '%s' does not appear in the " \
  139. "same library as object '%r'" % (target, ob))
  140. mod = gencache.GetModuleForCLSID(target_clsid)
  141. if mod is not None:
  142. target_class = getattr(mod, target)
  143. # resolve coclass to interface
  144. target_class = getattr(target_class, "default_interface", target_class)
  145. return target_class(ob) # auto QI magic happens
  146. raise ValueError
  147. class Constants:
  148. """A container for generated COM constants.
  149. """
  150. def __init__(self):
  151. self.__dicts__ = [] # A list of dictionaries
  152. def __getattr__(self, a):
  153. for d in self.__dicts__:
  154. if a in d:
  155. return d[a]
  156. raise AttributeError(a)
  157. # And create an instance.
  158. constants = Constants()
  159. # A helpers for DispatchWithEvents - this becomes __setattr__ for the
  160. # temporary class.
  161. def _event_setattr_(self, attr, val):
  162. try:
  163. # Does the COM object have an attribute of this name?
  164. self.__class__.__bases__[0].__setattr__(self, attr, val)
  165. except AttributeError:
  166. # Otherwise just stash it away in the instance.
  167. self.__dict__[attr] = val
  168. # An instance of this "proxy" is created to break the COM circular references
  169. # that exist (ie, when we connect to the COM events, COM keeps a reference
  170. # to the object. Thus, the Event connection must be manually broken before
  171. # our object can die. This solves the problem by manually breaking the connection
  172. # to the real object as the proxy dies.
  173. class EventsProxy:
  174. def __init__(self, ob):
  175. self.__dict__['_obj_'] = ob
  176. def __del__(self):
  177. try:
  178. # If there is a COM error on disconnection we should
  179. # just ignore it - object probably already shut down...
  180. self._obj_.close()
  181. except pythoncom.com_error:
  182. pass
  183. def __getattr__(self, attr):
  184. return getattr(self._obj_, attr)
  185. def __setattr__(self, attr, val):
  186. setattr(self._obj_, attr, val)
  187. def DispatchWithEvents(clsid, user_event_class):
  188. """Create a COM object that can fire events to a user defined class.
  189. clsid -- The ProgID or CLSID of the object to create.
  190. user_event_class -- A Python class object that responds to the events.
  191. This requires makepy support for the COM object being created. If
  192. this support does not exist it will be automatically generated by
  193. this function. If the object does not support makepy, a TypeError
  194. exception will be raised.
  195. The result is a class instance that both represents the COM object
  196. and handles events from the COM object.
  197. It is important to note that the returned instance is not a direct
  198. instance of the user_event_class, but an instance of a temporary
  199. class object that derives from three classes:
  200. * The makepy generated class for the COM object
  201. * The makepy generated class for the COM events
  202. * The user_event_class as passed to this function.
  203. If this is not suitable, see the getevents function for an alternative
  204. technique of handling events.
  205. Object Lifetimes: Whenever the object returned from this function is
  206. cleaned-up by Python, the events will be disconnected from
  207. the COM object. This is almost always what should happen,
  208. but see the documentation for getevents() for more details.
  209. Example:
  210. >>> class IEEvents:
  211. ... def OnVisible(self, visible):
  212. ... print "Visible changed:", visible
  213. ...
  214. >>> ie = DispatchWithEvents("InternetExplorer.Application", IEEvents)
  215. >>> ie.Visible = 1
  216. Visible changed: 1
  217. >>>
  218. """
  219. # Create/Get the object.
  220. disp = Dispatch(clsid)
  221. if not disp.__class__.__dict__.get("CLSID"): # Eeek - no makepy support - try and build it.
  222. try:
  223. ti = disp._oleobj_.GetTypeInfo()
  224. disp_clsid = ti.GetTypeAttr()[0]
  225. tlb, index = ti.GetContainingTypeLib()
  226. tla = tlb.GetLibAttr()
  227. gencache.EnsureModule(tla[0], tla[1], tla[3], tla[4], bValidateFile=0)
  228. # Get the class from the module.
  229. disp_class = gencache.GetClassForProgID(str(disp_clsid))
  230. except pythoncom.com_error:
  231. raise TypeError("This COM object can not automate the makepy process - please run makepy manually for this object")
  232. else:
  233. disp_class = disp.__class__
  234. # If the clsid was an object, get the clsid
  235. clsid = disp_class.CLSID
  236. # Create a new class that derives from 3 classes - the dispatch class, the event sink class and the user class.
  237. # XXX - we are still "classic style" classes in py2x, so we need can't yet
  238. # use 'type()' everywhere - revisit soon, as py2x will move to new-style too...
  239. try:
  240. from types import ClassType as new_type
  241. except ImportError:
  242. new_type = type # py3k
  243. events_class = getevents(clsid)
  244. if events_class is None:
  245. raise ValueError("This COM object does not support events.")
  246. result_class = new_type("COMEventClass", (disp_class, events_class, user_event_class), {"__setattr__" : _event_setattr_})
  247. instance = result_class(disp._oleobj_) # This only calls the first base class __init__.
  248. events_class.__init__(instance, instance)
  249. if hasattr(user_event_class, "__init__"):
  250. user_event_class.__init__(instance)
  251. return EventsProxy(instance)
  252. def WithEvents(disp, user_event_class):
  253. """Similar to DispatchWithEvents - except that the returned
  254. object is *not* also usable as the original Dispatch object - that is
  255. the returned object is not dispatchable.
  256. The difference is best summarised by example.
  257. >>> class IEEvents:
  258. ... def OnVisible(self, visible):
  259. ... print "Visible changed:", visible
  260. ...
  261. >>> ie = Dispatch("InternetExplorer.Application")
  262. >>> ie_events = WithEvents(ie, IEEvents)
  263. >>> ie.Visible = 1
  264. Visible changed: 1
  265. Compare with the code sample for DispatchWithEvents, where you get a
  266. single object that is both the interface and the event handler. Note that
  267. the event handler instance will *not* be able to use 'self.' to refer to
  268. IE's methods and properties.
  269. This is mainly useful where using DispatchWithEvents causes
  270. circular reference problems that the simple proxy doesn't deal with
  271. """
  272. disp = Dispatch(disp)
  273. if not disp.__class__.__dict__.get("CLSID"): # Eeek - no makepy support - try and build it.
  274. try:
  275. ti = disp._oleobj_.GetTypeInfo()
  276. disp_clsid = ti.GetTypeAttr()[0]
  277. tlb, index = ti.GetContainingTypeLib()
  278. tla = tlb.GetLibAttr()
  279. gencache.EnsureModule(tla[0], tla[1], tla[3], tla[4], bValidateFile=0)
  280. # Get the class from the module.
  281. disp_class = gencache.GetClassForProgID(str(disp_clsid))
  282. except pythoncom.com_error:
  283. raise TypeError("This COM object can not automate the makepy process - please run makepy manually for this object")
  284. else:
  285. disp_class = disp.__class__
  286. # Get the clsid
  287. clsid = disp_class.CLSID
  288. # Create a new class that derives from 2 classes - the event sink
  289. # class and the user class.
  290. try:
  291. from types import ClassType as new_type
  292. except ImportError:
  293. new_type = type # py3k
  294. events_class = getevents(clsid)
  295. if events_class is None:
  296. raise ValueError("This COM object does not support events.")
  297. result_class = new_type("COMEventClass", (events_class, user_event_class), {})
  298. instance = result_class(disp) # This only calls the first base class __init__.
  299. if hasattr(user_event_class, "__init__"):
  300. user_event_class.__init__(instance)
  301. return instance
  302. def getevents(clsid):
  303. """Determine the default outgoing interface for a class, given
  304. either a clsid or progid. It returns a class - you can
  305. conveniently derive your own handler from this class and implement
  306. the appropriate methods.
  307. This method relies on the classes produced by makepy. You must use
  308. either makepy or the gencache module to ensure that the
  309. appropriate support classes have been generated for the com server
  310. that you will be handling events from.
  311. Beware of COM circular references. When the Events class is connected
  312. to the COM object, the COM object itself keeps a reference to the Python
  313. events class. Thus, neither the Events instance or the COM object will
  314. ever die by themselves. The 'close' method on the events instance
  315. must be called to break this chain and allow standard Python collection
  316. rules to manage object lifetimes. Note that DispatchWithEvents() does
  317. work around this problem by the use of a proxy object, but if you use
  318. the getevents() function yourself, you must make your own arrangements
  319. to manage this circular reference issue.
  320. Beware of creating Python circular references: this will happen if your
  321. handler has a reference to an object that has a reference back to
  322. the event source. Call the 'close' method to break the chain.
  323. Example:
  324. >>>win32com.client.gencache.EnsureModule('{EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B}',0,1,1)
  325. <module 'win32com.gen_py.....
  326. >>>
  327. >>> class InternetExplorerEvents(win32com.client.getevents("InternetExplorer.Application.1")):
  328. ... def OnVisible(self, Visible):
  329. ... print "Visibility changed: ", Visible
  330. ...
  331. >>>
  332. >>> ie=win32com.client.Dispatch("InternetExplorer.Application.1")
  333. >>> events=InternetExplorerEvents(ie)
  334. >>> ie.Visible=1
  335. Visibility changed: 1
  336. >>>
  337. """
  338. # find clsid given progid or clsid
  339. clsid=str(pywintypes.IID(clsid))
  340. # return default outgoing interface for that class
  341. klass = gencache.GetClassForCLSID(clsid)
  342. try:
  343. return klass.default_source
  344. except AttributeError:
  345. # See if we have a coclass for the interfaces.
  346. try:
  347. return gencache.GetClassForCLSID(klass.coclass_clsid).default_source
  348. except AttributeError:
  349. return None
  350. # A Record object, as used by the COM struct support
  351. def Record(name, object):
  352. """Creates a new record object, given the name of the record,
  353. and an object from the same type library.
  354. Example usage would be:
  355. app = win32com.client.Dispatch("Some.Application")
  356. point = win32com.client.Record("SomeAppPoint", app)
  357. point.x = 0
  358. point.y = 0
  359. app.MoveTo(point)
  360. """
  361. # XXX - to do - probably should allow "object" to already be a module object.
  362. import gencache
  363. object = gencache.EnsureDispatch(object)
  364. module = sys.modules[object.__class__.__module__]
  365. # to allow us to work correctly with "demand generated" code,
  366. # we must use the typelib CLSID to obtain the module
  367. # (otherwise we get the sub-module for the object, which
  368. # does not hold the records)
  369. # thus, package may be module, or may be module's parent if demand generated.
  370. package = gencache.GetModuleForTypelib(module.CLSID, module.LCID, module.MajorVersion, module.MinorVersion)
  371. try:
  372. struct_guid = package.RecordMap[name]
  373. except KeyError:
  374. raise ValueError("The structure '%s' is not defined in module '%s'" % (name, package))
  375. return pythoncom.GetRecordFromGuids(module.CLSID, module.MajorVersion, module.MinorVersion, module.LCID, struct_guid)
  376. ############################################
  377. # The base of all makepy generated classes
  378. ############################################
  379. class DispatchBaseClass:
  380. def __init__(self, oobj=None):
  381. if oobj is None:
  382. oobj = pythoncom.new(self.CLSID)
  383. elif isinstance(oobj, DispatchBaseClass):
  384. try:
  385. oobj = oobj._oleobj_.QueryInterface(self.CLSID, pythoncom.IID_IDispatch) # Must be a valid COM instance
  386. except pythoncom.com_error, details:
  387. import winerror
  388. # Some stupid objects fail here, even tho it is _already_ IDispatch!!??
  389. # Eg, Lotus notes.
  390. # So just let it use the existing object if E_NOINTERFACE
  391. if details.hresult != winerror.E_NOINTERFACE:
  392. raise
  393. oobj = oobj._oleobj_
  394. self.__dict__["_oleobj_"] = oobj # so we dont call __setattr__
  395. # Provide a prettier name than the CLSID
  396. def __repr__(self):
  397. # Need to get the docstring for the module for this class.
  398. try:
  399. mod_doc = sys.modules[self.__class__.__module__].__doc__
  400. if mod_doc:
  401. mod_name = "win32com.gen_py." + mod_doc
  402. else:
  403. mod_name = sys.modules[self.__class__.__module__].__name__
  404. except KeyError:
  405. mod_name = "win32com.gen_py.unknown"
  406. return "<%s.%s instance at 0x%s>" % (mod_name, self.__class__.__name__, id(self))
  407. # Delegate comparison to the oleobjs, as they know how to do identity.
  408. def __eq__(self, other):
  409. other = getattr(other, "_oleobj_", other)
  410. return self._oleobj_ == other
  411. def __ne__(self, other):
  412. other = getattr(other, "_oleobj_", other)
  413. return self._oleobj_ != other
  414. def _ApplyTypes_(self, dispid, wFlags, retType, argTypes, user, resultCLSID, *args):
  415. return self._get_good_object_(
  416. self._oleobj_.InvokeTypes(dispid, 0, wFlags, retType, argTypes, *args),
  417. user, resultCLSID)
  418. def __getattr__(self, attr):
  419. args=self._prop_map_get_.get(attr)
  420. if args is None:
  421. raise AttributeError("'%s' object has no attribute '%s'" % (repr(self), attr))
  422. return self._ApplyTypes_(*args)
  423. def __setattr__(self, attr, value):
  424. if attr in self.__dict__: self.__dict__[attr] = value; return
  425. try:
  426. args, defArgs=self._prop_map_put_[attr]
  427. except KeyError:
  428. raise AttributeError("'%s' object has no attribute '%s'" % (repr(self), attr))
  429. self._oleobj_.Invoke(*(args + (value,) + defArgs))
  430. def _get_good_single_object_(self, obj, obUserName=None, resultCLSID=None):
  431. return _get_good_single_object_(obj, obUserName, resultCLSID)
  432. def _get_good_object_(self, obj, obUserName=None, resultCLSID=None):
  433. return _get_good_object_(obj, obUserName, resultCLSID)
  434. # XXX - These should be consolidated with dynamic.py versions.
  435. def _get_good_single_object_(obj, obUserName=None, resultCLSID=None):
  436. if _PyIDispatchType==type(obj):
  437. return Dispatch(obj, obUserName, resultCLSID)
  438. return obj
  439. def _get_good_object_(obj, obUserName=None, resultCLSID=None):
  440. if obj is None:
  441. return None
  442. elif isinstance(obj, tuple):
  443. obUserNameTuple = (obUserName,) * len(obj)
  444. resultCLSIDTuple = (resultCLSID,) * len(obj)
  445. return tuple(map(_get_good_object_, obj, obUserNameTuple, resultCLSIDTuple))
  446. else:
  447. return _get_good_single_object_(obj, obUserName, resultCLSID)
  448. class CoClassBaseClass:
  449. def __init__(self, oobj=None):
  450. if oobj is None: oobj = pythoncom.new(self.CLSID)
  451. self.__dict__["_dispobj_"] = self.default_interface(oobj)
  452. def __repr__(self):
  453. return "<win32com.gen_py.%s.%s>" % (__doc__, self.__class__.__name__)
  454. def __getattr__(self, attr):
  455. d=self.__dict__["_dispobj_"]
  456. if d is not None: return getattr(d, attr)
  457. raise AttributeError(attr)
  458. def __setattr__(self, attr, value):
  459. if attr in self.__dict__: self.__dict__[attr] = value; return
  460. try:
  461. d=self.__dict__["_dispobj_"]
  462. if d is not None:
  463. d.__setattr__(attr, value)
  464. return
  465. except AttributeError:
  466. pass
  467. self.__dict__[attr] = value
  468. # A very simple VARIANT class. Only to be used with poorly-implemented COM
  469. # objects. If an object accepts an arg which is a simple "VARIANT", but still
  470. # is very pickly about the actual variant type (eg, isn't happy with a VT_I4,
  471. # which it would get from a Python integer), you can use this to force a
  472. # particular VT.
  473. class VARIANT(object):
  474. def __init__(self, vt, value):
  475. self.varianttype = vt
  476. self._value = value
  477. # 'value' is a property so when set by pythoncom it gets any magic wrapping
  478. # which normally happens for result objects
  479. def _get_value(self):
  480. return self._value
  481. def _set_value(self, newval):
  482. self._value = _get_good_object_(newval)
  483. def _del_value(self):
  484. del self._value
  485. value = property(_get_value, _set_value, _del_value)
  486. def __repr__(self):
  487. return "win32com.client.VARIANT(%r, %r)" % (self.varianttype, self._value)