代码之家  ›  专栏  ›  技术社区  ›  ideasman42

为什么Python3在Windows上使用“代理进程”文件系统错误处理程序?

  •  0
  • ideasman42  · 技术社区  · 5 年前

    对于Python3(3.8和3.6之前的版本), surrogatepass 是默认的错误处理程序。

    这个可以 cause problems

    为什么windows使用 而不是 surrogateescape 与其他平台(Linux、macOS)一样,它可以处理这些字节。如:

    >>> import sys
    
    >>> sys.getfilesystemencoding(), sys.getfilesystemencodeerrors()
    ('utf-8', 'surrogateescape')
    >>>
    >>> # This raises an error:
    >>>
    >>> b'C:\\Users\\me\\OneDrive\\\xe0\xcd\xa1\xca\xd2\xc3\\my.txt'.decode('utf-8', errors="surrogatepass")
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe0 in position 24: invalid continuation byte
    
    >>> # Compared to:
    
    >>> b'C:\\Users\\me\\OneDrive\\\xe0\xcd\xa1\xca\xd2\xc3\\my.txt'.decode('utf-8', errors="surrogateescape")
    'C:\\Users\\me\\OneDrive\\\udce0Í¡\udcca\udcd2\udcc3\\my.txt'
    

    注意,我猜想这可能是必要的,因为底层NTFS文件系统使用UTF-16而不是以null结尾的字节,需要对Linux/macOS上不存在的Python文件系统编码进行一些限制。

    0 回复  |  直到 5 年前