def test(fn): print("The main thread is %d" % (win32api.GetCurrentThreadId())) GIT = CreateGIT() interp = win32com.client.Dispatch("Python.Interpreter") cookie = GIT.RegisterInterfaceInGlobal(interp._oleobj_, pythoncom.IID_IDispatch) events = fn(4, cookie) numFinished = 0 while 1: try: rc = win32event.MsgWaitForMultipleObjects(events, 0, 2000, win32event.QS_ALLINPUT) if rc >= win32event.WAIT_OBJECT_0 and rc < win32event.WAIT_OBJECT_0 + len( events): numFinished = numFinished + 1 if numFinished >= len(events): break elif rc == win32event.WAIT_OBJECT_0 + len(events): # a message # This is critical - whole apartment model demo will hang. pythoncom.PumpWaitingMessages() else: # Timeout print( "Waiting for thread to stop with interfaces=%d, gateways=%d" % (pythoncom._GetInterfaceCount(), pythoncom._GetGatewayCount())) except KeyboardInterrupt: break GIT.RevokeInterfaceFromGlobal(cookie) del interp del GIT
def test(fn): print "The main thread is %d" % (win32api.GetCurrentThreadId()) GIT = CreateGIT() interp = win32com.client.Dispatch("Python.Interpreter") cookie = GIT.RegisterInterfaceInGlobal(interp._oleobj_, pythoncom.IID_IDispatch) events = fn(4, cookie) numFinished = 0 while 1: try: rc = win32event.MsgWaitForMultipleObjects(events, 0, 2000, win32event.QS_ALLINPUT) if rc >= win32event.WAIT_OBJECT_0 and rc < win32event.WAIT_OBJECT_0 + len(events): numFinished = numFinished + 1 if numFinished >= len(events): break elif rc == win32event.WAIT_OBJECT_0 + len(events): # a message # This is critical - whole apartment model demo will hang. pythoncom.PumpWaitingMessages() else: # Timeout print "Waiting for thread to stop with interfaces=%d, gateways=%d" % ( pythoncom._GetInterfaceCount(), pythoncom._GetGatewayCount(), ) except KeyboardInterrupt: break GIT.RevokeInterfaceFromGlobal(cookie) del interp del GIT
def testall(): dotestall() pythoncom.CoUninitialize() print "AXScript Host worked correctly - %d/%d COM objects left alive." % ( pythoncom._GetInterfaceCount(), pythoncom._GetGatewayCount(), )
def _DoTestMarshal(self, fn, bCoWait=0): #print "The main thread is %d" % (win32api.GetCurrentThreadId()) threads, events = fn(2) numFinished = 0 while 1: try: if bCoWait: rc = pythoncom.CoWaitForMultipleHandles(0, 2000, events) else: # Specifying "bWaitAll" here will wait for messages *and* all events # (which is pretty useless) rc = win32event.MsgWaitForMultipleObjects( events, 0, 2000, win32event.QS_ALLINPUT) if rc >= win32event.WAIT_OBJECT_0 and rc < win32event.WAIT_OBJECT_0 + len( events): numFinished = numFinished + 1 if numFinished >= len(events): break elif rc == win32event.WAIT_OBJECT_0 + len(events): # a message # This is critical - whole apartment model demo will hang. pythoncom.PumpWaitingMessages() else: # Timeout print "Waiting for thread to stop with interfaces=%d, gateways=%d" % ( pythoncom._GetInterfaceCount(), pythoncom._GetGatewayCount()) except KeyboardInterrupt: break for t in threads: t.join(2) self.failIf(t.isAlive(), "thread failed to stop!?") threads = None # threads hold references to args
def __call__(self, result=None): # Always ensure we don't leak gateways/interfaces gc.collect() ni = _GetInterfaceCount() ng = _GetGatewayCount() self.real_test(result) # Failed - no point checking anything else if result.shouldStop or not result.wasSuccessful(): return self._do_leak_tests(result) gc.collect() lost_i = _GetInterfaceCount() - ni lost_g = _GetGatewayCount() - ng if lost_i or lost_g: msg = "%d interface objects and %d gateway objects leaked" \ % (lost_i, lost_g) result.addFailure(self.real_test, (AssertionError, msg, None))
def __call__(self, result = None): # Always ensure we don't leak gateways/interfaces gc.collect() ni = _GetInterfaceCount() ng = _GetGatewayCount() self.real_test(result) # Failed - no point checking anything else if result.shouldStop or not result.wasSuccessful(): return self._do_leak_tests(result) gc.collect() lost_i = _GetInterfaceCount() - ni lost_g = _GetGatewayCount() - ng if lost_i or lost_g: msg = "%d interface objects and %d gateway objects leaked" \ % (lost_i, lost_g) result.addFailure(self.real_test, (AssertionError, msg, None))
def release(): global _release_fns for fn in _release_fns: fn() time.sleep(0.1) refcount = pythoncom._GetInterfaceCount() if refcount > 0: print "Warning! %d COM references still alive" print "HFSS will likely refuse to shut down"
def release(): global _release_fns for fn in _release_fns: fn() time.sleep(0.1) refcount = pythoncom._GetInterfaceCount() if refcount > 0: print("Warning! %d COM references still alive") print("HFSS will likely refuse to shut down")
def CheckClean(): # Ensure no lingering exceptions - Python should have zero outstanding # COM objects sys.exc_traceback = sys.exc_value = sys.exc_type = None c = _GetInterfaceCount() if c: print "Warning - %d com interface objects still alive" % c c = _GetGatewayCount() if c: print "Warning - %d com gateway objects still alive" % c
def CheckClean(): # Ensure no lingering exceptions - Python should have zero outstanding # COM objects sys.exc_clear() c = _GetInterfaceCount() if c: print "Warning - %d com interface objects still alive" % c c = _GetGatewayCount() if c: print "Warning - %d com gateway objects still alive" % c
def __call__(self, result = None): # For the COM suite's sake, always ensure we don't leak # gateways/interfaces from pythoncom import _GetInterfaceCount, _GetGatewayCount gc.collect() ni = _GetInterfaceCount() ng = _GetGatewayCount() self.real_test(result) # Failed - no point checking anything else if result.shouldStop or not result.wasSuccessful(): return self._do_leak_tests(result) gc.collect() lost_i = _GetInterfaceCount() - ni lost_g = _GetGatewayCount() - ng if lost_i or lost_g: msg = "%d interface objects and %d gateway objects leaked" \ % (lost_i, lost_g) exc = AssertionError(msg) result.addFailure(self.real_test, (exc.__class__, exc, None))
def __call__(self, result=None): # For the COM suite's sake, always ensure we don't leak # gateways/interfaces from pythoncom import _GetInterfaceCount, _GetGatewayCount gc.collect() ni = _GetInterfaceCount() ng = _GetGatewayCount() self.real_test(result) # Failed - no point checking anything else if result.shouldStop or not result.wasSuccessful(): return self._do_leak_tests(result) gc.collect() lost_i = _GetInterfaceCount() - ni lost_g = _GetGatewayCount() - ng if lost_i or lost_g: msg = "%d interface objects and %d gateway objects leaked" \ % (lost_i, lost_g) exc = AssertionError(msg) result.addFailure(self.real_test, (exc.__class__, exc, None))
def CheckClean(): # Ensure no lingering exceptions - Python should have zero outstanding # COM objects try: sys.exc_clear() except AttributeError: pass # py3k c = _GetInterfaceCount() if c: print("Warning - %d com interface objects still alive" % c) c = _GetGatewayCount() if c: print("Warning - %d com gateway objects still alive" % c)
events = fn(4, cookie) numFinished = 0 while 1: try: rc = win32event.MsgWaitForMultipleObjects(events, 0, 2000, win32event.QS_ALLINPUT) if rc >= win32event.WAIT_OBJECT_0 and rc < win32event.WAIT_OBJECT_0+len(events): numFinished = numFinished + 1 if numFinished >= len(events): break elif rc==win32event.WAIT_OBJECT_0 + len(events): # a message # This is critical - whole apartment model demo will hang. pythoncom.PumpWaitingMessages() else: # Timeout print "Waiting for thread to stop with interfaces=%d, gateways=%d" % (pythoncom._GetInterfaceCount(), pythoncom._GetGatewayCount()) except KeyboardInterrupt: break GIT.RevokeInterfaceFromGlobal(cookie) del interp del GIT if __name__=='__main__': test(BeginThreadsSimpleMarshal) win32api.Sleep(500) # Doing CoUninit here stop Pythoncom.dll hanging when DLLMain shuts-down the process pythoncom.CoUninitialize() if pythoncom._GetInterfaceCount()!=0 or pythoncom._GetGatewayCount()!=0: print "Done with interfaces=%d, gateways=%d" % (pythoncom._GetInterfaceCount(), pythoncom._GetGatewayCount()) else: print "Done."
def test(): test_cxCpStockCur() # test_cxCpFutureCode() # test2() print 'com reference count :', pythoncom._GetInterfaceCount()
ret.append( HLIRegisteredTypeLibrary((keyName, versionStr), name)) num = num + 1 finally: win32api.RegCloseKey(key) win32ui.DoWaitCursor(0) ret.sort() return ret def main(): from pywin.tools import hierlist root = HLIRoot("COM Browser") if "app" in sys.modules: # do it in a window browser.MakeTemplate() browser.template.OpenObject(root) else: # list=hierlist.HierListWithItems( root, win32ui.IDB_BROWSER_HIER ) # dlg=hierlist.HierDialog("COM Browser",list) dlg = browser.dynamic_browser(root) dlg.DoModal() if __name__ == '__main__': main() ni = pythoncom._GetInterfaceCount() ng = pythoncom._GetGatewayCount() if ni or ng: print("Warning - exiting with %d/%d objects alive" % (ni, ng))
data = "METAFILE handle %d" % medium.data elif medium.tymed == pythoncom.TYMED_ENHMF: data = "ENHMETAFILE handle %d" % medium.data elif medium.tymed == pythoncom.TYMED_HGLOBAL: data = "%d bytes via HGLOBAL" % len(medium.data) elif medium.tymed == pythoncom.TYMED_FILE: data = "filename '%s'" % data elif medium.tymed == pythoncom.TYMED_ISTREAM: stream = medium.data stream.Seek(0, 0) bytes = 0 while 1: chunk = stream.Read(4096) if not chunk: break bytes += len(chunk) data = "%d bytes via IStream" % bytes elif medium.tymed == pythoncom.TYMED_ISTORAGE: data = "a IStorage" else: data = "*** unknown tymed!" print " -> got", data do = None if __name__ == '__main__': DumpClipboard() if pythoncom._GetInterfaceCount() + pythoncom._GetGatewayCount(): print "XXX - Leaving with %d/%d COM objects alive" % \ (pythoncom._GetInterfaceCount(), pythoncom._GetGatewayCount())
events): numFinished = numFinished + 1 if numFinished >= len(events): break elif rc == win32event.WAIT_OBJECT_0 + len(events): # a message # This is critical - whole apartment model demo will hang. pythoncom.PumpWaitingMessages() else: # Timeout print( "Waiting for thread to stop with interfaces=%d, gateways=%d" % (pythoncom._GetInterfaceCount(), pythoncom._GetGatewayCount())) except KeyboardInterrupt: break GIT.RevokeInterfaceFromGlobal(cookie) del interp del GIT if __name__ == '__main__': test(BeginThreadsSimpleMarshal) win32api.Sleep(500) # Doing CoUninit here stop Pythoncom.dll hanging when DLLMain shuts-down the process pythoncom.CoUninitialize() if pythoncom._GetInterfaceCount() != 0 or pythoncom._GetGatewayCount( ) != 0: print("Done with interfaces=%d, gateways=%d" % (pythoncom._GetInterfaceCount(), pythoncom._GetGatewayCount())) else: print("Done.")
elif medium.tymed==pythoncom.TYMED_MFPICT: data = "METAFILE handle %d" % medium.data elif medium.tymed==pythoncom.TYMED_ENHMF: data = "ENHMETAFILE handle %d" % medium.data elif medium.tymed==pythoncom.TYMED_HGLOBAL: data = "%d bytes via HGLOBAL" % len(medium.data) elif medium.tymed==pythoncom.TYMED_FILE: data = "filename '%s'" % data elif medium.tymed==pythoncom.TYMED_ISTREAM: stream = medium.data stream.Seek(0,0) bytes = 0 while 1: chunk = stream.Read(4096) if not chunk: break bytes += len(chunk) data = "%d bytes via IStream" % bytes elif medium.tymed==pythoncom.TYMED_ISTORAGE: data = "a IStorage" else: data = "*** unknown tymed!" print " -> got", data do = None if __name__=='__main__': DumpClipboard() if pythoncom._GetInterfaceCount()+pythoncom._GetGatewayCount(): print "XXX - Leaving with %d/%d COM objects alive" % \ (pythoncom._GetInterfaceCount(), pythoncom._GetGatewayCount())
# pyEngine2.AddCode(code) # from win32com.axdebug import axdebug # sessionProvider=pythoncom.CoCreateInstance(axdebug.CLSID_DefaultDebugSessionProvider,None,pythoncom.CLSCTX_ALL, axdebug.IID_IDebugSessionProvider) # sessionProvider.StartDebugSession(None) raw_input("Press enter to continue") # forthEngine.Start() pyEngine.Start() # Actually run the Python code vbEngine.Start() # Actually run the VB code except pythoncom.com_error, details: print "Script failed: %s (0x%x)" % (details[1], details[0]) # Now run the code expected to fail! # try: # pyEngine2.Start() # Actually run the Python code that fails! # print "Script code worked when it should have failed." # except pythoncom.com_error: # pass site._Close() if __name__ == '__main__': import win32com.axdebug.util try: TestEngine() except: traceback.print_exc() win32com.axdebug.util._dump_wrapped() sys.exc_type = sys.exc_value = sys.exc_traceback = None print pythoncom._GetInterfaceCount(),"com objects still alive"
_GetCurrentDebugger().Break() brk = Break set_trace = Break def dosomethingelse(): a = 2 b = "Hi there" def dosomething(): a = 1 b = 2 dosomethingelse() def test(): Break() raw_input("Waiting...") dosomething() print "Done" if __name__ == '__main__': print "About to test the debugging interfaces!" test() print " %d/%d com objects still alive" % (pythoncom._GetInterfaceCount(), pythoncom._GetGatewayCount())
def _DoTestMarshal(self, fn, bCoWait = 0): #print "The main thread is %d" % (win32api.GetCurrentThreadId()) threads, events = fn(2) numFinished = 0 while 1: try: if bCoWait: rc = pythoncom.CoWaitForMultipleHandles(0, 2000, events) else: # Specifying "bWaitAll" here will wait for messages *and* all events # (which is pretty useless) rc = win32event.MsgWaitForMultipleObjects(events, 0, 2000, win32event.QS_ALLINPUT) if rc >= win32event.WAIT_OBJECT_0 and rc < win32event.WAIT_OBJECT_0+len(events): numFinished = numFinished + 1 if numFinished >= len(events): break elif rc==win32event.WAIT_OBJECT_0 + len(events): # a message # This is critical - whole apartment model demo will hang. pythoncom.PumpWaitingMessages() else: # Timeout print "Waiting for thread to stop with interfaces=%d, gateways=%d" % (pythoncom._GetInterfaceCount(), pythoncom._GetGatewayCount()) except KeyboardInterrupt: break for t in threads: t.join(2) self.failIf(t.isAlive(), "thread failed to stop!?") threads = None # threads hold references to args
global currentDebugger if currentDebugger is None: currentDebugger = AXDebugger() return currentDebugger def Break(): _GetCurrentDebugger().Break() brk = Break set_trace = Break def dosomethingelse(): a=2 b = "Hi there" def dosomething(): a=1 b=2 dosomethingelse() def test(): Break() input("Waiting...") dosomething() print("Done") if __name__=='__main__': print("About to test the debugging interfaces!") test() print(" %d/%d com objects still alive" % (pythoncom._GetInterfaceCount(), pythoncom._GetGatewayCount()))
num = num + 1 finally: win32api.RegCloseKey(key) win32ui.DoWaitCursor(0) ret.sort() return ret def main(): from pywin.tools import hierlist root = HLIRoot("COM Browser") if sys.modules.has_key("app"): # do it in a window browser.MakeTemplate() browser.template.OpenObject(root) else: # list=hierlist.HierListWithItems( root, win32ui.IDB_BROWSER_HIER ) # dlg=hierlist.HierDialog("COM Browser",list) dlg = browser.dynamic_browser(root) dlg.DoModal() if __name__=='__main__': main() ni = pythoncom._GetInterfaceCount() ng = pythoncom._GetGatewayCount() if ni or ng: print "Warning - exiting with %d/%d objects alive" % (ni,ng)
def testall(): dotestall() pythoncom.CoUninitialize() print("AXScript Host worked correctly - %d/%d COM objects left alive." % ( pythoncom._GetInterfaceCount(), pythoncom._GetGatewayCount()))
events = fn(4, cookie) numFinished = 0 while 1: try: rc = win32event.MsgWaitForMultipleObjects(events, 0, 2000, win32event.QS_ALLINPUT) if rc >= win32event.WAIT_OBJECT_0 and rc < win32event.WAIT_OBJECT_0+len(events): numFinished = numFinished + 1 if numFinished >= len(events): break elif rc==win32event.WAIT_OBJECT_0 + len(events): # a message # This is critical - whole apartment model demo will hang. pythoncom.PumpWaitingMessages() else: # Timeout print("Waiting for thread to stop with interfaces=%d, gateways=%d" % (pythoncom._GetInterfaceCount(), pythoncom._GetGatewayCount())) except KeyboardInterrupt: break GIT.RevokeInterfaceFromGlobal(cookie) del interp del GIT if __name__=='__main__': test(BeginThreadsSimpleMarshal) win32api.Sleep(500) # Doing CoUninit here stop Pythoncom.dll hanging when DLLMain shuts-down the process pythoncom.CoUninitialize() if pythoncom._GetInterfaceCount()!=0 or pythoncom._GetGatewayCount()!=0: print("Done with interfaces=%d, gateways=%d" % (pythoncom._GetInterfaceCount(), pythoncom._GetGatewayCount())) else: print("Done.")
# from win32com.axdebug import axdebug # sessionProvider=pythoncom.CoCreateInstance(axdebug.CLSID_DefaultDebugSessionProvider,None,pythoncom.CLSCTX_ALL, axdebug.IID_IDebugSessionProvider) # sessionProvider.StartDebugSession(None) raw_input("Press enter to continue") # forthEngine.Start() pyEngine.Start() # Actually run the Python code vbEngine.Start() # Actually run the VB code except pythoncom.com_error, details: print "Script failed: %s (0x%x)" % (details[1], details[0]) # Now run the code expected to fail! # try: # pyEngine2.Start() # Actually run the Python code that fails! # print "Script code worked when it should have failed." # except pythoncom.com_error: # pass site._Close() if __name__ == '__main__': import win32com.axdebug.util try: TestEngine() except: traceback.print_exc() win32com.axdebug.util._dump_wrapped() sys.exc_type = sys.exc_value = sys.exc_traceback = None print pythoncom._GetInterfaceCount(), "com objects still alive"
# Minimize console window if not gui and window[1]: try: win32gui.CloseWindow(window[1]) except: traceback.print_exc(file=sys.stdout) # Infinite loop try: if gui: g.daemon = True g.start() while True: sleep(1) if doquit: raise SystemExit() except (KeyboardInterrupt, SystemExit) as e: echo("") echo("Ctrl-C quitting...") echo("") thread.doquit = True sleep(1) del thread # delete all references to the com object, otherwise iTunes won't close because of an open API connection thread = None sleep(1) # Wait so everything is really closed if pythoncom._GetInterfaceCount() != 0: echo("Not all references to the com object were cleared. %d references left." % pythoncom._GetInterfaceCount()) sleep(5) pythoncom.CoUninitialize()