Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Clean: Improve PIL.Image hook to not use directly __import__() statem…

…ent.
  • Loading branch information...
commit d437018f0fca2b15bbdd5b380c1654d354ae5672 1 parent 292045a
@matysek matysek authored
View
2  PyInstaller/hooks/hook-Image.py
@@ -1,3 +1,4 @@
+#
# Copyright (C) 2005, Giovanni Bajo
# Based on previous work under copyright (c) 2001, 2002 McMillan Enterprises, Inc.
#
@@ -15,6 +16,7 @@
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
+
# Forward to shared code for PIL. PIL can be imported either as a top-level package
# (from PIL import Image), or not (import Image), because it installs a
# PIL.pth.
View
8 PyInstaller/hooks/hook-PIL.Image.py
@@ -1,3 +1,4 @@
+#
# Copyright (C) 2005, Giovanni Bajo
# Based on previous work under copyright (c) 2001, 2002 McMillan Enterprises, Inc.
#
@@ -15,13 +16,6 @@
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
-# FIXME: for a strange bug in Python's import machinery, we need to adjust
-# the module name before proceeding to the PIL import. The name would
-# otherwise be "hooks.hook-PIL.Image", which will then produce this
-# monstruosity:
-# <module 'hooks.hook-PIL.PIL.Image' from 'C:\python24\lib\site-packages\PIL\Image.pyc'>
-#
-__name__ = "hook-image"
# Forward to shared code for PIL. PIL can be imported either as a top-level package
# (from PIL import Image), or not (import Image), because it installs a
View
38 PyInstaller/hooks/shared_PIL_Image.py
@@ -1,3 +1,4 @@
+#
# Copyright (C) 2005, Giovanni Bajo
# Based on previous work under copyright (c) 2001, 2002 McMillan Enterprises, Inc.
#
@@ -15,23 +16,38 @@
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
-import sys
+
+from PyInstaller.hooks import hookutils
+
hiddenimports = []
+
def hook(mod):
global hiddenimports
# `PIL.Image` may be imported as `PIL.Image` or as `Image`
# (without the prefix). We need to use the same module name to
# avoid the same module under two different names.
- __import__(mod.__name__)
- image_mod = sys.modules[mod.__name__]
- # PIL uses lazy initialization.
- # first import the default stuff ...
- image_mod.preinit()
- # ... then every available plugin
- image_mod.init()
- for name in sys.modules:
- if name.endswith("ImagePlugin"):
- hiddenimports.append(name)
+ # We cannot import modules directly in PyInstaller.
+ statement = """
+import sys
+__import__('%(modname)s')
+image_mod = sys.modules['%(modname)s']
+# PIL uses lazy initialization.
+# first import the default stuff ...
+image_mod.preinit()
+# ... then every available plugin
+image_mod.init()
+for name in sys.modules:
+ if name.endswith('ImagePlugin'):
+ # Modules are printed to stdout and the output is then parsed.
+ print name
+""" % {'modname': mod.__name__}
+ out = hookutils.exec_statement(statement)
+ hiddenimports = out.strip().splitlines()
+ # Ignore 'FixTk' to prevent inclusion of Tcl/Tk library.
+ for i, m in enumerate(mod.imports):
+ if m[0] == 'FixTk':
+ del mod.imports[i]
+ break
return mod
View
8 PyInstaller/hooks/shared_PIL_SpiderImagePlugin.py
@@ -1,3 +1,4 @@
+#
# Copyright (C) 2006, Giovanni Bajo
# Based on previous work under copyright (c) 2001, 2002 McMillan Enterprises, Inc.
#
@@ -15,15 +16,20 @@
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
+
# PIL's SpiderImagePlugin features a tkPhotoImage() method which imports
# ImageTk (and thus brings the whole Tcl/Tk library in).
# We cheat a little and remove the ImageTk import: I assume that if people
# are really using ImageTk in their application, they will also import it
# directly.
+
def hook(mod):
for i, m in enumerate(mod.imports):
- if m[0] == "ImageTk":
+ # Ignore these two modules to not include whole Tk or Qt stack.
+ # If these modules should be included then they will definitely
+ # be dependency as any other module.
+ if m[0] == 'ImageTk':
del mod.imports[i]
break
return mod
Please sign in to comment.
Something went wrong with that request. Please try again.