diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2013-01-15 15:59:53 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2013-01-15 15:59:53 (GMT) |
commit | fb3c6286a666d1ff16ed58c30a54498f1b152e22 (patch) | |
tree | 714e1ec3e4ffaed3768e45d0f028ef1a4466fa89 /Lib/tkinter/__init__.py | |
parent | a6b9e3bf4e8e407917ce155688ce9feec3d3b905 (diff) | |
parent | 1e2b7ee3e8b7040228e05c7ed38937bf6390c9b7 (diff) | |
download | cpython-fb3c6286a666d1ff16ed58c30a54498f1b152e22.zip cpython-fb3c6286a666d1ff16ed58c30a54498f1b152e22.tar.gz cpython-fb3c6286a666d1ff16ed58c30a54498f1b152e22.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__.py | 31 |
1 files changed, 30 insertions, 1 deletions
diff --git a/Lib/tkinter/__init__.py b/Lib/tkinter/__init__.py index 3956e85..b261144 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: |