From 34d04d3e821d4da333c3a3300a93900af42068b6 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 28 Dec 2019 09:57:49 +1100 Subject: [PATCH 1/3] Replaced distutils with C version check --- Tests/test_file_libtiff.py | 7 +------ src/PIL/TiffImagePlugin.py | 10 ++++------ src/_imaging.c | 15 +++++++++++++++ 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/Tests/test_file_libtiff.py b/Tests/test_file_libtiff.py index c26c503c4..66855bb97 100644 --- a/Tests/test_file_libtiff.py +++ b/Tests/test_file_libtiff.py @@ -1,5 +1,4 @@ import base64 -import distutils.version import io import itertools import logging @@ -272,12 +271,8 @@ class TestFileLibTiff(LibTiffTestCase): ) } - libtiff_version = TiffImagePlugin._libtiff_version() - libtiffs = [False] - if distutils.version.StrictVersion( - libtiff_version - ) >= distutils.version.StrictVersion("4.0"): + if Image.core.libtiff_v4_or_greater: libtiffs.append(True) for libtiff in libtiffs: diff --git a/src/PIL/TiffImagePlugin.py b/src/PIL/TiffImagePlugin.py index 47b69a003..0abac545c 100644 --- a/src/PIL/TiffImagePlugin.py +++ b/src/PIL/TiffImagePlugin.py @@ -38,7 +38,6 @@ # # See the README file for information on usage and redistribution. # -import distutils.version import io import itertools import os @@ -1559,11 +1558,10 @@ def _save(im, fp, filename): # Custom items are supported for int, float, unicode, string and byte # values. Other types and tuples require a tagtype. if tag not in TiffTags.LIBTIFF_CORE: - if TiffTags.lookup(tag).type == TiffTags.UNDEFINED: - continue - if distutils.version.StrictVersion( - _libtiff_version() - ) < distutils.version.StrictVersion("4.0"): + if ( + TiffTags.lookup(tag).type == TiffTags.UNDEFINED + or not Image.core.libtiff_v4_or_greater + ): continue if tag in ifd.tagtype: diff --git a/src/_imaging.c b/src/_imaging.c index a55ebb64b..12c07f0ad 100644 --- a/src/_imaging.c +++ b/src/_imaging.c @@ -82,6 +82,12 @@ #include "zlib.h" #endif +#ifdef HAVE_LIBTIFF +#ifndef _TIFFIO_ +#include +#endif +#endif + #include "Imaging.h" #define _USE_MATH_DEFINES @@ -3961,6 +3967,15 @@ setup_module(PyObject* m) { { extern const char * ImagingTiffVersion(void); PyDict_SetItemString(d, "libtiff_version", PyUnicode_FromString(ImagingTiffVersion())); + + // Test for libtiff 4.0 or later, excluding libtiff 3.9.6 and 3.9.7 + PyObject* v4_or_greater; +#if TIFFLIB_VERSION >= 20111221 && TIFFLIB_VERSION != 20120218 && TIFFLIB_VERSION != 20120922 + v4_or_greater = Py_True; +#else + v4_or_greater = Py_False; +#endif + PyDict_SetItemString(d, "libtiff_v4_or_greater", v4_or_greater); } #endif From 82356f7f36394f86c490a9554228ab45cbbce75e Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 28 Dec 2019 12:57:13 +1100 Subject: [PATCH 2/3] Removed _libtiff_version --- src/PIL/TiffImagePlugin.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/PIL/TiffImagePlugin.py b/src/PIL/TiffImagePlugin.py index 0abac545c..a78f791ee 100644 --- a/src/PIL/TiffImagePlugin.py +++ b/src/PIL/TiffImagePlugin.py @@ -263,10 +263,6 @@ def _limit_rational(val, max_val): return n_d[::-1] if inv else n_d -def _libtiff_version(): - return Image.core.libtiff_version.split("\n")[0].split("Version ")[1] - - ## # Wrapper for TIFF IFDs. From f77c4097425a45e38e9042590b3d5d978bc1146f Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Tue, 31 Dec 2019 09:04:25 +1100 Subject: [PATCH 3/3] Renamed variable --- Tests/test_file_libtiff.py | 2 +- src/PIL/TiffImagePlugin.py | 2 +- src/_imaging.c | 8 ++++---- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Tests/test_file_libtiff.py b/Tests/test_file_libtiff.py index 66855bb97..4414dbe5e 100644 --- a/Tests/test_file_libtiff.py +++ b/Tests/test_file_libtiff.py @@ -272,7 +272,7 @@ class TestFileLibTiff(LibTiffTestCase): } libtiffs = [False] - if Image.core.libtiff_v4_or_greater: + if Image.core.libtiff_support_custom_tags: libtiffs.append(True) for libtiff in libtiffs: diff --git a/src/PIL/TiffImagePlugin.py b/src/PIL/TiffImagePlugin.py index a78f791ee..fa8c852c2 100644 --- a/src/PIL/TiffImagePlugin.py +++ b/src/PIL/TiffImagePlugin.py @@ -1556,7 +1556,7 @@ def _save(im, fp, filename): if tag not in TiffTags.LIBTIFF_CORE: if ( TiffTags.lookup(tag).type == TiffTags.UNDEFINED - or not Image.core.libtiff_v4_or_greater + or not Image.core.libtiff_support_custom_tags ): continue diff --git a/src/_imaging.c b/src/_imaging.c index 12c07f0ad..190b312bc 100644 --- a/src/_imaging.c +++ b/src/_imaging.c @@ -3969,13 +3969,13 @@ setup_module(PyObject* m) { PyDict_SetItemString(d, "libtiff_version", PyUnicode_FromString(ImagingTiffVersion())); // Test for libtiff 4.0 or later, excluding libtiff 3.9.6 and 3.9.7 - PyObject* v4_or_greater; + PyObject* support_custom_tags; #if TIFFLIB_VERSION >= 20111221 && TIFFLIB_VERSION != 20120218 && TIFFLIB_VERSION != 20120922 - v4_or_greater = Py_True; + support_custom_tags = Py_True; #else - v4_or_greater = Py_False; + support_custom_tags = Py_False; #endif - PyDict_SetItemString(d, "libtiff_v4_or_greater", v4_or_greater); + PyDict_SetItemString(d, "libtiff_support_custom_tags", support_custom_tags); } #endif