From 08d741927bea209e75c6dc28c212e84271e6e287 Mon Sep 17 00:00:00 2001 From: Nicholas-Xiong <2482929840@qq.com> Date: Sun, 31 May 2026 09:54:12 +0000 Subject: [PATCH] fix: decode bytes from conn.info.get_parameters() to avoid TypeError on macOS (Python 3.13+) --- pgcli/pgexecute.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/pgcli/pgexecute.py b/pgcli/pgexecute.py index fbb8e09d3..905d2cb9c 100644 --- a/pgcli/pgexecute.py +++ b/pgcli/pgexecute.py @@ -243,6 +243,16 @@ def connect( # user, etc. we connected to. Let's read it. # Note: moved this after setting autocommit because of #664. dsn_parameters = conn.info.get_parameters() + # On some platforms (e.g., macOS with Homebrew psycopg, Python 3.13+), + # get_parameters() can return bytes values instead of str. + # Decode them to str to avoid TypeError: replace() argument 2 must be str, not bytes + # in main.py get_prompt() and TypeError: cannot use a string pattern on a bytes-like object + # in pgcompleter.py escape_name(). See issues #1518. + if dsn_parameters: + dsn_parameters = { + k: v.decode() if isinstance(v, bytes) else v + for k, v in dsn_parameters.items() + } if dsn_parameters: self.dbname = dsn_parameters.get("dbname") @@ -481,7 +491,7 @@ def search_path(self): with self.conn.cursor() as cur: _logger.debug("Search path query. sql: %r", self.search_path_query) cur.execute(self.search_path_query) - return [x[0] for x in cur.fetchall()] + return [x[0].decode() if isinstance(x[0], bytes) else x[0] for x in cur.fetchall()] except psycopg.ProgrammingError: fallback = "SELECT * FROM current_schemas(true)" with self.conn.cursor() as cur: