guysv / ilua

Portable Lua kernel for Jupyter
GNU General Public License v2.0
115 stars 11 forks source link

The included "inspect" module poorly respects __tostring metamethods #29

Open rhaberkorn opened 8 months ago

rhaberkorn commented 8 months ago

When "inspecting" a table with a __tostring metamethod, e.g. by evaluating a Jupyter cell, I would expect it to "respect" the representation established by the meta method. What it does instead is mixing the tostring-value with a dump of the entire table. This can look very ugly especially for large objects. Just to clarify using the Lua 5.2 interpreter:

$ lua5.2
Lua 5.2.4  Copyright (C) 1994-2015 Lua.org, PUC-Rio
> inspect = require "ext.inspect"
> t = setmetatable({1, 2, 3}, {__tostring = function(t) return "FOO" end})
> = inspect.inspect(t)
{ -- FOO
   1, 2, 3,
  <metatable> = {
    __tostring = <function 1>
  }
}

The output is the same in ILua. Using the latest version of inspect.lua v3.1.3 is even worse since it entirely omits the tostring-representation after --. Also since they use Teal in recent versions, it might even be tricky to bundle a readable preprocessed version of inspect.lua v3.1.3 with ILua.

For the time being, I have patched my inspect.lua. Perhaps we should always check whether the inspected value contains a __tostring metamethod and only if not pass it to the inspect-library at all. But @guysv might have differing strong opinions about that.

I could prepare a merge request.