Skip to content

Commit

Permalink
Python3.12: Added support for "cv2" with updated workaround
Browse files Browse the repository at this point in the history
* There is no "imp" module to help us anymore, so we need to
  do things manually at runtime for the strange "cv2" native
  module import.
  • Loading branch information
kayhayen committed Jun 14, 2024
1 parent 9c2a979 commit 41ab41e
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions nuitka/plugins/standard/standard.nuitka-package.config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,7 @@
# Not necessary on Windows
when: 'not win32'

- module-name: 'cv2' # checksum: 2aa22aa9
- module-name: 'cv2' # checksum: 586ad888
data-files:
dirs:
- 'qt/fonts'
Expand All @@ -794,21 +794,26 @@
- 'opencv_videoio'
when: 'win32'
anti-bloat:
- description: 'workaround for colliding native module import'
- description: 'workaround for config module as code'
context:
- 'import textwrap'
- 'import pkgutil'
- 'from nuitka.utils.Importing import getSharedLibrarySuffix'
replacements_plain:
? "load_first_config([\n"
: 'if False: (['
replacements:
"load_first_config(['config.py'], True)": "textwrap.indent(pkgutil.get_data('cv2', 'config.py').decode('utf8'), ' ') + textwrap.indent(pkgutil.get_data('cv2', 'config-3.py').decode('utf8'), ' ')"
- description: 'workaround for colliding native module import'
replacements:
# Before OpenCV 4.6
'native_module = importlib.import_module("cv2.cv2")': '"import imp; native_module = imp.load_dynamic(\"cv2\", os.path.join(os.path.dirname(__file__), \"cv2%s\"))" % getSharedLibrarySuffix(False)'
# After OpenCV 4.6
'native_module = importlib.import_module("cv2")': '"import imp; native_module = imp.load_dynamic(\"cv2\", os.path.join(os.path.dirname(__file__), \"cv2%s\"))" % getSharedLibrarySuffix(False)'
when: 'standalone'
'native_module = importlib.import_module("cv2.cv2")': '"import imp; native_module = imp.load_dynamic(\"cv2\", os.path.join(os.path.dirname(__file__), \"cv2%s\"))" % extension_suffix'
# After OpenCV 4.6
'native_module = importlib.import_module("cv2")': '"import imp; native_module = imp.load_dynamic(\"cv2\", os.path.join(os.path.dirname(__file__), \"cv2%s\"))" % extension_suffix'
when: 'standalone and before_python312'
- description: 'workaround for colliding native module import'
replacements:
'native_module = importlib.import_module("cv2")': '"from importlib.machinery import ExtensionFileLoader; from importlib.util import spec_from_file_location; from importlib._bootstrap import _load; __path = os.path.join(os.path.dirname(__file__), \"cv2%s\"); native_module = _load(spec_from_file_location(\"cv2\", __path, loader=ExtensionFileLoader(\"cv2\", __path)))" % extension_suffix'
when: 'standalone and python312_or_higher'
implicit-imports:
- depends:
- 'cv2.cv2'
Expand Down

0 comments on commit 41ab41e

Please sign in to comment.