banister / method_source

return the sourcecode for a method
MIT License
361 stars 43 forks source link

method_source can raise a EOFError. #11

Open ghost opened 12 years ago

ghost commented 12 years ago

To reproduce:


require 'rubygems'
require 'method_source'

block     = proc { puts "Hello, world!" }
original = block.source

File.open('/tmp/foo.txt', 'a') { |f| f.write(original) }

buf     = File.read '/tmp/foo.txt'
block = eval(buf, binding, __FILE__, __LINE__)

block.source

If you change the mode from a to w, it works fine. It looks like method_source loops past the end of the file (valid_expression? never returns true).

ghost commented 12 years ago

You'll need to run this twice or three times to trigger it.

rf- commented 12 years ago

Try changing the last line of your file to puts block.source. I'm not sure why it advances a line each time you run it, but the root issue in your example is that you're lying to eval about the location of the code.

ghost commented 12 years ago

Adjusting __LINE__ and __FILE__ in Kernel#eval, I still get the same error:


require 'rubygems'
require 'method_source'

block     = proc { puts "Hello, world!" }
original = block.source

File.open('/tmp/foo.txt', 'a') { |f| f.write(original) }
line = File.readlines('/tmp/foo.txt').size

buf     = File.read '/tmp/foo.txt'
block   = eval(buf, binding, '/tmp/foo.txt', line )

block.source
banister commented 12 years ago

It should be able to show the prompt source in this example, but it fails with an EOFError

90:   NAV_PROMPT = [
 91:                 proc do |_, level, pry|
 92:                   tree = pry.binding_stack.map { |b| Pry.view_clip(b.eval("self")) }.join " / "
 93:                   "[#{pry.input_array.size}] (pry) #{tree}: #{level}> "
 94:                 end,
 95:                 proc do |_, level, pry|
 96:                   tree = pry.binding_stack.map { |b| Pry.view_clip(b.eval("self")) }.join " / "
 97:                   "[#{pry.input_array.size}] (pry) #{tree}: #{level}* "
 98:                 end,
 99:                ]
100: 
101:   # Deal with the ^D key being pressed, different behaviour in
102:   # different cases:
103:   # 1) In an expression     - behave like `!` command   (clear input buffer)
104:   # 2) At top-level session - behave like `exit command (break out of repl loop)
105:   # 3) In a nested session  - behave like `cd ..`       (pop a binding)
106:   DEFAULT_CONTROL_D_HANDLER = proc do |eval_string, _pry_|
107:     if !eval_string.empty?
108:       # clear input buffer
109:       eval_string.replace("")
110:     elsif _pry_.binding_stack.one?
111:       # ^D at top-level breaks out of loop
112:       _pry_.binding_stack.clear
113:       throw(:breakout)
114:     else
115:       # otherwise just pops a binding
116:       _pry_.binding_stack.pop
117:     end
118:   end
[37] (pry) unknown: 0> Pry.config.prompt.first.source_location
=> ["/Users/john/.rvm/gems/ruby-1.9.2-p290/gems/pry-0.9.8pre4/lib/pry.rb", 91]
[38] (pry) unknown: 0> Pry.config.prompt.first.source
EOFError: end of file reached
from /Users/john/.rvm/gems/ruby-1.9.2-p290/gems/method_source-0.7.0/lib/method_source.rb:44:in `readline'
[39] (pry) unknown: 0>