Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

startup crashes on `TypeError: replace() argument 2 must be str, not bytes`, followed by `TypeError: cannot use a string pattern on a bytes-like object`

未关闭
#1,518 4 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 2 天内回复

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
68/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
活跃
技术栈
postgresql, python
领域
cli, databases

调研方向

在 Python 3.13 上复现 pgcli 4.3.0 的启动失败,然后阅读 pgcli/main.py 中 get_prompt 第 1323-1324 行附近的代码,以及 pgcli/completion_refresher.py 中 refresh_schemata 附近的代码。跟踪传入 pgcompleter.py 的 escaped_names 和 escape_name 方法的值;当启动过程在没有报告的 string/bytes TypeErrors 的情况下到达 prompt 时,即表示完成。

由索引模型根据 Issue 内容生成。

描述

bug

Installed pgcli 4.3.0 using pip install pgcli in a Python 3.13 venv (MacOS 15.x, recent Sequoia).

pgcli crashes on startup due to a string-handling error (or byte string-handling error). Superficial patches lead to deeper problems during startup.

First I tried to start pgcli with the command pgcli my_database_name

Got a stacktrace ending with this error, related to handling string versus bytes:

  File "/path/to/my/venv/lib/python3.13/site-packages/pgcli/main.py", line 1051, in get_message
    prompt = self.get_prompt(prompt_format)
  File "/path/to/my/venv/lib/python3.13/site-packages/pgcli/main.py", line 1323, in get_prompt
    string = string.replace("\\H", self.pgexecute.host or "(none)")
TypeError: replace() argument 2 must be str, not bytes

I modified line 1323 like this, and this initial error went away...

        string = string.replace("\\H", str(self.pgexecute.host) or "(none)")  # HACK added str() 20250802

...however then I got the same error from the subsequent line 1324:

  File "/path/to/my/venv/lib/python3.13/site-packages/pgcli/main.py", line 1324, in get_prompt
    string = string.replace("\\h", self.pgexecute.short_host or "(none)")
TypeError: replace() argument 2 must be str, not bytes

So I modified line 1324 the same way to bypass the error:

        string = string.replace("\\h", str(self.pgexecute.short_host) or "(none)")  # HACK added str() 20250802

These two hacks permitted pgcli to get farther in start up, however then I received another TypeError, seemingly related to the same issues. At this point I gave up debugging it. It should be an easy fix for someone who knows the pgcli codebase.

$ pgcli my_database_name

Using local time zone MyCountry/MyCity (server uses b'MyCountry/MyCity')
Use `set time zone <TZ>` to override, or set `use_local_timezone = False` in the config

Exception in thread completion_refresh:
Traceback (most recent call last):
  File "/opt/homebrew/Cellar/[email protected]/3.13.5/Frameworks/Python.framework/Versions/3.13/lib/python3.13/threading.py", line 1043, in _bootstrap_inner
    self.run()
    ~~~~~~~~^^
  File "/opt/homebrew/Cellar/[email protected]/3.13.5/Frameworks/Python.framework/Versions/3.13/lib/python3.13/threading.py", line 994, in run
    self._target(*self._args, **self._kwargs)
    ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/path/to/my/venv/lib/python3.13/site-packages/pgcli/completion_refresher.py", line 67, in _bg_refresh
    refresher(completer, executor)
    ~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^
Server: PostgreSQL 17.5 (Homebrew)
  File "/path/to/my/venv/lib/python3.13/site-packages/pgcli/completion_refresher.py", line 108, in refresh_schemata
    completer.set_search_path(executor.search_path())
    ~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^
  File "/path/to/my/venv/lib/python3.13/site-packages/pgcli/pgcompleter.py", line 335, in set_search_path
    self.search_path = self.escaped_names(search_path)
                       ~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^

Version: 4.3.0
Home: http://pgcli.com

  File "/path/to/my/venv/lib/python3.13/site-packages/pgcli/pgcompleter.py", line 183, in escaped_names
    return [self.escape_name(name) for name in names]
            ~~~~~~~~~~~~~~~~^^^^^^
  File "/path/to/my/venv/lib/python3.13/site-packages/pgcli/pgcompleter.py", line 164, in escape_name
    (not self.name_pattern.match(name))
         ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^

TypeError: cannot use a string pattern on a bytes-like object

Traceback (most recent call last):
  File "/path/to/my/venv/bin/pgcli", line 8, in <module>
    sys.exit(cli())
             ~~~^^
  File "/path/to/my/venv/lib/python3.13/site-packages/click/core.py", line 1442, in __call__
    return self.main(*args, **kwargs)
           ~~~~~~~~~^^^^^^^^^^^^^^^^^
  File "/path/to/my/venv/lib/python3.13/site-packages/click/core.py", line 1363, in main
    rv = self.invoke(ctx)
  File "/path/to/my/venv/lib/python3.13/site-packages/click/core.py", line 1226, in invoke
    return ctx.invoke(self.callback, **ctx.params)
           ~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/path/to/my/venv/lib/python3.13/site-packages/click/core.py", line 794, in invoke
    return callback(*args, **kwargs)
  File "/path/to/my/venv/lib/python3.13/site-packages/pgcli/main.py", line 1720, in cli
    pgcli.run_cli()
    ~~~~~~~~~~~~~^^
  File "/path/to/my/venv/lib/python3.13/site-packages/pgcli/main.py", line 979, in run_cli
    text = self.prompt_app.prompt()
  File "/path/to/my/venv/lib/python3.13/site-packages/prompt_toolkit/shortcuts/prompt.py", line 1035, in prompt
    return self.app.run(
           ~~~~~~~~~~~~^
        set_exception_handler=set_exception_handler,
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ...<2 lines>...
        inputhook=inputhook,
        ^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/path/to/my/venv/lib/python3.13/site-packages/prompt_toolkit/application/application.py", line 1002, in run
    return asyncio.run(coro)
           ~~~~~~~~~~~^^^^^^
  File "/opt/homebrew/Cellar/[email protected]/3.13.5/Frameworks/Python.framework/Versions/3.13/lib/python3.13/asyncio/runners.py", line 195, in run
    return runner.run(main)
           ~~~~~~~~~~^^^^^^
  File "/opt/homebrew/Cellar/[email protected]/3.13.5/Frameworks/Python.framework/Versions/3.13/lib/python3.13/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
  File "/opt/homebrew/Cellar/[email protected]/3.13.5/Frameworks/Python.framework/Versions/3.13/lib/python3.13/asyncio/base_events.py", line 725, in run_until_complete
    return future.result()
           ~~~~~~~~~~~~~^^
  File "/path/to/my/venv/lib/python3.13/site-packages/prompt_toolkit/application/application.py", line 886, in run_async
    return await _run_async(f)
           ^^^^^^^^^^^^^^^^^^^
  File "/path/to/my/venv/lib/python3.13/site-packages/prompt_toolkit/application/application.py", line 739, in _run_async
    self._redraw()
    ~~~~~~~~~~~~^^
  File "/path/to/my/venv/lib/python3.13/site-packages/prompt_toolkit/application/application.py", line 543, in _redraw
    self.context.copy().run(run_in_context)
    ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^
  File "/path/to/my/venv/lib/python3.13/site-packages/prompt_toolkit/application/application.py", line 526, in run_in_context
    self.renderer.render(self, self.layout)
    ~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^
  File "/path/to/my/venv/lib/python3.13/site-packages/prompt_toolkit/renderer.py", line 647, in render
    layout.container.preferred_height(size.columns, size.rows).preferred,
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/path/to/my/venv/lib/python3.13/site-packages/prompt_toolkit/layout/containers.py", line 319, in preferred_height
    c.preferred_height(width, max_available_height) for c in self._all_children
    ~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/path/to/my/venv/lib/python3.13/site-packages/prompt_toolkit/layout/containers.py", line 786, in preferred_height
    return self.content.preferred_height(width, max_available_height)
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/path/to/my/venv/lib/python3.13/site-packages/prompt_toolkit/layout/containers.py", line 319, in preferred_height
    c.preferred_height(width, max_available_height) for c in self._all_children
    ~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/path/to/my/venv/lib/python3.13/site-packages/prompt_toolkit/layout/containers.py", line 2623, in preferred_height
    if self.filter():
       ~~~~~~~~~~~^^
  File "/path/to/my/venv/lib/python3.13/site-packages/prompt_toolkit/filters/base.py", line 253, in __call__
    return self.func()
           ~~~~~~~~~^^
  File "/path/to/my/venv/lib/python3.13/site-packages/prompt_toolkit/shortcuts/prompt.py", line 161, in has_before_fragments
    for fragment, char, *_ in get_prompt_text():
                              ~~~~~~~~~~~~~~~^^
  File "/path/to/my/venv/lib/python3.13/site-packages/prompt_toolkit/shortcuts/prompt.py", line 1280, in _get_prompt
    return to_formatted_text(self.message, style="class:prompt")
  File "/path/to/my/venv/lib/python3.13/site-packages/prompt_toolkit/formatted_text/base.py", line 82, in to_formatted_text
    return to_formatted_text(value(), style=style)
                             ~~~~~^^
  File "/path/to/my/venv/lib/python3.13/site-packages/pgcli/main.py", line 1051, in get_message
    prompt = self.get_prompt(prompt_format)
  File "/path/to/my/venv/lib/python3.13/site-packages/pgcli/main.py", line 1324, in get_prompt
    string = string.replace("\\h", self.pgexecute.short_host or "(none)")

TypeError: replace() argument 2 must be str, not bytes
主要语言
Python
星标
13.4k
派生
613
平均合并
15 小时 57 分钟
30 天内合并 PR
4

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

dbcli/pgcli 的其他 Issue

查看 dbcli/pgcli 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。