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

Restore MAIN_CONTEXT correctly #937

Merged
merged 1 commit into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/irb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -992,6 +992,7 @@ def debug_readline(binding)
def run(conf = IRB.conf)
in_nested_session = !!conf[:MAIN_CONTEXT]
conf[:IRB_RC].call(context) if conf[:IRB_RC]
prev_context = conf[:MAIN_CONTEXT]
conf[:MAIN_CONTEXT] = context

save_history = !in_nested_session && conf[:SAVE_HISTORY] && context.io.support_history_saving?
Expand All @@ -1014,6 +1015,9 @@ def run(conf = IRB.conf)
eval_input
end
ensure
# Do not restore to nil. It will cause IRB crash when used with threads.
IRB.conf[:MAIN_CONTEXT] = prev_context if prev_context

RubyVM.keep_script_lines = keep_script_lines_backup if defined?(RubyVM.keep_script_lines)
trap("SIGINT", prev_trap)
conf[:AT_EXIT].each{|hook| hook.call}
Expand Down
20 changes: 20 additions & 0 deletions test/irb/test_irb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,26 @@ def test_empty_input_echoing_behaviour
end
end

class NestedBindingIrbTest < IntegrationTestCase
def test_current_context_restore
write_ruby <<~'RUBY'
binding.irb
RUBY

output = run_ruby_file do
type '$ctx = IRB.CurrentContext'
type 'binding.irb'
type 'p context_changed: IRB.CurrentContext != $ctx'
type 'exit'
type 'p context_restored: IRB.CurrentContext == $ctx'
type 'exit'
end

assert_include output, '{:context_changed=>true}'
assert_include output, '{:context_restored=>true}'
end
end

class IrbIOConfigurationTest < TestCase
Row = Struct.new(:content, :current_line_spaces, :new_line_spaces, :indent_level)

Expand Down
Loading