test_api.py 文件源码

python
阅读 26 收藏 0 点赞 0 评论 0

项目:senf 作者: quodlibet 项目源码 文件源码
def test_python_handling_broken_utf16():
    # Create a file with an invalid utf-16 name.
    # Mainly to see how Python handles it

    tmp = mkdtemp()
    try:
        path = os.path.join(tmp, "foo")
        with open(path, "wb") as h:
            h.write(b"content")
        assert "foo" in os.listdir(tmp)

        if os.name == "nt":
            faulty = (path.encode("utf-16-le") + b"=\xd8\x01\xde" +
                      b"=\xd8-\x00\x01\xde")
            buf = ctypes.create_string_buffer(faulty + b"\x00\x00")

            if winapi.MoveFileW(path, ctypes.cast(buf, ctypes.c_wchar_p)) == 0:
                raise ctypes.WinError()
            assert "foo" not in os.listdir(tmp)

            newpath = os.path.join(tmp, os.listdir(tmp)[0])
            if not is_wine:  # this is broken on wine..
                assert newpath.encode("utf-16-le", _surrogatepass) == faulty

            with open(newpath, "rb") as h:
                assert h.read() == b"content"
    finally:
        shutil.rmtree(tmp)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号