From 7a3097586d84e5b6faa55972a94cd9873966751c Mon Sep 17 00:00:00 2001 From: wmayer Date: Sat, 14 Nov 2020 16:38:02 +0100 Subject: [PATCH] Win32: [skip ci] DLL resolution in Python 3.8 on Windows has changed --- src/App/FreeCADInit.py | 62 ++++++++++++++++++++++++------------------ 1 file changed, 36 insertions(+), 26 deletions(-) diff --git a/src/App/FreeCADInit.py b/src/App/FreeCADInit.py index 6f6868567d..8f1c946934 100644 --- a/src/App/FreeCADInit.py +++ b/src/App/FreeCADInit.py @@ -45,6 +45,41 @@ def removeFromPath(module_name): else: Wrn(module_name + " not found in sys.path\n") +def setupSearchPaths(PathExtension): + # DLL resolution in Python 3.8 on Windows has changed + if sys.platform == 'win32' and hasattr(os, "add_dll_directory"): + if "FREECAD_LIBPACK_BIN" in os.environ: + os.add_dll_directory(os.environ["FREECAD_LIBPACK_BIN"]) + for path in PathExtension: + os.add_dll_directory(path) + + PathEnvironment = PathExtension.pop(0) + os.pathsep + for path in PathExtension: + try: + PathEnvironment += path + os.pathsep + except UnicodeDecodeError: + Wrn('Filter invalid module path: u{}\n'.format(repr(path))) + + # new paths must be prepended to avoid to load a wrong version of a library + try: + os.environ["PATH"] = PathEnvironment + os.environ["PATH"] + except UnicodeDecodeError: + # See #0002238. FIXME: check again once ported to Python 3.x + Log('UnicodeDecodeError was raised when concatenating unicode string with PATH. Try to remove non-ascii paths...\n') + path = os.environ["PATH"].split(os.pathsep) + cleanpath=[] + for i in path: + if test_ascii(i): + cleanpath.append(i) + os.environ["PATH"] = PathEnvironment + os.pathsep.join(cleanpath) + Log('done\n') + except UnicodeEncodeError: + Log('UnicodeEncodeError was raised when concatenating unicode string with PATH. Try to replace non-ascii chars...\n') + os.environ["PATH"] = PathEnvironment.encode(errors='replace') + os.environ["PATH"] + Log('done\n') + except KeyError: + os.environ["PATH"] = PathEnvironment + FreeCAD._importFromFreeCAD = removeFromPath @@ -185,32 +220,7 @@ def InitApplications(): Log("Using "+ModDir+" as module path!\n") # In certain cases the PathExtension list can contain invalid strings. We concatenate them to a single string # but check that the output is a valid string - PathEnvironment = PathExtension.pop(0) + os.pathsep - for path in PathExtension: - try: - PathEnvironment += path + os.pathsep - except UnicodeDecodeError: - Wrn('Filter invalid module path: u{}\n'.format(repr(path))) - - # new paths must be prepended to avoid to load a wrong version of a library - try: - os.environ["PATH"] = PathEnvironment + os.environ["PATH"] - except UnicodeDecodeError: - # See #0002238. FIXME: check again once ported to Python 3.x - Log('UnicodeDecodeError was raised when concatenating unicode string with PATH. Try to remove non-ascii paths...\n') - path = os.environ["PATH"].split(os.pathsep) - cleanpath=[] - for i in path: - if test_ascii(i): - cleanpath.append(i) - os.environ["PATH"] = PathEnvironment + os.pathsep.join(cleanpath) - Log('done\n') - except UnicodeEncodeError: - Log('UnicodeEncodeError was raised when concatenating unicode string with PATH. Try to replace non-ascii chars...\n') - os.environ["PATH"] = PathEnvironment.encode(errors='replace') + os.environ["PATH"] - Log('done\n') - except KeyError: - os.environ["PATH"] = PathEnvironment + setupSearchPaths(PathExtension) path = os.environ["PATH"].split(os.pathsep) Log("System path after init:\n") for i in path: