From 878881b1b9997cb9891ce03590f0e988f955d930 Mon Sep 17 00:00:00 2001 From: wiredfool Date: Tue, 20 Dec 2016 07:40:06 -0800 Subject: [PATCH] Fix Access to be reloaded if the python interpreter is restarted when embedded. Fixes #2268. --- Tests/test_image_access.py | 34 +++++++++++++++++++++++++++++++++- libImaging/Access.c | 2 +- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/Tests/test_image_access.py b/Tests/test_image_access.py index 005bd5d35..92e8913a8 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 @@ -249,5 +249,37 @@ class TestCffi(AccessTest): self.assertEqual(px[i, 0], 0) +class TestEmbeddable(unittest.TestCase): + @unittest.skipIf(not sys.platform.startswith('win32'), "requires Windows") + def test_embeddable(self): + import subprocess + from distutils import ccompiler + + with open('embed_pil.c', 'w') as fh: + fh.write(""" +#include "Python.h" + +int main(int argc, char* argv[]) +{ + Py_SetPythonHome( "%s" ); + + Py_InitializeEx( 0 ); + Py_DECREF(PyImport_ImportModule( "PIL.Image" )); + Py_Finalize(); + + Py_InitializeEx( 0 ); + Py_DECREF(PyImport_ImportModule( "PIL.Image" )); + Py_Finalize(); + + return 0; +} + """ % sys.prefix.replace('\\', '\\\\')) + + compiler = ccompiler.new_compiler() + objects = compiler.compile(['embed_pil.c']) + compiler.link_executable(objects, 'embed_pil') + + subprocess.call(['embed_pil.exe']) + if __name__ == '__main__': unittest.main() diff --git a/libImaging/Access.c b/libImaging/Access.c index 059f2aaeb..292968f1c 100644 --- a/libImaging/Access.c +++ b/libImaging/Access.c @@ -32,7 +32,7 @@ add_item(const char* mode) { UINT32 i = hash(mode); /* printf("hash %s => %d\n", mode, i); */ - if (access_table[i].mode) { + if (access_table[i].mode && strcmp(access_table[i].mode, mode) != 0) { fprintf(stderr, "AccessInit: hash collision: %d for both %s and %s\n", i, mode, access_table[i].mode); exit(1);