diff options
-rw-r--r-- | README.rst | 15 | ||||
-rw-r--r-- | markupsafe/__init__.py | 6 | ||||
-rw-r--r-- | markupsafe/_native.py | 9 | ||||
-rw-r--r-- | markupsafe/_speedups.c | 12 | ||||
-rw-r--r-- | markupsafe/tests.py | 7 | ||||
-rw-r--r-- | setup.py | 2 |
6 files changed, 46 insertions, 5 deletions
@@ -31,3 +31,18 @@ the `__html__` function: Markup(u'<strong>Nice</strong>') >>> Markup(Foo()) Markup(u'<strong>Nice</strong>') + +Since MarkupSafe 0.10 there is now also a separate escape function +called `escape_silent` that returns an empty string for `None` for +consistency with other systems that return empty strings for `None` +when escaping (for instance Pylons' webhelpers). + +If you also want to use this for the escape method of the Markup +object, you can create your own subclass that does that:: + + from markupsafe import Markup, escape_silent as escape + + class SilentMarkup(Markup): + @classmethod + def escape(cls, s): + return cls(escape(s)) diff --git a/markupsafe/__init__.py b/markupsafe/__init__.py index 88049c1..01c77ce 100644 --- a/markupsafe/__init__.py +++ b/markupsafe/__init__.py @@ -12,7 +12,7 @@ import re from itertools import imap -__all__ = ['Markup', 'soft_unicode', 'escape'] +__all__ = ['Markup', 'soft_unicode', 'escape', 'escape_silent'] _striptags_re = re.compile(r'(<!--.*?-->|<[^>]*>)') @@ -220,6 +220,6 @@ class _MarkupEscapeHelper(object): # we have to import it down here as the speedups and native # modules imports the markup type which is define above. try: - from markupsafe._speedups import escape, soft_unicode + from markupsafe._speedups import escape, escape_silent, soft_unicode except ImportError: - from markupsafe._native import escape, soft_unicode + from markupsafe._native import escape, escape_silent, soft_unicode diff --git a/markupsafe/_native.py b/markupsafe/_native.py index df8ff3d..3acf91b 100644 --- a/markupsafe/_native.py +++ b/markupsafe/_native.py @@ -27,6 +27,15 @@ def escape(s): ) +def escape_silent(s): + """Like :func:`escape` but converts `None` into an empty + markup string. + """ + if s is None: + return Markup() + return escape(s) + + def soft_unicode(s): """Make a string unicode if it isn't already. That way a markup string is not converted back to unicode. diff --git a/markupsafe/_speedups.c b/markupsafe/_speedups.c index 844f811..8b0325e 100644 --- a/markupsafe/_speedups.c +++ b/markupsafe/_speedups.c @@ -160,6 +160,15 @@ escape(PyObject *self, PyObject *text) static PyObject* +escape_silent(PyObject *self, PyObject *text) +{ + if (text != Py_None) + return escape(self, text); + return PyObject_CallFunctionObjArgs(markup, NULL); +} + + +static PyObject* soft_unicode(PyObject *self, PyObject *s) { if (!PyUnicode_Check(s)) @@ -179,6 +188,9 @@ static PyMethodDef module_methods[] = { "Convert the characters &, <, >, ', and \" in string s to HTML-safe\n" "sequences. Use this if you need to display text that might contain\n" "such characters in HTML. Marks return value as markup string."}, + {"escape_silent", (PyCFunction)escape_silent, METH_O, + "escape_silent(s) -> markup\n\n" + "Like escape but converts None to an empty string."}, {"soft_unicode", (PyCFunction)soft_unicode, METH_O, "soft_unicode(object) -> string\n\n" "Make a string unicode if it isn't already. That way a markup\n" diff --git a/markupsafe/tests.py b/markupsafe/tests.py index 7c81cda..77a0efb 100644 --- a/markupsafe/tests.py +++ b/markupsafe/tests.py @@ -1,6 +1,6 @@ import gc import unittest -from markupsafe import Markup, escape +from markupsafe import Markup, escape, escape_silent class MarkupTestCase(unittest.TestCase): @@ -45,6 +45,11 @@ class MarkupTestCase(unittest.TestCase): for item in markup.__all__: getattr(markup, item) + def test_escape_silent(self): + assert escape_silent(None) == Markup() + assert escape(None) == Markup(None) + assert escape_silent('<foo>') == Markup(u'<foo>') + class MarkupLeakTestCase(unittest.TestCase): @@ -63,7 +63,7 @@ def run_setup(with_binary): features['speedups'] = speedups setup( name='MarkupSafe', - version='0.9.3', + version='0.10', url='http://dev.pocoo.org/', license='BSD', author='Armin Ronacher', |