From 66c345058ff3e903be5c935e8b9e9cf3a2312b53 Mon Sep 17 00:00:00 2001 From: Jelle van der Waa Date: Sun, 18 Oct 2026 15:06:54 +0200 Subject: [PATCH 1/2] Use lazy urllib imports in saxutils Both urllib imports are only used for prepare_input_source, normally not used when parsing or writing XML files. A real life example of the benefit of lazy loading is the "firewalld" daemon on Fedora/RHEL Linux systems where lazy loading urllib shaves off 3MB memory. --- Lib/test/test_sax.py | 9 ++++++++- Lib/xml/sax/saxutils.py | 4 +++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_sax.py b/Lib/test/test_sax.py index 46b29650d9383b..0a4964ce4b98cc 100644 --- a/Lib/test/test_sax.py +++ b/Lib/test/test_sax.py @@ -24,8 +24,9 @@ import sys from urllib.error import URLError import urllib.request -from test.support import os_helper +from test.support import cpython_only, os_helper from test.support import findfile, check__all__ +from test.support import import_helper from test.support.os_helper import FakePath, TESTFN @@ -1600,6 +1601,12 @@ def test_deprecated__version__and__date__(self): getattr(module, "version") self.assertEqual(cm.filename, __file__) + @cpython_only + def test_lazy_import(self): + import_helper.ensure_lazy_imports( + "xml.sax.saxutils", {"urllib.parse", "urllib.request"} + ) + if __name__ == "__main__": unittest.main() diff --git a/Lib/xml/sax/saxutils.py b/Lib/xml/sax/saxutils.py index 920283c3f3496f..4bdfff1b9b8bc1 100644 --- a/Lib/xml/sax/saxutils.py +++ b/Lib/xml/sax/saxutils.py @@ -3,7 +3,9 @@ convenience of application and driver writers. """ -import os, urllib.parse, urllib.request +import os +lazy import urllib.parse +lazy import urllib.request import io import codecs from . import handler From b961b188809d10cfd26e9971529bec87600e2868 Mon Sep 17 00:00:00 2001 From: Stan Ulbrych Date: Mon, 19 Oct 2026 18:10:21 +0100 Subject: [PATCH 2/2] Tidy up a few more `import`s --- Lib/test/test_sax.py | 5 ++--- Lib/xml/sax/saxutils.py | 6 +++--- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/Lib/test/test_sax.py b/Lib/test/test_sax.py index 0a4964ce4b98cc..14e5306398107a 100644 --- a/Lib/test/test_sax.py +++ b/Lib/test/test_sax.py @@ -24,9 +24,8 @@ import sys from urllib.error import URLError import urllib.request -from test.support import cpython_only, os_helper -from test.support import findfile, check__all__ -from test.support import import_helper +from test.support import (check__all__, cpython_only, findfile, + import_helper, os_helper) from test.support.os_helper import FakePath, TESTFN diff --git a/Lib/xml/sax/saxutils.py b/Lib/xml/sax/saxutils.py index 4bdfff1b9b8bc1..8225989d60ecec 100644 --- a/Lib/xml/sax/saxutils.py +++ b/Lib/xml/sax/saxutils.py @@ -3,11 +3,12 @@ convenience of application and driver writers. """ +import codecs +import io import os +import sys lazy import urllib.parse lazy import urllib.request -import io -import codecs from . import handler from . import xmlreader @@ -72,7 +73,6 @@ def quoteattr(data, entities={}): def _gettextwriter(out, encoding): if out is None: - import sys return sys.stdout if isinstance(out, io.TextIOBase):