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

Skip to content

Commit 50ae3f6

Browse files
Issue #18101: Tcl.split() now process strings nested in a tuple as it
do with byte strings. Added tests for Tcl.split() and Tcl.splitline().
2 parents e3ed4ed + 275d5fd commit 50ae3f6

3 files changed

Lines changed: 78 additions & 0 deletions

File tree

Lib/test/test_tcl.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,66 @@ def passValue(value):
175175
self.assertEqual(passValue(f), f)
176176
self.assertEqual(passValue((1, '2', (3.4,))), (1, '2', (3.4,)))
177177

178+
def test_splitlist(self):
179+
splitlist = self.interp.tk.splitlist
180+
call = self.interp.tk.call
181+
self.assertRaises(TypeError, splitlist)
182+
self.assertRaises(TypeError, splitlist, 'a', 'b')
183+
self.assertRaises(TypeError, splitlist, 2)
184+
testcases = [
185+
('2', ('2',)),
186+
('', ()),
187+
('{}', ('',)),
188+
('""', ('',)),
189+
('a\n b\t\r c\n ', ('a', 'b', 'c')),
190+
(b'a\n b\t\r c\n ', ('a', 'b', 'c')),
191+
('a \u20ac', ('a', '\u20ac')),
192+
(b'a \xe2\x82\xac', ('a', '\u20ac')),
193+
('a {b c}', ('a', 'b c')),
194+
(r'a b\ c', ('a', 'b c')),
195+
(('a', 'b c'), ('a', 'b c')),
196+
('a 2', ('a', '2')),
197+
(('a', 2), ('a', 2)),
198+
('a 3.4', ('a', '3.4')),
199+
(('a', 3.4), ('a', 3.4)),
200+
((), ()),
201+
(call('list', 1, '2', (3.4,)), (1, '2', (3.4,))),
202+
]
203+
for arg, res in testcases:
204+
self.assertEqual(splitlist(arg), res, msg=arg)
205+
self.assertRaises(TclError, splitlist, '{')
206+
207+
def test_split(self):
208+
split = self.interp.tk.split
209+
call = self.interp.tk.call
210+
self.assertRaises(TypeError, split)
211+
self.assertRaises(TypeError, split, 'a', 'b')
212+
self.assertRaises(TypeError, split, 2)
213+
testcases = [
214+
('2', '2'),
215+
('', ''),
216+
('{}', ''),
217+
('""', ''),
218+
('{', '{'),
219+
('a\n b\t\r c\n ', ('a', 'b', 'c')),
220+
(b'a\n b\t\r c\n ', ('a', 'b', 'c')),
221+
('a \u20ac', ('a', '\u20ac')),
222+
(b'a \xe2\x82\xac', ('a', '\u20ac')),
223+
('a {b c}', ('a', ('b', 'c'))),
224+
(r'a b\ c', ('a', ('b', 'c'))),
225+
(('a', b'b c'), ('a', ('b', 'c'))),
226+
(('a', 'b c'), ('a', ('b', 'c'))),
227+
('a 2', ('a', '2')),
228+
(('a', 2), ('a', 2)),
229+
('a 3.4', ('a', '3.4')),
230+
(('a', 3.4), ('a', 3.4)),
231+
(('a', (2, 3.4)), ('a', (2, 3.4))),
232+
((), ()),
233+
(call('list', 1, '2', (3.4,)), (1, '2', (3.4,))),
234+
]
235+
for arg, res in testcases:
236+
self.assertEqual(split(arg), res, msg=arg)
237+
178238

179239
def test_main():
180240
support.run_unittest(TclTest, TkinterTest)

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,9 @@ Core and Builtins
148148
Library
149149
-------
150150

151+
- Issue #18101: Tcl.split() now process strings nested in a tuple as it
152+
do with byte strings.
153+
151154
- Issue #18116: getpass was always getting an error when testing /dev/tty,
152155
and thus was always falling back to stdin. It also leaked an open file
153156
when it did so. Both of these issues are now fixed.

Modules/_tkinter.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,21 @@ SplitObj(PyObject *arg)
423423
return result;
424424
/* Fall through, returning arg. */
425425
}
426+
else if (PyUnicode_Check(arg)) {
427+
int argc;
428+
char **argv;
429+
char *list = PyUnicode_AsUTF8(arg);
430+
431+
if (list == NULL ||
432+
Tcl_SplitList((Tcl_Interp *)NULL, list, &argc, &argv) != TCL_OK) {
433+
Py_INCREF(arg);
434+
return arg;
435+
}
436+
Tcl_Free(FREECAST argv);
437+
if (argc > 1)
438+
return Split(list);
439+
/* Fall through, returning arg. */
440+
}
426441
else if (PyBytes_Check(arg)) {
427442
int argc;
428443
char **argv;

0 commit comments

Comments
 (0)