Decode text results defensively when the client encoding cannot decode them (SQL_ASCII) - #1628
Open
DiegoDAF wants to merge 1 commit into
Open
Decode text results defensively when the client encoding cannot decode them (SQL_ASCII)#1628DiegoDAF wants to merge 1 commit into
DiegoDAF wants to merge 1 commit into
Conversation
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.
DiegoDAF
added a commit
to DiegoDAF/pgcli.daf
that referenced
this pull request
Sep 1, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1518. Fixes #1484.
When the client encoding cannot decode a text value (most commonly
SQL_ASCII), psycopg returns text columns as rawbytesinstead ofstr. Three code paths were passing those values through unguarded, producing the crashes reported in #1518 and #1484:TypeError: expected str, not bytes.show time zoneresult is printed in the startup message, showing ab'...'-prefixed value.parse_defaults()killed the refresh thread withTypeError: can only concatenate str (not "int") to str.The fix decodes those values defensively when they come back as bytes, with the same guard the completion metadata fix for #1405 already uses (
decode("utf-8", "replace")): a_decode_if_bytes()helper, plus a_decode_row()variant for the function metadata rows, which contain array columns.Deliberately narrow in scope:
function_definition(),search_path(),schemata()anddatabases()also return text and are still unguarded, but they are not involved in the reported crashes. Guarding them can be a follow-up if anyone hits them.Reproduced against a real
SQL_ASCIIcluster (3396 catalog functions; all metadata fields come back as plainstrafter the fix). 6 unit tests: 3 fail without the fix, 3 pin the already-workingstrpath so it stays unchanged.