Skip to content

Commit b9c9c56

Browse files
committed
Defensively decode SQL_ASCII text columns in prompt, timezone, and
function metadata When the client encoding cannot decode a text value (most commonly SQL_ASCII), psycopg returns text columns as raw bytes. Decode those values defensively, using the same guard as the completion metadata fix for dbcli#1405: - get_socket_directory() and get_timezone() now decode their results, so the prompt no longer raises a TypeError on Unix socket connections and the timezone startup message no longer shows a b'...' value. - functions() now decodes every metadata row before yielding, so the background completion refresh no longer dies in parse_defaults with a TypeError. Closes dbcli#1484 and dbcli#1518. Related: dbcli#1405.
1 parent 9da9c8d commit b9c9c56

3 files changed

Lines changed: 123 additions & 3 deletions

File tree

‎changelog.rst‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@ Bug fixes:
1616
as the OS user. The database argument is now kept, like psql; only when no
1717
database is given at all does the listing connect to ``postgres``.
1818

19+
* Fix a prompt crash, garbled timezone output and a completion-refresh crash
20+
when the client encoding cannot decode text (e.g. SQL_ASCII), where psycopg
21+
returns text columns as raw bytes: the socket directory and timezone query
22+
results are now decoded defensively (so the prompt no longer raises a
23+
``TypeError`` on Unix socket connections and the timezone startup message no
24+
longer shows a ``b'...'``-prefixed value), and the function metadata rows
25+
are decoded before building completions (so the background completion
26+
refresh no longer dies in ``parse_defaults`` with a ``TypeError``). Same
27+
guard as the completion metadata fix for issue #1405; see upstream issues
28+
#1484 and #1518.
29+
1930
4.6.0 (2026-08-26)
2031
==================
2132

‎pgcli/pgexecute.py‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,23 @@ def register_typecasters(connection):
5555
connection.adapters.register_loader(forced_text_type, psycopg.types.string.TextLoader)
5656

5757

58+
def _decode_if_bytes(value):
59+
"""psycopg returns text columns as raw bytes when the client encoding
60+
cannot be decoded (e.g. SQL_ASCII); decode defensively so callers can
61+
treat the value as a regular str. See issues #1484 and #1518."""
62+
if isinstance(value, bytes):
63+
return value.decode("utf-8", "replace")
64+
return value
65+
66+
67+
def _decode_row(row):
68+
"""psycopg returns text columns as raw bytes when the client encoding
69+
cannot be decoded (e.g. SQL_ASCII); decode every scalar value and every
70+
array element so callers can treat the whole row as regular str values.
71+
See issues #1484 and #1518."""
72+
return [[_decode_if_bytes(item) for item in value] if isinstance(value, list) else _decode_if_bytes(value) for value in row]
73+
74+
5875
# pg3: I don't know what is this
5976
class ProtocolSafeCursor(psycopg.Cursor):
6077
"""This class wraps and suppresses Protocol Errors with pgbouncer database.
@@ -667,7 +684,7 @@ def get_socket_directory(self):
667684
_logger.debug("Socket directory Query. sql: %r", self.socket_directory_query)
668685
cur.execute(self.socket_directory_query)
669686
result = cur.fetchone()
670-
return result[0] if result else ""
687+
return _decode_if_bytes(result[0]) if result else ""
671688

672689
def foreignkeys(self):
673690
"""Yields ForeignKey named tuples"""
@@ -797,7 +814,7 @@ def functions(self):
797814
_logger.debug("Functions Query. sql: %r", query)
798815
cur.execute(query)
799816
for row in cur:
800-
yield FunctionMetadata(*row)
817+
yield FunctionMetadata(*_decode_row(row))
801818

802819
def datatypes(self):
803820
"""Yields tuples of (schema_name, type_name)"""
@@ -899,7 +916,8 @@ def get_timezone(self) -> str:
899916
query = psycopg.sql.SQL("show time zone")
900917
with self.conn.cursor() as cur:
901918
cur.execute(query)
902-
return cur.fetchone()[0]
919+
result = cur.fetchone()
920+
return _decode_if_bytes(result[0]) if result else ""
903921

904922
def set_timezone(self, timezone: str):
905923
query = psycopg.sql.SQL("set time zone {}").format(psycopg.sql.Identifier(timezone))

‎tests/test_pgexecute.py‎

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,3 +842,94 @@ def test_virtual_database(executor):
842842
with patch.object(executor, "conn", virtual_connection):
843843
result = run(executor, "select 1")
844844
assert "Command not supported" in result
845+
846+
847+
# When the client encoding is one psycopg cannot decode (e.g. SQL_ASCII),
848+
# text columns come back as raw bytes. See issues #1484 and #1518.
849+
850+
851+
@dbtest
852+
def test_get_socket_directory_decodes_sql_ascii_bytes(executor):
853+
with patch.object(executor.conn, "cursor") as mock_cursor:
854+
mock_cursor.return_value.__enter__.return_value.fetchone.return_value = (b"/var/run/postgresql",)
855+
assert executor.get_socket_directory() == "/var/run/postgresql"
856+
857+
858+
@dbtest
859+
def test_get_socket_directory_str_unchanged(executor):
860+
with patch.object(executor.conn, "cursor") as mock_cursor:
861+
mock_cursor.return_value.__enter__.return_value.fetchone.return_value = ("/var/run/postgresql",)
862+
assert executor.get_socket_directory() == "/var/run/postgresql"
863+
864+
865+
@dbtest
866+
def test_get_timezone_decodes_sql_ascii_bytes(executor):
867+
with patch.object(executor.conn, "cursor") as mock_cursor:
868+
mock_cursor.return_value.__enter__.return_value.fetchone.return_value = (b"America/Argentina/Buenos_Aires",)
869+
assert executor.get_timezone() == "America/Argentina/Buenos_Aires"
870+
871+
872+
@dbtest
873+
def test_get_timezone_str_unchanged(executor):
874+
with patch.object(executor.conn, "cursor") as mock_cursor:
875+
mock_cursor.return_value.__enter__.return_value.fetchone.return_value = ("UTC",)
876+
assert executor.get_timezone() == "UTC"
877+
878+
879+
@dbtest
880+
def test_functions_decodes_sql_ascii_bytes(executor):
881+
row = (
882+
b"public",
883+
b"func_with_default",
884+
[b"x"],
885+
[b"integer"],
886+
[b"i"],
887+
b"integer",
888+
False,
889+
False,
890+
False,
891+
False,
892+
b"'10'::integer, NULL",
893+
)
894+
with patch.object(executor.conn, "cursor") as mock_cursor:
895+
mock_cursor.return_value.__enter__.return_value.__iter__.return_value = iter([row])
896+
funcs = list(executor.functions())
897+
898+
assert len(funcs) == 1
899+
func = funcs[0]
900+
assert func.schema_name == "public"
901+
assert func.func_name == "func_with_default"
902+
assert func.arg_names == ("x",)
903+
assert func.arg_types == ("integer",)
904+
assert func.arg_modes == ("i",)
905+
assert func.return_type == "integer"
906+
assert func.is_public is True
907+
assert func.arg_defaults == ("'10'::integer", "NULL")
908+
909+
910+
@dbtest
911+
def test_functions_str_unchanged(executor):
912+
row = (
913+
"public",
914+
"func_plain",
915+
["x"],
916+
["integer"],
917+
["i"],
918+
"integer",
919+
False,
920+
False,
921+
False,
922+
False,
923+
None,
924+
)
925+
with patch.object(executor.conn, "cursor") as mock_cursor:
926+
mock_cursor.return_value.__enter__.return_value.__iter__.return_value = iter([row])
927+
funcs = list(executor.functions())
928+
929+
assert len(funcs) == 1
930+
func = funcs[0]
931+
assert func.schema_name == "public"
932+
assert func.func_name == "func_plain"
933+
assert func.arg_names == ("x",)
934+
assert func.return_type == "integer"
935+
assert func.arg_defaults == ()

0 commit comments

Comments
 (0)