]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-118761: Improve import time for `csv` (#128858)
authorBénédikt Tran <10796600+picnixz@users.noreply.github.com>
Sat, 18 Jan 2025 10:45:18 +0000 (11:45 +0100)
committerGitHub <noreply@github.com>
Sat, 18 Jan 2025 10:45:18 +0000 (11:45 +0100)
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`.

Lib/csv.py
Misc/NEWS.d/next/Library/2025-01-15-09-45-43.gh-issue-118761.TvAC8E.rst [new file with mode: 0644]

index cd202659873811a2ea47c7374cc268d26a9ac5f3..0a627ba7a512fa8ab84dc9d442d5724bf2eaf9f2 100644 (file)
@@ -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<delim>[^\w\n"\'])(?P<space> ?)(?P<quote>["\']).*?(?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 (file)
index 0000000..38d18b7
--- /dev/null
@@ -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.