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

Merge pull request #1145 from robgleeson/fix-last_exception

Add Pry::LastException class wrapping last_exception
This commit is contained in:
Ryan Fitzgerald 2014-03-09 19:04:21 -07:00
commit 0ae0d8d1e4
6 changed files with 71 additions and 23 deletions

View file

@ -23,8 +23,10 @@
* default configuration(Pry.config) lazy loads its values. (#1096)
* require of 'readline' is delayed until Pry.start() has been called for the first time. (#1117)
* add option to disable input completer through `_pry_.config.completer = nil`
* add `Pry::LastException` (#1145)
#### Bug fixes, etc.
* `Pry#last_exception=` supports exception objects who have been frozen (#1145)
* `binding.pry` inside `.pryrc` file now works, with some limitations (@richo / #1118)
* Add support for BasicObjects to `ls` (#984)
* Allow `ls -c <anything>` (#891)

View file

@ -184,3 +184,4 @@ require 'pry/terminal'
require 'pry/editor'
require 'pry/rubygem'
require "pry/indent"
require "pry/last_exception"

35
lib/pry/last_exception.rb Normal file
View file

@ -0,0 +1,35 @@
class Pry::LastException < BasicObject
attr_reader :file, :line
attr_accessor :bt_index
def initialize(e)
@e = e
@bt_index = 0
@file, @line = bt_source_location_for(0)
end
def method_missing(name, *args, &block)
if @e.respond_to?(name)
@e.public_send(name, *args, &block)
else
super
end
end
def respond_to_missing?(name, boolean=false)
@e.respond_to?(name)
end
def pry?
true
end
def bt_source_location_for(index)
backtrace[index] =~ /(.*):(\d+)/
[$1, $2.to_i]
end
def inc_bt_index
@bt_index = (@bt_index + 1) % backtrace.size
end
end

View file

@ -467,27 +467,17 @@ class Pry
self.last_result = result unless code =~ /\A\s*\z/
end
#
# Set the last exception for a session.
# @param [Exception] ex
def last_exception=(ex)
class << ex
attr_accessor :file, :line, :bt_index
def bt_source_location_for(index)
backtrace[index] =~ /(.*):(\d+)/
[$1, $2.to_i]
end
def inc_bt_index
@bt_index = (@bt_index + 1) % backtrace.size
end
end
ex.bt_index = 0
ex.file, ex.line = ex.bt_source_location_for(0)
#
# @param [Exception] e
# the last exception.
#
def last_exception=(e)
last_exception = Pry::LastException.new(e)
@last_result_is_exception = true
@output_array << ex
@last_exception = ex
@output_array << last_exception
@last_exception = last_exception
end
# Update Pry's internal state after evalling code.

View file

@ -68,11 +68,9 @@ module PryTestHelpers
end
def mock_exception(*mock_backtrace)
e = StandardError.new("mock exception")
(class << e; self; end).class_eval do
define_method(:backtrace) { mock_backtrace }
StandardError.new.tap do |e|
e.define_singleton_method(:backtrace) { mock_backtrace }
end
e
end
end

View file

@ -67,6 +67,28 @@ describe Pry do
end
end
describe "#last_exception=" do
before do
@pry = Pry.new binding: binding
@e = mock_exception "foo.rb:1"
end
it "returns an instance of Pry::LastException" do
@pry.last_exception = @e
should.satisfy { @pry.last_exception.pry? == true }
end
it "returns a frozen exception" do
@pry.last_exception = @e.freeze
@pry.last_exception.should.be.frozen?
end
it "returns an object who mirrors itself as the wrapped exception" do
@pry.last_exception = @e.freeze
@pry.last_exception.should.be.instance_of?(StandardError)
end
end
describe "open a Pry session on an object" do
describe "rep" do
before do