[Mechanize-users] WWW::Mechanize::Link.inspect needs some TLC

Eric Promislow ericp at activestate.com
Thu Jul 12 14:49:36 EDT 2007


The problem: users trying to debug Mechanize apps with Komodo
are finding the debugger times out once it's loaded a web
page.  They don't run into this in the ruby-debug debugger,
or running in normal mode.

The reason: Komodo's debugger is graphical, which means that
whenever it hits a breakpoint it automatically shows the
contents of each local variable.  It has a limit on how
much data it will retrieve, but it currently doesn't guard
an object from loading too much data (which it should, but
that's a separate bug).

I can duplicate the cause of this crash in ruby-debug as
well.  Here's a sample session, with this code:

require 'rubygems'
require 'mechanize'
require 'logger'

agent = WWW::Mechanize.new { |a| a.log = Logger.new("mech.log") }
agent.user_agent_alias = 'Mac Safari'
page = agent.get("http://www.google.com/")
search_form = page.forms.name("f").first
search_form.fields.name("q").value = "bratislava tournament"
search_results = agent.submit(search_form)
puts search_results.body

Given this ruby-debug session:

bugs $ rdebug mechanize01.rb
./mechanize01.rb:1 require 'rubygems'
(rdb:1) b 11
Set breakpoint 1 at mechanize01.rb:11
(rdb:1) c
Breakpoint 1 at mechanize01.rb:11
./mechanize01.rb:11 puts search_results.body
(rdb:1) p page.links[0].inspect.size
1521039
# That's way too big, since the page is a simple google results page:
(rdb:1) p page.body.size
3441

Using mechanize/inspect.rb:

(rdb:1) p page.links[0].pretty_inspect
"#<WWW::Mechanize::Link\n \"iGoogle\"\n \"/url?sa=p&pref=ig&pval=3&q=http://www.google.ca/ig%3Fhl%3Den&usg=AFQjCNH9TTed08sJL_DKraFsuSMDFvW1gw\">\n"
(rdb:1) p page.links[0].pretty_inspect.size
138
(rdb:1) p page.inspect.size
1480219
(rdb:1) p page.pretty_inspect.size
2172

With this change:
--- mechanize/inspect.rb~       2007-07-12 10:55:20.375000000 -0700
+++ mechanize/inspect.rb        2007-07-12 11:42:58.203125000 -0700
@@ -40,6 +40,7 @@
            }
          }
        end
+      alias :inspect  :pretty_inspect
      end

      class Link
@@ -49,6 +50,7 @@
            q.breakable; q.pp href
          }
        end
+      alias :inspect  :pretty_inspect
      end

      class Form
lib $

I get these much better results:

(rdb:1) p page.links[0].inspect.size
138
(rdb:1) p page.body.size
3441
(rdb:1) p page.inspect.size
2172

Is this patch reasonable or have I missed something?

Thanks,
Eric




More information about the Mechanize-users mailing list