-
Notifications
You must be signed in to change notification settings - Fork 586
fix: handle bytes/string mismatch in replace at startup #1577
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1301,21 +1301,24 @@ def get_completions(self, text, cursor_positition): | |
| return self.completer.get_completions(Document(text=text, cursor_position=cursor_positition), None) | ||
|
|
||
| def get_prompt(self, string): | ||
| # should be before replacing \\d | ||
| string = string.replace("\\dsn_alias", self.dsn_alias or "") | ||
| string = string.replace("\\t", self.now.strftime("%x %X")) | ||
| string = string.replace("\\u", self.pgexecute.user or "(none)") | ||
| string = string.replace("\\H", self.pgexecute.host or "(none)") | ||
| string = string.replace("\\h", self.pgexecute.short_host or "(none)") | ||
| string = string.replace("\\d", self.pgexecute.dbname or "(none)") | ||
| # should be before replacing \d | ||
| def _to_str(val): | ||
| return val.decode("utf-8") if isinstance(val, bytes) else val or "" | ||
|
|
||
| string = string.replace("\dsn_alias", self.dsn_alias or "") | ||
| string = string.replace("\t", self.now.strftime("%x %X")) | ||
| string = string.replace("\u", _to_str(self.pgexecute.user) or "(none)") | ||
| string = string.replace("\H", _to_str(self.pgexecute.host) or "(none)") | ||
| string = string.replace("\h", _to_str(self.pgexecute.short_host) or "(none)") | ||
| string = string.replace("\d", _to_str(self.pgexecute.dbname) or "(none)") | ||
|
Comment on lines
+1308
to
+1313
|
||
| string = string.replace( | ||
| "\\p", | ||
| "\p", | ||
| str(self.pgexecute.port) if self.pgexecute.port is not None else "5432", | ||
| ) | ||
| string = string.replace("\\i", str(self.pgexecute.pid) or "(none)") | ||
| string = string.replace("\\#", "#" if self.pgexecute.superuser else ">") | ||
| string = string.replace("\\n", "\n") | ||
| string = string.replace("\\T", self.pgexecute.transaction_indicator) | ||
| string = string.replace("\i", str(self.pgexecute.pid) or "(none)") | ||
| string = string.replace("\#", "#" if self.pgexecute.superuser else ">") | ||
| string = string.replace("\n", "\n") | ||
| string = string.replace("\T", _to_str(self.pgexecute.transaction_indicator)) | ||
|
Comment on lines
1314
to
+1321
|
||
| return string | ||
|
|
||
| def get_last_query(self): | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -141,6 +141,8 @@ def __init__(self, smart_completion=True, pgspecial=None, settings=None): | |||||||||||||||||||||||||
| self.all_completions = set(self.keywords + self.functions) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| def escape_name(self, name): | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
| def escape_name(self, name): | |
| def escape_name(self, name): | |
| """Quote an identifier when needed. | |
| Regression tests for bytes input: | |
| >>> completer = PGCompleter() | |
| >>> completer.escape_name(b"my_schema") | |
| 'my_schema' | |
| >>> completer.escape_name(b"needs space") | |
| '"needs space"' | |
| """ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New bytes->str handling in
get_prompt()is important for startup stability, but there are existing prompt-format tests that only coverstrvalues. Please add a regression test wherepgexecute.user/host/dbname(and/ortransaction_indicator) arebytesto ensure the TypeError is fixed and stays fixed (e.g.,cli.get_prompt("\\u@\\h")works with byte inputs).