diff options
author | David Lord <davidism@gmail.com> | 2017-05-24 15:24:09 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-05-24 15:24:09 -0700 |
commit | 026d620d5f39a6fc630158f14d5ad3f3eaf9377c (patch) | |
tree | 112403ad605ed6bfde2d3a9bac33573007912126 | |
parent | d2a40c41dd1930345628ea9412d97e159f828157 (diff) | |
parent | 674a969c2342dfad20e12bb4c3d9310c9d0fd8f7 (diff) | |
download | markupsafe-026d620d5f39a6fc630158f14d5ad3f3eaf9377c.tar.gz |
Merge pull request #69 from dawran6/issue-68
Fix the return type of escape to Markup
-rw-r--r-- | CHANGES | 10 | ||||
-rw-r--r-- | markupsafe/_native.py | 2 | ||||
-rw-r--r-- | markupsafe/_speedups.c | 5 | ||||
-rwxr-xr-x | tests.py | 8 |
4 files changed, 23 insertions, 2 deletions
@@ -1,6 +1,16 @@ MarkupSafe Changelog ==================== +Version 1.1 +----------- + +unreleased + +- ``escape`` wraps ``__html__`` result in ``Markup``, consistent with + documented behavior. (`#69`_) + +.. _#69: https://github.com/pallets/markupsafe/pull/69 + Version 1.0 ----------- diff --git a/markupsafe/_native.py b/markupsafe/_native.py index 5e83f10..2c54a2d 100644 --- a/markupsafe/_native.py +++ b/markupsafe/_native.py @@ -18,7 +18,7 @@ def escape(s): such characters in HTML. Marks return value as markup string. """ if hasattr(s, '__html__'): - return s.__html__() + return Markup(s.__html__()) return Markup(text_type(s) .replace('&', '&') .replace('>', '>') diff --git a/markupsafe/_speedups.c b/markupsafe/_speedups.c index d779a68..afe3425 100644 --- a/markupsafe/_speedups.c +++ b/markupsafe/_speedups.c @@ -131,8 +131,11 @@ escape(PyObject *self, PyObject *text) /* if the object has an __html__ method that performs the escaping */ html = PyObject_GetAttrString(text, "__html__"); if (html) { - rv = PyObject_CallObject(html, NULL); + s = PyObject_CallObject(html, NULL); Py_DECREF(html); + /* Convert to Markup object */ + rv = PyObject_CallFunctionObjArgs(markup, (PyObject*)s, NULL); + Py_DECREF(s); return rv; } @@ -173,6 +173,14 @@ class MarkupTestCase(unittest.TestCase): def test_mul(self): self.assertEqual(Markup('a') * 3, Markup('aaa')) + def test_escape_return_type(self): + self.assertTrue(isinstance(escape('a'), Markup)) + self.assertTrue(isinstance(escape(Markup('a')), Markup)) + class Foo: + def __html__(self): + return '<strong>Foo</strong>' + self.assertTrue(isinstance(escape(Foo()), Markup)) + class MarkupLeakTestCase(unittest.TestCase): |