From 4f633bcd0b2e9f3e1c2568639ca01b3e5a820fec Mon Sep 17 00:00:00 2001 From: Eklavya Sharma Date: Wed, 13 Jul 2016 23:04:24 +0530 Subject: [PATCH] zerver/lib/notifications.py: Fix string encoding/decoding. Correctly encode and decode strings in convert_html_to_markdown. It wasn't possible to use universal_newlines=True since Popen.communicate doesn't encode/decode strings correctly on python 2. --- zerver/lib/notifications.py | 6 +++--- zerver/tests/test_hooks.py | 3 --- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/zerver/lib/notifications.py b/zerver/lib/notifications.py index e7ca99da06..a856342f0a 100644 --- a/zerver/lib/notifications.py +++ b/zerver/lib/notifications.py @@ -508,11 +508,11 @@ def convert_html_to_markdown(html): except OSError: continue - markdown = p.communicate(input=html.encode("utf-8"))[0].strip() + markdown = p.communicate(input=html.encode('utf-8'))[0].decode('utf-8').strip() # We want images to get linked and inline previewed, but html2text will turn # them into links of the form `![](http://foo.com/image.png)`, which is # ugly. Run a regex over the resulting description, turning links of the # form `![](http://foo.com/image.png?12345)` into # `[image.png](http://foo.com/image.png)`. - return re.sub(r"!\[\]\((\S*)/(\S*)\?(\S*)\)", - r"[\2](\1/\2)", markdown.decode('utf-8')) + return re.sub(u"!\\[\\]\\((\\S*)/(\\S*)\\?(\\S*)\\)", + u"[\\2](\\1/\\2)", markdown) diff --git a/zerver/tests/test_hooks.py b/zerver/tests/test_hooks.py index c826d291d4..c6ff423dd5 100644 --- a/zerver/tests/test_hooks.py +++ b/zerver/tests/test_hooks.py @@ -763,7 +763,6 @@ class FreshdeskHookTests(WebhookTestCase): STREAM_NAME = 'freshdesk' URL_TEMPLATE = u"/api/v1/external/freshdesk?stream={stream}" - @skip_py3 def test_ticket_creation(self): # type: () -> None """ @@ -830,7 +829,6 @@ Priority: **High** => **Low**""" # type: () -> None self.note_change("public_note", "public") - @skip_py3 def test_inline_image(self): # type: () -> None """ @@ -934,7 +932,6 @@ class PagerDutyHookTests(WebhookTestCase): expected_message = u':grinning: Incident [48219](https://dropbox.pagerduty.com/incidents/PJKGZF9) resolved\n\n>mp_error_block_down_critical\u2119\u01b4' self.send_and_test_stream_message('mp_fail', u"incident 48219", expected_message) - @skip_py3 def test_bad_message(self): # type: () -> None expected_message = 'Unknown pagerduty message\n``` py\n{u\'type\': u\'incident.triggered\'}\n```'