Skip to content

Instantly share code, notes, and snippets.

@vadimkantorov
Last active January 3, 2022 15:41
Show Gist options
  • Save vadimkantorov/1fca5a6515962f106c103a77819c37e2 to your computer and use it in GitHub Desktop.
Save vadimkantorov/1fca5a6515962f106c103a77819c37e2 to your computer and use it in GitHub Desktop.
A Lua repl that copies local variables onto globals for inspection
local torch_repl = repl
function repl(restoreGlobals)
-- copied from http://stackoverflow.com/questions/33068607/how-can-i-use-the-torch-repl-for-debugging
require 'trepl'
restoreGlobals = restoreGlobals or false
-- optionally make a shallow copy of _G
local oldG = {}
if restoreGlobals then
for k, v in pairs(_G) do
oldG[k] = v
end
end
-- copy upvalues to _G
local i = 1
local func = debug.getinfo(2, "f").func
while true do
local k, v = debug.getupvalue(func, i)
if k ~= nil then
_G[k] = v
else
break
end
i = i + 1
end
-- copy locals to _G
local i = 1
while true do
local k, v = debug.getlocal(2, i)
if k ~= nil then
_G[k] = v
else
break
end
i = i + 1
end
torch_repl()
if restoreGlobals then
_G = oldG
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment