Skip to content

Commit

Permalink
MTA mode for COM client (pywinauto#427)
Browse files Browse the repository at this point in the history
* Switch on COM client MTA mode by default

* Prevent pythoncom conflicts while setting COM client in MTA mode
  • Loading branch information
airelil authored and vasily-v-ryabov committed Oct 25, 2017
1 parent f0dbeb8 commit 946f152
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
15 changes: 13 additions & 2 deletions pywinauto/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,21 @@

__version__ = "0.6.3"

import sys
import sys # noqa: E402

if sys.platform == 'win32':
# Set up COM client MTA mode as early as possible as it affects not only
# the optional comtypes package we use, but pywin32.pythoncom module as well.
sys.coinit_flags = 0 # COINIT_MULTITHREADED = 0x0

# Also try to un-initialze pythoncom if it's already been loaded.
# However, importing only pythoncom can fail with the errors like:
# ImportError: No system module 'pywintypes' (pywintypes27.dll)
# So try to facilitate pywintypes*.dll loading with implicit import of win32api
import win32api # noqa: E402
import pythoncom # noqa: E402
pythoncom.CoUninitialize()

from . import findwindows
WindowAmbiguousError = findwindows.WindowAmbiguousError
ElementNotFoundError = findwindows.ElementNotFoundError
Expand All @@ -52,7 +64,6 @@

from .application import Application, WindowSpecification


class Desktop(object):

"""Simple class to call something like ``Desktop().WindowName.ControlName.method()``"""
Expand Down
11 changes: 6 additions & 5 deletions pywinauto/sysinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,18 @@
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

"""Simple module for checking whether Python and Windows are 32-bit or 64-bit"""
import os
import sys
import platform
import ctypes
import logging
import os # noqa: E402
import platform # noqa: E402
import ctypes # noqa: E402
import logging # noqa: E402


try:
# Disable 'INFO' logs from comtypes
log = logging.getLogger('comtypes')
log.setLevel('WARNING')
import comtypes
import comtypes # noqa: E402
UIA_support = True
except ImportError:
UIA_support = False
Expand Down
16 changes: 9 additions & 7 deletions pywinauto/unittests/test_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,16 @@ def testConnectWarning3264(self):
return

app = Application().start(self.sample_exe_inverted_bitness)
warnings.filterwarnings('always', category=UserWarning, append=True)
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
Application().connect(path=self.sample_exe_inverted_bitness)
# Appveyor misteries...
self.assertEqual(app.is_process_running(), True)

with mock.patch("warnings.warn") as mockWarn:
Application().connect(process=app.process)
app.kill_()
assert len(w) >= 1
assert issubclass(w[-1].category, UserWarning)
assert "64-bit" in str(w[-1].message)
args, kw = mockWarn.call_args
assert len(args) == 2
assert "64-bit" in args[0]
assert args[1].__name__ == 'UserWarning'


class ApplicationTestCases(unittest.TestCase):
Expand Down

0 comments on commit 946f152

Please sign in to comment.