Skip to content

Commit

Permalink
Merge pull request #2296 from wiredfool/issue_2268
Browse files Browse the repository at this point in the history
Fix Access to be reloadable -- Issue #2268
  • Loading branch information
wiredfool committed Apr 3, 2017
2 parents 53df626 + fe0a331 commit bfa80cd
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
66 changes: 64 additions & 2 deletions Tests/test_image_access.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from helper import unittest, PillowTestCase, hopper
from helper import unittest, PillowTestCase, hopper, on_appveyor

try:
from PIL import PyAccess
Expand All @@ -7,7 +7,8 @@
pass

from PIL import Image

import sys
import os

class AccessTest(PillowTestCase):
# initial value
Expand Down Expand Up @@ -249,5 +250,66 @@ def test_reference_counting(self):
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)) or
on_appveyor(), # failing on appveyor when run from
# subprocess, not from shell
"requires Python 2.7 or >=3.5 for Windows")
def test_embeddable(self):
import subprocess
import ctypes
import setuptools
from distutils import ccompiler, sysconfig

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()
compiler.add_include_dir(sysconfig.get_python_inc())

libdir = sysconfig.get_config_var('LIBDIR') or sysconfig.get_python_inc().replace('include', 'libs')
print (libdir)
compiler.add_library_dir(libdir)
objects = compiler.compile(['embed_pil.c'])
compiler.link_executable(objects, 'embed_pil')

env = os.environ.copy()
env["PATH"] = sys.prefix + ';' + env["PATH"]

# do not display the Windows Error Reporting dialog
ctypes.windll.kernel32.SetErrorMode(0x0002)

process = subprocess.Popen(['embed_pil.exe'], env=env)
process.communicate()
self.assertEqual(process.returncode, 0)

if __name__ == '__main__':
unittest.main()
2 changes: 1 addition & 1 deletion libImaging/Access.c
Original file line number Diff line number Diff line change
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 bfa80cd

Please sign in to comment.