Skip to content

Commit

Permalink
Merge pull request #69 from dawran6/issue-68
Browse files Browse the repository at this point in the history
Fix the return type of escape to Markup
  • Loading branch information
davidism committed May 24, 2017
2 parents d2a40c4 + 674a969 commit 026d620
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
10 changes: 10 additions & 0 deletions CHANGES
@@ -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
-----------

Expand Down
2 changes: 1 addition & 1 deletion markupsafe/_native.py
Expand Up @@ -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('>', '>')
Expand Down
5 changes: 4 additions & 1 deletion markupsafe/_speedups.c
Expand Up @@ -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;
}

Expand Down
8 changes: 8 additions & 0 deletions tests.py
Expand Up @@ -173,6 +173,14 @@ def test_splitting(self):
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):

Expand Down

0 comments on commit 026d620

Please sign in to comment.