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`.
This commit is contained in:
Bénédikt Tran 2025-01-18 11:45:18 +01:00 committed by GitHub
parent d3adf02c90
commit d5e9aa690a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 4 additions and 1 deletions

View 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)', # ,".*?",

View file

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