summaryrefslogtreecommitdiff
path: root/markupsafe
diff options
context:
space:
mode:
authorArmin Ronacher <armin.ronacher@active-4.com>2010-08-13 00:54:23 +0200
committerArmin Ronacher <armin.ronacher@active-4.com>2010-08-13 00:54:23 +0200
commit3051422b114d4292fef31c1e2660498c5b7d2353 (patch)
tree77b3c2691252c0f8ca583cc4666d7844c6506fce /markupsafe
parent4fa38b6fb0923789b2a9e6c7f980687c1725384d (diff)
downloadmarkupsafe-3051422b114d4292fef31c1e2660498c5b7d2353.tar.gz
MarkupSafe now provides a escape_silent method as well.
Diffstat (limited to 'markupsafe')
-rw-r--r--markupsafe/__init__.py6
-rw-r--r--markupsafe/_native.py9
-rw-r--r--markupsafe/_speedups.c12
-rw-r--r--markupsafe/tests.py7
4 files changed, 30 insertions, 4 deletions
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'&lt;foo&gt;')
+
class MarkupLeakTestCase(unittest.TestCase):