summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArmin Ronacher <armin.ronacher@active-4.com>2014-05-08 16:52:53 +0200
committerArmin Ronacher <armin.ronacher@active-4.com>2014-05-08 16:52:53 +0200
commitb18391aad21f6ce3b2f21644ecd1b1a976375713 (patch)
treede009265ac12f28af03586adef3f5c64b747d19b
parentd2129f2b37529d9fa890e8975f21f0b1cf9956c2 (diff)
downloadmarkupsafe-b18391aad21f6ce3b2f21644ecd1b1a976375713.tar.gz
Added a workaround for Python bug 13598
-rw-r--r--markupsafe/__init__.py35
-rw-r--r--markupsafe/tests.py4
2 files changed, 37 insertions, 2 deletions
diff --git a/markupsafe/__init__.py b/markupsafe/__init__.py
index 6040dc1..2755401 100644
--- a/markupsafe/__init__.py
+++ b/markupsafe/__init__.py
@@ -10,6 +10,7 @@
"""
import re
import string
+from collections import Mapping
from markupsafe._compat import text_type, string_types, int_types, \
unichr, iteritems, PY2
@@ -196,7 +197,8 @@ class Markup(text_type):
def format(*args, **kwargs):
self, args = args[0], args[1:]
formatter = EscapeFormatter(self.escape)
- return self.__class__(formatter.format(self, *args, **kwargs))
+ kwargs = _MagicFormatMapping(args, kwargs)
+ return self.__class__(formatter.vformat(self, args, kwargs))
def __html_format__(self, format_spec):
if format_spec:
@@ -211,6 +213,37 @@ class Markup(text_type):
del method, make_simple_escaping_wrapper
+class _MagicFormatMapping(Mapping):
+ """This class implements a dummy wrapper to fix a bug in the Python
+ standard library for string formatting.
+
+ See http://bugs.python.org/issue13598 for information about why
+ this is necessary.
+ """
+
+ def __init__(self, args, kwargs):
+ self._args = args
+ self._kwargs = kwargs
+ self._last_index = 0
+
+ def __getitem__(self, key):
+ if key == '':
+ idx = self._last_index
+ self._last_index += 1
+ try:
+ return self._args[idx]
+ except LookupError:
+ pass
+ key = str(idx)
+ return self._kwargs[key]
+
+ def __iter__(self):
+ return iter(self._kwargs)
+
+ def __len__(self):
+ return len(self._kwargs)
+
+
if hasattr(text_type, 'format'):
class EscapeFormatter(string.Formatter):
diff --git a/markupsafe/tests.py b/markupsafe/tests.py
index 0731926..02083c6 100644
--- a/markupsafe/tests.py
+++ b/markupsafe/tests.py
@@ -75,7 +75,9 @@ class MarkupTestCase(unittest.TestCase):
(Markup('{0[1][bar]}').format([0, {'bar': '<bar/>'}]),
'&lt;bar/&gt;'),
(Markup('{0[1][bar]}').format([0, {'bar': Markup('<bar/>')}]),
- '<bar/>')):
+ '<bar/>'),
+ (Markup('{}').format(0),
+ Markup('0'))):
assert actual == expected, "%r should be %r!" % (actual, expected)
def test_custom_formatting(self):