Skip to content

Commit f7b03f6

Browse files
authored
ctypes: don't fail to load when embedded as a "frozen" package (pearu#187)
* ctypes: don't fail to load when embedded as a "frozen" package When the module is embedded in a executable (aka "frozen"), the __file__ path is virtual, and might not actually exists on the computer. This path is used at init to look for the DLL, by changing the current working directory to the containing folder. As it doesn't exist, it fails, and libtiff_ctypes cannot be imported. Instead of completely failing, now handle such case, and just don't use that folder as an extra location to look for the DLL. * lsm: drop useless "global" usage As noted by the latest flake8: F824 `global tiff_module_dict` is unused: name is never assigned in scope As the tiff_module_dict variable is only read, there is no need to use a "global".
1 parent e4e8a58 commit f7b03f6

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

libtiff/libtiff_ctypes.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,15 @@
2323

2424
cwd = os.getcwd()
2525
try:
26-
os.chdir(os.path.dirname(__file__))
26+
try:
27+
# Typically, on Windows, the CWD is among the folders searched to locate
28+
# a DLL. So change it to the directory containing this module, in case
29+
# the libtiff DLL was installed aside it (although that's not typically the case).
30+
os.chdir(os.path.dirname(__file__))
31+
except FileNotFoundError:
32+
# If "frozen" (ie, embedded in an executable), the directory is not real, and chdir fails
33+
# => just ignore (and look for the DLL in all the other standard locations)
34+
pass
2735
if os.name == 'nt':
2836
# assume that the directory of the libtiff DLL is in PATH.
2937
for lib in ('tiff', 'libtiff', 'libtiff3'):

libtiff/lsm.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ def IFDEntry_lsm_str_hook(entry):
3434
def IFDEntry_lsm_init_hook(ifdentry):
3535
"""Make tiff.IFDENTRYEntry CZ_LSM aware.
3636
"""
37-
global tiff_module_dict
3837
if ifdentry.tag == CZ_LSMInfo_tag:
3938
# replace type,count=(BYTE,500) with (CZ_LSMInfo, 1)
4039
reserved_bytes = (ifdentry.count - CZ_LSMInfo_dtype_fields_size)

0 commit comments

Comments
 (0)