From abef3af9f48e3a703a7de4b0bc59d64ab716ffc5 Mon Sep 17 00:00:00 2001 From: t3573393 <283616181@qq.com> Date: Mon, 10 Sep 2018 15:44:06 +0800 Subject: [PATCH] fix the python3 compatibility --- html2text.py | 7 ++++++- test/run_tests.py | 18 ++++++++++++++---- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/html2text.py b/html2text.py index 17528901..99b36b83 100755 --- a/html2text.py +++ b/html2text.py @@ -35,6 +35,11 @@ def has_key(x, y): try: from textwrap import wrap except: pass +#support the python3 API +if sys.version_info[0] == 3: + unichr=chr + xrange=range + # Use Unicode characters instead of their ascii psuedo-replacements UNICODE_SNOB = 0 @@ -543,7 +548,7 @@ def handle_tag(self, tag, attrs, start): nest_count = self.google_nest_count(tag_style) else: nest_count = len(self.list) - self.o(" " * nest_count) #TODO: line up
  1. s > 9 correctly. + self.o(" " * int(nest_count)) #TODO: line up
    1. s > 9 correctly. if li['name'] == "ul": self.o(self.ul_item_mark + " ") elif li['name'] == "ol": li['num'] += 1 diff --git a/test/run_tests.py b/test/run_tests.py index 7ebfd394..b2257536 100644 --- a/test/run_tests.py +++ b/test/run_tests.py @@ -20,11 +20,18 @@ def test_module(fn, google_doc=False, **kwargs): h.body_width = 0 h.hide_strikethrough = True - for k, v in kwargs.iteritems(): - setattr(h, k, v) + if sys.version_info[0] == 3: + for k, v in kwargs.items(): + setattr(h, k, v) + else: + for k, v in kwargs.iteritems(): + setattr(h, k, v) result = get_baseline(fn) - actual = h.handle(file(fn).read()) + if sys.version_info[0] == 3: + actual = h.handle(codecs.open(fn, mode='r', encoding='utf8').read()) + else: + actual = h.handle(file(fn).read()) return print_result(fn, 'module', result, actual) def test_command(fn, *args): @@ -43,7 +50,10 @@ def test_command(fn, *args): cmd += [fn] result = get_baseline(fn) - actual = subprocess.Popen(cmd, stdout=subprocess.PIPE).stdout.read() + if sys.version_info[0] == 3: + actual = subprocess.Popen(cmd, stdout=subprocess.PIPE).stdout.read().decode('utf8') + else: + actual = subprocess.Popen(cmd, stdout=subprocess.PIPE).stdout.read() if os.name == 'nt': # Fix the unwanted CR to CRCRLF replacement