Skip to content

Commit

Permalink
Catch UnicodeDecodeError (Python 3).
Browse files Browse the repository at this point in the history
  • Loading branch information
peteradrichem committed Dec 26, 2024
1 parent 5e722e2 commit dd024e0
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
/build/
/dist/
/docs/_build/
/venv/
/xml/
4 changes: 4 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ Changelog

This document records all notable changes to `Xul <https://xul.readthedocs.io/>`_.

`2.5.1 <https://github.com/peteradrichem/Xul/compare/2.5.0...2.5.1>`_ (2024-12-26)
----------------------------------------------------------------------------------
* Catch UnicodeDecodeError (Python 3).

`2.5.0 <https://github.com/peteradrichem/Xul/compare/2.4.2...2.5.0>`_ (2024-12-25)
----------------------------------------------------------------------------------
* Documentation updates.
Expand Down
2 changes: 1 addition & 1 deletion src/xul/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
"""

# Xul version.
__version_info__ = ("2", "5", "0")
__version_info__ = ("2", "5", "1")
__version__ = ".".join(__version_info__)
17 changes: 8 additions & 9 deletions src/xul/etree.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-

"""ElementTree XML API.
The ElementTree XML API:
Expand All @@ -12,15 +10,13 @@
https://lxml.de/tutorial.html
"""


from logging import getLogger
import sys
from logging import getLogger

# pylint: disable=no-member
# lxml ElementTree <https://lxml.de/>
from lxml import etree


# Module logging initialisation.
logger = getLogger(__name__)

Expand Down Expand Up @@ -59,16 +55,19 @@ def build_etree(xml_source, parser=None, lenient=True, silent=False):
except etree.XMLSyntaxError:
if silent:
return None

if lenient:
xmllogger = logger.warning
else:
xmllogger = logger.error
if xml_source in ('-', sys.stdin):

if xml_source in ("-", sys.stdin):
name = sys.stdin.name
xml_type = "object"
else:
name = xml_source
xml_type = "file"

xmllogger("%s is not a valid XML %s:", name, xml_type)

# Parsers have an error_log property that lists the errors and warnings
Expand All @@ -84,8 +83,8 @@ def build_etree(xml_source, parser=None, lenient=True, silent=False):
return None
# Catch EnvironmentError (IOError) exceptions, for example:
# "failed to load external entity" (lxml.etree._raiseParseError)
except EnvironmentError as e:
except (EnvironmentError, UnicodeDecodeError) as e:
logger.error(e)
return None
else:
return el_tree

return el_tree

0 comments on commit dd024e0

Please sign in to comment.