lua.c and OS error codes

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

lua.c and OS error codes

Thomas Lauer-3
A 'return 1453' in a Lua main chunk doesn't set the error code that's
handed back to the OS. It would be nice if utility scripts could set
this value.

(Sure, os.exit(1453) does that... but it also skips normal program
termination. That means, among other things, that no full garbage
collection is performed at program exit. Not always desirable,
especially if one has OS resources in userdata.)

The following patch lets the value of a 'return <number>' in a lua main
chunk bubble up to the OS. It is pretty small and seems to have no dire
side effects (tested on a Windows box, should run everywhere though).

diff -ruN lua-5.1.2\src\lua.c lua-5.1.2-errcode-patch\src\lua.c
--- lua-5.1.2\src\lua.c	Sun Jan 01 00:00:00 2006
+++ lua-5.1.2-errcode-patch\src\lua.c	Wed Jun 13 22:01:06 2007
@@ -332,6 +332,7 @@
   int argc;
   char **argv;
   int status;
+  int errcode;
 };
 
 
@@ -368,6 +369,10 @@
     }
     else dofile(L, NULL);  /* executes stdin as a file */
   }
+  if (lua_gettop(L) == 2 && lua_type(L,-1) == LUA_TNUMBER) {
+    s->errcode = lua_tointeger(L, -1);
+    lua_pop(L, 1);
+  }
   return 0;
 }
 
@@ -382,9 +387,10 @@
   }
   s.argc = argc;
   s.argv = argv;
+  s.errcode = 0;
   status = lua_cpcall(L, &pmain, &s);
   report(L, status);
   lua_close(L);
-  return (status || s.status) ? EXIT_FAILURE : EXIT_SUCCESS;
+  return s.errcode;
 }
 
-- 
cheers  thomasl

web : http://thomaslauer.com/start


Reply | Threaded
Open this post in threaded view
|

Re: lua.c and OS error codes

Tony Finch
On Thu, 14 Jun 2007, Thomas Lauer wrote:

> A 'return 1453' in a Lua main chunk doesn't set the error code that's
> handed back to the OS. It would be nice if utility scripts could set
> this value.

I wrote some very similar code for my specialized lua wrapper yesterday,
so I agree that it's useful :-) I'd suggest amending your patch to set
s.errcode to EXIT_FAILURE if the (lua error) status is non-zero.

Tony.
-- 
f.a.n.finch  <[hidden email]>  http://dotat.at/
LUNDY FASTNET: SOUTHEASTERLY 4 OR 5, OCCASIONALLY 6. SLIGHT OR MODERATE. RAIN
OR SHOWERS. GOOD, OCCASIONALLY MODERATE OR POOR.

Reply | Threaded
Open this post in threaded view
|

Re: lua.c and OS error codes

Thomas Lauer-3
Tony Finch <[hidden email]> wrote:
> > A 'return 1453' in a Lua main chunk doesn't set the error code that's
> > handed back to the OS. It would be nice if utility scripts could set
> > this value.
> 
> I wrote some very similar code for my specialized lua wrapper yesterday,
> so I agree that it's useful :-) I'd suggest amending your patch to set
> s.errcode to EXIT_FAILURE if the (lua error) status is non-zero.

I thought of that, but for the time being decided against. Main reason
was that external tools can't easily distinguish between a non-zero code
that means "lua has a problem with my script" (i.e. lua error status<>0)
vs "my script has a problem with whatever" (i.e. my error code<>0). And
whatever I do, I can construct cases where doing the opposite would be
better.

(Anyway, Lua is not supposed to have problems with my scripts...;-)

-- 
cheers  thomasl

web : http://thomaslauer.com/start


Reply | Threaded
Open this post in threaded view
|

Re: lua.c and OS error codes

Tony Finch
On Thu, 14 Jun 2007, Thomas Lauer wrote:
>
> I thought of that, but for the time being decided against. Main reason
> was that external tools can't easily distinguish between a non-zero code
> that means "lua has a problem with my script" (i.e. lua error status<>0)
> vs "my script has a problem with whatever" (i.e. my error code<>0). And
> whatever I do, I can construct cases where doing the opposite would be
> better.

Yes, this is a problem. However I was thinking it would be preferable to
make the patch extend the current behaviour (add more ways of returning
non-zero) and avoid incompatibilities (like returning zero after a
compilation error).

Tony.
-- 
f.a.n.finch  <[hidden email]>  http://dotat.at/
SOUTH FITZROY: SOUTHWEST VEERING WEST 5 TO 7, PERHAPS GALE 8 LATER. MODERATE
OR ROUGH, OCCASIONALLY VERY ROUGH LATER. RAIN OR SHOWERS. MODERATE OR GOOD.

Reply | Threaded
Open this post in threaded view
|

Re: lua.c and OS error codes

Thomas Lauer-3
Tony Finch <[hidden email]> wrote:
> > I thought of that, but for the time being decided against. Main reason
> > was that external tools can't easily distinguish between a non-zero code
> > that means "lua has a problem with my script" (i.e. lua error status<>0)
> > vs "my script has a problem with whatever" (i.e. my error code<>0). And
> > whatever I do, I can construct cases where doing the opposite would be
> > better.
> 
> Yes, this is a problem. However I was thinking it would be preferable to
> make the patch extend the current behaviour (add more ways of returning
> non-zero) and avoid incompatibilities (like returning zero after a
> compilation error).

Well, my reasoning was that people who know how to patch their C code
can change the last line in the patch in such a way that it does that.
The whole thing is not exactly rocket science;-)

But you're right, I should at least have mentioned that problem.

-- 
cheers  thomasl

web : http://thomaslauer.com/start


Reply | Threaded
Open this post in threaded view
|

Re: lua.c and OS error codes

Luiz Henrique de Figueiredo
In reply to this post by Thomas Lauer-3
> A 'return 1453' in a Lua main chunk doesn't set the error code that's

I think error codes are restricted to 0-255. This is not your point, but it's
good to keep in mind.

Reply | Threaded
Open this post in threaded view
|

Re: lua.c and OS error codes

Thomas Lauer-3
Luiz Henrique de Figueiredo <[hidden email]> wrote:
> > A 'return 1453' in a Lua main chunk doesn't set the error code that's
> 
> I think error codes are restricted to 0-255. This is not your point, but it's
> good to keep in mind.

Not under Windows, AFAIK.

-- 
cheers  thomasl

web : http://thomaslauer.com/start


Reply | Threaded
Open this post in threaded view
|

Re: lua.c and OS error codes

eugeny gladkih
In reply to this post by Luiz Henrique de Figueiredo
>>>>> "LHdF" == Luiz Henrique de Figueiredo <[hidden email]> writes:

 >> A 'return 1453' in a Lua main chunk doesn't set the error code that's
 LHdF> I think error codes are restricted to 0-255. This is not
 LHdF> your point, but it's good to keep in mind.

Windows supports full 32bit process exit codes.

-- 
Yours sincerely, Eugeny.
Doctor Web, Ltd. http://www.drweb.com

Reply | Threaded
Open this post in threaded view
|

Re: lua.c and OS error codes

Mark Edgar-2
On 6/14/07, eugeny gladkih <[hidden email]> wrote:
>>>>> "LHdF" == Luiz Henrique de Figueiredo <[hidden email]> writes:

 >> A 'return 1453' in a Lua main chunk doesn't set the error code that's
 LHdF> I think error codes are restricted to 0-255. This is not
 LHdF> your point, but it's good to keep in mind.

Windows supports full 32bit process exit codes.

Windows apparently supports exit codes from 0 to 4294967295 and POSIX
supports exit codes from 0 to 255, but C only supports two exit codes:
EXIT_SUCCESS==0 and EXIT_FAILURE, which is a non-zero but otherwise
unspecified value.

    -Mark

Reply | Threaded
Open this post in threaded view
|

Re: lua.c and OS error codes

Thomas Lavergne
>From my ANSI/C book it seems that return code is an int, and the
interpretation of his value is implementation defined with just two
values predefined EXIT_SUCCES and EXIT_FAILURE.
So ANSI/C also support at least 16bit exit code (least precision
allowed for int) but this can be more limited due to the implementation

Tom

On Thu, Jun 14, 2007 at 11:17:44AM -0700, Mark Edgar wrote:
> Windows apparently supports exit codes from 0 to 4294967295 and POSIX
> supports exit codes from 0 to 255, but C only supports two exit codes:
> EXIT_SUCCESS==0 and EXIT_FAILURE, which is a non-zero but otherwise
> unspecified value.
> 
>     -Mark

-- 
Thomas Lavergne                       "Le vrai rêveur est celui qui rêve
                                       de l'impossible."  (Elsa Triolet)
[hidden email]                           http://reveurs.org

Reply | Threaded
Open this post in threaded view
|

Re: lua.c and OS error codes

Dave Dodge
On Fri, Jun 15, 2007 at 04:35:09AM +0200, Lavergne Thomas wrote:
> On Thu, Jun 14, 2007 at 11:17:44AM -0700, Mark Edgar wrote:
> > Windows apparently supports exit codes from 0 to 4294967295 and POSIX
> > supports exit codes from 0 to 255, but C only supports two exit codes:
> > EXIT_SUCCESS==0 and EXIT_FAILURE, which is a non-zero but otherwise
> > unspecified value.

It's a subtle point, but C doesn't actually require that
EXIT_SUCCESS==0.  It just has to have the same _effect_ as 0.

> From my ANSI/C book it seems that return code is an int, and the
> interpretation of his value is implementation defined with just two
> values predefined EXIT_SUCCES and EXIT_FAILURE.
> So ANSI/C also support at least 16bit exit code (least precision
> allowed for int) but this can be more limited due to the implementation

Right, pretty much everything about exit() is implementation-defined,
For example if you use exit(0) there's no guarantee that the actual
numeric value 0 becomes visible anywhere; it just says that some
implementation-defined way of representing "success" is returned to
the host environment.

                                                  -Dave Dodge