<commit_before>class Exception

  attr_writer :message
  attr_accessor :locations
  attr_accessor :parent

  def initialize(message = nil)
    @message = message
    @locations = nil
    @backtrace = nil
  end

  def backtrace
    if @backtrace
      if @backtrace.kind_of? Array
        return @backtrace
      end

      return @backtrace.to_mri
    end

    if @locations
      awesome_backtrace.to_mri
    else
      nil
    end
  end

  def awesome_backtrace
    @backtrace = Backtrace.backtrace(@locations)
  end

  def render(header="An exception occurred", io=STDOUT)
    io.puts header
    io.puts "    #{message} (#{self.class})"
    io.puts "\nBacktrace:"
    io.puts awesome_backtrace.show

    extra = @parent
    while extra
      io.puts "\nCaused by: #{extra.message} (#{extra.class})"
      io.puts "\nBacktrace:"
      io.puts extra.awesome_backtrace.show

      extra = extra.parent
    end

  end

  def set_backtrace(bt)
    @backtrace = bt
  end

  def set_context(ctx)
    if ctx.kind_of? Exception
      @parent = ctx
    else
      set_backtrace(ctx)
    end
  end

  def to_s
    @message || self.class.to_s
  end

  alias_method :message, :to_s
  alias_method :to_str, :to_s

  def inspect
    "#<#{self.class.name}: #{self.to_s}>"
  end

  class << self
    alias_method :exception, :new
  end

  def exception(message=nil)
    if message
      unless message.equal? self
        return self.class.new(message)
      end
    end

    self
  end

  def location
    [context.file.to_s, context.line]
  end

  # HACK
  def self.===(obj)
    return false if obj.kind_of? Rubinius::ThrownValue
    super
  end
end

##
# Primitive fails from opcode "send_primitive"

class PrimitiveFailure < Exception
end<commit_msg>Exception#exception is crazy. Fixes #168.<commit_after>class Exception

  attr_writer :message
  attr_accessor :locations
  attr_accessor :parent

  def initialize(message = nil)
    @message = message
    @locations = nil
    @backtrace = nil
  end

  # Needed to properly implement #exception, which must clone and call
  # #initialize again, BUT not a subclasses initialize.
  alias_method :__initialize__, :initialize

  def backtrace
    if @backtrace
      if @backtrace.kind_of? Array
        return @backtrace
      end

      return @backtrace.to_mri
    end

    if @locations
      awesome_backtrace.to_mri
    else
      nil
    end
  end

  def awesome_backtrace
    @backtrace = Backtrace.backtrace(@locations)
  end

  def render(header="An exception occurred", io=STDOUT)
    io.puts header
    io.puts "    #{message} (#{self.class})"
    io.puts "\nBacktrace:"
    io.puts awesome_backtrace.show

    extra = @parent
    while extra
      io.puts "\nCaused by: #{extra.message} (#{extra.class})"
      io.puts "\nBacktrace:"
      io.puts extra.awesome_backtrace.show

      extra = extra.parent
    end

  end

  def set_backtrace(bt)
    @backtrace = bt
  end

  def set_context(ctx)
    if ctx.kind_of? Exception
      @parent = ctx
    else
      set_backtrace(ctx)
    end
  end

  def to_s
    @message || self.class.to_s
  end

  alias_method :message, :to_s
  alias_method :to_str, :to_s

  def inspect
    "#<#{self.class.name}: #{self.to_s}>"
  end

  class << self
    alias_method :exception, :new
  end

  def exception(message=nil)
    if message
      unless message.equal? self
        # As strange as this might seem, this IS actually the protocol
        # that MRI implements for this. The explicit call to
        # Exception#initialize (via __initialize__) is exactly what MRI
        # does.
        e = clone
        e.__initialize__(message)
        return e
      end
    end

    self
  end

  def location
    [context.file.to_s, context.line]
  end

  # HACK
  def self.===(obj)
    return false if obj.kind_of? Rubinius::ThrownValue
    super
  end
end

##
# Primitive fails from opcode "send_primitive"

class PrimitiveFailure < Exception
end
{'fraction_non_alphanumeric': 0.08702290076335878, 'fraction_numerical': 0.0007633587786259542, 'mean_word_length': 2.5350719424460433, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 5, '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': '5738997', 'n_tokens_mistral': 1334, 'n_tokens_neox': 1284, 'n_words': 354}