From 6bb266d2628cf197995ff63e4ef2176d84ae312b Mon Sep 17 00:00:00 2001 From: Eklavya Sharma Date: Mon, 4 Jul 2016 12:46:58 +0530 Subject: [PATCH] bugdown's __init__.py: Add python 3 compatibility. * Use Response.text instead of Response.content. * Make unescaping work on python 3. --- tools/run-mypy | 1 - zerver/lib/bugdown/__init__.py | 19 +++++++++++++------ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/tools/run-mypy b/tools/run-mypy index 01d8586ff6..3f7e3b7380 100755 --- a/tools/run-mypy +++ b/tools/run-mypy @@ -41,7 +41,6 @@ exclude_py2 = [] exclude_py3 = """ zerver/lib/actions.py -zerver/lib/bugdown/__init__.py zerver/lib/ccache.py zerver/lib/debug.py zerver/lib/email_mirror.py diff --git a/zerver/lib/bugdown/__init__.py b/zerver/lib/bugdown/__init__.py index 4712bc8216..a1c9671402 100644 --- a/zerver/lib/bugdown/__init__.py +++ b/zerver/lib/bugdown/__init__.py @@ -14,7 +14,6 @@ import glob import twitter import platform import time -import six.moves.html_parser import httplib2 import itertools from six.moves import urllib @@ -40,9 +39,12 @@ import zerver.lib.alert_words as alert_words import zerver.lib.mention as mention from zerver.lib.str_utils import force_text import six -from six.moves import range +from six.moves import range, html_parser from six import text_type +if six.PY3: + import html + # Format version of the bugdown rendering; stored along with rendered # messages so that we can efficiently determine what needs to be re-rendered version = 1 @@ -55,6 +57,13 @@ if False: # mypy requires the Optional to be inside Union ElementStringNone = Union[Element, Optional[text_type]] +def unescape(s): + # type: (text_type) -> (text_type) + if six.PY2: + return html_parser.HTMLParser().unescape(s) + else: + return html.unescape(s) + def list_of_tlds(): # type: () -> List[text_type] # HACK we manually blacklist .py @@ -181,7 +190,7 @@ def fetch_open_graph_image(url): # TODO: What if response content is huge? Should we get headers first? try: - content = requests.get(url, timeout=1).content + content = requests.get(url, timeout=1).text except: return None @@ -456,9 +465,7 @@ class InlineInterestingLinkProcessor(markdown.treeprocessors.Treeprocessor): image_url = user.get('profile_image_url_https', user['profile_image_url']) profile_img.set('src', image_url) - ## TODO: unescape is an internal function, so we should - ## use something else if we can find it - text = six.moves.html_parser.HTMLParser().unescape(res['text']) + text = unescape(res['text']) urls = res.get('urls', {}) user_mentions = res.get('user_mentions', []) media = res.get('media', [])