BUG: Register SQLite adapters only once per process (GH#64337)#66296
Open
q121212 wants to merge 2 commits into
Open
BUG: Register SQLite adapters only once per process (GH#64337)#66296q121212 wants to merge 2 commits into
q121212 wants to merge 2 commits into
Conversation
Every call to DataFrame.to_sql() with a SQLite connection created a new SQLiteTable instance whose __init__ unconditionally called sqlite3.register_adapter() and sqlite3.register_converter(). These are global mutations, overwriting any custom adapters/converters the user may have registered. Fix: use a module-level _SQLITE_ADAPTERS_REGISTERED flag so adapters and converters are registered only once per process. After the first to_sql() call, subsequent calls are no-ops. This preserves pandas' required adapters (needed because Python 3.12+ removed default datetime adapters) while not stomping on user handlers.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Every call to
DataFrame.to_sql()with a SQLite connection creates a newSQLiteTableinstance, whose
__init__unconditionally callssqlite3.register_adapter()andsqlite3.register_converter()— global mutations. After Python 3.12 removed thedefault adapters for datetime types, pandas added its own (commit
c9a8f95f).But re-registering them on every
to_sql()call overwrites any custom adapters orconverters the user may have registered.
Side-effect analysis
Without pandas' adapters,
to_sql()crashes on Python 3.12+ when insertingdatetime.time,datetime.date, ordatetime.datetimecolumns. The built-insqlite3module no longer provides default adapters for these types.Therefore, we cannot simply stop registering — we must keep the registration
but make it non-destructive.
Fix
Use a module-level
_SQLITE_ADAPTERS_REGISTEREDflag to ensure adapters andconverters are registered only once per process lifetime. After the first
to_sql()call, subsequent calls are no-ops and no longer overwrite userhandlers.
File:
pandas/io/sql.pyAdded test
File:
pandas/tests/io/test_sql.pytest_to_sql_preserves_user_sqlite_adapters— verifies that a customadapter registered after the first
to_sql()call is NOT overwrittenby subsequent
to_sql()calls.Limitations (documented)
If the user registers custom adapters before their first
to_sql()call,pandas will still overwrite them on that first call. This is the same as
current behavior. A follow-up PR can implement save/restore for full
protection.
Checklist
pytest pandas/tests/io/test_sql.py -k test_to_sql_preservesto_sqlbehavior preserved