From 223bb04090344b1972dc2a7910a54b46210f0d40 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 31 Jul 2019 11:41:59 -0300 Subject: [PATCH] Correction in the documentation of 'io.lines' The loop does not end on end of file, but when the iterator function fails to read a value. (In particular, the format "a" never fails, so a loop with 'io.lines(fname, "a")' never ends.) --- liolib.c | 2 +- manual/manual.of | 10 ++++------ 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/liolib.c b/liolib.c index 83fbb172..56507d5e 100644 --- a/liolib.c +++ b/liolib.c @@ -624,7 +624,7 @@ static int io_readline (lua_State *L) { lua_pushvalue(L, lua_upvalueindex(3 + i)); n = g_read(L, p->f, 2); /* 'n' is number of results */ lua_assert(n > 0); /* should return at least a nil */ - if (lua_toboolean(L, -n)) /* read at least one value? */ + if (!lua_isnil(L, -n)) /* read at least one value? */ return n; /* return them */ else { /* first result is nil: EOF or error */ if (n > 1) { /* is there error information? */ diff --git a/manual/manual.of b/manual/manual.of index 8eebe9cb..c1ee8eb7 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -7926,8 +7926,8 @@ instead of returning an error code. Opens the given file name in read mode and returns an iterator function that works like @T{file:lines(@Cdots)} over the opened file. -When the iterator function detects the end of file, -it returns no values (to finish the loop) and automatically closes the file. +When the iterator function fails to read any value, +it automatically closes the file. Besides the iterator function, @id{io.lines} returns three other values: two @nil values as placeholders, @@ -7941,7 +7941,8 @@ to @T{io.input():lines("l")}; that is, it iterates over the lines of the default input file. In this case, the iterator does not close the file when the loop ends. -In case of errors this function raises the error, +In case of errors opening the file, +this function raises the error, instead of returning an error code. } @@ -8053,9 +8054,6 @@ starting at the current position. Unlike @Lid{io.lines}, this function does not close the file when the loop ends. -In case of errors this function raises the error, -instead of returning an error code. - } @LibEntry{file:read (@Cdots)|