summaryrefslogtreecommitdiff
path: root/markupsafe
diff options
context:
space:
mode:
Diffstat (limited to 'markupsafe')
-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):