]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #18101: Tcl.split() now process strings nested in a tuple as it
authorSerhiy Storchaka <storchaka@gmail.com>
Thu, 11 Jul 2013 17:34:47 +0000 (20:34 +0300)
committerSerhiy Storchaka <storchaka@gmail.com>
Thu, 11 Jul 2013 17:34:47 +0000 (20:34 +0300)
do with byte strings.

Added tests for Tcl.split() and Tcl.splitline().

Lib/test/test_tcl.py
Misc/NEWS
Modules/_tkinter.c

index 0cc21c9f2b9a930b4893f41fa0a0e827ae3874f4..a5aaf9b72528168625a4c643faa8f71fea721ae6 100644 (file)
@@ -175,6 +175,66 @@ class TclTest(unittest.TestCase):
                 self.assertEqual(passValue(f), f)
         self.assertEqual(passValue((1, '2', (3.4,))), (1, '2', (3.4,)))
 
+    def test_splitlist(self):
+        splitlist = self.interp.tk.splitlist
+        call = self.interp.tk.call
+        self.assertRaises(TypeError, splitlist)
+        self.assertRaises(TypeError, splitlist, 'a', 'b')
+        self.assertRaises(TypeError, splitlist, 2)
+        testcases = [
+            ('2', ('2',)),
+            ('', ()),
+            ('{}', ('',)),
+            ('""', ('',)),
+            ('a\n b\t\r c\n ', ('a', 'b', 'c')),
+            (b'a\n b\t\r c\n ', ('a', 'b', 'c')),
+            ('a \u20ac', ('a', '\u20ac')),
+            (b'a \xe2\x82\xac', ('a', '\u20ac')),
+            ('a {b c}', ('a', 'b c')),
+            (r'a b\ c', ('a', 'b c')),
+            (('a', 'b c'), ('a', 'b c')),
+            ('a 2', ('a', '2')),
+            (('a', 2), ('a', 2)),
+            ('a 3.4', ('a', '3.4')),
+            (('a', 3.4), ('a', 3.4)),
+            ((), ()),
+            (call('list', 1, '2', (3.4,)), (1, '2', (3.4,))),
+        ]
+        for arg, res in testcases:
+            self.assertEqual(splitlist(arg), res, msg=arg)
+        self.assertRaises(TclError, splitlist, '{')
+
+    def test_split(self):
+        split = self.interp.tk.split
+        call = self.interp.tk.call
+        self.assertRaises(TypeError, split)
+        self.assertRaises(TypeError, split, 'a', 'b')
+        self.assertRaises(TypeError, split, 2)
+        testcases = [
+            ('2', '2'),
+            ('', ''),
+            ('{}', ''),
+            ('""', ''),
+            ('{', '{'),
+            ('a\n b\t\r c\n ', ('a', 'b', 'c')),
+            (b'a\n b\t\r c\n ', ('a', 'b', 'c')),
+            ('a \u20ac', ('a', '\u20ac')),
+            (b'a \xe2\x82\xac', ('a', '\u20ac')),
+            ('a {b c}', ('a', ('b', 'c'))),
+            (r'a b\ c', ('a', ('b', 'c'))),
+            (('a', b'b c'), ('a', ('b', 'c'))),
+            (('a', 'b c'), ('a', ('b', 'c'))),
+            ('a 2', ('a', '2')),
+            (('a', 2), ('a', 2)),
+            ('a 3.4', ('a', '3.4')),
+            (('a', 3.4), ('a', 3.4)),
+            (('a', (2, 3.4)), ('a', (2, 3.4))),
+            ((), ()),
+            (call('list', 1, '2', (3.4,)), (1, '2', (3.4,))),
+        ]
+        for arg, res in testcases:
+            self.assertEqual(split(arg), res, msg=arg)
+
 
 def test_main():
     support.run_unittest(TclTest, TkinterTest)
index 4d55853af7f961509aad6e4e953938f7480b2cb8..ea221cb805056488b95caf6b4bd078c1e6a2b312 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -44,6 +44,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #18101: Tcl.split() now process strings nested in a tuple as it
+  do with byte strings.
+
 - Issue #17198: Fix a NameError in the dbm module.  Patch by Valentina
   Mukhamedzhanova.
 
index d18c7f06cb093a8316b67a72d696eb02fb8aaa9d..cdb28e52c311345f1816e914a1f4a75d20f2af02 100644 (file)
@@ -519,6 +519,21 @@ SplitObj(PyObject *arg)
             return result;
         /* Fall through, returning arg. */
     }
+    else if (PyUnicode_Check(arg)) {
+        int argc;
+        char **argv;
+        char *list = PyUnicode_AsUTF8(arg);
+
+        if (list == NULL ||
+            Tcl_SplitList((Tcl_Interp *)NULL, list, &argc, &argv) != TCL_OK) {
+            Py_INCREF(arg);
+            return arg;
+        }
+        Tcl_Free(FREECAST argv);
+        if (argc > 1)
+            return Split(list);
+        /* Fall through, returning arg. */
+    }
     else if (PyBytes_Check(arg)) {
         int argc;
         char **argv;