Skip to content

Commit

Permalink
Fix Access to be reloaded if the python interpreter is restarted when…
Browse files Browse the repository at this point in the history
… embedded. Fixes python-pillow#2268.
  • Loading branch information
wiredfool committed Jan 10, 2017
1 parent b7a2753 commit 9a0dfec
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
34 changes: 33 additions & 1 deletion Tests/test_image_access.py
Expand Up @@ -7,7 +7,7 @@
pass

from PIL import Image

import sys

class AccessTest(PillowTestCase):
# initial value
Expand Down Expand Up @@ -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()
2 changes: 1 addition & 1 deletion libImaging/Access.c
Expand Up @@ -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);
Expand Down

0 comments on commit 9a0dfec

Please sign in to comment.