]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Dropped "unknown loader", use the text loader for everything
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Fri, 25 Dec 2020 14:05:59 +0000 (15:05 +0100)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Fri, 25 Dec 2020 14:05:59 +0000 (15:05 +0100)
Register the C implementation on all the types where it is useful.

psycopg3/psycopg3/types/text.py
psycopg3_c/psycopg3_c/types/text.pyx
tests/types/test_text.py

index dfc3667e5bd40d841d530c2624cea9995ae222de..1e393523150dd3f57ccc1a8059e40cd33b299758 100644 (file)
@@ -48,11 +48,15 @@ class StringDumper(_StringDumper):
             return obj.encode(self._encoding)
 
 
+@Loader.text(INVALID_OID)
+@Loader.text(builtins["bpchar"].oid)
+@Loader.text(builtins["name"].oid)
 @Loader.text(builtins["text"].oid)
-@Loader.binary(builtins["text"].oid)
 @Loader.text(builtins["varchar"].oid)
+@Loader.binary(builtins["bpchar"].oid)
+@Loader.binary(builtins["name"].oid)
+@Loader.binary(builtins["text"].oid)
 @Loader.binary(builtins["varchar"].oid)
-@Loader.text(INVALID_OID)
 class TextLoader(Loader):
 
     _encoding = "utf-8"
@@ -72,24 +76,6 @@ class TextLoader(Loader):
             return data
 
 
-@Loader.text(builtins["name"].oid)
-@Loader.binary(builtins["name"].oid)
-@Loader.text(builtins["bpchar"].oid)
-@Loader.binary(builtins["bpchar"].oid)
-class UnknownLoader(Loader):
-
-    _encoding = "utf-8"
-
-    def __init__(self, oid: int, context: AdaptContext):
-        super().__init__(oid, context)
-        conn = self.connection
-        if conn:
-            self._encoding = conn.client_encoding
-
-    def load(self, data: bytes) -> str:
-        return data.decode(self._encoding)
-
-
 @Dumper.text(bytes)
 @Dumper.text(bytearray)
 @Dumper.text(memoryview)
index 0d7f770c64c63dcb56df66cd1cb80f6c2c8b09a2..1c48d80ff1b15f83d7285a33582809b18669be8d 100644 (file)
@@ -142,6 +142,10 @@ cdef void register_text_c_adapters():
     StringBinaryDumper.register(str, format=Format.BINARY)
 
     TextLoader.register(oids.INVALID_OID)
+    TextLoader.register(oids.BPCHAR_OID)
+    TextLoader.register(oids.BPCHAR_OID, format=Format.BINARY)
+    TextLoader.register(oids.NAME_OID)
+    TextLoader.register(oids.NAME_OID, format=Format.BINARY)
     TextLoader.register(oids.TEXT_OID)
     TextLoader.register(oids.TEXT_OID, format=Format.BINARY)
     TextLoader.register(oids.VARCHAR_OID)
@@ -156,3 +160,4 @@ cdef void register_text_c_adapters():
 
     ByteaLoader.register(oids.BYTEA_OID)
     ByteaBinaryLoader.register(oids.BYTEA_OID, format=Format.BINARY)
+    ByteaBinaryLoader.register(oids.INVALID_OID, format=Format.BINARY)
index fd35fb889d53b760fb1c514a54f894c9f88c3caf..095d0d24ced74aa3e8eec3e512c6e6dabcc3a265 100644 (file)
@@ -120,7 +120,7 @@ def test_load_badenc(conn, typename, fmt_out):
 
 
 @pytest.mark.parametrize("fmt_out", [Format.TEXT, Format.BINARY])
-@pytest.mark.parametrize("typename", ["text", "varchar"])
+@pytest.mark.parametrize("typename", ["text", "varchar", "name", "bpchar"])
 def test_load_ascii(conn, typename, fmt_out):
     cur = conn.cursor(format=fmt_out)
 
@@ -131,16 +131,6 @@ def test_load_ascii(conn, typename, fmt_out):
     assert res == eur.encode("utf8")
 
 
-@pytest.mark.parametrize("fmt_out", [Format.TEXT, Format.BINARY])
-@pytest.mark.parametrize("typename", ["name", "bpchar"])
-def test_load_ascii_encanyway(conn, typename, fmt_out):
-    cur = conn.cursor(format=fmt_out)
-
-    conn.client_encoding = "ascii"
-    (res,) = cur.execute(f"select 'aa'::{typename}").fetchone()
-    assert res == "aa"
-
-
 @pytest.mark.parametrize("fmt_in", [Format.TEXT, Format.BINARY])
 @pytest.mark.parametrize("fmt_out", [Format.TEXT, Format.BINARY])
 @pytest.mark.parametrize("typename", ["text", "varchar", "name", "bpchar"])