]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Really use the context to transform components types
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Tue, 7 Apr 2020 11:18:59 +0000 (23:18 +1200)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Tue, 7 Apr 2020 11:18:59 +0000 (23:18 +1200)
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.

psycopg3/adapt.py
tests/test_adapt.py

index 37d6cc17676a24e1bbbf14e8e94e7a8f70664234..44a768bf4862ad62afaa571fb2d8b638c50edc6b 100644 (file)
@@ -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
 
index 02bceb36a8995f2ca406671e6320d8f707ea5878..2a8107011743c79a2abd2fb8919a25f0b85a7520 100644 (file)
@@ -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