Skip to content

Commit fab6542

Browse files
Issue #18101: Tcl.split() now process Unicode strings nested in a tuple as it
do with byte strings. Added tests for Tcl.split() and tcl.splitline().
1 parent fe2e839 commit fab6542

3 files changed

Lines changed: 90 additions & 0 deletions

File tree

Lib/test/test_tcl.py

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

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

188248
def test_main():
189249
test_support.run_unittest(TclTest, TkinterTest)

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ Core and Builtins
2424
Library
2525
-------
2626

27+
- Issue #18101: Tcl.split() now process Unicode strings nested in a tuple as it
28+
do with byte strings.
29+
2730
- Issue #18427: str.replace could crash the interpreter with huge strings.
2831

2932
- Issue #18347: ElementTree's html serializer now preserves the case of

Modules/_tkinter.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,33 @@ SplitObj(PyObject *arg)
547547
return Split(PyString_AsString(arg));
548548
/* Fall through, returning arg. */
549549
}
550+
else if (PyUnicode_Check(arg)) {
551+
int argc;
552+
char **argv;
553+
char *list;
554+
PyObject *s = PyUnicode_AsUTF8String(arg);
555+
556+
if (s == NULL) {
557+
Py_INCREF(arg);
558+
return arg;
559+
}
560+
list = PyString_AsString(s);
561+
562+
if (list == NULL ||
563+
Tcl_SplitList((Tcl_Interp *)NULL, list, &argc, &argv) != TCL_OK) {
564+
Py_DECREF(s);
565+
Py_INCREF(arg);
566+
return arg;
567+
}
568+
Tcl_Free(FREECAST argv);
569+
if (argc > 1) {
570+
PyObject *v = Split(list);
571+
Py_DECREF(s);
572+
return v;
573+
}
574+
Py_DECREF(s);
575+
/* Fall through, returning arg. */
576+
}
550577
Py_INCREF(arg);
551578
return arg;
552579
}

0 commit comments

Comments
 (0)