Giter Club home page Giter Club logo

Comments (9)

hishamhm avatar hishamhm commented on May 26, 2024 3

@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.

kikito avatar kikito commented on May 26, 2024

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.

eliasdaler avatar eliasdaler commented on May 26, 2024

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.

eliasdaler avatar eliasdaler commented on May 26, 2024

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.

kikito avatar kikito commented on May 26, 2024

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.

eliasdaler avatar eliasdaler commented on May 26, 2024

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.

eliasdaler avatar eliasdaler commented on May 26, 2024

... 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.

roman-orekhov avatar roman-orekhov commented on May 26, 2024

On a related note -- why isn't inspect showing metatables for userdatas?

from inspect.lua.

svermeulen avatar svermeulen commented on May 26, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.