-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatch.py
More file actions
73 lines (60 loc) · 2.34 KB
/
Copy pathpatch.py
File metadata and controls
73 lines (60 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
"""
Patch for MediaPipe C bindings to work with Python 3.14+
This patches the ctypes library loading to avoid the 'free' function lookup error.
Run this ONCE after installing mediapipe to patch your installation.
"""
import sys
import os
from pathlib import Path
def apply_patch():
"""Apply the fix to mediapipe's C bindings"""
# Find the MediaPipe installation
try:
import mediapipe
mediapipe_path = Path(mediapipe.__file__).parent
except ImportError:
print("ERROR: MediaPipe is not installed")
return False
bindings_file = mediapipe_path / "tasks" / "python" / "core" / "mediapipe_c_bindings.py"
if not bindings_file.exists():
print(f"ERROR: Could not find {bindings_file}")
return False
print(f"Found MediaPipe at: {mediapipe_path}")
print(f"Patching: {bindings_file}")
# Read the file
content = bindings_file.read_text(encoding='utf-8')
# Check if already patched
if "PYTHON314_PATCH_APPLIED" in content:
print("✓ Already patched!")
return True
# Apply the patch - replace the problematic function
original_snippet = """def load_raw_library():
\"\"\"Loads the native library.\"\"\"
_shared_lib = _load_library()
_shared_lib.free.argtypes = [ctypes.c_void_p]
_shared_lib.free.restype = None
return _shared_lib"""
patched_snippet = """def load_raw_library():
\"\"\"Loads the native library.\"\"\"
_shared_lib = _load_library()
# PYTHON314_PATCH_APPLIED: Skip 'free' function binding for Python 3.14+ compatibility
try:
_shared_lib.free.argtypes = [ctypes.c_void_p]
_shared_lib.free.restype = None
except AttributeError:
# Python 3.14+ ctypes issue - 'free' may not be available
# This is safe to skip as Python's garbage collector handles cleanup
pass
return _shared_lib"""
if original_snippet in content:
new_content = content.replace(original_snippet, patched_snippet)
bindings_file.write_text(new_content, encoding='utf-8')
print("✓ Patch applied successfully!")
return True
else:
print("WARNING: Could not find the exact code snippet to patch")
print("The MediaPipe version may be different than expected")
return False
if __name__ == "__main__":
success = apply_patch()
sys.exit(0 if success else 1)