Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the return type of escape to Markup #69

Merged
merged 3 commits into from
May 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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