<s> import unittest <EOL> from ctypes import PyDLL , py_object , c_void_p , byref , POINTER <EOL> from ctypes . wintypes import BOOL <EOL> from comtypes import IUnknown <EOL> from comtypes . client import CreateObject <EOL> from comtypes . automation import IDispatch <EOL> from comtypes . test import requires <EOL> requires ( "<STR_LIT>" ) <EOL> import pythoncom <EOL> import win32com . client <EOL> _PyCom_PyObjectFromIUnknown = PyDLL ( pythoncom . __file__ ) . PyCom_PyObjectFromIUnknown <EOL> _PyCom_PyObjectFromIUnknown . restype = py_object <EOL> _PyCom_PyObjectFromIUnknown . argtypes = ( POINTER ( IUnknown ) , c_void_p , BOOL ) <EOL> def comtypes2pywin ( ptr , interface = None ) : <EOL> """<STR_LIT>""" <EOL> if interface is None : <EOL> interface = IUnknown <EOL> return _PyCom_PyObjectFromIUnknown ( ptr , byref ( interface . _iid_ ) , True ) <EOL> def comtypes_get_refcount ( ptr ) : <EOL> """<STR_LIT>""" <EOL> ptr . AddRef ( ) <EOL> return ptr . Release ( ) <EOL> from comtypes import COMObject <EOL> class MyComObject ( COMObject ) : <EOL> """<STR_LIT>""" <EOL> _com_interfaces_ = [ IDispatch ] <EOL> class Test ( unittest . TestCase ) : <EOL> def tearDown ( self ) : <EOL> if hasattr ( self , "<STR_LIT>" ) : <EOL> self . ie . Quit ( ) <EOL> del self . ie <EOL> def test_mycomobject ( self ) : <EOL> o = MyComObject ( ) <EOL> p = comtypes2pywin ( o , IDispatch ) <EOL> disp = win32com . client . Dispatch ( p ) <EOL> self . failUnlessEqual ( repr ( disp ) , "<STR_LIT>" ) <EOL> def test_ie ( self ) : <EOL> ie = self . ie = CreateObject ( "<STR_LIT>" ) <EOL> self . failUnlessEqual ( comtypes_get_refcount ( ie ) , <NUM_LIT:1> ) <EOL> self . failUnlessEqual ( ie . Visible , False ) <EOL> p = comtypes2pywin ( ie , interface = IDispatch ) <EOL> self . failUnlessEqual ( comtypes_get_refcount ( ie ) , <NUM_LIT:2> ) <EOL> disp = win32com . client . Dispatch ( p ) <EOL> self . failUnlessEqual ( comtypes_get_refcount ( ie ) , <NUM_LIT:2> ) <EOL> self . failUnlessEqual ( disp . Visible , False ) <EOL> del p , disp <EOL> self . failUnlessEqual ( comtypes_get_refcount ( ie ) , <NUM_LIT:1> ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> unittest . main ( ) </s>
