Skip to content

Commit dd024e0

Browse files
committed
Catch UnicodeDecodeError (Python 3).
1 parent 5e722e2 commit dd024e0

File tree

4 files changed

+14
-10
lines changed

4 files changed

+14
-10
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55
/build/
66
/dist/
77
/docs/_build/
8+
/venv/
89
/xml/

docs/changelog.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ Changelog
33

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

6+
`2.5.1 <https://github.com/peteradrichem/Xul/compare/2.5.0...2.5.1>`_ (2024-12-26)
7+
----------------------------------------------------------------------------------
8+
* Catch UnicodeDecodeError (Python 3).
9+
610
`2.5.0 <https://github.com/peteradrichem/Xul/compare/2.4.2...2.5.0>`_ (2024-12-25)
711
----------------------------------------------------------------------------------
812
* Documentation updates.

src/xul/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
"""
1010

1111
# Xul version.
12-
__version_info__ = ("2", "5", "0")
12+
__version_info__ = ("2", "5", "1")
1313
__version__ = ".".join(__version_info__)

src/xul/etree.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# -*- coding: utf-8 -*-
2-
31
"""ElementTree XML API.
42
53
The ElementTree XML API:
@@ -12,15 +10,13 @@
1210
https://lxml.de/tutorial.html
1311
"""
1412

15-
16-
from logging import getLogger
1713
import sys
14+
from logging import getLogger
1815

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

23-
2420
# Module logging initialisation.
2521
logger = getLogger(__name__)
2622

@@ -59,16 +55,19 @@ def build_etree(xml_source, parser=None, lenient=True, silent=False):
5955
except etree.XMLSyntaxError:
6056
if silent:
6157
return None
58+
6259
if lenient:
6360
xmllogger = logger.warning
6461
else:
6562
xmllogger = logger.error
66-
if xml_source in ('-', sys.stdin):
63+
64+
if xml_source in ("-", sys.stdin):
6765
name = sys.stdin.name
6866
xml_type = "object"
6967
else:
7068
name = xml_source
7169
xml_type = "file"
70+
7271
xmllogger("%s is not a valid XML %s:", name, xml_type)
7372

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

0 commit comments

Comments
 (0)