diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2025-04-03 12:56:52 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2025-04-03 12:56:52 -0300 |
| commit | 620f49a7aae8a5c982b21f0accbf2ff9019a55f6 (patch) | |
| tree | 4ba9f6063434759963cc5207b4703248687be793 | |
| parent | 3f4f28010aa5065456f1edf97de1ab268cc49944 (diff) | |
| download | lua-620f49a7aae8a5c982b21f0accbf2ff9019a55f6.tar.gz lua-620f49a7aae8a5c982b21f0accbf2ff9019a55f6.tar.bz2 lua-620f49a7aae8a5c982b21f0accbf2ff9019a55f6.zip | |
Tiny refactoring in io.flush
| -rw-r--r-- | liolib.c | 13 | ||||
| -rw-r--r-- | testes/files.lua | 19 |
2 files changed, 25 insertions, 7 deletions
| @@ -732,18 +732,19 @@ static int f_setvbuf (lua_State *L) { | |||
| 732 | } | 732 | } |
| 733 | 733 | ||
| 734 | 734 | ||
| 735 | 735 | static int aux_flush (lua_State *L, FILE *f) { | |
| 736 | static int io_flush (lua_State *L) { | ||
| 737 | FILE *f = getiofile(L, IO_OUTPUT); | ||
| 738 | errno = 0; | 736 | errno = 0; |
| 739 | return luaL_fileresult(L, fflush(f) == 0, NULL); | 737 | return luaL_fileresult(L, fflush(f) == 0, NULL); |
| 740 | } | 738 | } |
| 741 | 739 | ||
| 742 | 740 | ||
| 743 | static int f_flush (lua_State *L) { | 741 | static int f_flush (lua_State *L) { |
| 744 | FILE *f = tofile(L); | 742 | return aux_flush(L, tofile(L)); |
| 745 | errno = 0; | 743 | } |
| 746 | return luaL_fileresult(L, fflush(f) == 0, NULL); | 744 | |
| 745 | |||
| 746 | static int io_flush (lua_State *L) { | ||
| 747 | return aux_flush(L, getiofile(L, IO_OUTPUT)); | ||
| 747 | } | 748 | } |
| 748 | 749 | ||
| 749 | 750 | ||
diff --git a/testes/files.lua b/testes/files.lua index 2c802047..53edf314 100644 --- a/testes/files.lua +++ b/testes/files.lua | |||
| @@ -347,7 +347,7 @@ collectgarbage() | |||
| 347 | 347 | ||
| 348 | assert(io.write(' ' .. t .. ' ')) | 348 | assert(io.write(' ' .. t .. ' ')) |
| 349 | assert(io.write(';', 'end of file\n')) | 349 | assert(io.write(';', 'end of file\n')) |
| 350 | f:flush(); io.flush() | 350 | assert(f:flush()); assert(io.flush()) |
| 351 | f:close() | 351 | f:close() |
| 352 | print('+') | 352 | print('+') |
| 353 | 353 | ||
| @@ -461,6 +461,23 @@ do -- testing closing file in line iteration | |||
| 461 | end | 461 | end |
| 462 | 462 | ||
| 463 | 463 | ||
| 464 | do print("testing flush") | ||
| 465 | local f = io.output("/dev/null") | ||
| 466 | assert(f:write("abcd")) -- write to buffer | ||
| 467 | assert(f:flush()) -- write to device | ||
| 468 | assert(f:write("abcd")) -- write to buffer | ||
| 469 | assert(io.flush()) -- write to device | ||
| 470 | assert(f:close()) | ||
| 471 | |||
| 472 | local f = io.output("/dev/full") | ||
| 473 | assert(f:write("abcd")) -- write to buffer | ||
| 474 | assert(not f:flush()) -- cannot write to device | ||
| 475 | assert(f:write("abcd")) -- write to buffer | ||
| 476 | assert(not io.flush()) -- cannot write to device | ||
| 477 | assert(f:close()) | ||
| 478 | end | ||
| 479 | |||
| 480 | |||
| 464 | -- test for multipe arguments in 'lines' | 481 | -- test for multipe arguments in 'lines' |
| 465 | io.output(file); io.write"0123456789\n":close() | 482 | io.output(file); io.write"0123456789\n":close() |
| 466 | for a,b in io.lines(file, 1, 1) do | 483 | for a,b in io.lines(file, 1, 1) do |
