From: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> Date: Sat, 18 Jan 2025 10:45:18 +0000 (+0100) Subject: gh-118761: Improve import time for `csv` (#128858) X-Git-Tag: v3.14.0a5~383 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=d5e9aa690a190bcfa9b43cd3858e26b7b2f72d4f;p=thirdparty%2FPython%2Fcpython.git gh-118761: Improve import time for `csv` (#128858) This reduces the import time of the `csv` module by up to five times, by importing `re` on demand. In particular, the `re` module is no more implicitly exposed as `csv.re`. --- diff --git a/Lib/csv.py b/Lib/csv.py index cd2026598738..0a627ba7a512 100644 --- a/Lib/csv.py +++ b/Lib/csv.py @@ -63,7 +63,6 @@ SETTINGS: written as two quotes """ -import re import types from _csv import Error, writer, reader, register_dialect, \ unregister_dialect, get_dialect, list_dialects, \ @@ -281,6 +280,7 @@ class Sniffer: If there is no quotechar the delimiter can't be determined this way. """ + import re matches = [] for restr in (r'(?P[^\w\n"\'])(?P ?)(?P["\']).*?(?P=quote)(?P=delim)', # ,".*?", diff --git a/Misc/NEWS.d/next/Library/2025-01-15-09-45-43.gh-issue-118761.TvAC8E.rst b/Misc/NEWS.d/next/Library/2025-01-15-09-45-43.gh-issue-118761.TvAC8E.rst new file mode 100644 index 000000000000..38d18b7f4ca0 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-01-15-09-45-43.gh-issue-118761.TvAC8E.rst @@ -0,0 +1,3 @@ +Reduce the import time of :mod:`csv` by up to five times, by importing +:mod:`re` on demand. In particular, ``re`` is no more implicitly exposed +as ``csv.re``. Patch by Bénédikt Tran.