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] 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