Method: RSQL::EvalContext#safe_eval
- Defined in:
- lib/rsql/eval_context.rb
#safe_eval(content, results, stdout) ⇒ Object
Safely evaluate Ruby content within our context.
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/rsql/eval_context.rb', line 195 def safe_eval(content, results, stdout) @results = results # allow a simple reload to be called directly as it requires a # little looser safety valve... if 'reload' == content reload return end # same relaxed call to load too if m = content.match(/^\s*load\s+'(.+)'\s*$/) self.load(m[1]) return end # help out the poor user and fix up any describes # requested so they don't need to remember that it needs # to be a symbol passed in if m = content.match(/^\s*desc\s+([^:]\S+)\s*$/) content = "desc :#{m[1]}" end if stdout # capture stdout orig_stdout = $stdout $stdout = stdout end begin # in order to print out errors in a loaded script so # that we have file/line info, we need to rescue their # exceptions inside the evaluation th = Thread.new do eval('begin;' << content << %q{ rescue Exception => ex if @verbose $stderr.puts("#{ex.class}: #{ex.message}", ex.backtrace) else bt = [] ex.backtrace.each do |t| break if t.include?('bin/rsql') bt << t unless t.include?('lib/rsql/') || t.include?('(eval)') end $stderr.puts(ex.message.gsub(/\(eval\):\d+:/,''),bt) end end }) end value = th.value rescue Exception => ex $stderr.puts(ex..gsub(/\(eval\):\d+:/,'')) ensure $stdout = orig_stdout if stdout end return value end |