@@ -53,18 +53,21 @@ def initialize(workspace = nil, interactive = true, input_method = nil)
53
53
$stdout = STDOUT
54
54
end
55
55
56
- def output_value
56
+ def output_value ( omit = false )
57
57
# Suppress output if last_value is 'nil'
58
58
# Otherwise, when user types help, get ugly 'nil'
59
59
# after all output.
60
- super unless @context . last_value . nil?
60
+ super ( omit ) unless @context . last_value . nil?
61
61
end
62
62
63
- # Copied from irb.rb and overrides the rescue Exception block so the
63
+ # Copied from https://github.com/ruby/irb/blob/v1.4.2/lib/irb.rb
64
+ # We override the rescue Exception block so the
64
65
# Shell::exception_handler can deal with the exceptions.
65
66
def eval_input
67
+ exc = nil
68
+
66
69
@scanner . set_prompt do
67
- |ltype , indent , continue , line_no |
70
+ |ltype , indent , continue , line_no |
68
71
if ltype
69
72
f = @context . prompt_s
70
73
elsif continue
@@ -80,17 +83,19 @@ def eval_input
80
83
else
81
84
@context . io . prompt = p = ""
82
85
end
83
- if @context . auto_indent_mode
86
+ if @context . auto_indent_mode and ! @context . io . respond_to? ( :auto_indent )
84
87
unless ltype
85
- ind = prompt ( @context . prompt_i , ltype , indent , line_no ) [ /.*\z / ] . size +
88
+ prompt_i = @context . prompt_i . nil? ? "" : @context . prompt_i
89
+ ind = prompt ( prompt_i , ltype , indent , line_no ) [ /.*\z / ] . size +
86
90
indent * 2 - p . size
87
91
ind += 2 if continue
88
92
@context . io . prompt = p + " " * ind if ind > 0
89
93
end
90
94
end
95
+ @context . io . prompt
91
96
end
92
97
93
- @scanner . set_input ( @context . io ) do
98
+ @scanner . set_input ( @context . io , context : @context ) do
94
99
signal_status ( :IN_INPUT ) do
95
100
if l = @context . io . gets
96
101
print l if @context . verbose?
@@ -101,24 +106,51 @@ def eval_input
101
106
printf "Use \" exit\" to leave %s\n " , @context . ap_name
102
107
end
103
108
else
104
- print "\n "
109
+ print "\n " if @context . prompting?
105
110
end
106
111
end
107
112
l
108
113
end
109
114
end
110
115
116
+ @scanner . set_auto_indent ( @context ) if @context . auto_indent_mode
117
+
111
118
@scanner . each_top_level_statement do |line , line_no |
112
119
signal_status ( :IN_EVAL ) do
113
120
begin
114
- line . untaint
115
- @context . evaluate ( line , line_no )
116
- output_value if @context . echo?
117
- exc = nil
121
+ line . untaint if RUBY_VERSION < '2.7'
122
+ if IRB . conf [ :MEASURE ] && IRB . conf [ :MEASURE_CALLBACKS ] . empty?
123
+ IRB . set_measure_callback
124
+ end
125
+ if IRB . conf [ :MEASURE ] && !IRB . conf [ :MEASURE_CALLBACKS ] . empty?
126
+ result = nil
127
+ last_proc = proc { result = @context . evaluate ( line , line_no , exception : exc ) }
128
+ IRB . conf [ :MEASURE_CALLBACKS ] . inject ( last_proc ) { |chain , item |
129
+ _name , callback , arg = item
130
+ proc {
131
+ callback . ( @context , line , line_no , arg , exception : exc ) do
132
+ chain . call
133
+ end
134
+ }
135
+ } . call
136
+ @context . set_last_value ( result )
137
+ else
138
+ @context . evaluate ( line , line_no , exception : exc )
139
+ end
140
+ if @context . echo?
141
+ if assignment_expression? ( line )
142
+ if @context . echo_on_assignment?
143
+ output_value ( @context . echo_on_assignment? == :truncate )
144
+ end
145
+ else
146
+ output_value
147
+ end
148
+ end
118
149
rescue Interrupt => exc
119
150
rescue SystemExit , SignalException
120
151
raise
121
152
rescue SyntaxError => exc
153
+ # HBASE-27726: Ignore SyntaxError to prevent exiting Shell on unexpected syntax.
122
154
raise exc unless @interactive
123
155
rescue NameError => exc
124
156
raise exc unless @interactive
@@ -128,43 +160,13 @@ def eval_input
128
160
# This modifies this copied method from JRuby so that the HBase shell can
129
161
# manage the exception and set a proper exit code on the process.
130
162
raise exc
163
+ else
164
+ exc = nil
165
+ next
131
166
end
132
- if exc
133
- if exc . backtrace && exc . backtrace [ 0 ] =~ /irb(2)?(\/ .*|-.*|\. rb)?:/ && exc . class . to_s !~ /^IRB/ &&
134
- !( SyntaxError === exc )
135
- irb_bug = true
136
- else
137
- irb_bug = false
138
- end
139
-
140
- messages = [ ]
141
- lasts = [ ]
142
- levels = 0
143
- if exc . backtrace
144
- count = 0
145
- exc . backtrace . each do |m |
146
- m = @context . workspace . filter_backtrace ( m ) or next unless irb_bug
147
- m = sprintf ( "%9d: from %s" , ( count += 1 ) , m )
148
- if messages . size < @context . back_trace_limit
149
- messages . push ( m )
150
- elsif lasts . size < @context . back_trace_limit
151
- lasts . push ( m ) . shift
152
- levels += 1
153
- end
154
- end
155
- end
156
- attr = STDOUT . tty? ? ATTR_TTY : ATTR_PLAIN
157
- print "#{ attr [ 1 ] } Traceback#{ attr [ ] } (most recent call last):\n "
158
- unless lasts . empty?
159
- puts lasts . reverse
160
- printf "... %d levels...\n " , levels if levels > 0
161
- end
162
- puts messages . reverse
163
- messages = exc . to_s . split ( /\n / )
164
- print "#{ attr [ 1 ] } #{ exc . class } (#{ attr [ 4 ] } #{ messages . shift } #{ attr [ 0 , 1 ] } )#{ attr [ ] } \n "
165
- puts messages . map { |s | "#{ attr [ 1 ] } #{ s } #{ attr [ ] } \n " }
166
- print "Maybe IRB bug!\n " if irb_bug
167
- end
167
+ handle_exception ( exc )
168
+ @context . workspace . local_variable_set ( :_ , exc )
169
+ exc = nil
168
170
end
169
171
end
170
172
end
0 commit comments