Fix Access to be reloadable -- Issue #2268 #2296

Open
wants to merge 5 commits into
from

Projects

None yet

2 participants

@wiredfool
Member

Fixes Access to be reloaded if the python interpreter is restarted when embedded. Fixes #2268.

Test from cgohlke

@wiredfool
Member

@cgohlke I'm not getting this reloading working with my poking here? Any suggestions?

@cgohlke
Contributor
cgohlke commented Jan 4, 2017

Works for me on Python 2.7.

I don't know about the AppVeyor failure, but for Python 3 and to catch crashes, the test need to be adjusted. This worked for me on Python 2.7 and >= 3.5:

diff --git a/Tests/test_image_access.py b/Tests/test_image_access.py
index 900f39e..3d9a3c0 100644
--- a/Tests/test_image_access.py
+++ b/Tests/test_image_access.py
@@ -7,7 +7,7 @@ except ImportError:
     pass
 
 from PIL import Image
-
+import sys
 
 class AccessTest(PillowTestCase):
     # initial value
@@ -246,5 +246,56 @@ class TestCffi(AccessTest):
                 self.assertEqual(px[i, 0], 0)
 
 
+class TestEmbeddable(unittest.TestCase):
+    @unittest.skipIf(not sys.platform.startswith('win32') or
+                     sys.version_info[:2] in ((3, 3), (3, 4)),
+                     "requires Python 2.7 or >=3.5 for Windows")
+    def test_embeddable(self):
+        import subprocess
+        import ctypes
+        from distutils import ccompiler
+
+        with open('embed_pil.c', 'w') as fh:
+            fh.write("""
+#include "Python.h"
+
+int main(int argc, char* argv[])
+{
+    char *home = "%s";
+#if PY_MAJOR_VERSION >= 3
+    wchar_t *whome = Py_DecodeLocale(home, NULL);
+    Py_SetPythonHome(whome);
+#else
+    Py_SetPythonHome(home);
+#endif
+
+    Py_InitializeEx(0);
+    Py_DECREF(PyImport_ImportModule("PIL.Image"));
+    Py_Finalize();
+
+    Py_InitializeEx(0);
+    Py_DECREF(PyImport_ImportModule("PIL.Image"));
+    Py_Finalize();
+
+#if PY_MAJOR_VERSION >= 3
+    PyMem_RawFree(whome);
+#endif
+
+    return 0;
+}
+        """ % sys.prefix.replace('\\', '\\\\'))
+
+        compiler = ccompiler.new_compiler()
+        objects = compiler.compile(['embed_pil.c'])
+        compiler.link_executable(objects, 'embed_pil')
+        
+        # do not display the Windows Error Reporting dialog
+        ctypes.windll.kernel32.SetErrorMode(0x0002)
+        
+        process = subprocess.Popen(['embed_pil.exe'])
+        process.communicate()
+        self.assertEqual(process.returncode, 0)
+
+
 if __name__ == '__main__':
     unittest.main()
wiredfool added some commits Dec 20, 2016
@wiredfool @wiredfool wiredfool Fix Access to be reloaded if the python interpreter is restarted when…
… embedded. Fixes #2268.
9a0dfec
@wiredfool wiredfool fixed test on python27 OMM
eafc265
@wiredfool wiredfool fix for appveyor environment?
ef4407c
@wiredfool wiredfool in-progress appveyor
4645dbf
@wiredfool wiredfool appveyor -- disable fast finish
d447a28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment