问题描述
与一个团队一起在一个相当大/复杂的Django项目上工作,我们偶尔会看到运行服务器崩溃,并出现 ValueError:Embedded null byte
。我们重新启动runserver,这可以-几分钟或几天。我们无法检测到导致崩溃的模式(似乎是完全随机的)。幸运的是,它仅发生在本地开发中,而不是在我们的服务器上,但是我担心它会咬我们一路走。
Working on a fairly large/complex Django project with a team, we occasionally see runserver crash with ValueError: embedded null byte
. We restart runserver and it's fine - either for a few minutes or a few days. We can detect no pattern to what causes the crashes (seems totally random). Fortunately it's only happening in local development, not on our servers, but I'm worried that it will bite us down the road.
下面的堆栈跟踪并不指向我们代码中的任何位置-似乎来自Django或virtualenv本身。
The stack trace below does not point to any location in our code -- seems to come either from Django or the virtualenv itself.
在El Capitan上使用Django 1.9.8,Python 3.5.0。
Using Django 1.9.8, Python 3.5.0, on El Capitan.
我看不到任何调试方法。理论?
I can't see any way to debug this. Theories?
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/path/to/virtualenvs/ourproj/lib/python3.5/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
utility.execute()
File "/path/to/virtualenvs/ourproj/lib/python3.5/site-packages/django/core/management/__init__.py", line 345, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/path/to/virtualenvs/ourproj/lib/python3.5/site-packages/django/core/management/base.py", line 348, in run_from_argv
self.execute(*args, **cmd_options)
File "/path/to/virtualenvs/ourproj/lib/python3.5/site-packages/django/core/management/commands/runserver.py", line 54, in execute
super(Command, self).execute(*args, **options)
File "/path/to/virtualenvs/ourproj/lib/python3.5/site-packages/django/core/management/base.py", line 399, in execute
output = self.handle(*args, **options)
File "/path/to/virtualenvs/ourproj/lib/python3.5/site-packages/django/core/management/commands/runserver.py", line 93, in handle
self.run(**options)
File "/path/to/virtualenvs/ourproj/lib/python3.5/site-packages/django/core/management/commands/runserver.py", line 102, in run
autoreload.main(self.inner_run, None, options)
File "/path/to/virtualenvs/ourproj/lib/python3.5/site-packages/django/utils/autoreload.py", line 333, in main
reloader(wrapped_main_func, args, kwargs)
File "/path/to/virtualenvs/ourproj/lib/python3.5/site-packages/django/utils/autoreload.py", line 299, in python_reloader
reloader_thread()
File "/path/to/virtualenvs/ourproj/lib/python3.5/site-packages/django/utils/autoreload.py", line 275, in reloader_thread
change = fn()
File "/path/to/virtualenvs/ourproj/lib/python3.5/site-packages/django/utils/autoreload.py", line 204, in code_changed
for filename in gen_filenames():
File "/path/to/virtualenvs/ourproj/lib/python3.5/site-packages/django/utils/autoreload.py", line 114, in gen_filenames
basedirs = [os.path.abspath(basedir) for basedir in basedirs
File "/path/to/virtualenvs/ourproj/lib/python3.5/site-packages/django/utils/autoreload.py", line 115, in <listcomp>
if os.path.isdir(basedir)]
File "/path/to/virtualenvs/ourproj/bin/../lib/python3.5/genericpath.py", line 42, in isdir
st = os.stat(s)
ValueError: embedded null byte
推荐答案
-
AppConfig
对象之一的路径中有空字节
属性。 -
LOCALE_PATHS
中的一个具有空字节。 - 一个文件中的文件采用错误编码,因此Django将某些内容视为空字节(例如
AppConfig.path
)。 - 一个项目目录中的文件或目录名称中具有空字节(
\x00
)。 - 其他原因。
- One of the
AppConfig
objects has null byte in itspath
attribute. - One of the
LOCALE_PATHS
has null byte. - One of the files is in the "wrong" encoding, so Django treats something has null byte (e. g.
AppConfig.path
). - One of the files or directories in project directory has null byte (
\x00
) in its name. - Other reason.
与该问题有关。 os.path.isdir()
引发 ValueError:嵌入式空字节
,如果其参数为空字节,例如e。 G。 os.path.isdir('foo\x00bar')
。
autoreload.py
lines related to this issue. os.path.isdir()
raises ValueError: embedded null byte
if its argument has null byte, e. g. os.path.isdir('foo\x00bar')
.
您可以尝试编辑 autoreload.py
,暂时注释以下行:
You can try to edit autoreload.py
, comment out these lines temporarily:
basedirs = [os.path.abspath(basedir) for basedir in basedirs
if os.path.isdir(basedir)]
并添加以下内容:
temp_basedirs = []
for basedir in basedirs:
try:
if os.path.isdir(basedir):
temp_basedirs.append(os.path.abspath(basedir))
except ValueError:
print(basedir)
raise
basedirs = temp_basedirs
这篇关于神秘的“嵌入空字节”错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!