https://github.com/python/cpython/commit/d5e9aa690a190bcfa9b43cd3858e26b7b2f72d4f
commit: d5e9aa690a190bcfa9b43cd3858e26b7b2f72d4f
branch: main
author: Bénédikt Tran <[email protected]>
committer: picnixz <[email protected]>
date: 2025-01-18T11:45:18+01:00
summary:

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`.

files:
A Misc/NEWS.d/next/Library/2025-01-15-09-45-43.gh-issue-118761.TvAC8E.rst
M Lib/csv.py

diff --git a/Lib/csv.py b/Lib/csv.py
index cd202659873811..0a627ba7a512fa 100644
--- a/Lib/csv.py
+++ b/Lib/csv.py
@@ -63,7 +63,6 @@ class excel:
         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 @@ def _guess_quote_and_delimiter(self, data, delimiters):
         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
index 00000000000000..38d18b7f4ca05e
--- /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.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to