diff --git a/Tests/test_image_access.py b/Tests/test_image_access.py index bb30b462d2f..4e3435cfeec 100644 --- a/Tests/test_image_access.py +++ b/Tests/test_image_access.py @@ -282,7 +282,7 @@ def test_embeddable(self) -> None: home = sys.prefix.replace("\\", "\\\\") fh.write( f""" -#include "Python.h" +#include int main(int argc, char* argv[]) {{ diff --git a/docs/reference/internal_modules.rst b/docs/reference/internal_modules.rst index 2fb4ff8c018..51f77ab8bc7 100644 --- a/docs/reference/internal_modules.rst +++ b/docs/reference/internal_modules.rst @@ -73,7 +73,7 @@ on some Python versions. :mod:`PIL.Image.core` Module ---------------------------- -.. module:: PIL._imaging +.. module:: PIL.lib_imaging .. module:: PIL.Image.core An internal interface module previously known as :mod:`~PIL._imaging`, diff --git a/setup.py b/setup.py index 863d102cf85..ef4129a6ece 100644 --- a/setup.py +++ b/setup.py @@ -14,11 +14,14 @@ import struct import subprocess import sys +import sysconfig import warnings from setuptools import Extension, setup from setuptools.command.build_ext import build_ext +EXT_SUFFIX, _, _ = sysconfig.get_config_var("EXT_SUFFIX").rpartition(".") + def get_version(): version_file = "src/PIL/_version.py" @@ -333,7 +336,6 @@ def __iter__(self): ("disable-platform-guessing", None, "Disable platform guessing on Linux"), ("debug", None, "Debug logging"), ] - + [("add-imaging-libs=", None, "Add libs to _imaging build")] ) @staticmethod @@ -344,7 +346,6 @@ def initialize_options(self): self.disable_platform_guessing = self.check_configuration( "platform-guessing", "disable" ) - self.add_imaging_libs = "" build_ext.initialize_options(self) for x in self.feature: setattr(self, f"disable_{x}", self.check_configuration(x, "disable")) @@ -413,7 +414,7 @@ def _update_extension(self, name, libraries, define_macros=None, sources=None): if FUZZING_BUILD: extension.language = "c++" extension.extra_link_args = ["--stdlib=libc++"] - break + return extension def _remove_extension(self, name): for extension in self.extensions: @@ -821,7 +822,7 @@ def build_extensions(self): # # core library - libs = self.add_imaging_libs.split() + libs = [] defs = [] if feature.tiff: libs.append(feature.tiff) @@ -856,7 +857,7 @@ def build_extensions(self): defs.append(("PILLOW_VERSION", f'"{PILLOW_VERSION}"')) - self._update_extension("PIL._imaging", libs, defs) + self._update_extension("PIL.lib_imaging", libs, defs) # # additional libraries @@ -893,8 +894,15 @@ def build_extensions(self): self._remove_extension("PIL._imagingcms") if feature.webp: - libs = [feature.webp, feature.webp + "mux", feature.webp + "demux"] - self._update_extension("PIL._webp", libs) + libs = [ + f"_imaging{EXT_SUFFIX}", + feature.webp, + feature.webp + "mux", + feature.webp + "demux", + ] + ext = self._update_extension("PIL._webp", libs) + ext.library_dirs.append(f"{self.build_lib}/PIL/") + ext.runtime_library_dirs.append("$ORIGIN") else: self._remove_extension("PIL._webp") @@ -974,7 +982,7 @@ def debug_build(): for src_file in _LIB_IMAGING: files.append(os.path.join("src/libImaging", src_file + ".c")) ext_modules = [ - Extension("PIL._imaging", files), + Extension("PIL.lib_imaging", files), Extension("PIL._imagingft", ["src/_imagingft.c"]), Extension("PIL._imagingcms", ["src/_imagingcms.c"]), Extension("PIL._webp", ["src/_webp.c"]), diff --git a/src/PIL/features.py b/src/PIL/features.py index 24c5ee978b3..d8eb421ede5 100644 --- a/src/PIL/features.py +++ b/src/PIL/features.py @@ -12,7 +12,7 @@ from ._deprecate import deprecate modules = { - "pil": ("PIL._imaging", "PILLOW_VERSION"), + "pil": ("PIL.lib_imaging", "PILLOW_VERSION"), "tkinter": ("PIL._tkinter_finder", "tk_version"), "freetype2": ("PIL._imagingft", "freetype2_version"), "littlecms2": ("PIL._imagingcms", "littlecms_version"), @@ -128,7 +128,7 @@ def get_supported_codecs() -> list[str]: "harfbuzz": ("PIL._imagingft", "HAVE_HARFBUZZ", "harfbuzz_version"), "libjpeg_turbo": ("PIL._imaging", "HAVE_LIBJPEGTURBO", "libjpeg_turbo_version"), "libimagequant": ("PIL._imaging", "HAVE_LIBIMAGEQUANT", "imagequant_version"), - "xcb": ("PIL._imaging", "HAVE_XCB", None), + "xcb": ("PIL.lib_imaging", "HAVE_XCB", None), } diff --git a/src/_imaging.c b/src/_imaging.c index 07d9a64ccff..0a770655d43 100644 --- a/src/_imaging.c +++ b/src/_imaging.c @@ -71,9 +71,6 @@ * See the README file for information on usage and redistribution. */ -#define PY_SSIZE_T_CLEAN -#include "Python.h" - #ifdef HAVE_LIBJPEG #include "jconfig.h" #endif @@ -90,8 +87,6 @@ #include "libImaging/Imaging.h" -#define _USE_MATH_DEFINES -#include #include #undef VERBOSE @@ -4417,15 +4412,15 @@ setup_module(PyObject *m) { } PyMODINIT_FUNC -PyInit__imaging(void) { +PyInit_lib_imaging(void) { PyObject *m; static PyModuleDef module_def = { PyModuleDef_HEAD_INIT, - "_imaging", /* m_name */ - NULL, /* m_doc */ - -1, /* m_size */ - functions, /* m_methods */ + "lib_imaging", /* m_name */ + NULL, /* m_doc */ + -1, /* m_size */ + functions, /* m_methods */ }; m = PyModule_Create(&module_def); diff --git a/src/_imagingcms.c b/src/_imagingcms.c index bafe787a743..8103b3d5eb3 100644 --- a/src/_imagingcms.c +++ b/src/_imagingcms.c @@ -26,15 +26,11 @@ kevin@cazabon.com\n\ https://www.cazabon.com\n\ " -#define PY_SSIZE_T_CLEAN -#include "Python.h" // Include before wchar.h so _GNU_SOURCE is set -#include "wchar.h" -#include "datetime.h" +#include "libImaging/Imaging.h" // Include before wchar.h so _GNU_SOURCE is set -#include "lcms2.h" -#include "libImaging/Imaging.h" - -#define PYCMSVERSION "1.0.0 pil" +#include +#include +#include /* version history */ @@ -619,12 +615,6 @@ cms_profile_is_intent_supported(CmsProfileObject *self, PyObject *args) { #ifdef _WIN32 -#ifdef _WIN64 -#define F_HANDLE "K" -#else -#define F_HANDLE "k" -#endif - static PyObject * cms_get_display_profile_win32(PyObject *self, PyObject *args) { char filename[MAX_PATH]; diff --git a/src/_imagingft.c b/src/_imagingft.c index f8143e0cc32..6f94c906a2b 100644 --- a/src/_imagingft.c +++ b/src/_imagingft.c @@ -18,10 +18,8 @@ * Copyright (c) 1998-2007 by Secret Labs AB */ -#define PY_SSIZE_T_CLEAN -#include "Python.h" -#include "thirdparty/pythoncapi_compat.h" #include "libImaging/Imaging.h" +#include "thirdparty/pythoncapi_compat.h" #include #include FT_FREETYPE_H diff --git a/src/_imagingmath.c b/src/_imagingmath.c index 550a1090382..54b49ee6246 100644 --- a/src/_imagingmath.c +++ b/src/_imagingmath.c @@ -13,12 +13,9 @@ * See the README file for information on usage and redistribution. */ -#include "Python.h" - #include "libImaging/Imaging.h" -#include "math.h" -#include "float.h" +#include #define MAX_INT32 2147483647.0 #define MIN_INT32 -2147483648.0 diff --git a/src/_imagingmorph.c b/src/_imagingmorph.c index 614dfbe7fe7..c7548b4e953 100644 --- a/src/_imagingmorph.c +++ b/src/_imagingmorph.c @@ -11,7 +11,6 @@ * See the README file for information on usage and redistribution. */ -#include "Python.h" #include "libImaging/Imaging.h" #define LUT_SIZE (1 << 9) diff --git a/src/_imagingtk.c b/src/_imagingtk.c index c70d044bb86..076995daf27 100644 --- a/src/_imagingtk.c +++ b/src/_imagingtk.c @@ -12,7 +12,6 @@ * See the README file for information on usage and redistribution. */ -#include "Python.h" #include "libImaging/Imaging.h" #include "Tk/_tkmini.h" diff --git a/src/_webp.c b/src/_webp.c index f59ad30367b..eaef92e32f5 100644 --- a/src/_webp.c +++ b/src/_webp.c @@ -1,6 +1,5 @@ -#define PY_SSIZE_T_CLEAN -#include #include "libImaging/Imaging.h" + #include #include #include @@ -17,16 +16,6 @@ #error libwebp 0.5.0 and above is required. Upgrade libwebp or build Pillow with --disable-webp flag #endif -void -ImagingSectionEnter(ImagingSectionCookie *cookie) { - *cookie = (PyThreadState *)PyEval_SaveThread(); -} - -void -ImagingSectionLeave(ImagingSectionCookie *cookie) { - PyEval_RestoreThread((PyThreadState *)*cookie); -} - /* -------------------------------------------------------------------- */ /* WebP Muxer Error Handling */ /* -------------------------------------------------------------------- */ diff --git a/src/decode.c b/src/decode.c index 51d0aced2bd..d002f0649af 100644 --- a/src/decode.c +++ b/src/decode.c @@ -29,11 +29,7 @@ /* FIXME: make these pluggable! */ -#define PY_SSIZE_T_CLEAN -#include "Python.h" - #include "libImaging/Imaging.h" - #include "libImaging/Bit.h" #include "libImaging/Bcn.h" #include "libImaging/Gif.h" diff --git a/src/display.c b/src/display.c index b4e2e38991a..07801679769 100644 --- a/src/display.c +++ b/src/display.c @@ -22,9 +22,6 @@ * See the README file for information on usage and redistribution. */ -#define PY_SSIZE_T_CLEAN -#include "Python.h" - #include "libImaging/Imaging.h" /* -------------------------------------------------------------------- */ @@ -34,12 +31,6 @@ #include "libImaging/ImDib.h" -#if SIZEOF_VOID_P == 8 -#define F_HANDLE "K" -#else -#define F_HANDLE "k" -#endif - typedef struct { PyObject_HEAD ImagingDIB dib; } ImagingDisplayObject; diff --git a/src/encode.c b/src/encode.c index 529982dadc5..f828401b6dd 100644 --- a/src/encode.c +++ b/src/encode.c @@ -22,11 +22,8 @@ /* FIXME: make these pluggable! */ -#define PY_SSIZE_T_CLEAN -#include "Python.h" - -#include "thirdparty/pythoncapi_compat.h" #include "libImaging/Imaging.h" +#include "thirdparty/pythoncapi_compat.h" #include "libImaging/Gif.h" #ifdef HAVE_UNISTD_H @@ -1411,10 +1408,3 @@ PyImaging_Jpeg2KEncoderNew(PyObject *self, PyObject *args) { } #endif - -/* - * Local Variables: - * c-basic-offset: 4 - * End: - * - */ diff --git a/src/libImaging/ColorLUT.c b/src/libImaging/ColorLUT.c index 5559de689b2..33633f555d1 100644 --- a/src/libImaging/ColorLUT.c +++ b/src/libImaging/ColorLUT.c @@ -1,5 +1,4 @@ #include "Imaging.h" -#include /* 8 bits for result. Table can overflow [0, 1.0] range, so we need extra bits for overflow and negative values. diff --git a/src/libImaging/Dib.c b/src/libImaging/Dib.c index c69e9e552ae..a34fba85aa2 100644 --- a/src/libImaging/Dib.c +++ b/src/libImaging/Dib.c @@ -19,10 +19,9 @@ * See the README file for information on usage and redistribution. */ -#include "Imaging.h" - #ifdef _WIN32 +#include "Imaging.h" #include "ImDib.h" char * diff --git a/src/libImaging/Draw.c b/src/libImaging/Draw.c index f1c8ffcff8d..730bba8acec 100644 --- a/src/libImaging/Draw.c +++ b/src/libImaging/Draw.c @@ -34,9 +34,6 @@ #include "Imaging.h" -#include -#include - #define CEIL(v) (int)ceil(v) #define FLOOR(v) ((v) >= 0.0 ? (int)(v) : (int)floor(v)) diff --git a/src/libImaging/Effects.c b/src/libImaging/Effects.c index 93e7af0bce9..64d6188d917 100644 --- a/src/libImaging/Effects.c +++ b/src/libImaging/Effects.c @@ -17,8 +17,6 @@ #include "Imaging.h" -#include - Imaging ImagingEffectMandelbrot(int xsize, int ysize, double extent[4], int quality) { /* Generate a Mandelbrot set covering the given extent */ diff --git a/src/libImaging/Fill.c b/src/libImaging/Fill.c index 8fb481e7e4d..1e4bb9ba0b5 100644 --- a/src/libImaging/Fill.c +++ b/src/libImaging/Fill.c @@ -17,8 +17,6 @@ #include "Imaging.h" -#include "math.h" - Imaging ImagingFill(Imaging im, const void *colour) { int x, y; diff --git a/src/libImaging/ImDib.h b/src/libImaging/ImDib.h index 91ff3f322ff..a0c2348fd08 100644 --- a/src/libImaging/ImDib.h +++ b/src/libImaging/ImDib.h @@ -12,8 +12,6 @@ #ifdef _WIN32 -#include "ImPlatform.h" - #if defined(__cplusplus) extern "C" { #endif diff --git a/src/libImaging/Imaging.h b/src/libImaging/Imaging.h index 321dce988c3..34ffae0f324 100644 --- a/src/libImaging/Imaging.h +++ b/src/libImaging/Imaging.h @@ -10,16 +10,12 @@ * See the README file for information on usage and redistribution. */ -#include "ImPlatform.h" +#include "ImagingPlatform.h" #if defined(__cplusplus) extern "C" { #endif -#ifndef M_PI -#define M_PI 3.1415926535897932384626433832795 -#endif - /* -------------------------------------------------------------------- */ /* diff --git a/src/libImaging/ImPlatform.h b/src/libImaging/ImagingPlatform.h similarity index 51% rename from src/libImaging/ImPlatform.h rename to src/libImaging/ImagingPlatform.h index f6e7fb6b921..3a3875daf6e 100644 --- a/src/libImaging/ImPlatform.h +++ b/src/libImaging/ImagingPlatform.h @@ -7,7 +7,11 @@ * Copyright (c) Fredrik Lundh 1995-2003. */ -#include "Python.h" +#define PY_SSIZE_T_CLEAN +#include + +#define _USE_MATH_DEFINES +#include /* Check that we have an ANSI compliant compiler */ #ifndef HAVE_PROTOTYPES @@ -17,34 +21,25 @@ #error Sorry, this library requires ANSI header files. #endif -#if defined(PIL_NO_INLINE) -#define inline -#else #if defined(_MSC_VER) && !defined(__GNUC__) #define inline __inline #endif -#endif - -#if defined(_WIN32) || defined(__CYGWIN__) /* WIN */ +#ifdef _WIN32 #define WIN32_LEAN_AND_MEAN #include -#ifdef __CYGWIN__ -#undef _WIN64 -#undef _WIN32 -#undef __WIN32__ -#undef WIN32 +#ifdef _WIN64 +#define F_HANDLE "K" +#else +#define F_HANDLE "k" #endif -#else /* not WIN */ -/* For System that are not Windows, we'll need to define these. */ -/* We have to define them instead of using typedef because the JPEG lib also +#endif /* _WIN32 */ + +/* We have to define types instead of using typedef because the JPEG lib also defines their own types with the same names, so we need to be able to undef ours before including the JPEG code. */ - -#if __STDC_VERSION__ >= 199901L /* C99+ */ - #include #define INT8 int8_t @@ -54,45 +49,11 @@ #define INT32 int32_t #define UINT32 uint32_t -#else /* < C99 */ - -#define INT8 signed char - -#if SIZEOF_SHORT == 2 -#define INT16 short -#elif SIZEOF_INT == 2 -#define INT16 int -#else -#error Cannot find required 16-bit integer type -#endif - -#if SIZEOF_SHORT == 4 -#define INT32 short -#elif SIZEOF_INT == 4 -#define INT32 int -#elif SIZEOF_LONG == 4 -#define INT32 long -#else -#error Cannot find required 32-bit integer type -#endif - -#define UINT8 unsigned char -#define UINT16 unsigned INT16 -#define UINT32 unsigned INT32 - -#endif /* < C99 */ - -#endif /* not WIN */ - /* assume IEEE; tweak if necessary (patches are welcome) */ #define FLOAT16 UINT16 #define FLOAT32 float #define FLOAT64 double -#ifdef _MSC_VER -typedef signed __int64 int64_t; -#endif - #ifdef __GNUC__ #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) #endif diff --git a/src/libImaging/Jpeg2K.h b/src/libImaging/Jpeg2K.h index e8d92f7b6bc..6fbbf7311a1 100644 --- a/src/libImaging/Jpeg2K.h +++ b/src/libImaging/Jpeg2K.h @@ -104,10 +104,3 @@ typedef struct { int plt; } JPEG2KENCODESTATE; - -/* - * Local Variables: - * c-basic-offset: 4 - * End: - * - */ diff --git a/src/libImaging/Jpeg2KDecode.c b/src/libImaging/Jpeg2KDecode.c index 5b3d7ffc424..61bb396eb5c 100644 --- a/src/libImaging/Jpeg2KDecode.c +++ b/src/libImaging/Jpeg2KDecode.c @@ -980,10 +980,3 @@ ImagingJpeg2KVersion(void) { } #endif /* HAVE_OPENJPEG */ - -/* - * Local Variables: - * c-basic-offset: 4 - * End: - * - */ diff --git a/src/libImaging/Jpeg2KEncode.c b/src/libImaging/Jpeg2KEncode.c index cb21a186c0b..d30ccde603e 100644 --- a/src/libImaging/Jpeg2KEncode.c +++ b/src/libImaging/Jpeg2KEncode.c @@ -652,10 +652,3 @@ ImagingJpeg2KEncodeCleanup(ImagingCodecState state) { } #endif /* HAVE_OPENJPEG */ - -/* - * Local Variables: - * c-basic-offset: 4 - * End: - * - */ diff --git a/src/libImaging/Palette.c b/src/libImaging/Palette.c index 78916bca52b..032ce8ab26a 100644 --- a/src/libImaging/Palette.c +++ b/src/libImaging/Palette.c @@ -18,8 +18,6 @@ #include "Imaging.h" -#include - ImagingPalette ImagingPaletteNew(const char *mode) { /* Create a palette object */ diff --git a/src/libImaging/Quant.c b/src/libImaging/Quant.c index a489a882db2..147d8ae52cb 100644 --- a/src/libImaging/Quant.c +++ b/src/libImaging/Quant.c @@ -1428,7 +1428,6 @@ quantize( } #ifdef TEST_NEAREST_NEIGHBOUR -#include { uint32_t bestmatch, bestdist, dist; HashTable *h2; diff --git a/src/libImaging/QuantOctree.c b/src/libImaging/QuantOctree.c index 7e02ebf65bc..7e1a5489963 100644 --- a/src/libImaging/QuantOctree.c +++ b/src/libImaging/QuantOctree.c @@ -28,7 +28,7 @@ #include #include -#include "ImagingUtils.h" +#include "Imaging.h" #include "QuantOctree.h" typedef struct _ColorBucket { diff --git a/src/libImaging/QuantTypes.h b/src/libImaging/QuantTypes.h index 986b70806dc..e01023de7e1 100644 --- a/src/libImaging/QuantTypes.h +++ b/src/libImaging/QuantTypes.h @@ -9,15 +9,10 @@ * See the README file for information on usage and redistribution. */ -#ifndef __TYPES_H__ -#define __TYPES_H__ +#ifndef __QUANTTYPES_H__ +#define __QUANTTYPES_H__ -#ifdef _MSC_VER -typedef unsigned __int32 uint32_t; -typedef unsigned __int64 uint64_t; -#else #include -#endif typedef union { struct { diff --git a/src/libImaging/Reduce.c b/src/libImaging/Reduce.c index 022daa0003f..6da18d773d5 100644 --- a/src/libImaging/Reduce.c +++ b/src/libImaging/Reduce.c @@ -1,7 +1,5 @@ #include "Imaging.h" -#include - #define ROUND_UP(f) ((int)((f) >= 0.0 ? (f) + 0.5F : (f) - 0.5F)) UINT32 diff --git a/src/libImaging/Resample.c b/src/libImaging/Resample.c index 222d6bca433..0cc8c2b69c3 100644 --- a/src/libImaging/Resample.c +++ b/src/libImaging/Resample.c @@ -1,7 +1,5 @@ #include "Imaging.h" -#include - #define ROUND_UP(f) ((int)((f) >= 0.0 ? (f) + 0.5F : (f) - 0.5F)) struct filter { diff --git a/src/libImaging/codec_fd.c b/src/libImaging/codec_fd.c index 5261681107b..f5541c332f2 100644 --- a/src/libImaging/codec_fd.c +++ b/src/libImaging/codec_fd.c @@ -1,4 +1,3 @@ -#include "Python.h" #include "Imaging.h" Py_ssize_t diff --git a/src/map.c b/src/map.c index c66702981d3..2fcaa74d222 100644 --- a/src/map.c +++ b/src/map.c @@ -18,8 +18,6 @@ * FIXME: should move the memory mapping primitives into libImaging! */ -#include "Python.h" - #include "libImaging/Imaging.h" /* compatibility wrappers (defined in _imaging.c) */ diff --git a/src/outline.c b/src/outline.c index 27cc255cf84..b84bef437c3 100644 --- a/src/outline.c +++ b/src/outline.c @@ -17,8 +17,6 @@ * See the README file for information on usage and redistribution. */ -#include "Python.h" - #include "libImaging/Imaging.h" /* -------------------------------------------------------------------- */ diff --git a/src/path.c b/src/path.c index b96e8b78a70..d42b021dac9 100644 --- a/src/path.c +++ b/src/path.c @@ -25,11 +25,8 @@ * See the README file for information on usage and redistribution. */ -#include "Python.h" -#include "thirdparty/pythoncapi_compat.h" #include "libImaging/Imaging.h" - -#include +#include "thirdparty/pythoncapi_compat.h" /* compatibility wrappers (defined in _imaging.c) */ extern int