From: Daniele Varrazzo Date: Tue, 7 Apr 2020 11:18:59 +0000 (+1200) Subject: Really use the context to transform components types X-Git-Tag: 3.0.dev0~596 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=033105c4c487a3445250eaf3b202260f4b6df80f;p=thirdparty%2Fpsycopg.git Really use the context to transform components types It works for arrays and records, except for text arrays because the decorator registers the TypeCaster directly, not the oid. This must change, we need a different decorator/registration. --- diff --git a/psycopg3/adapt.py b/psycopg3/adapt.py index 37d6cc176..44a768bf4 100644 --- a/psycopg3/adapt.py +++ b/psycopg3/adapt.py @@ -260,7 +260,7 @@ class Transformer: adapter = self.lookup_adapter(src, format) func: AdapterFunc if isinstance(adapter, type): - func = adapter(src, self.connection).adapt + func = adapter(src, self).adapt else: func = adapter @@ -308,7 +308,7 @@ class Transformer: caster = self.lookup_caster(oid, format) func: TypeCasterFunc if isinstance(caster, type): - func = caster(oid, self.connection).cast + func = caster(oid, self).cast else: func = caster diff --git a/tests/test_adapt.py b/tests/test_adapt.py index 02bceb36a..2a8107011 100644 --- a/tests/test_adapt.py +++ b/tests/test_adapt.py @@ -102,3 +102,19 @@ def test_cast_cursor_ctx(conn): cur.binary = True r = cur.execute("select 'hello'::text").fetchone() assert r == ("hellob",) + + +@pytest.mark.xfail +@pytest.mark.parametrize( + "sql, obj", + [("'{hello}'::text[]", ["helloc"]), ("row('hello'::text)", ("helloc",))], +) +@pytest.mark.parametrize("fmt_out", [Format.TEXT, Format.BINARY]) +def test_cast_cursor_ctx_nested(conn, sql, obj, fmt_out): + cur = conn.cursor(binary=fmt_out == Format.BINARY) + TypeCaster.register( + TEXT_OID, lambda b: b.decode("ascii") + "c", cur, format=fmt_out + ) + cur.execute(f"select {sql}") + res = cur.fetchone()[0] + assert res == obj