Reputation: 2919
I just wanted to know if there is any Python 3 equivalent of the HTMLParseError
as in Python 2. HTMLParseError
seems to have been deprecated from Python 3.3 onward and removed in Python 3.5.
Is there any way to catch the HTMLParseError
in Python versions > 3.5?
Following is a traceback I receive:
File "/opt/Projects/WAFToast/main.py", line 12, in <module>
HTMLParseError = html.parser.HTMLParseError
AttributeError: module 'html.parser' has no attribute 'HTMLParseError'
Doing some Googling around, I found the same issue being fixed with a patch as below which imho is not a wholesome solution:
try:
from html.parser import HTMLParseError
except ImportError: # Python 3.5+
class HTMLParseError(Exception):
pass
It would be really great if someone could point out the necessary I am missing. =)
Upvotes: 1
Views: 1602
Reputation: 81594
The docs say:
Deprecated since version 3.3, will be removed in version 3.5: This exception has been deprecated because it’s never raised by the parser (when the default non-strict mode is used).
It was removed because nothing raised it unless the (presumably) little used strict mode is used, so a question must be asked: "are you using the strict mode?"
If you are not, you can safely remove the import and the code that catches it.
If you are, check what exception is raised instead (if at all), and import it instead.
If you use strict mode and have to support both versions of Python, you can do something along the lines of
try:
from html.parser import HTMLParseError as ParseError
except ImportError: # Python 3.5+
from html.parser import NewTypeOfFancyException as ParseError
then use except ParseError
where applicable.
Upvotes: 0