From: Serhiy Storchaka Date: Thu, 31 Jul 2014 04:48:14 +0000 (+0300) Subject: Issue #21580: Now Tkinter correctly handles binary "data" and "maskdata" X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=04fa704161358bd656ab8b85966efb00f6620f2a;p=python Issue #21580: Now Tkinter correctly handles binary "data" and "maskdata" configure options of tkinter.PhotoImage. Added private Tkapp method _createbytearray(). --- diff --git a/Lib/lib-tk/Tkinter.py b/Lib/lib-tk/Tkinter.py index ee49ed7293..aade6491cc 100644 --- a/Lib/lib-tk/Tkinter.py +++ b/Lib/lib-tk/Tkinter.py @@ -3282,6 +3282,8 @@ class Image: for k, v in cnf.items(): if hasattr(v, '__call__'): v = self._register(v) + elif k in ('data', 'maskdata'): + v = self.tk._createbytearray(v) options = options + ('-'+k, v) self.tk.call(('image', 'create', imgtype, name,) + options) self.name = name @@ -3305,6 +3307,8 @@ class Image: if k[-1] == '_': k = k[:-1] if hasattr(v, '__call__'): v = self._register(v) + elif k in ('data', 'maskdata'): + v = self.tk._createbytearray(v) res = res + ('-'+k, v) self.tk.call((self.name, 'config') + res) config = configure diff --git a/Lib/lib-tk/test/test_tkinter/test_images.py b/Lib/lib-tk/test/test_tkinter/test_images.py index a27b7634f3..b3c5b75dd8 100644 --- a/Lib/lib-tk/test/test_tkinter/test_images.py +++ b/Lib/lib-tk/test/test_tkinter/test_images.py @@ -151,7 +151,8 @@ class PhotoImageTest(unittest.TestCase): self.assertEqual(image.type(), 'photo') self.assertEqual(image.width(), 16) self.assertEqual(image.height(), 16) - self.assertEqual(image['data'], data) + self.assertEqual(image['data'], data if self.wantobjects + else data.decode('latin1')) self.assertEqual(image['file'], '') self.assertIn('::img::test', self.root.image_names()) del image @@ -160,21 +161,18 @@ class PhotoImageTest(unittest.TestCase): def test_create_from_ppm_file(self): self.check_create_from_file('ppm') - @unittest.skip('issue #21580') def test_create_from_ppm_data(self): self.check_create_from_data('ppm') def test_create_from_pgm_file(self): self.check_create_from_file('pgm') - @unittest.skip('issue #21580') def test_create_from_pgm_data(self): self.check_create_from_data('pgm') def test_create_from_gif_file(self): self.check_create_from_file('gif') - @unittest.skip('issue #21580') def test_create_from_gif_data(self): self.check_create_from_data('gif') @@ -182,19 +180,18 @@ class PhotoImageTest(unittest.TestCase): def test_create_from_png_file(self): self.check_create_from_file('png') - @unittest.skip('issue #21580') @requires_tcl(8, 6) def test_create_from_png_data(self): self.check_create_from_data('png') - @unittest.skip('issue #21580') def test_configure_data(self): image = tkinter.PhotoImage('::img::test', master=self.root) self.assertEqual(image['data'], '') with open(self.testfile, 'rb') as f: data = f.read() image.configure(data=data) - self.assertEqual(image['data'], data) + self.assertEqual(image['data'], data if self.wantobjects + else data.decode('latin1')) self.assertEqual(image.width(), 16) self.assertEqual(image.height(), 16) diff --git a/Misc/NEWS b/Misc/NEWS index 8c9a2bb9af..27a632213c 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -19,6 +19,9 @@ Core and Builtins Library ------- +- Issue #21580: Now Tkinter correctly handles binary "data" and "maskdata" + configure options of tkinter.PhotoImage. + - Issue #19612: subprocess.communicate() now also ignores EINVAL when using at least two pipes. diff --git a/Modules/_tkinter.c b/Modules/_tkinter.c index 8288c961b6..fe35882a73 100644 --- a/Modules/_tkinter.c +++ b/Modules/_tkinter.c @@ -2939,6 +2939,33 @@ Tkapp_WillDispatch(PyObject *self, PyObject *args) return Py_None; } +/* Convert Python string or any buffer compatible object to Tcl byte-array + * object. Use it to pass binary data (e.g. image's data) to Tcl/Tk commands. + */ +static PyObject * +Tkapp_CreateByteArray(PyObject *self, PyObject *args) +{ + Py_buffer view; + Tcl_Obj* obj; + PyObject *res = NULL; + + if (!PyArg_ParseTuple(args, "s*:_createbytearray", &view)) + return NULL; + + if (view.len >= INT_MAX) { + PyErr_SetString(PyExc_OverflowError, "string is too long"); + return NULL; + } + obj = Tcl_NewByteArrayObj(view.buf, (int)view.len); + if (obj == NULL) { + PyBuffer_Release(&view); + return Tkinter_Error(self); + } + res = newPyTclObject(obj); + PyBuffer_Release(&view); + return res; +} + /**** Tkapp Method List ****/ @@ -2981,6 +3008,7 @@ static PyMethodDef Tkapp_methods[] = {"quit", Tkapp_Quit, METH_VARARGS}, {"interpaddr", Tkapp_InterpAddr, METH_VARARGS}, {"loadtk", Tkapp_TkInit, METH_NOARGS}, + {"_createbytearray", Tkapp_CreateByteArray, METH_VARARGS}, {NULL, NULL} };