使用 pdb 时出现 Django UnicodeDecodeError

发布于 2024-09-19 19:20:16 字数 471 浏览 5 评论 0原文

我注意到每次我

import pdb; pdb.set_trace()

在我的西班牙语 Django 项目中放置一个:时,如果我在字符串中包含特定的 Unicode 字符,例如:

Gracias por tu colaboración

我会在 Django 调试窗口中收到带有“序号不在范围(128)”的 UnicodeDecodeError 。问题是我无法轻松调试我的应用程序。另一方面,如果我使用 ipdb,我会得到类似这样的信息:

ERROR - failed to write data to stream: <open file '<stdout>', mode 'w' at 0x7f3d43e34140>

我已经在 google 上搜索找到了解决方案,但看起来我是唯一遇到此问题的人:)

I've notice every time I put an:

import pdb; pdb.set_trace()

in My Spanish Django project, if I have a specific Unicode character in a string like:

Gracias por tu colaboración

I get a UnicodeDecodeError with an 'ordinal not in range(128)' in a Django Debug window. The problem is that I can not debug my application easily. On the other hand If I use ipdb I get things like:

ERROR - failed to write data to stream: <open file '<stdout>', mode 'w' at 0x7f3d43e34140>

I've googled to find a solution, but it looks like I'm the only one having this issue :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

顾挽 2024-09-26 19:20:16

我发现另一个程序员已将其添加到视图文件的开头,这会破坏 pdb 和 ipdb 行为,正如我所描述的:

import sys, codecs
sys.stdout = codecs.getwriter('utf-8')(sys.stdout)

删除或注释这些行修复了问题,谢谢。

I found out that another programmer had added this to the beginning of the views file, this breaks pdb and ipdb behaviour, as I described:

import sys, codecs
sys.stdout = codecs.getwriter('utf-8')(sys.stdout)

Removing or commenting these lines fixed the issue, thanks.

苏璃陌 2024-09-26 19:20:16

通常,如果文件中包含非 ASCII 字符(可能在注释或字符串中),则会出现该错误。
如果你尝试在没有 ipdb/pdb 的情况下打开它,Django 将显示回溯

Usually, that error shows up if you have non-ascii characters in your file (mabe in a comment or a string).
If you try to open it without ipdb/pdb Django will display a traceback

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文