From 9a0dfec2ff990fc7eaf385d73990655c9ac77b74 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 900f39eb488..f538eea72c2 100644 --- a/Tests/test_image_access.py +++ b/Tests/test_image_access.py @@ -7,7 +7,7 @@ pass from PIL import Image - +import sys class AccessTest(PillowTestCase): # initial value @@ -246,5 +246,37 @@ def test_reference_counting(self): 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 059f2aaebce..292968f1c46 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);