pcall(pairs, true) with Lua 5.3.5

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

pcall(pairs, true) with Lua 5.3.5

Roland
Hi List,

I was experimenting some things with various versions of Lua. Especially
this :

print(pcall(pairs, true))

Under Lua 5.1.5, it returns : false bad argument #1 to '?' (table expected,
got boolean)
With Lua 5.2.2, it returns : false bad argument #1 to 'pairs' (table
expected, got boolean)
Under Lua 5.3.5, it returns : true function: 6455a2c0 true nil

I do not understand what is going on under Lua 5.3.5. Note that, with a
previous version (Lua 5.3.2 for example), I still have the same result than
Lua5.2 and 5.1 case.

Similarly, still with Lua 5.3.5, the call pcall(pairs,function() end))
returns :
true function: 6455a2c0 function: 008f7fb0 nil

whereas it would have been returning false for previous versions of Lua.

Can anyone help me understand ?

Roland.



--
Sent from: http://lua.2524044.n2.nabble.com/Lua-l-f2524044.html

Reply | Threaded
Open this post in threaded view
|

Re: pcall(pairs, true) with Lua 5.3.5

nobody
On 2018-08-23 10:20, Roland wrote:
*snip*
> print(pcall(pairs, true))
*snip*
> I do not understand what is going on under Lua 5.3.5. Note that,
> with a previous version (Lua 5.3.2 for example), I still have the
> same result than Lua5.2 and 5.1 case.

The behavior already changed in 5.3.4.

See <http://lua-users.org/lists/lua-l/2017-01/msg00242.html> for more
details.

-- nobody

Reply | Threaded
Open this post in threaded view
|

Re: pcall(pairs, true) with Lua 5.3.5

Dirk Laurie-2
In reply to this post by Roland
Op Do., 23 Aug. 2018 om 10:20 het Roland <[hidden email]> geskryf:

>
> Hi List,
>
> I was experimenting some things with various versions of Lua. Especially
> this :
>
> print(pcall(pairs, true))
>
> Under Lua 5.1.5, it returns : false     bad argument #1 to '?' (table expected,
> got boolean)
> With Lua 5.2.2, it returns : false      bad argument #1 to 'pairs' (table
> expected, got boolean)
> Under Lua 5.3.5, it returns : true      function: 6455a2c0      true    nil
>
> I do not understand what is going on under Lua 5.3.5. Note that, with a
> previous version (Lua 5.3.2 for example), I still have the same result than
> Lua5.2 and 5.1 case.
>
> Similarly, still with Lua 5.3.5, the call pcall(pairs,function() end))
> returns :
> true    function: 6455a2c0      function: 008f7fb0      nil
>
> whereas it would have been returning false for previous versions of Lua.
>
> Can anyone help me understand ?

Is it less puzzling if you don't use pcall?

$ lua5.2
Lua 5.2.4  Copyright (C) 1994-2015 Lua.org, PUC-Rio
> for k,v in pairs(1) do print(k,v) end
stdin:1: bad argument #1 to 'pairs' (table expected, got number)
stack traceback:
    [C]: in function 'pairs'
    stdin:1: in main chunk
    [C]: in ?

$ lua
Lua 5.3.5  Copyright (C) 1994-2018 Lua.org, PUC-Rio
> for k,v in pairs(1) do print(k,v) end
stdin:1: bad argument #1 to 'for iterator' (table expected, got number)
stack traceback:
    [C]: in function 'next'
    stdin:1: in main chunk
    [C]: in ?
>

The error occurs not when calling 'pairs', but when calling the
iterator that it returns.

Thus the new behaviour makes

for k,v in pairs(1) do

and

for k,v in next.1 do

behave the same way (except, of course, if you debug.setmetamethod
__pairs for numbers).