From: Daniele Varrazzo Date: Tue, 24 Aug 2021 19:11:20 +0000 (+0200) Subject: Add "char" data type loading X-Git-Tag: 3.0.beta1~43^2~2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=7eb6baeeb5907b8f3373027eb3e5866c1be380df;p=thirdparty%2Fpsycopg.git Add "char" data type loading Not very used so won't implement it in C, but useful to read catalog data. --- diff --git a/psycopg/psycopg/types/string.py b/psycopg/psycopg/types/string.py index b99cf4c19..b3bac6b0c 100644 --- a/psycopg/psycopg/types/string.py +++ b/psycopg/psycopg/types/string.py @@ -191,15 +191,18 @@ def register_default_adapters(context: AdaptContext) -> None: # the role of unknown, so it can be cast automatically to other types). adapters.register_dumper(str, StrBinaryDumper) adapters.register_dumper(str, StrDumperUnknown) + adapters.register_loader(postgres.INVALID_OID, TextLoader) adapters.register_loader("bpchar", TextLoader) adapters.register_loader("name", TextLoader) adapters.register_loader("text", TextLoader) adapters.register_loader("varchar", TextLoader) + adapters.register_loader('"char"', TextLoader) adapters.register_loader("bpchar", TextBinaryLoader) adapters.register_loader("name", TextBinaryLoader) adapters.register_loader("text", TextBinaryLoader) adapters.register_loader("varchar", TextBinaryLoader) + adapters.register_loader('"char"', TextBinaryLoader) adapters.register_dumper(bytes, BytesDumper) adapters.register_dumper(bytearray, BytesDumper) @@ -207,6 +210,7 @@ def register_default_adapters(context: AdaptContext) -> None: adapters.register_dumper(bytes, BytesBinaryDumper) adapters.register_dumper(bytearray, BytesBinaryDumper) adapters.register_dumper(memoryview, BytesBinaryDumper) + adapters.register_loader("bytea", ByteaLoader) adapters.register_loader(postgres.INVALID_OID, ByteaBinaryLoader) adapters.register_loader("bytea", ByteaBinaryLoader) diff --git a/tests/types/test_string.py b/tests/types/test_string.py index 0fa7a6bd5..1633090d0 100644 --- a/tests/types/test_string.py +++ b/tests/types/test_string.py @@ -73,11 +73,17 @@ def test_quote_percent(conn): assert cur.fetchone()[0] is True -@pytest.mark.parametrize("typename", ["text", "varchar", "name", "bpchar"]) +@pytest.mark.parametrize( + "typename", ["text", "varchar", "name", "bpchar", '"char"'] +) @pytest.mark.parametrize("fmt_out", [pq.Format.TEXT, pq.Format.BINARY]) def test_load_1char(conn, typename, fmt_out): cur = conn.cursor(binary=fmt_out) for i in range(1, 256): + if typename == '"char"' and i > 127: + # for char > 128 the client receives only 194 or 195. + continue + cur.execute(f"select chr(%s)::{typename}", (i,)) res = cur.fetchone()[0] assert res == chr(i)