From d71cda4f0979b2fd60e9deaa1787ab8c1c9c89b9 Mon Sep 17 00:00:00 2001 From: Christoph Gohlke Date: Wed, 13 Mar 2013 19:34:43 -0700 Subject: [PATCH 1/4] Update py3.h --- py3.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/py3.h b/py3.h index b94b83b6c..8adfac081 100644 --- a/py3.h +++ b/py3.h @@ -43,12 +43,13 @@ #endif /* Map PyBytes -> PyString */ +#define PyBytesObject PyStringObject #define PyBytes_AsString PyString_AsString #define PyBytes_AS_STRING PyString_AS_STRING #define PyBytes_Check PyString_Check +#define PyBytes_AsStringAndSize PyString_AsStringAndSize #define PyBytes_FromStringAndSize PyString_FromStringAndSize #define PyBytes_FromString PyString_FromString #define _PyBytes_Resize _PyString_Resize #endif /* PY_VERSION_HEX < 0x03000000 */ - From c768e793a42c47515044b820a6a4bbf221fe63b2 Mon Sep 17 00:00:00 2001 From: Christoph Gohlke Date: Wed, 13 Mar 2013 19:37:00 -0700 Subject: [PATCH 2/4] Port _webp.c to Python 3 --- _webp.c | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/_webp.c b/_webp.c index 8e479b3b0..84418980c 100644 --- a/_webp.c +++ b/_webp.c @@ -1,10 +1,11 @@ #include +#include "py3.h" #include #include PyObject* WebPEncodeRGB_wrapper(PyObject* self, PyObject* args) { - PyStringObject *rgb_string; + PyBytesObject *rgb_string; int width; int height; int stride; @@ -19,7 +20,7 @@ PyObject* WebPEncodeRGB_wrapper(PyObject* self, PyObject* args) return Py_None; } - PyString_AsStringAndSize((struct PyObject *) rgb_string, &rgb, &size); + PyBytes_AsStringAndSize((PyObject *) rgb_string, &rgb, &size); if (stride * height > size) { Py_INCREF(Py_None); @@ -28,7 +29,7 @@ PyObject* WebPEncodeRGB_wrapper(PyObject* self, PyObject* args) ret_size = WebPEncodeRGB(rgb, width, height, stride, quality_factor, &output); if (ret_size > 0) { - PyObject *ret = PyString_FromStringAndSize(output, ret_size); + PyObject *ret = PyBytes_FromStringAndSize(output, ret_size); free(output); return ret; } @@ -39,7 +40,7 @@ PyObject* WebPEncodeRGB_wrapper(PyObject* self, PyObject* args) PyObject* WebPDecodeRGB_wrapper(PyObject* self, PyObject* args) { - PyStringObject *webp_string; + PyBytesObject *webp_string; float quality_factor; int width; int height; @@ -53,11 +54,11 @@ PyObject* WebPDecodeRGB_wrapper(PyObject* self, PyObject* args) return Py_None; } - PyString_AsStringAndSize((struct PyObject *) webp_string, &webp, &size); + PyBytes_AsStringAndSize((PyObject *) webp_string, &webp, &size); output = WebPDecodeRGB(webp, size, &width, &height); - ret = PyString_FromStringAndSize(output, width * height * 3); + ret = PyBytes_FromStringAndSize(output, width * height * 3); free(output); return Py_BuildValue("Sii", ret, width, height); } @@ -69,8 +70,27 @@ static PyMethodDef webpMethods[] = {NULL, NULL} }; -void init_webp() +#if PY_VERSION_HEX >= 0x03000000 +PyMODINIT_FUNC +PyInit__webp(void) { + PyObject* m; + + static PyModuleDef module_def = { + PyModuleDef_HEAD_INIT, + "_webp", /* m_name */ + NULL, /* m_doc */ + -1, /* m_size */ + webpMethods, /* m_methods */ + }; + + m = PyModule_Create(&module_def); + return m; +} +#else +PyMODINIT_FUNC +init_webp() { PyObject* m; m = Py_InitModule("_webp", webpMethods); } +#endif From b9b6972097c9fb03342830d5a9894f6e7f09d817 Mon Sep 17 00:00:00 2001 From: Christoph Gohlke Date: Wed, 13 Mar 2013 19:42:21 -0700 Subject: [PATCH 3/4] Use BytesIO instead of StringIO --- PIL/WebPImagePlugin.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/PIL/WebPImagePlugin.py b/PIL/WebPImagePlugin.py index d1558d113..5492f6641 100644 --- a/PIL/WebPImagePlugin.py +++ b/PIL/WebPImagePlugin.py @@ -1,6 +1,6 @@ from PIL import Image from PIL import ImageFile -import StringIO +from io import BytesIO import _webp def _accept(prefix): @@ -15,7 +15,7 @@ class WebPImageFile(ImageFile.ImageFile): self.mode = "RGB" data, width, height = _webp.WebPDecodeRGB(self.fp.read()) self.size = width, height - self.fp = StringIO.StringIO(data) + self.fp = BytesIO(data) self.tile = [("raw", (0, 0) + self.size, 0, 'RGB')] def _save(im, fp, filename): From 64c25bcf8972ad81feedae8acade826bbca4ab38 Mon Sep 17 00:00:00 2001 From: Christoph Gohlke Date: Wed, 13 Mar 2013 23:28:30 -0700 Subject: [PATCH 4/4] Compare prefix to byte strings --- PIL/WebPImagePlugin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PIL/WebPImagePlugin.py b/PIL/WebPImagePlugin.py index 5492f6641..628ac7ff6 100644 --- a/PIL/WebPImagePlugin.py +++ b/PIL/WebPImagePlugin.py @@ -4,7 +4,7 @@ from io import BytesIO import _webp def _accept(prefix): - return prefix[:4] == "RIFF" and prefix[8:16] == "WEBPVP8 " + return prefix[:4] == b"RIFF" and prefix[8:16] == b"WEBPVP8 " class WebPImageFile(ImageFile.ImageFile):