Commits

Ronald Oussoren  committed be78bb0

Tweak PIL recipe to work better with Pillow and Python 3

The JPEG plugin still doesn't work because that imports
JpegPresets using a relative import instead of from the PIL
package. That works in Pillow because it adjusts sys.path
before importing plugins, but doesn't work yet with py2app's
prescript for PIL.

  • Participants
  • Parent commits 80f35cd
  • Branches branch-0.7

Comments (0)

Files changed (4)

File examples/PIL/hello.py

+from __future__ import print_function
 import sys, os
-import Image
+import pprint
+import PIL.Image
 
-print "globals() is %r" % id(globals())
+PATH="/Library/Application Support/Carbonite/CarboniteAlerts.app/Contents/Resources/background_left_illustration.jpg"
+
+print("globals() is %r" % id(globals()))
 def somefunc():
-    print "globals() is %r" % id(globals())
-    print "Hello from py2app"
+    print("globals() is %r" % id(globals()))
+    print("Hello from py2app")
 
-    print "frozen", repr(getattr(sys, "frozen", None))
+    print("frozen", repr(getattr(sys, "frozen", None)))
 
-    print "sys.path", sys.path
-    print "sys.executable", sys.executable
-    print "sys.prefix", sys.prefix
-    print "sys.argv", sys.argv
-    print "os.getcwd()", os.getcwd()
+    print("sys.path", sys.path)
+    print("sys.executable", sys.executable)
+    print("sys.prefix", sys.prefix)
+    print("sys.argv", sys.argv)
+    print("os.getcwd()", os.getcwd())
+
+
 if __name__ == '__main__':
     somefunc()
+    img = PIL.Image.open(PATH)
+    print(type(img))
+    print(img.size)
+    pprint.pprint(list(sorted(sys.modules.keys())))

File py2app/build_app.py

                     flatpackages[pkg[0]] = pkg[1]
                 filters.extend(rval.get('filters', ()))
                 loader_files.extend(rval.get('loader_files', ()))
-                newbootstraps = map(self.get_bootstrap,
-                    rval.get('prescripts', ()))
+                newbootstraps = list(map(self.get_bootstrap,
+                    rval.get('prescripts', ())))
 
                 if rval.get('includes'):
                     find_needed_modules(mf, includes=rval['includes'])
                 for fn in newbootstraps:
                     if isinstance(fn, basestring):
                         mf.run_script(fn)
+
                 for target in self.targets:
                     target.prescripts.extend(newbootstraps)
                 break

File py2app/recipes/PIL/__init__.py

     if m is None or m.filename is None:
         return None
 
+    if mf.findNode('PIL.Image'):
+        have_PIL = True
+    else:
+        have_PIL = False
+
     plugins = set()
     visited = set()
     for folder in sys.path:
                 continue
             mod, ext = os.path.splitext(fn)
             try:
+                sys.path.insert(0, folder)
                 imp_find_module(mod)
-            except ImportError:
+                del sys.path[0]
+            except ImportError as exc:
                 pass
             else:
                 plugins.add(mod)
         visited.add(folder)
     s = StringIO('_recipes_pil_prescript(%r)\n' % list(plugins))
     for plugin in plugins:
-        mf.implyNodeReference(m, plugin)
+        if have_PIL:
+            mf.implyNodeReference(m, plugin)
+        else:
+            mf.implyNodeReference(m, 'PIL.' + plugin)
     mf.removeReference(m, 'FixTk')
     # Since Imaging-1.1.5, SpiderImagePlugin imports ImageTk conditionally.
     # This is not ever used unless the user is explicitly using Tk elsewhere.
 
     return dict(
         prescripts = ['py2app.recipes.PIL.prescript', s],
+        include = "PIL.JpegPresets", # Dodgy import from PIL.JpegPlugin in Pillow 2.0
         flatpackages = [os.path.dirname(m.filename)],
     )

File py2app/recipes/PIL/prescript.py

 def _recipes_pil_prescript(plugins):
     try:
         import Image
+        have_PIL = False
     except ImportError:
         from PIL import Image
+        have_PIL = True
 
     import sys
     def init():
         if Image._initialized >= 2:
             return
+
         for plugin in plugins:
             try:
+                if have_PIL:
+                    try:
+                        # First try absolute import through PIL (for Pillow support)
+                        # only then try relative imports
+                        m = __import__('PIL.' + plugin, globals(), locals(), [])
+                        m = getattr(m, plugin)
+                        sys.modules[plugin] = m
+                        continue
+                    except ImportError:
+                        pass
+
                 __import__(plugin, globals(), locals(), [])
             except ImportError:
                 if Image.DEBUG:
                     print('Image: failed to import')
+
         if Image.OPEN or Image.SAVE:
             Image._initialized = 2
     Image.init = init