io.write returns number of written bytes on error

This commit is contained in:
Roberto Ierusalimschy
2025-04-03 11:32:49 -03:00
parent 93e347b519
commit 3f4f28010a
4 changed files with 65 additions and 7 deletions

View File

@@ -696,6 +696,37 @@ do
end
if T and T.nonblock then
print("testing failed write")
-- unable to write anything to /dev/full
local f = io.open("/dev/full", "w")
assert(f:setvbuf("no"))
local _, _, err, count = f:write("abcd")
assert(err > 0 and count == 0)
assert(f:close())
-- receiver will read a "few" bytes (enough to empty a large buffer)
local receiver = [[
lua -e 'assert(io.stdin:setvbuf("no")); assert(#io.read(1e4) == 1e4)' ]]
local f = io.popen(receiver, "w")
assert(f:setvbuf("no"))
T.nonblock(f)
-- able to write a few bytes
assert(f:write(string.rep("a", 1e2)))
-- Unable to write more bytes than the pipe buffer supports.
-- (In Linux, the pipe buffer size is 64K (2^16). Posix requires at
-- least 512 bytes.)
local _, _, err, count = f:write("abcd", string.rep("a", 2^17))
assert(err > 0 and count >= 512 and count < 2^17)
assert(f:close())
end
if not _soft then
print("testing large files (> BUFSIZ)")
io.output(file)