Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
rhaberkorn opened this issue Nov 16, 2023 · 0 comments
Open

Comments

@rhaberkorn
Copy link

rhaberkorn commented Nov 16, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant