diff --git a/sconstruct b/sconstruct index 6132a8fe360..062e7a8882e 100755 --- a/sconstruct +++ b/sconstruct @@ -38,6 +38,7 @@ def recursiveCopy(env,targetDir,sourceDir): import gettext gettext.install("nvda") sys.path.append("source") +import comtypesMonkeyPatches import versionInfo del sys.path[-1] diff --git a/source/comInterfaces_sconscript b/source/comInterfaces_sconscript index fe29e33ac76..ab05f52d13d 100755 --- a/source/comInterfaces_sconscript +++ b/source/comInterfaces_sconscript @@ -31,11 +31,9 @@ interfaceBuilder=env.Builder( ) env['BUILDERS']['comtypesInterface']=interfaceBuilder - #Bit of a dance to force comtypes generated interfaces in to our directory +# Force comtypes generated interfaces in to our directory import comtypes.client comtypes.client.gen_dir=Dir('comInterfaces').abspath -import sys -sys.modules['comtypes.gen']=comtypes.gen=__import__("comInterfaces",globals(),locals(),[]) COM_INTERFACES = { "IAccessible2Lib.py": "typelibs/ia2.tlb", diff --git a/source/comtypesMonkeyPatches.py b/source/comtypesMonkeyPatches.py index c5031c13f6c..c599c475ea4 100644 --- a/source/comtypesMonkeyPatches.py +++ b/source/comtypesMonkeyPatches.py @@ -1,10 +1,12 @@ -#A part of NonVisual Desktop Access (NVDA) -#Copyright (C) 2009-2016 NV Access Limited -#This file is covered by the GNU General Public License. -#See the file COPYING for more details. +# A part of NonVisual Desktop Access (NVDA) +# Copyright (C) 2009-2019 NV Access Limited, Babbage B.V. +# This file is covered by the GNU General Public License. +# See the file COPYING for more details. import ctypes import _ctypes +import importlib +import comtypes.client._generate # A version of ctypes.WINFUNCTYPE # that produces a WinFunctionType class whose instance will convert COMError into a CallCancelled exception when called as a function. @@ -125,3 +127,15 @@ def _check_version(actual): if actual != required: raise ImportError("Wrong version") comtypes._check_version = _check_version + + +# Monkeypatch comtypes to clear the importlib cache when importing a new module +old_my_import = comtypes.client._generate._my_import + + +def new_my_import(fullname): + importlib.invalidate_caches() + return old_my_import(fullname) + + +comtypes.client._generate._my_import = new_my_import