Skip to content

Commit bfca55e

Browse files
committed
import cElementTree where possible
1 parent aa18b90 commit bfca55e

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

htmlement.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@
2626
from __future__ import unicode_literals
2727

2828
# Standard library imports
29-
from xml.etree import ElementTree as Etree
3029
from codecs import open as _open
3130
import warnings
3231
import sys
3332
import re
3433

3534
# Check python version to set the object that can detect non unicode strings
3635
if sys.version_info >= (3, 0):
36+
import xml.etree.ElementTree as Etree
3737
# noinspection PyUnresolvedReferences,PyCompatibility
3838
from html.parser import HTMLParser
3939
# noinspection PyUnresolvedReferences, PyCompatibility
@@ -48,6 +48,14 @@
4848
# noinspection PyUnresolvedReferences
4949
_chr = unichr
5050

51+
try:
52+
# This attemps to import the C version of ElementTree
53+
import xml.etree.cElementTree as Etree
54+
# This will fail if the implementation is broken
55+
Etree.Comment("Test for broken cElementTree")
56+
except (ImportError, TypeError):
57+
import xml.etree.ElementTree as Etree
58+
5159
__all__ = ["HTMLement", "fromstring", "fromstringlist", "parse"]
5260
__repo__ = "https://github.com/willforde/python-htmlement"
5361
__copyright__ = "Copyright (C) 2016 William Forde"

0 commit comments

Comments
 (0)