Просмотр исходного кода

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.
Roberto Ierusalimschy 5 лет назад
Родитель
Сommit
ba9cd0d25a
2 измененных файлов с 32 добавлено и 5 удалено
  1. 9 5
      loadlib.c
  2. 23 0
      testes/attrib.lua

+ 9 - 5
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 */
+    }
   }
 }
 

+ 23 - 0
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('+')