Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f44611c

Browse files
Issue #21580: Now Tkinter correctly handles bytes arguments passed to Tk.
In particular this allows to initialize images from binary data.
2 parents da565a7 + 74596a8 commit f44611c

4 files changed

Lines changed: 20 additions & 16 deletions

File tree

Lib/test/test_tcl.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -389,8 +389,12 @@ def passValue(value):
389389
self.assertEqual(passValue('str\x00ing'), 'str\x00ing')
390390
self.assertEqual(passValue('str\x00ing\xbd'), 'str\x00ing\xbd')
391391
self.assertEqual(passValue('str\x00ing\u20ac'), 'str\x00ing\u20ac')
392-
self.assertEqual(passValue(b'str\x00ing'), 'str\x00ing')
393-
self.assertEqual(passValue(b'str\xc0\x80ing'), 'str\x00ing')
392+
self.assertEqual(passValue(b'str\x00ing'),
393+
b'str\x00ing' if self.wantobjects else 'str\x00ing')
394+
self.assertEqual(passValue(b'str\xc0\x80ing'),
395+
b'str\xc0\x80ing' if self.wantobjects else 'str\xc0\x80ing')
396+
self.assertEqual(passValue(b'str\xbding'),
397+
b'str\xbding' if self.wantobjects else 'str\xbding')
394398
for i in (0, 1, -1, 2**31-1, -2**31):
395399
self.assertEqual(passValue(i), i if self.wantobjects else str(i))
396400
for f in (0.0, 1.0, -1.0, 1/3,
@@ -440,12 +444,14 @@ def float_eq(actual, expected):
440444
check('string\xbd', 'string\xbd')
441445
check('string\u20ac', 'string\u20ac')
442446
check(b'string', 'string')
443-
check(b'string\xe2\x82\xac', 'string\u20ac')
447+
check(b'string\xe2\x82\xac', 'string\xe2\x82\xac')
448+
check(b'string\xbd', 'string\xbd')
444449
check('str\x00ing', 'str\x00ing')
445450
check('str\x00ing\xbd', 'str\x00ing\xbd')
446451
check('str\x00ing\u20ac', 'str\x00ing\u20ac')
447-
check(b'str\xc0\x80ing', 'str\x00ing')
448-
check(b'str\xc0\x80ing\xe2\x82\xac', 'str\x00ing\u20ac')
452+
check(b'str\x00ing', 'str\x00ing')
453+
check(b'str\xc0\x80ing', 'str\xc0\x80ing')
454+
check(b'str\xc0\x80ing\xe2\x82\xac', 'str\xc0\x80ing\xe2\x82\xac')
449455
for i in (0, 1, -1, 2**31-1, -2**31):
450456
check(i, str(i))
451457
for f in (0.0, 1.0, -1.0):
@@ -493,9 +499,9 @@ def test_splitlist(self):
493499
if tcl_version >= (8, 5):
494500
if not self.wantobjects or get_tk_patchlevel() < (8, 5, 5):
495501
# Before 8.5.5 dicts were converted to lists through string
496-
expected = ('12', '\u20ac', '\u20ac', '3.4')
502+
expected = ('12', '\u20ac', '\xe2\x82\xac', '3.4')
497503
else:
498-
expected = (12, '\u20ac', '\u20ac', (3.4,))
504+
expected = (12, '\u20ac', b'\xe2\x82\xac', (3.4,))
499505
testcases += [
500506
(call('dict', 'create', 12, '\u20ac', b'\xe2\x82\xac', (3.4,)),
501507
expected),
@@ -543,9 +549,9 @@ def test_split(self):
543549
if tcl_version >= (8, 5):
544550
if not self.wantobjects or get_tk_patchlevel() < (8, 5, 5):
545551
# Before 8.5.5 dicts were converted to lists through string
546-
expected = ('12', '\u20ac', '\u20ac', '3.4')
552+
expected = ('12', '\u20ac', '\xe2\x82\xac', '3.4')
547553
else:
548-
expected = (12, '\u20ac', '\u20ac', (3.4,))
554+
expected = (12, '\u20ac', b'\xe2\x82\xac', (3.4,))
549555
testcases += [
550556
(call('dict', 'create', 12, '\u20ac', b'\xe2\x82\xac', (3.4,)),
551557
expected),

Lib/tkinter/test/test_tkinter/test_images.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,34 +161,29 @@ def check_create_from_data(self, ext):
161161
def test_create_from_ppm_file(self):
162162
self.check_create_from_file('ppm')
163163

164-
@unittest.skip('issue #21580')
165164
def test_create_from_ppm_data(self):
166165
self.check_create_from_data('ppm')
167166

168167
def test_create_from_pgm_file(self):
169168
self.check_create_from_file('pgm')
170169

171-
@unittest.skip('issue #21580')
172170
def test_create_from_pgm_data(self):
173171
self.check_create_from_data('pgm')
174172

175173
def test_create_from_gif_file(self):
176174
self.check_create_from_file('gif')
177175

178-
@unittest.skip('issue #21580')
179176
def test_create_from_gif_data(self):
180177
self.check_create_from_data('gif')
181178

182179
@requires_tcl(8, 6)
183180
def test_create_from_png_file(self):
184181
self.check_create_from_file('png')
185182

186-
@unittest.skip('issue #21580')
187183
@requires_tcl(8, 6)
188184
def test_create_from_png_data(self):
189185
self.check_create_from_data('png')
190186

191-
@unittest.skip('issue #21580')
192187
def test_configure_data(self):
193188
image = tkinter.PhotoImage('::img::test', master=self.root)
194189
self.assertEqual(image['data'], '')

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ Core and Builtins
113113
Library
114114
-------
115115

116+
- Issue #21580: Now Tkinter correctly handles bytes arguments passed to Tk.
117+
In particular this allows to initialize images from binary data.
118+
116119
- Issue #22003: When initialized from a bytes object, io.BytesIO() now
117120
defers making a copy until it is mutated, improving performance and
118121
memory use on some use cases. Patch by David Wilson.

Modules/_tkinter.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -901,8 +901,8 @@ AsObj(PyObject *value)
901901
int overflow;
902902

903903
if (PyBytes_Check(value))
904-
return Tcl_NewStringObj(PyBytes_AS_STRING(value),
905-
PyBytes_GET_SIZE(value));
904+
return Tcl_NewByteArrayObj((unsigned char *)PyBytes_AS_STRING(value),
905+
PyBytes_GET_SIZE(value));
906906
else if (PyBool_Check(value))
907907
return Tcl_NewBooleanObj(PyObject_IsTrue(value));
908908
else if (PyLong_CheckExact(value) &&

0 commit comments

Comments
 (0)