Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

method_source can raise a EOFError. #11

Open
ghost opened this issue Jan 5, 2012 · 4 comments
Open

method_source can raise a EOFError. #11

ghost opened this issue Jan 5, 2012 · 4 comments

Comments

@ghost
Copy link

ghost commented Jan 5, 2012

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
Copy link
Author

ghost commented Jan 5, 2012

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

@rf-
Copy link
Collaborator

rf- commented Jan 6, 2012

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
Copy link
Author

ghost commented Jan 6, 2012

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
Copy link
Owner

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> 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants