diff --git a/PIL/WebPImagePlugin.py b/PIL/WebPImagePlugin.py index d1558d113..628ac7ff6 100644 --- a/PIL/WebPImagePlugin.py +++ b/PIL/WebPImagePlugin.py @@ -1,10 +1,10 @@ from PIL import Image from PIL import ImageFile -import StringIO +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): @@ -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): 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 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 */ -