[packages/python3-sphinx-jinja2-compat] Rel 2

arekm arekm at pld-linux.org
Sat Apr 11 18:33:41 CEST 2026


commit fcd0729cc7150a9c6aadd17489d9e03ba7801d78
Author: Arkadiusz Miśkiewicz <arekm at maven.pl>
Date:   Sat Apr 11 18:33:29 2026 +0200

    Rel 2

 lazy-loading.patch                | 133 ++++++++++++++++++++++++++++++++++++++
 python3-sphinx-jinja2-compat.spec |   4 +-
 2 files changed, 136 insertions(+), 1 deletion(-)
---
diff --git a/python3-sphinx-jinja2-compat.spec b/python3-sphinx-jinja2-compat.spec
index a0d2294..bf454eb 100644
--- a/python3-sphinx-jinja2-compat.spec
+++ b/python3-sphinx-jinja2-compat.spec
@@ -2,13 +2,14 @@ Summary:	Patches Jinja2 v3 to restore compatibility with earlier Sphinx versions
 Summary(pl.UTF-8):	Modyfikacja Jinja2 v3 w celu odzyskania zgodności ze starszymi wersjami Sphinksa
 Name:		python3-sphinx-jinja2-compat
 Version:	0.4.1
-Release:	1
+Release:	2
 License:	MIT
 Group:		Libraries/Python
 #Source0Download: https://pypi.org/simple/sphinx-jinja2-compat/
 Source0:	https://files.pythonhosted.org/packages/source/s/sphinx_jinja2_compat/sphinx_jinja2_compat-%{version}.tar.gz
 # Source0-md5:	84662d2b577e559a7f6165cacd4f8e39
 Patch0:		deps.patch
+Patch1:		lazy-loading.patch
 URL:		https://github.com/sphinx-toolbox/sphinx-jinja2-compat
 BuildRequires:	python3 >= 1:3.6
 BuildRequires:	python3-build
@@ -32,6 +33,7 @@ wersjami Sphinksa.
 %prep
 %setup -q -n sphinx_jinja2_compat-%{version}
 %patch -P0 -p1
+%patch -P1 -p1
 
 %build
 %py3_build_pyproject
diff --git a/lazy-loading.patch b/lazy-loading.patch
new file mode 100644
index 0000000..185300a
--- /dev/null
+++ b/lazy-loading.patch
@@ -0,0 +1,133 @@
+Do not eagerly import jinja2/markupsafe/sphinx_prompt from the
+site.py-triggered .pth file.
+
+Upstream's __init__.py, when invoked from the installed
+_sphinx_jinja2_compat.pth at site.py time, eagerly imports markupsafe,
+jinja2, and sphinx_prompt (when sphinx-prompt is installed) to patch
+them. On any host where sphinx-prompt is installed, that transitively
+imports sphinx.application, which pulls in sphinx.util.logging,
+docutils, pygments, babel, pytz, uuid, unicodedata, and ~180 other
+modules into every Python subprocess at interpreter startup.
+
+This pollutes sys.modules and warnings.filters for every unrelated
+Python invocation on the system, breaks CPython's own test suite
+(test_logging.test_relativeCreated_has_higher_precision,
+test_cmd_line.test_xdev, test_cmd_line.test_warnings_filter_precedence,
+test_io.test_check_encoding_warning, test_unicodedata.*,
+test_perf_profiler.*, test_subprocess.*), and adds ~200 ms to every
+Python subprocess startup.
+
+The package already ships a proper importlib meta-path finder in
+_meta_path.py which patches jinja2/markupsafe only when they are
+actually imported by the consumer. Make that the default (rather than
+an ImportError fallback), and extend the finder to also handle the
+"sphinx-prompt" (dashed) alias that upstream installs eagerly — some
+sphinx documentation conf.py files list extensions = ['sphinx-prompt']
+with a dash and rely on that alias being populated. Doing it via the
+meta-path finder keeps that compatibility for doc builds that really
+do load sphinx, without paying the cost for every python -c that
+does not.
+
+diff -urN sphinx_jinja2_compat-0.4.1/sphinx_jinja2_compat/__init__.py sphinx_jinja2_compat-0.4.1/sphinx_jinja2_compat/__init__.py
+--- sphinx_jinja2_compat-0.4.1.orig/sphinx_jinja2_compat/__init__.py	2026-04-11 18:06:13.306034692 +0200
++++ sphinx_jinja2_compat-0.4.1/sphinx_jinja2_compat/__init__.py	2026-04-11 18:06:28.183086051 +0200
+@@ -31,8 +31,8 @@
+ import sys
+ from typing import List
+ 
+-# this package
+-from sphinx_jinja2_compat._installers import install_jinja2, install_markupsafe
++# this package (jinja2/markupsafe are patched lazily via the meta-path finder)
++from sphinx_jinja2_compat._meta_path import _Finder
+ 
+ __all__: List[str] = []
+ 
+@@ -49,32 +49,13 @@
+ 		import types
+ 		types.Union = types.UnionType
+ 
+-	try:
+-
+-		# 3rd party
+-		import markupsafe
+-
+-		install_markupsafe(markupsafe)
+-
+-		# 3rd party
+-		import jinja2
+-		import jinja2.filters
+-		import jinja2.utils
+-
+-		install_jinja2(jinja2, jinja2.filters, jinja2.utils)
+-
+-	except ImportError:
+-		# Unable to import one module
+-		# Perhaps they are in global site-packages and we're not,
+-		# so they aren't available yet?
+-
+-		# this package
+-		from sphinx_jinja2_compat._meta_path import _Finder
+-		sys.meta_path.insert(0, _Finder())
+-
+-	# Ensure sphinx_prompt can also be imported from sphinx-prompt
+-	# (whether due to https://github.com/sbrunner/sphinx-prompt/issues/612
+-	# or building from source or an eventual deliberate removal)
++	# Always install the lazy meta-path finder. It patches jinja2 and
++	# markupsafe, and populates the "sphinx-prompt" (dashed) alias, only
++	# when those names are actually imported by the consumer — rather
++	# than dragging jinja2, sphinx, docutils, logging, and ~180 other
++	# modules into sys.modules at site.py time for every unrelated
++	# Python subprocess on the system.
++	sys.meta_path.insert(0, _Finder())
+ 
+ 	if sys.version_info >= (3, 12):
+ 		# Ensure distutils is patched first, if setuptools is installed
+@@ -82,11 +63,3 @@
+ 			__import__("_distutils_hack").add_shim()
+ 		except ImportError:
+ 			pass
+-
+-	try:
+-		# 3rd party
+-		import sphinx_prompt  # type: ignore[import]
+-	except ImportError:
+-		pass
+-	else:
+-		sys.modules["sphinx-prompt"] = sphinx_prompt
+diff -urN sphinx_jinja2_compat-0.4.1/sphinx_jinja2_compat/_meta_path.py sphinx_jinja2_compat-0.4.1/sphinx_jinja2_compat/_meta_path.py
+--- sphinx_jinja2_compat-0.4.1.orig/sphinx_jinja2_compat/_meta_path.py	2026-04-11 18:06:13.306100664 +0200
++++ sphinx_jinja2_compat-0.4.1/sphinx_jinja2_compat/_meta_path.py	2026-04-11 18:06:43.743086052 +0200
+@@ -94,3 +94,33 @@
+ 						pass
+ 
+ 				return importlib.util.spec_from_loader("jinja2", _Jinja2Loader(), origin=jinja2.__file__)
++
++		elif fullname == "sphinx-prompt":
++
++			if self in sys.meta_path:
++
++				# Lazily import the real sphinx_prompt module and alias it
++				# under the dashed name. This preserves the upstream
++				# behaviour (some sphinx conf.py files reference the
++				# dashed name in their extensions list) without eagerly
++				# importing sphinx at site.py time for every Python
++				# subprocess on the system.
++				self._stack.append("sphinx-prompt")
++				try:
++					sphinx_prompt = importlib.import_module("sphinx_prompt")
++				finally:
++					self._stack.pop()
++
++				class _SphinxPromptAliasLoader(importlib.abc.Loader):
++
++					def create_module(self, spec):
++						return sphinx_prompt
++
++					def exec_module(self, module):
++						pass
++
++				return importlib.util.spec_from_loader(
++						"sphinx-prompt",
++						_SphinxPromptAliasLoader(),
++						origin=getattr(sphinx_prompt, "__file__", None),
++						)
================================================================

---- gitweb:

http://git.pld-linux.org/gitweb.cgi/packages/python3-sphinx-jinja2-compat.git/commitdiff/fcd0729cc7150a9c6aadd17489d9e03ba7801d78



More information about the pld-cvs-commit mailing list