Comments (9)
@kikito, can we have this for userdata only? It is unlikely that __tostring
of userdata will ever be inspect
, because that has no useful behavior.
One out-of-the-box benefit of doing this is identifying ngx.null
in OpenResty applications (I've seen colleagues get stumped by this when debugging with inspect
).
from inspect.lua.
Hi! You must be using a very recent version of inspect.lua, because I removed that functionality from it on my last commit, 6 days ago.
Explanation: I had exactly that functionality before, but I decided to remove it when I realized that it precluded using inspect
on a table's __tostring
method. If you do:
local t = setmetatable({}, { __tostring = inspect })
print(t)
On the previous version this would enter an infinite loop (the first tostring
will call inspect
, which will call tostring
again to generate the comment like you mentioned, and that will call inspect
again).
The reason why I didn't release a new version of inspect.lua after I made this change is that I'm still thinking about how to best resolve this problem. I'm open to suggestions.
For now, if you need that feature right away, I recommend you download v3.1.1.
EDIT: upon re-reading your issue, it seems that you want to include it only on userdata. I will think about this as well.
from inspect.lua.
Yes, I mainly want it for userdata as it should be safe from this. As for usual tables - I'll try to work out a way to solve this problem.
Btw, this code works fine (if I checkout to this commit):
local inspect = require 'inspect'
local t = setmetatable({f = 5}, { __tostring = inspect })
print(t)
But this one enters the infinite loop (you probably meant this one, right?):
local inspect = require 'inspect'
local t = setmetatable({f = 5}, { __tostring = inspect.inspect })
print(t)
from inspect.lua.
Seems like I've fixed it. See my tostring-fix branch on my fork and feel free to point out cases where it may fail.
Here's what I've used for testing:
local inspect = require 'inspect'
local t = setmetatable({}, { __tostring = inspect.inspect })
print("> print(t)")
print(t)
print('\n')
print("> print(inspect(t))")
print(inspect(t))
print('\n')
local t2 = setmetatable({}, { __tostring = function() return "ok" end })
print("> print(t2)")
print(t2)
print('\n')
print("> print(inspect(t2))")
print(inspect(t2))
This outputs:
> print(t)
{
<metatable> = {
__tostring = <function 1>
}
}
> print(inspect(t))
{
<metatable> = {
__tostring = <function 1>
}
}
> print(t2)
ok
> print(inspect(t2))
{ -- ok
<metatable> = {
__tostring = <function 1>
}
}
from inspect.lua.
Hi, thanks for giving this a try! Unfortunately it only solves the most obvious case - if you wrap inspect
in a function it will still enter infinite loops:
local f = function(x) return inspect(x) end
local t = setmetatable({}, { __tostring = f })
print(t)
I don't think this can be completely solved if you want to execute __tostrings
at all. Maybe the right
solution is just using a config option and make it default to false. I still have to think about it.
from inspect.lua.
Can you check out the latest commit at my branch? Seems like the problem you pointed out is fixable, but possibly has some problems with it... (ah, the wonderful world of recursion).
local f = function(x) return inspect(x) end
local t = setmetatable({}, { __tostring = f })
print(t)
now prints:
{ -- {\n <metatable> = {\n __tostring = <function 1>\n }\n}
<metatable> = {
__tostring = <function 1>
}
}
We can also make some error for user doing it instead of showing this. Or maybe we can make the output better somehow...
P.S. It may be worth leaving this safe guarding in the code even if this __tostring stuff is optional. Will probably save someone from nasty crash.
from inspect.lua.
... and this won't work in this case:
local inspect = require 'inspect'
local t2 = setmetatable({}, { __tostring = function() return "HEHE" end })
local f = function(x)
inspect(t2) -- this will reset inspect.GUARD :(
return inspect(x)
end
local t = setmetatable({}, { __tostring = f })
print(t)
And we can go further - what if __tostring calls a C function which calls inspect on the same table... and so on.
I don't know, maybe it's better to make __tostring
behaviour optional and warn users that they're gonna have a bad time if their __tostring
calls inspect...
from inspect.lua.
On a related note -- why isn't inspect showing metatables for userdatas?
from inspect.lua.
I really wanted inspect to use __tostring, so I made this modification. It will use __tostring, and if it encounters a recursive attempt to call inspect again it will throw an error
from inspect.lua.
Related Issues (20)
- custom __metatable metafield that is not nil or table breaks inspecting HOT 3
- Error when inspecting table returned by luaposix.dir HOT 3
- inspect does not render non printable values correctly HOT 7
- "Array"-style display option for tables HOT 2
- Error: attempt to compare number with nil HOT 3
- Can you please make metatables output optionally disablable? HOT 5
- Option to display strings with newlines as multi-line strings HOT 1
- Use dofile in Gideros HOT 1
- Looks strange with `inspect {{ x = 1 }}` HOT 3
- PERF: countCycles doesn't respect options.depth HOT 1
- No option to print lua table in one line HOT 1
- How to remove `<table id>`? HOT 2
- Lua crashes when inspecting _G HOT 6
- Idea: integration with middleclass
- compat requirement unmentioned in installation guide HOT 1
- inspect option to output binary value as hex codes instead of escaped characters
- Use LuaBuffers in LuaJIT
- Typo in the examples, oops
- Buggy escape in smartQuote HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from inspect.lua.