From f2a4780b2dfa3afa88ef0f2bc2450a6d1faa57b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9drik=20Fuoco?= Date: Fri, 6 Jan 2023 11:53:56 -0500 Subject: [PATCH 1/2] Allow PyOpenColorIO module to load DLLs from Windows PATH environment variable with an opt-out option in case the user want the default behavior of Python 3.8+. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Cédrik Fuoco --- src/bindings/python/CMakeLists.txt | 12 +++++++++--- src/bindings/python/__init__.py | 24 ++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 src/bindings/python/__init__.py diff --git a/src/bindings/python/CMakeLists.txt b/src/bindings/python/CMakeLists.txt index f674139934..6f6daff2fd 100644 --- a/src/bindings/python/CMakeLists.txt +++ b/src/bindings/python/CMakeLists.txt @@ -227,16 +227,22 @@ target_compile_definitions(PyOpenColorIO PY_VERSION_PATCH=${Python_VERSION_PATCH} ) +# Set to site-package location. if(WIN32) set(_Python_VARIANT_PATH "${CMAKE_INSTALL_LIBDIR}/site-packages") else() set(_Python_VARIANT_PATH "${CMAKE_INSTALL_LIBDIR}/python${Python_VERSION_MAJOR}.${Python_VERSION_MINOR}/site-packages") endif() -# Create an internal global variable to access it in another scope but not publicly visible -# using ccmake. +# Create an internal global variable to access it in another scope but not publicly visible. +# The site-package location is needed in in setup_ocio.bat.in and setup_ocio.sh.in. set(PYTHON_VARIANT_PATH ${_Python_VARIANT_PATH} CACHE INTERNAL "") +# Set to PyOpenColorIO site-package location. +set(_PyOpenColorIO_SITE_PACKAGE_DIR "${PYTHON_VARIANT_PATH}/PyOpenColorIO") + install(TARGETS PyOpenColorIO - LIBRARY DESTINATION ${_Python_VARIANT_PATH} + LIBRARY DESTINATION ${_PyOpenColorIO_SITE_PACKAGE_DIR} ) + +install(FILES __init__.py DESTINATION ${_PyOpenColorIO_SITE_PACKAGE_DIR}) \ No newline at end of file diff --git a/src/bindings/python/__init__.py b/src/bindings/python/__init__.py new file mode 100644 index 0000000000..f791e1dd09 --- /dev/null +++ b/src/bindings/python/__init__.py @@ -0,0 +1,24 @@ +# SPDX-License-Identifier: BSD-3-Clause +# Copyright Contributors to the OpenColorIO Project. + +import os, sys, platform + +# +# Python 3.8+ has stopped loading DLLs from PATH environment variable on Windows. +# +# This code reproduce the old behavior (loading DLLs from PATH) by doing the following: +# 1 - Tokenizing PATH +# 2 - Checking that the directories exist and are not "." +# 3 - Add them to the DLL load path. +# +# The behavior describe above is opt-out which means that it is activated by default. +# A user can use the default behavior of Python 3.8+ by setting OCIO_PYTHON_LOAD_DLLS_FROM_PATH +# environment variable to 1. +# + +if sys.version_info >= (3, 8) and platform.system() == "Windows" and os.getenv("OCIO_PYTHON_LOAD_DLLS_FROM_PATH", "1") == "1": + for path in os.getenv("PATH", "").split(os.pathsep): + if os.path.exists(path) and path != ".": + os.add_dll_directory(path) + +from .PyOpenColorIO import * \ No newline at end of file From b8bd8d01071d465c712546b6f12a449cb0168b13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9drik=20Fuoco?= Date: Wed, 11 Jan 2023 08:46:16 -0500 Subject: [PATCH 2/2] Fixing typos in comments MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Cédrik Fuoco --- src/bindings/python/CMakeLists.txt | 2 +- src/bindings/python/__init__.py | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/bindings/python/CMakeLists.txt b/src/bindings/python/CMakeLists.txt index 6f6daff2fd..0d35f4a8fb 100644 --- a/src/bindings/python/CMakeLists.txt +++ b/src/bindings/python/CMakeLists.txt @@ -235,7 +235,7 @@ else() endif() # Create an internal global variable to access it in another scope but not publicly visible. -# The site-package location is needed in in setup_ocio.bat.in and setup_ocio.sh.in. +# The site-package location is needed in setup_ocio.bat.in and setup_ocio.sh.in. set(PYTHON_VARIANT_PATH ${_Python_VARIANT_PATH} CACHE INTERNAL "") # Set to PyOpenColorIO site-package location. diff --git a/src/bindings/python/__init__.py b/src/bindings/python/__init__.py index f791e1dd09..3ce17e051e 100644 --- a/src/bindings/python/__init__.py +++ b/src/bindings/python/__init__.py @@ -11,9 +11,9 @@ # 2 - Checking that the directories exist and are not "." # 3 - Add them to the DLL load path. # -# The behavior describe above is opt-out which means that it is activated by default. -# A user can use the default behavior of Python 3.8+ by setting OCIO_PYTHON_LOAD_DLLS_FROM_PATH -# environment variable to 1. +# The behavior described above is opt-out which means that it is activated by default. +# A user can opt-out and use the default behavior of Python 3.8+ by setting OCIO_PYTHON_LOAD_DLLS_FROM_PATH +# environment variable to 0. # if sys.version_info >= (3, 8) and platform.system() == "Windows" and os.getenv("OCIO_PYTHON_LOAD_DLLS_FROM_PATH", "1") == "1":