removed all occurences of Pry.active_instance and Pry.last_exception. Now using inject_local() method. Also initializing all locals in repl_prologue

This commit is contained in:
John Mair 2011-08-31 05:35:41 +12:00
parent 662a75ff45
commit d16e396e51
7 changed files with 40 additions and 51 deletions

View File

@ -15,6 +15,9 @@
* show-method and show-doc now accept multiple method names
* control_d hook added (Pry.config.control_d_handler)
* behaviour of ^d is now to break out of current expr if in multi-line expr, or break out of current context if nested, or break out of pry repl loop if at top-level
* can no longer interpolate command name itself e.g #{x}-#{y} where x = "show" and y = "doc"
* ^C no longer captured
* got rid of Pry.active_instance, Pry.last_exception and friends.
*/7/2011 version 0.9.3
* cat --ex (cats 5 lines above and below line in file where exception was raised)

View File

@ -109,7 +109,7 @@ class Pry
alias_command "quit-program", "exit-program", ""
alias_command "!!!", "exit-program", ""
command "!pry", "Start a Pry session on current self; this even works mid-expression." do
command "!pry", "Start a Pry session on current self; this even works mid multi-line expression." do
target.pry
end

View File

@ -131,10 +131,12 @@ class Pry
should_reload = opts[:r]
if opts.ex?
next output.puts "No Exception found." if Pry.last_exception.nil?
last_exception = target.eval("_ex_")
file_name = Pry.last_exception.file
line = Pry.last_exception.line
next output.puts "No Exception found." if last_exception.nil?
file_name = last_exception.file
line = last_exception.line
next output.puts "Exception has no associated file." if file_name.nil?
next output.puts "Cannot edit exceptions raised in REPL." if Pry.eval_path == file_name
elsif opts.t?

View File

@ -38,6 +38,7 @@ class Pry
start_line = 0
end_line = -1
file_name = nil
last_exception = target.eval("_ex_")
opts = Slop.parse!(args) do |opt|
opt.on :s, :start, "Start line (defaults to start of file)Line 1 is the first line.", true, :as => Integer do |line|
@ -50,7 +51,7 @@ class Pry
opt.on :ex, "Show a window of N lines either side of the last exception (defaults to 5).", :optional => true, :as => Integer do |window_size|
window_size ||= 5
ex = Pry.last_exception
ex = last_exception
next if !ex
start_line = (ex.line - 1) - window_size
start_line = start_line < 0 ? 0 : start_line
@ -93,7 +94,7 @@ class Pry
contents = contents.lines.each_with_index.map do |line, idx|
l = idx + start_line
if l == (Pry.last_exception.line - 1)
if l == (last_exception.line - 1)
" =>#{line}"
else
" #{line}"
@ -101,8 +102,8 @@ class Pry
end.join
# header for exceptions
output.puts "\n#{Pry::Helpers::Text.bold('Exception:')}: #{Pry.last_exception.class}: #{Pry.last_exception.message}"
output.puts "#{Pry::Helpers::Text.bold('From:')} #{file_name} @ line #{Pry.last_exception.line}\n\n"
output.puts "\n#{Pry::Helpers::Text.bold('Exception:')}: #{last_exception.class}: #{last_exception.message}"
output.puts "#{Pry::Helpers::Text.bold('From:')} #{file_name} @ line #{last_exception.line}\n\n"
end
set_file_and_dir_locals(file_name)

View File

@ -36,10 +36,8 @@ class Pry
def set_file_and_dir_locals(file_name)
return if !target
$_file_temp = File.expand_path(file_name)
$_dir_temp = File.dirname($_file_temp)
target.eval("_file_ = $_file_temp")
target.eval("_dir_ = $_dir_temp")
_pry_.inject_local("_file_", File.expand_path(file_name), target)
_pry_.inject_local("_dir_", File.dirname(File.expand_path(file_name)), target)
end
def stub_proc(name, options)

View File

@ -18,21 +18,6 @@ class Pry
def_delegators delagatee, *names.map { |v| "#{v}=" }
end
# Get last value evaluated by Pry.
# This method should not need to be accessed directly.
# @return [Object] The last result.
attr_accessor :last_result
# Get last exception raised.
# This method should not need to be accessed directly.
# @return [Exception] The last exception.
attr_accessor :last_exception
# Get the active Pry instance that manages the active Pry session.
# This method should not need to be accessed directly.
# @return [Pry] The active Pry instance.
attr_accessor :active_instance
# Get/Set the Proc that defines extra Readline completions (on top
# of the ones defined for IRB).
# @return [Proc] The Proc that defines extra Readline completions (on top

View File

@ -71,6 +71,18 @@ class Pry
end
end
# Injects a local variable into the provided binding.
# @param [String] name The name of the local to inject.
# @param [Object] value The value to set the local to.
# @param [Binding] b The binding to set the local on.
# @return [Object] The value the local was set to.
def inject_local(name, value, b)
Thread.current[:__pry_local__] = value
b.eval("#{name} = Thread.current[:__pry_local__]")
ensure
Thread.current[:__pry_local__] = nil
end
# @return [Integer] The maximum amount of objects remembered by the inp and
# out arrays. Defaults to 100.
def memory_size
@ -93,15 +105,17 @@ class Pry
# @param [Binding] target The target binding for the session.
def repl_prologue(target)
exec_hook :before_session, output, target
Pry.active_instance = self
# Make sure special locals exist
target.eval("inp = ::Pry.active_instance.instance_eval { @input_array }")
target.eval("out = ::Pry.active_instance.instance_eval { @output_array }")
inject_local("inp", @input_array, target)
inject_local("out", @output_array, target)
inject_local("_pry_", self, target)
inject_local("_ex_", nil, target)
inject_local("file_", nil, target)
inject_local("_dir_", nil, target)
set_last_result(nil, target)
set_active_instance(target)
@input_array << nil # add empty input so inp and out match
set_last_result(Pry.last_result, target)
Pry.active_sessions += 1
binding_stack.push target
@ -171,13 +185,9 @@ class Pry
Readline.completion_proc = Pry::InputCompleter.build_completion_proc target, instance_eval(&custom_completions)
end
# save the pry instance to active_instance
Pry.active_instance = self
target.eval("inp = ::Pry.active_instance.instance_eval { @input_array }")
target.eval("out = ::Pry.active_instance.instance_eval { @output_array }")
set_active_instance(target)
inject_local("inp", @input_array, target)
inject_local("out", @output_array, target)
inject_local("_pry_", self, target)
code = r(target)
@ -289,8 +299,7 @@ class Pry
@last_result_is_exception = false
@output_array << result
Pry.last_result = result
target.eval("_ = ::Pry.last_result")
inject_local("_", result, target)
end
# Set the last exception for a session.
@ -308,8 +317,7 @@ class Pry
@last_result_is_exception = true
@output_array << ex
Pry.last_exception = ex
target.eval("_ex_ = ::Pry.last_exception")
inject_local("_ex_", ex, target)
end
# Update Pry's internal state after evalling code.
@ -338,14 +346,6 @@ class Pry
end
end
# Set the active instance for a session.
# This method should not need to be invoked directly.
# @param [Binding] target The binding to set `_ex_` on.
def set_active_instance(target)
Pry.active_instance = self
target.eval("_pry_ = ::Pry.active_instance")
end
# @return [Boolean] True if the last result is an exception that was raised,
# as opposed to simply an instance of Exception (like the result of
# Exception.new)