lunarmodules / Penlight

A set of pure Lua libraries focusing on input data handling (such as reading configuration files), functional programming (such as map, reduce, placeholder expressions,etc), and OS path management. Much of the functionality is inspired by the Python standard libraries.
https://lunarmodules.github.io/Penlight/
MIT License
1.9k stars 238 forks source link

Why "No error" in `compat.execute` #480

Closed jlaurens closed 3 months ago

jlaurens commented 3 months ago

I don't see any mention of "No error" in lua doc for 5.1-4. Isn't it "exit" instead which is a documented output of os.execute?

Tieske commented 3 months ago

Your question isn't clear to me? can you elaborate/rephrase please?

jlaurens commented 3 months ago

compat.execute reads

function compat.execute(cmd)
    local res1,res2,res3 = os.execute(cmd)
    if res2 == "No error" and res3 == 0 and compat.is_windows then
...

The test condition only concerns lua 5.2+ because in lua 5.1 both res2 and res3 are nil. In lua 5.2-4

This function is equivalent to the ISO C function system.
It passes command to be executed by an operating system shell.
Its first result is true if the command terminated successfully, or nil otherwise.
After this first result the function returns a string plus a number, as follows:

"exit": the command terminated normally; the following number is the exit status of the command.
"signal": the command was terminated by a signal; the following number is the signal that terminated the command.

and res2 is either 'exit' or 'signal'. So, if we stick to the official Lua online documentation, the test condition is never met.

Tieske commented 3 months ago

this is Windows specifc. Windows can return errors as negative number I think, or something. That is why Lua may report an error from os.execute, but then the error message it gets from the cruntime is passed along. In the case where the exit code hits this special case, then Lua will report an error, but the error message returned by Lua will be "No error" (from the runtime). So Lua treats it as an error, but it wasn't.

You can check the Lua mailing archives on some discussion on this. iirc it was finally fixed in Lua 5.4.x

So now I recall; Lua code checks for a -1 return value of the C call. But in Windows -1 can be valid exitcodes. So the proper thing to do is clear errno, and check that afterwards. Because just checking for -1 will match the case of an error as well as a -1 exit code (but not an error).

Tieske commented 3 months ago

see: https://www.lua-users.org/lists/lua-l/2018-11/msg00321.html and follow up: https://www.lua-users.org/lists/lua-l/2019-01/msg00125.html

jlaurens commented 3 months ago

So the official documentation of lua is not 100% accurate...

Tieske commented 3 months ago

Those docs are typically very accurate. But especially older versions of Lua didn't take that into account. 5.4 now does the right thing, but 5.1/2/3 do not, hence those special cases.

jlaurens commented 3 months ago

I came to that question in the very particular situation of luatex that embeds lua 5.3 except for os.execute that returns 5.1 like output. Of course, this can cause problems with penlight. Hence the documentation must be very clean in the luatex ecosystem as far as windows is concerned of course. But all this is out of purpose here. Thanks.