<commit_before>require 'timeout'

def debug_read_cmd(io,wait)
  begin
    if wait
      if ($_cmd !~/[\n\r]/)
        $_cmd << io.readpartial(4096)
      end
    else
      $_cmd << io.read_nonblock(4096)
    end
  rescue
    # puts $!.inspect
  end
end

def execute_cmd(cmd, include_code)
  puts "[Debugger] Executing: #{cmd}"
  result = ""
  begin
    result = eval(cmd, $_binding).inspect
  rescue
    result = "ERROR: #{$!}"
  end
  
  result = result.gsub(/\n/,"\\n")
  cmd = cmd.sub(/[\n\r]+$/, '').gsub(/\n/,"\\n").gsub(/:/,'#')
  $_s.write("EV" + (include_code ? "L:#{cmd}:" : ':') + result + "\n")
end

def get_variables(scope)
  if (scope =~ /^GVARS/)
   cmd = "global_variables"
   prefix = ""
   vartype = "G"
  elsif (scope =~ /^LVARS/)
   cmd = "local_variables"
   prefix = ""
   vartype = "L"
  elsif (scope =~ /^CVARS/)
   if $_classname =~ /^\s*$/
     return
   end
   cmd = "class_variables"
   prefix = "#{$_classname}."
   vartype = "C"
  elsif (scope =~ /^IVARS/)
   if ($_classname =~ /^\s*$/) or ($_methodname =~ /^\s*$/)
     return
   end
   cmd = "instance_variables"
   prefix = "self."
   vartype = "I"<commit_msg>fixed error handling in evaluation of Ruby code in debugger extension<commit_after>require 'uri'
require 'timeout'

def debug_read_cmd(io,wait)
  begin
    if wait
      if ($_cmd !~/[\n\r]/)
        $_cmd << io.readpartial(4096)
      end
    else
      $_cmd << io.read_nonblock(4096)
    end
  rescue
    # puts $!.inspect
  end
end

def execute_cmd(cmd, include_code)
  puts "[Debugger] Executing: #{cmd}"
  result = ""
  begin
    result = eval(cmd, $_binding).inspect
  rescue Exception => exc
    result = "ERROR: #{$!}"
  end
  
  result = result.gsub(/\n/,"\\n")
  cmd = URI.escape(cmd.sub(/[\n\r]+$/, ''), Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
  $_s.write("EV" + (include_code ? "L:#{cmd}:" : ':') + result + "\n")
end

def get_variables(scope)
  if (scope =~ /^GVARS/)
   cmd = "global_variables"
   prefix = ""
   vartype = "G"
  elsif (scope =~ /^LVARS/)
   cmd = "local_variables"
   prefix = ""
   vartype = "L"
  elsif (scope =~ /^CVARS/)
   if $_classname =~ /^\s*$/
     return
   end
   cmd = "class_variables"
   prefix = "#{$_classname}."
   vartype = "C"
  elsif (scope =~ /^IVARS/)
   if ($_classname =~ /^\s*$/) or ($_methodname =~ /^\s*$/)
     return
   end
   cmd = "instance_variables"
   prefix = "self."
   vartype = "I"
{'fraction_non_alphanumeric': 0.20149875104079934, 'fraction_numerical': 0.006661115736885929, 'mean_word_length': 2.6189759036144578, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '802751', 'n_tokens_mistral': 972, 'n_tokens_neox': 947, 'n_words': 209}