Browse Source

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.)
Roberto Ierusalimschy 6 years ago
parent
commit
223bb04090
2 changed files with 5 additions and 7 deletions
  1. 1 1
      liolib.c
  2. 4 6
      manual/manual.of

+ 1 - 1
liolib.c

@@ -624,7 +624,7 @@ static int io_readline (lua_State *L) {
     lua_pushvalue(L, lua_upvalueindex(3 + i));
     lua_pushvalue(L, lua_upvalueindex(3 + i));
   n = g_read(L, p->f, 2);  /* 'n' is number of results */
   n = g_read(L, p->f, 2);  /* 'n' is number of results */
   lua_assert(n > 0);  /* should return at least a nil */
   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 */
     return n;  /* return them */
   else {  /* first result is nil: EOF or error */
   else {  /* first result is nil: EOF or error */
     if (n > 1) {  /* is there error information? */
     if (n > 1) {  /* is there error information? */

+ 4 - 6
manual/manual.of

@@ -7926,8 +7926,8 @@ instead of returning an error code.
 Opens the given file name in read mode
 Opens the given file name in read mode
 and returns an iterator function that
 and returns an iterator function that
 works like @T{file:lines(@Cdots)} over the opened file.
 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,
 Besides the iterator function,
 @id{io.lines} returns three other values:
 @id{io.lines} returns three other values:
 two @nil values as placeholders,
 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.
 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 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.
 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
 Unlike @Lid{io.lines}, this function does not close the file
 when the loop ends.
 when the loop ends.
 
 
-In case of errors this function raises the error,
-instead of returning an error code.
-
 }
 }
 
 
 @LibEntry{file:read (@Cdots)|
 @LibEntry{file:read (@Cdots)|