From: Daniele Varrazzo Date: Fri, 25 Dec 2020 14:05:59 +0000 (+0100) Subject: Dropped "unknown loader", use the text loader for everything X-Git-Tag: 3.0.dev0~249 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d0aebb60fea0355b690b870f629ee39b015744bd;p=thirdparty%2Fpsycopg.git Dropped "unknown loader", use the text loader for everything Register the C implementation on all the types where it is useful. --- diff --git a/psycopg3/psycopg3/types/text.py b/psycopg3/psycopg3/types/text.py index dfc3667e5..1e3935231 100644 --- a/psycopg3/psycopg3/types/text.py +++ b/psycopg3/psycopg3/types/text.py @@ -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) diff --git a/psycopg3_c/psycopg3_c/types/text.pyx b/psycopg3_c/psycopg3_c/types/text.pyx index 0d7f770c6..1c48d80ff 100644 --- a/psycopg3_c/psycopg3_c/types/text.pyx +++ b/psycopg3_c/psycopg3_c/types/text.pyx @@ -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) diff --git a/tests/types/test_text.py b/tests/types/test_text.py index fd35fb889..095d0d24c 100644 --- a/tests/types/test_text.py +++ b/tests/types/test_text.py @@ -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"])