Skip to content

Commit

Permalink
Fallback to Reline when require 'readline' fails (#1076)
Browse files Browse the repository at this point in the history
Require readline may fail because it is a bundled gem in 3.5.0.dev.
  • Loading branch information
tompng authored Jan 27, 2025
1 parent 5623f0a commit 1ca7472
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
19 changes: 12 additions & 7 deletions lib/irb/input-method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,15 @@ def close
class ReadlineInputMethod < StdioInputMethod
class << self
def initialize_readline
require "readline"
rescue LoadError
else
include ::Readline
return if defined?(self::Readline)

begin
require 'readline'
const_set(:Readline, ::Readline)
rescue LoadError
const_set(:Readline, ::Reline)
end
const_set(:HISTORY, self::Readline::HISTORY)
end
end

Expand Down Expand Up @@ -216,8 +221,8 @@ def completion_info
def gets
Readline.input = @stdin
Readline.output = @stdout
if l = readline(@prompt, false)
HISTORY.push(l) if !l.empty?
if l = Readline.readline(@prompt, false)
Readline::HISTORY.push(l) if !l.empty?
@line[@line_no += 1] = l + "\n"
else
@eof = true
Expand All @@ -239,7 +244,7 @@ def prompting?

# For debug message
def inspect
readline_impl = (defined?(Reline) && Readline == Reline) ? 'Reline' : 'ext/readline'
readline_impl = Readline == ::Reline ? 'Reline' : 'ext/readline'
str = "ReadlineInputMethod with #{readline_impl} #{Readline::VERSION}"
inputrc_path = File.expand_path(ENV['INPUTRC'] || '~/.inputrc')
str += " and #{inputrc_path}" if File.exist?(inputrc_path)
Expand Down
8 changes: 7 additions & 1 deletion test/irb/test_history.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
# frozen_string_literal: false
require 'irb'
require 'readline'
require "tempfile"

require_relative "helper"

return if RUBY_PLATFORM.match?(/solaris|mswin|mingw/i)

module TestIRB
begin
require 'readline'
Readline = ::Readline
rescue LoadError
Readline = ::Reline
end

class HistoryTest < TestCase
def setup
@conf_backup = IRB.conf.dup
Expand Down

0 comments on commit 1ca7472

Please sign in to comment.