From ba9cd0d25a022cf61c0b747d8e26f9ba81201112 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 24 Oct 2019 10:49:44 -0300 Subject: [PATCH] Change in the prefix of messages from searchers The initial "\n\t" to properly indent a searcher message is being added by 'findloader' when building the error message, instead of being included in the original message by each searcher itself. --- loadlib.c | 14 +++++++++----- testes/attrib.lua | 23 +++++++++++++++++++++++ 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/loadlib.c b/loadlib.c index 689767f3..56167f64 100644 --- a/loadlib.c +++ b/loadlib.c @@ -458,13 +458,13 @@ static const char *getnextfilename (char **path, char *end) { /* ** Given a path such as ";blabla.so;blublu.so", pushes the string ** -** no file 'blabla.so' +** no file 'blabla.so' ** no file 'blublu.so' */ static void pusherrornotfound (lua_State *L, const char *path) { luaL_Buffer b; luaL_buffinit(L, &b); - luaL_addstring(&b, "\n\tno file '"); + luaL_addstring(&b, "no file '"); luaL_addgsub(&b, path, LUA_PATH_SEP, "'\n\tno file '"); luaL_addstring(&b, "'"); luaL_pushresult(&b); @@ -591,7 +591,7 @@ static int searcher_Croot (lua_State *L) { if (stat != ERRFUNC) return checkload(L, 0, filename); /* real error */ else { /* open function not found */ - lua_pushfstring(L, "\n\tno module '%s' in file '%s'", name, filename); + lua_pushfstring(L, "no module '%s' in file '%s'", name, filename); return 1; } } @@ -604,7 +604,7 @@ static int searcher_preload (lua_State *L) { const char *name = luaL_checkstring(L, 1); lua_getfield(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE); if (lua_getfield(L, -1, name) == LUA_TNIL) { /* not found? */ - lua_pushfstring(L, "\n\tno field package.preload['%s']", name); + lua_pushfstring(L, "no field package.preload['%s']", name); return 1; } else { @@ -623,8 +623,10 @@ static void findloader (lua_State *L, const char *name) { luaL_buffinit(L, &msg); /* iterate over available searchers to find a loader */ for (i = 1; ; i++) { + luaL_addstring(&msg, "\n\t"); /* error-message prefix */ if (lua_rawgeti(L, 3, i) == LUA_TNIL) { /* no more searchers? */ lua_pop(L, 1); /* remove nil */ + luaL_buffsub(&msg, 2); /* remove prefix */ luaL_pushresult(&msg); /* create error message */ luaL_error(L, "module '%s' not found:%s", name, lua_tostring(L, -1)); } @@ -636,8 +638,10 @@ static void findloader (lua_State *L, const char *name) { lua_pop(L, 1); /* remove extra return */ luaL_addvalue(&msg); /* concatenate error message */ } - else + else { /* no error message */ lua_pop(L, 2); /* remove both returns */ + luaL_buffsub(&msg, 2); /* remove prefix */ + } } } diff --git a/testes/attrib.lua b/testes/attrib.lua index b1a4a199..76a447c8 100644 --- a/testes/attrib.lua +++ b/testes/attrib.lua @@ -47,6 +47,29 @@ do package.path = oldpath end + +do print"testing 'require' message" + local oldpath = package.path + local oldcpath = package.cpath + + package.path = "?.lua;?/?" + package.cpath = "?.so;?/init" + + local st, msg = pcall(require, 'XXX') + + local expected = [[module 'XXX' not found: + no field package.preload['XXX'] + no file 'XXX.lua' + no file 'XXX/XXX' + no file 'XXX.so' + no file 'XXX/init']] + + assert(msg == expected) + + package.path = oldpath + package.cpath = oldcpath +end + print('+')