@@ -53,18 +53,21 @@ def initialize(workspace = nil, interactive = true, input_method = nil)
5353 $stdout = STDOUT
5454 end
5555
56- def output_value
56+ def output_value ( omit = false )
5757 # Suppress output if last_value is 'nil'
5858 # Otherwise, when user types help, get ugly 'nil'
5959 # after all output.
60- super unless @context . last_value . nil?
60+ super ( omit ) unless @context . last_value . nil?
6161 end
6262
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
6465 # Shell::exception_handler can deal with the exceptions.
6566 def eval_input
67+ exc = nil
68+
6669 @scanner . set_prompt do
67- |ltype , indent , continue , line_no |
70+ |ltype , indent , continue , line_no |
6871 if ltype
6972 f = @context . prompt_s
7073 elsif continue
@@ -80,17 +83,19 @@ def eval_input
8083 else
8184 @context . io . prompt = p = ""
8285 end
83- if @context . auto_indent_mode
86+ if @context . auto_indent_mode and ! @context . io . respond_to? ( :auto_indent )
8487 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 +
8690 indent * 2 - p . size
8791 ind += 2 if continue
8892 @context . io . prompt = p + " " * ind if ind > 0
8993 end
9094 end
95+ @context . io . prompt
9196 end
9297
93- @scanner . set_input ( @context . io ) do
98+ @scanner . set_input ( @context . io , context : @context ) do
9499 signal_status ( :IN_INPUT ) do
95100 if l = @context . io . gets
96101 print l if @context . verbose?
@@ -101,24 +106,51 @@ def eval_input
101106 printf "Use \" exit\" to leave %s\n " , @context . ap_name
102107 end
103108 else
104- print "\n "
109+ print "\n " if @context . prompting?
105110 end
106111 end
107112 l
108113 end
109114 end
110115
116+ @scanner . set_auto_indent ( @context ) if @context . auto_indent_mode
117+
111118 @scanner . each_top_level_statement do |line , line_no |
112119 signal_status ( :IN_EVAL ) do
113120 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
118149 rescue Interrupt => exc
119150 rescue SystemExit , SignalException
120151 raise
121152 rescue SyntaxError => exc
153+ # HBASE-27726: Ignore SyntaxError to prevent exiting Shell on unexpected syntax.
122154 raise exc unless @interactive
123155 rescue NameError => exc
124156 raise exc unless @interactive
@@ -128,43 +160,13 @@ def eval_input
128160 # This modifies this copied method from JRuby so that the HBase shell can
129161 # manage the exception and set a proper exit code on the process.
130162 raise exc
163+ else
164+ exc = nil
165+ next
131166 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
168170 end
169171 end
170172 end
0 commit comments