From a102221a0b9f5aa9347b9b15943e39e9d631e0ae Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 5 Jun 2002 13:59:37 -0300 Subject: better error messages --- liolib.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) (limited to 'liolib.c') diff --git a/liolib.c b/liolib.c index 6d6024cf..5ed8fae2 100644 --- a/liolib.c +++ b/liolib.c @@ -1,5 +1,5 @@ /* -** $Id: liolib.c,v 2.4 2002/05/02 17:12:27 roberto Exp roberto $ +** $Id: liolib.c,v 2.5 2002/05/06 19:05:10 roberto Exp roberto $ ** Standard I/O (and system) library ** See Copyright Notice in lua.h */ @@ -60,8 +60,9 @@ static FILE *tofile (lua_State *L, int findex) { lua_pop(L, 1); return *f; } - luaL_argerror(L, findex, "bad file"); - return NULL; /* to avoid warnings */ + if (findex > 0) + luaL_argerror(L, findex, "bad file"); + return NULL; } @@ -133,14 +134,18 @@ static int io_tmpfile (lua_State *L) { static FILE *getiofile (lua_State *L, const char *name) { + FILE *f; lua_pushstring(L, name); lua_rawget(L, lua_upvalueindex(1)); - return tofile(L, -1); + f = tofile(L, -1); + if (f == NULL) + luaL_verror(L, "%s is closed", name); + return f; } static int g_iofile (lua_State *L, const char *name, const char *mode) { - if (lua_isnone(L, 1)) { + if (lua_isnoneornil(L, 1)) { lua_pushstring(L, name); lua_rawget(L, lua_upvalueindex(1)); return 1; @@ -154,8 +159,8 @@ static int g_iofile (lua_State *L, const char *name, const char *mode) { newfile(L, f); } else { + tofile(L, 1); /* check that it's a valid file handle */ lua_pushvalue(L, 1); - tofile(L, -1); /* check that it's a valid file handle */ } lua_rawset(L, lua_upvalueindex(1)); return 0; -- cgit v1.2.3-55-g6feb