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)
评论列表
文章目录