Skip to content

Commit 8c74cea

Browse files
committed
remove python2 support
(cherry picked from commit 4efef11)
1 parent 19e3f9b commit 8c74cea

File tree

2 files changed

+15
-40
lines changed

2 files changed

+15
-40
lines changed

htmlement.py

Lines changed: 13 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -29,50 +29,25 @@
2929
3030
Github: https://github.com/willforde/python-htmlement
3131
Documentation: https://python-htmlement.readthedocs.io/en/stable/?badge=stable
32-
Testing: https://www.travis-ci.com/willforde/python-htmlement
33-
Coverage: https://coveralls.io/github/willforde/python-htmlement?branch=master
32+
Testing: https://github.com/willforde/python-htmlement/actions
33+
Coverage: https://codecov.io/gh/willforde/python-htmlement
3434
Maintainability: https://codeclimate.com/github/willforde/python-htmlement/maintainability
3535
"""
3636

37-
38-
# Python 2 compatibility
39-
from __future__ import unicode_literals
40-
41-
# Standard library imports
42-
from codecs import open as _open
37+
# Standard Lib
38+
import xml.etree.ElementTree as Etree
4339
import warnings
44-
import sys
4540
import re
4641

47-
# Check python version to set the object that can detect non unicode strings
48-
if sys.version_info >= (3, 0):
49-
import xml.etree.ElementTree as Etree
50-
# noinspection PyUnresolvedReferences,PyCompatibility
51-
from html.parser import HTMLParser
52-
# noinspection PyUnresolvedReferences, PyCompatibility
53-
from html.entities import name2codepoint
54-
# Python2 compatibility
55-
_chr = chr
56-
else:
57-
# noinspection PyUnresolvedReferences,PyCompatibility
58-
from HTMLParser import HTMLParser
59-
# noinspection PyUnresolvedReferences, PyCompatibility
60-
from htmlentitydefs import name2codepoint
61-
# noinspection PyUnresolvedReferences
62-
_chr = unichr
63-
64-
try:
65-
# This attemps to import the C version of ElementTree
66-
import xml.etree.cElementTree as Etree
67-
# This will fail if the implementation is broken
68-
Etree.Comment("Test for broken cElementTree")
69-
except (ImportError, TypeError):
70-
import xml.etree.ElementTree as Etree
42+
# HTML Parser
43+
from html.entities import name2codepoint
44+
from html.parser import HTMLParser
7145

7246
__all__ = ["HTMLement", "fromstring", "fromstringlist", "parse"]
73-
__version__ = "1.0.1"
47+
__version__ = "2.0.0"
7448

7549
# Add missing codepoints
50+
# TODO: This may no longer be required
7651
name2codepoint["apos"] = 0x0027
7752

7853

@@ -152,7 +127,7 @@ def parse(source, tag="", attrs=None, encoding=None):
152127
"""
153128
# Assume that source is a file pointer if no read methods is found
154129
if not hasattr(source, "read"):
155-
source = _open(source, "rb", encoding=encoding)
130+
source = open(source, "rb", encoding=encoding)
156131
close_source = True
157132
else:
158133
close_source = False
@@ -385,7 +360,7 @@ def handle_data(self, data):
385360
def handle_entityref(self, name):
386361
if self.enabled:
387362
try:
388-
name = _chr(name2codepoint[name])
363+
name = chr(name2codepoint[name])
389364
except KeyError:
390365
pass
391366
self._data.append(name)
@@ -394,9 +369,9 @@ def handle_charref(self, name):
394369
if self.enabled:
395370
try:
396371
if name[0].lower() == "x":
397-
name = _chr(int(name[1:], 16))
372+
name = chr(int(name[1:], 16))
398373
else:
399-
name = _chr(int(name))
374+
name = chr(int(name))
400375
except ValueError:
401376
pass
402377
self._data.append(name)

tests/test_module.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
# -*- coding: utf-8 -*-
33

44
# Python 2 compatibility
5-
from __future__ import unicode_literals
65
import xml.etree.ElementTree as Etree
76
import htmlement
87
import examples
@@ -56,7 +55,7 @@ def test_basic_partial():
5655

5756

5857
def test_nohtml_tree():
59-
# Check the the missing html starting tag is created
58+
# Check that the missing html starting tag is created
6059
html = "<body></body>"
6160
root = quick_parsehtml(html)
6261
assert root.tag == "html"
@@ -179,6 +178,7 @@ def test_text_iterator():
179178
body = root.find(".//body")
180179
assert "".join(body.itertext()) == "sample text content"
181180

181+
182182
def test_text_iterator_unclosed_tag():
183183
html = "<html><body><div>hello <span>to <span>the <span>world!</div></body><footer>unrelated</footer></html>"
184184
root = quick_parsehtml(html)

0 commit comments

Comments
 (0)