1
0
Fork 0
mirror of https://github.com/pry/pry.git synced 2022-11-09 12:35:05 -05:00

New backtrace command. :when_started hook now yields binding_stack, too.

Pry saves the backtrace (output of caller method) before a session starts in the Pry#backtrace= accessor.
The 'backtrace' command always uses this backtrace.

*BUG* may exist where need to take into account whether Pry was invoked with Pry.start of binding.pry -- so that we know how much of the backtrace to cut off.

:when_started hook now yields binding_stack, giving the hook opportunity to change the target of the session.
This commit is contained in:
John Mair 2011-11-30 01:35:45 +13:00
parent b21062c001
commit 28560c0d73
2 changed files with 19 additions and 5 deletions

View file

@ -127,6 +127,11 @@ class Pry
target.pry
end
command "backtrace", "Show the backtrace for the Pry session" do
output.puts "\n#{text.bold('Backtrace:')}\n--\n"
output.puts _pry_.backtrace
end
command "whereami", "Show the code context for the session. (whereami <n> shows <n> extra lines of code around the invocation line. Default: 5)" do |num|
file = target.eval('__FILE__')
line_num = target.eval('__LINE__')

View file

@ -87,8 +87,7 @@ class Pry
return if !initial_session?
# note these have to be loaded here rather than in pry_instance as
# we only want them loaded once per entire Pry lifetime, not
# multiple times per each new session (i.e in debugging)
# we only want them loaded once per entire Pry lifetime.
load_rc if Pry.config.should_load_rc
load_plugins if Pry.config.plugins.enabled
load_requires if Pry.config.should_load_requires
@ -110,10 +109,20 @@ class Pry
target = Pry.binding_for(target)
initial_session_setup
Pry.config.hooks.exec_hook(:when_started, target)
# create the Pry instance to manage the session
pry_instance = new(options)
pry_instance.backtrace = caller(1)
pry_instance.repl(target)
# save backtrace
pry_instance.backtrace = caller.tap(&:shift)
# yield the binding_stack to the hook for modification
Pry.config.hooks.exec_hook(:when_started, binding_stack = [target], pry_instance)
head, *tail = binding_stack
pry_instance.binding_stack.push(*tail)
# Enter the matrix
pry_instance.repl(head)
end
# An inspector that clips the output to `max_length` chars.