From: Mike Bayer Date: Mon, 16 Dec 2024 15:58:01 +0000 (-0500) Subject: harden HSTORE registration X-Git-Tag: rel_2_0_37~20 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1f726a3a8819f518e760a544076228dbc2dd169d;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git harden HSTORE registration * use the driver_connection when we register on the connection * assert targets passed to register_hstore assert as boolean true; psycopg docs say "if None, register globally" but looking in the source it's actually registering globally if any false-evaluating object is passed. Change-Id: Ie1fd7c96714b7fe76ef964501691fa48352be259 (cherry picked from commit 29569ccfde7247a7e0ed2afe43db53494da62fb2) --- diff --git a/lib/sqlalchemy/dialects/postgresql/hstore.py b/lib/sqlalchemy/dialects/postgresql/hstore.py index 5a2d451316..291af36c69 100644 --- a/lib/sqlalchemy/dialects/postgresql/hstore.py +++ b/lib/sqlalchemy/dialects/postgresql/hstore.py @@ -195,6 +195,9 @@ class HSTORE(sqltypes.Indexable, sqltypes.Concatenable, sqltypes.TypeEngine): comparator_factory = Comparator def bind_processor(self, dialect): + # note that dialect-specific types like that of psycopg and + # psycopg2 will override this method to allow driver-level conversion + # instead, see _PsycopgHStore def process(value): if isinstance(value, dict): return _serialize_hstore(value) @@ -204,6 +207,9 @@ class HSTORE(sqltypes.Indexable, sqltypes.Concatenable, sqltypes.TypeEngine): return process def result_processor(self, dialect, coltype): + # note that dialect-specific types like that of psycopg and + # psycopg2 will override this method to allow driver-level conversion + # instead, see _PsycopgHStore def process(value): if value is not None: return _parse_hstore(value) diff --git a/lib/sqlalchemy/dialects/postgresql/psycopg.py b/lib/sqlalchemy/dialects/postgresql/psycopg.py index 3c8a5e4c59..b880bc7f41 100644 --- a/lib/sqlalchemy/dialects/postgresql/psycopg.py +++ b/lib/sqlalchemy/dialects/postgresql/psycopg.py @@ -406,10 +406,12 @@ class PGDialect_psycopg(_PGDialect_common_psycopg): # register the adapter for connections made subsequent to # this one + assert self._psycopg_adapters_map register_hstore(info, self._psycopg_adapters_map) # register the adapter for this connection - register_hstore(info, connection.connection) + assert connection.connection + register_hstore(info, connection.connection.driver_connection) @classmethod def import_dbapi(cls):