【问题标题】:google colab UnicodeDecodeError: 'utf-8' codec can't decode byte 0xfa in position 21: invalid start bytegoogle colab UnicodeDecodeError: 'utf-8' codec can't decode byte 0xfa in position 21: invalid start byte
【发布时间】:2020-12-10 16:55:53
【问题描述】:

我正在使用 Google Colab 运行 git 命令(我在 colab 中安装了一个包含 git repo 的 google 驱动器)。所有命令都正常工作,但突然有些命令停止工作。

这些命令在我的 Colab 中仍然有效:

!git branch
!git stash pop
!git log -1

但是这些命令会在我的 Colab 中产生以前没有发生过的错误:

!git status
!git checkout master
!git pull origin master

输出

!git stash pop 
!git status
No stash entries found.
---------------------------------------------------------------------------
UnicodeDecodeError                        Traceback (most recent call last)
<ipython-input-27-7155109ce28e> in <module>()
     12 get_ipython().system('git stash pop')
     13 # !git branch
---> 14 get_ipython().system('git status')
     15 # !git pull origin master
     16 # if os.path.isdir(WORKING_DIR):

5 frames
/usr/local/lib/python3.6/dist-packages/google/colab/_shell.py in system(self, *args, **kwargs)
    100       kwargs.update({'also_return_output': True})
    101 
--> 102     output = _system_commands._system_compat(self, *args, **kwargs)  # pylint:disable=protected-access
    103 
    104     if pip_warn:

/usr/local/lib/python3.6/dist-packages/google/colab/_system_commands.py in _system_compat(shell, cmd, also_return_output)
    438   # stack.
    439   result = _run_command(
--> 440       shell.var_expand(cmd, depth=2), clear_streamed_output=False)
    441   shell.user_ns['_exit_code'] = result.returncode
    442   if -result.returncode in _INTERRUPTED_SIGNALS:

/usr/local/lib/python3.6/dist-packages/google/colab/_system_commands.py in _run_command(cmd, clear_streamed_output)
    193       os.close(child_pty)
    194 
--> 195       return _monitor_process(parent_pty, epoll, p, cmd, update_stdin_widget)
    196   finally:
    197     epoll.close()

/usr/local/lib/python3.6/dist-packages/google/colab/_system_commands.py in _monitor_process(parent_pty, epoll, p, cmd, update_stdin_widget)
    220   while True:
    221     try:
--> 222       result = _poll_process(parent_pty, epoll, p, cmd, decoder, state)
    223       if result is not None:
    224         return result

/usr/local/lib/python3.6/dist-packages/google/colab/_system_commands.py in _poll_process(parent_pty, epoll, p, cmd, decoder, state)
    273       output_available = True
    274       raw_contents = os.read(parent_pty, _PTY_READ_MAX_BYTES_FOR_TEST)
--> 275       decoded_contents = decoder.decode(raw_contents)
    276 
    277       sys.stdout.write(decoded_contents)

/usr/lib/python3.6/codecs.py in decode(self, input, final)
    319         # decode input (taking the buffer into account)
    320         data = self.buffer + input
--> 321         (result, consumed) = self._buffer_decode(data, self.errors, final)
    322         # keep undecoded input until the next call
    323         self.buffer = data[consumed:]

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xfa in position 21: invalid start byte

如您所见,!git stash pop 有效,但 !git status 无效。请注意,直到最近,我的 Colab 中的所有 git 命令都运行良好。 Colab 中似乎发生了一些事情,导致它拒绝了一些 git 命令。

是的,我已经尝试创建一个新的 Colab 并在那里重写有问题的 git 命令,但错误仍然存​​在。我还尝试删除每个字符并从头开始重写它们(以防有隐藏字符导致问题)。

在我的本地机器上,这些命令可以正常工作,并且不会打印任何奇怪的字符:

$ git status
On branch tf2
nothing to commit, working tree clean

有什么想法吗?

【问题讨论】:

    标签: python git unicode google-colaboratory python-unicode


    【解决方案1】:

    首先,阅读https://www.joelonsoftware.com/2003/10/08/the-absolute-minimum-every-software-developer-absolutely-positively-must-know-about-unicode-and-character-sets-no-excuses/,以便此答案的其余部分有意义。

    现在,这里的问题是您告诉不同的子系统(在本例中为 Python 和 Git)使用不同的编码。具体来说,您已经告诉 Python 期待 UTF-8 编码的数据。您的 Git 可能根本没有使用任何特定的编码,而只是通过复制原始字节。通常,您的git status 输出将使用八进制转义编码(参见例如How to make Git properly display UTF-8 encoded pathnames in the console window? 和Git gets confused with ä in file name)。其他命令不进行任何特殊编码(参见,例如,Make git diff show UTF8 encoded characters properly)并且可以告诉git status 如何表现。 --porcelain=v2 和 -z 选项在 git status 中特别有用,但您必须随后重写自己的 Python 代码以获取字节序列。

    您可能需要弄清楚底层文件系统中实际使用的编码是什么。如果您不想处理此类问题,请确保所有文件名都使用简单的 ASCII 字符:例如,没有名为 schön 的文件。

    【讨论】:

    • 感谢您的详细回复。请记住,我不是在本地机器上运行它,而是在 Google Colab 上运行它,我对 Python 子系统和预安装 git 系统的位置几乎没有控制权。所以我不太确定你的答案是如何相关的。另外,为什么有些 git 命令会起作用而其他命令不起作用?
    • 如果你没有控制权,你就是 SOL。如果您有控制权,请使用 Python 2(这样所有编码和解码的东西都消失了)或更改代码。您有一些现有文件,其名称中包含重音字符或非 ASCII 字符(Windows CP1252 中的0xfa 尤其代表ú,尽管我不知道您是否在这里使用 CP1252)。
    • 有效的命令是那些不打印这个字符的命令。那些不起作用的命令就是那些。
    • 我尝试在我的本地机器上运行这些命令,但它们没有打印任何可能有问题的字符(请参阅我更新的问题)。此外,我能够将运行时更改为 Python 2,但仍然出现相同的错误。
    • 很好奇。您的回溯现在显示 python2 路径(而不是 /usr/local/lib/python3.6)?
    【解决方案2】:

    我尝试使用 GitPython 操作 repo,但得到了不同的错误:

    Error: bad index – Fatal: index file corrupt”
    

    这表明问题出在损坏的 git 索引文件上。我通过执行以下操作在我的 Colab 中解决了它:

    rm -f .git/index
    git reset
    

    谢谢to this answer。

    【讨论】:

      猜你喜欢
      • 2020-12-26
      • 2018-10-15
      • 2021-11-24
      • 2022-09-26
      • 2020-03-12
      • 2020-05-08
      • 2021-01-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多