From dd024e0c186b16a96d6a3a85e50e9a09296f7716 Mon Sep 17 00:00:00 2001 From: Peter Adrichem Date: Wed, 25 Dec 2024 20:52:14 +0100 Subject: [PATCH] Catch UnicodeDecodeError (Python 3). --- .gitignore | 1 + docs/changelog.rst | 4 ++++ src/xul/__init__.py | 2 +- src/xul/etree.py | 17 ++++++++--------- 4 files changed, 14 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index 65c79e7..4a0aa0b 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,5 @@ /build/ /dist/ /docs/_build/ +/venv/ /xml/ diff --git a/docs/changelog.rst b/docs/changelog.rst index 68c0419..494e6cf 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -3,6 +3,10 @@ Changelog This document records all notable changes to `Xul `_. +`2.5.1 `_ (2024-12-26) +---------------------------------------------------------------------------------- +* Catch UnicodeDecodeError (Python 3). + `2.5.0 `_ (2024-12-25) ---------------------------------------------------------------------------------- * Documentation updates. diff --git a/src/xul/__init__.py b/src/xul/__init__.py index f0471b6..d2b576a 100644 --- a/src/xul/__init__.py +++ b/src/xul/__init__.py @@ -9,5 +9,5 @@ """ # Xul version. -__version_info__ = ("2", "5", "0") +__version_info__ = ("2", "5", "1") __version__ = ".".join(__version_info__) diff --git a/src/xul/etree.py b/src/xul/etree.py index b5c57cb..398b4f7 100644 --- a/src/xul/etree.py +++ b/src/xul/etree.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- - """ElementTree XML API. The ElementTree XML API: @@ -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 from lxml import etree - # Module logging initialisation. logger = getLogger(__name__) @@ -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 @@ -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