11import os
2- import re
32import site
43import sys
54
@@ -8,14 +7,11 @@ def get_fisher_py_path():
87 path = os .path .join (site_pkg , 'fisher_py' )
98 if os .path .exists (path ):
109 return path
11-
12- # Also check virtual environments where site.getsitepackages() might be missing
1310 for p in sys .path :
1411 path = os .path .join (p , 'fisher_py' )
1512 if os .path .exists (path ):
1613 return path
17-
18- raise RuntimeError ("fisher_py not found in sys.path or site-packages" )
14+ raise RuntimeError ("fisher_py not found" )
1915
2016def patch_fisher_py ():
2117 try :
@@ -25,34 +21,38 @@ def patch_fisher_py():
2521 with open (init_file , 'r' ) as f :
2622 content = f .read ()
2723
28- # Add import sys if needed
29- if "import sys" not in content :
30- content = "import sys\n " + content
31-
32- # Ensure sys.path.append(dll_path) is added
33- if "sys.path.append(os.path.realpath(dll_path))" not in content :
34- content = content .replace ("clr.AddReference('mscorlib')" , "clr.AddReference('mscorlib')\n sys.path.append(os.path.realpath(dll_path))" )
24+ if "from System.Reflection import Assembly" not in content :
25+ content = content .replace ("from System import Environment" , "from System import Environment\n from System.Reflection import Assembly" )
26+
27+ import re
28+
29+ # Add helper function at the top of the file after imports
30+ helper = """
31+ def _safe_load_assembly(path):
32+ try:
33+ Assembly.LoadFrom(path)
34+ except Exception:
35+ pass
36+ """
37+ if "_safe_load_assembly" not in content :
38+ content = content .replace ("import os" , "import os\n " + helper , 1 )
39+
40+ def replacer (match ):
41+ original = match .group (1 )
42+ return f"_safe_load_assembly(os.path.realpath({ original } ))"
3543
36- # Replace clr.AddReference(os.path.join(dll_path, 'AssemblyName.dll'))
37- # with clr.AddReference('AssemblyName')
38- content = re .sub (
39- r"clr\.AddReference\((?:os\.path\.join\()?dll_path,\s*'([^']+)\.dll'(?:\))?\)" ,
40- r"clr.AddReference('\1')" ,
41- content
42- )
43- # Also clean up any lingering Assembly.LoadFrom from previous patches just in case
4444 content = re .sub (
45- r"Assembly\.LoadFrom\(os\.path\.realpath\ (os\.path\.join\(dll_path,\s*'( [^']+)\.dll '\)\ )\)" ,
46- r"clr.AddReference('\1')" ,
45+ r"clr\.AddReference\( (os\.path\.join\(dll_path,\s*'[^']+'\))\)" ,
46+ replacer ,
4747 content
4848 )
49-
49+
5050 with open (init_file , 'w' ) as f :
5151 f .write (content )
5252
5353 print (f"Successfully patched { init_file } " )
5454 except Exception as e :
55- print (f"Failed to patch fisher_py : { e } " )
55+ print (f"Failed to patch: { e } " )
5656 sys .exit (1 )
5757
5858if __name__ == '__main__' :
0 commit comments