Skip to content

Commit

Permalink
gh-118761: Improve import time for csv (#128858)
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
picnixz authored Jan 18, 2025
1 parent d3adf02 commit d5e9aa6
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Lib/csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, \
Expand Down Expand Up @@ -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)', # ,".*?",
Expand Down
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit d5e9aa6

Please sign in to comment.