Top Level Namespace
Defined Under Namespace
Classes: EvalError, Object, SafeEval
Constant Summary
collapse
- DEFAULT_SAFE_LEVEL =
0
Instance Method Summary
collapse
Instance Method Details
56
57
58
59
60
|
# File 'lib/safe_eval.rb', line 56
def bt
raise 'foo'
rescue => exp
return exp.backtrace.join("\n")
end
|
#eval_safe_level ⇒ Object
4
5
6
|
# File 'lib/safe_eval.rb', line 4
def eval_safe_level
$eval_safe_level || DEFAULT_SAFE_LEVEL
end
|
#safe_eval(str, ops = {}) ⇒ Object
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/safe_eval.rb', line 41
def safe_eval(str,ops={})
if true
res = nil
seconds = ops[:timeout] || 5
Timeout::timeout(seconds) { res = safe_eval_thread(str,ops) }
res
else
safe_eval_fork(str,ops)
end
rescue(SecurityError) => exp
exp = exp.exception("Eval code \"#{str}\"\n#{exp.message}")
raise exp
end
|
#safe_eval_fork(str, ops) ⇒ Object
18
19
20
21
22
23
24
25
|
# File 'lib/safe_eval.rb', line 18
def safe_eval_fork(str,ops)
fork do
new_str = "$SAFE=#{eval_safe_level}; #{str}"
$res = res = eval(new_str)
puts "RES: #{res}"
end
Process.wait
end
|
#safe_eval_thread(str, ops = {}) ⇒ Object
27
28
29
30
31
32
33
34
35
36
|
# File 'lib/safe_eval.rb', line 27
def safe_eval_thread(str,ops={})
res = nil
level = ops[:safe] || eval_safe_level
Thread.new do
new_str = "$SAFE=#{level}; #{str}"
res = eval(new_str)
end.join
res
end
|