summaryrefslogtreecommitdiffstats
path: root/Lib/tkinter/__init__.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2013-01-15 15:58:14 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2013-01-15 15:58:14 (GMT)
commit1e2b7ee3e8b7040228e05c7ed38937bf6390c9b7 (patch)
tree7a76229b267d3b5e9fb1c85a848d89e7a451d9c4 /Lib/tkinter/__init__.py
parentd94c554f7ddf29acbec8918bd53d93999cfd08a5 (diff)
parentb1396523660ac74435976fcaa2d490f3124c6add (diff)
downloadcpython-1e2b7ee3e8b7040228e05c7ed38937bf6390c9b7.zip
cpython-1e2b7ee3e8b7040228e05c7ed38937bf6390c9b7.tar.gz
cpython-1e2b7ee3e8b7040228e05c7ed38937bf6390c9b7.tar.bz2
Issue #15861: tkinter now correctly works with lists and tuples containing
strings with whitespaces, backslashes or unbalanced braces.
Diffstat (limited to 'Lib/tkinter/__init__.py')
-rw-r--r--Lib/tkinter/__init__.py31
1 files changed, 30 insertions, 1 deletions
diff --git a/Lib/tkinter/__init__.py b/Lib/tkinter/__init__.py
index 6755be6..ea23705 100644
--- a/Lib/tkinter/__init__.py
+++ b/Lib/tkinter/__init__.py
@@ -40,6 +40,7 @@ import warnings
import _tkinter # If this fails your Python may not be configured for Tk
TclError = _tkinter.TclError
from tkinter.constants import *
+import re
wantobjects = 1
@@ -52,6 +53,34 @@ WRITABLE = _tkinter.WRITABLE
EXCEPTION = _tkinter.EXCEPTION
+_magic_re = re.compile(r'([\\{}])')
+_space_re = re.compile(r'([\s])', re.ASCII)
+
+def _join(value):
+ """Internal function."""
+ return ' '.join(map(_stringify, value))
+
+def _stringify(value):
+ """Internal function."""
+ if isinstance(value, (list, tuple)):
+ if len(value) == 1:
+ value = _stringify(value[0])
+ if value[0] == '{':
+ value = '{%s}' % value
+ else:
+ value = '{%s}' % _join(value)
+ else:
+ value = str(value)
+ if not value:
+ value = '{}'
+ elif _magic_re.search(value):
+ # add '\' before special characters and spaces
+ value = _magic_re.sub(r'\\\1', value)
+ value = _space_re.sub(r'\\\1', value)
+ elif value[0] == '"' or _space_re.search(value):
+ value = '{%s}' % value
+ return value
+
def _flatten(seq):
"""Internal function."""
res = ()
@@ -1089,7 +1118,7 @@ class Misc:
if isinstance(item, int):
nv.append(str(item))
elif isinstance(item, str):
- nv.append(('{%s}' if ' ' in item else '%s') % item)
+ nv.append(_stringify(item))
else:
break
else: