Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/alt/ruby18/lib64/ruby/1.8
File: timeout.rb
#--
[0] Fix | Delete
# = timeout.rb
[1] Fix | Delete
#
[2] Fix | Delete
# execution timeout
[3] Fix | Delete
#
[4] Fix | Delete
# = Copyright
[5] Fix | Delete
#
[6] Fix | Delete
# Copyright:: (C) 2000 Network Applied Communication Laboratory, Inc.
[7] Fix | Delete
# Copyright:: (C) 2000 Information-technology Promotion Agency, Japan
[8] Fix | Delete
#
[9] Fix | Delete
#++
[10] Fix | Delete
#
[11] Fix | Delete
# = Description
[12] Fix | Delete
#
[13] Fix | Delete
# A way of performing a potentially long-running operation in a thread, and
[14] Fix | Delete
# terminating it's execution if it hasn't finished within fixed amount of
[15] Fix | Delete
# time.
[16] Fix | Delete
#
[17] Fix | Delete
# Previous versions of timeout didn't use a module for namespace. This version
[18] Fix | Delete
# provides both Timeout.timeout, and a backwards-compatible #timeout.
[19] Fix | Delete
#
[20] Fix | Delete
# = Synopsis
[21] Fix | Delete
#
[22] Fix | Delete
# require 'timeout'
[23] Fix | Delete
# status = Timeout::timeout(5) {
[24] Fix | Delete
# # Something that should be interrupted if it takes too much time...
[25] Fix | Delete
# }
[26] Fix | Delete
#
[27] Fix | Delete
[28] Fix | Delete
module Timeout
[29] Fix | Delete
[30] Fix | Delete
##
[31] Fix | Delete
# Raised by Timeout#timeout when the block times out.
[32] Fix | Delete
[33] Fix | Delete
class Error < Interrupt
[34] Fix | Delete
end
[35] Fix | Delete
class ExitException < ::Exception # :nodoc:
[36] Fix | Delete
end
[37] Fix | Delete
[38] Fix | Delete
THIS_FILE = /\A#{Regexp.quote(__FILE__)}:/o
[39] Fix | Delete
CALLER_OFFSET = ((c = caller[0]) && THIS_FILE =~ c) ? 1 : 0
[40] Fix | Delete
[41] Fix | Delete
##
[42] Fix | Delete
# Executes the method's block. If the block execution terminates before +sec+
[43] Fix | Delete
# seconds has passed, it returns true. If not, it terminates the execution
[44] Fix | Delete
# and raises +exception+ (which defaults to Timeout::Error).
[45] Fix | Delete
#
[46] Fix | Delete
# Note that this is both a method of module Timeout, so you can 'include
[47] Fix | Delete
# Timeout' into your classes so they have a #timeout method, as well as a
[48] Fix | Delete
# module method, so you can call it directly as Timeout.timeout().
[49] Fix | Delete
[50] Fix | Delete
def timeout(sec, klass = nil)
[51] Fix | Delete
return yield if sec == nil or sec.zero?
[52] Fix | Delete
raise ThreadError, "timeout within critical session" if Thread.critical
[53] Fix | Delete
exception = klass || Class.new(ExitException)
[54] Fix | Delete
begin
[55] Fix | Delete
x = Thread.current
[56] Fix | Delete
y = Thread.start {
[57] Fix | Delete
begin
[58] Fix | Delete
sleep sec
[59] Fix | Delete
rescue => e
[60] Fix | Delete
x.raise e
[61] Fix | Delete
else
[62] Fix | Delete
x.raise exception, "execution expired" if x.alive?
[63] Fix | Delete
end
[64] Fix | Delete
}
[65] Fix | Delete
yield sec
[66] Fix | Delete
# return true
[67] Fix | Delete
rescue exception => e
[68] Fix | Delete
rej = /\A#{Regexp.quote(__FILE__)}:#{__LINE__-4}\z/o
[69] Fix | Delete
(bt = e.backtrace).reject! {|m| rej =~ m}
[70] Fix | Delete
level = -caller(CALLER_OFFSET).size
[71] Fix | Delete
while THIS_FILE =~ bt[level]
[72] Fix | Delete
bt.delete_at(level)
[73] Fix | Delete
level += 1
[74] Fix | Delete
end
[75] Fix | Delete
raise if klass # if exception class is specified, it
[76] Fix | Delete
# would be expected outside.
[77] Fix | Delete
raise Error, e.message, e.backtrace
[78] Fix | Delete
ensure
[79] Fix | Delete
if y and y.alive?
[80] Fix | Delete
y.kill
[81] Fix | Delete
y.join # make sure y is dead.
[82] Fix | Delete
end
[83] Fix | Delete
end
[84] Fix | Delete
end
[85] Fix | Delete
[86] Fix | Delete
module_function :timeout
[87] Fix | Delete
[88] Fix | Delete
end
[89] Fix | Delete
[90] Fix | Delete
##
[91] Fix | Delete
# Identical to:
[92] Fix | Delete
#
[93] Fix | Delete
# Timeout::timeout(n, e, &block).
[94] Fix | Delete
#
[95] Fix | Delete
# Defined for backwards compatibility with earlier versions of timeout.rb, see
[96] Fix | Delete
# Timeout#timeout.
[97] Fix | Delete
[98] Fix | Delete
def timeout(n, e = nil, &block) # :nodoc:
[99] Fix | Delete
Timeout::timeout(n, e, &block)
[100] Fix | Delete
end
[101] Fix | Delete
[102] Fix | Delete
##
[103] Fix | Delete
# Another name for Timeout::Error, defined for backwards compatibility with
[104] Fix | Delete
# earlier versions of timeout.rb.
[105] Fix | Delete
[106] Fix | Delete
TimeoutError = Timeout::Error # :nodoc:
[107] Fix | Delete
[108] Fix | Delete
if __FILE__ == $0
[109] Fix | Delete
p timeout(5) {
[110] Fix | Delete
45
[111] Fix | Delete
}
[112] Fix | Delete
p timeout(5, TimeoutError) {
[113] Fix | Delete
45
[114] Fix | Delete
}
[115] Fix | Delete
p timeout(nil) {
[116] Fix | Delete
54
[117] Fix | Delete
}
[118] Fix | Delete
p timeout(0) {
[119] Fix | Delete
54
[120] Fix | Delete
}
[121] Fix | Delete
p timeout(5) {
[122] Fix | Delete
loop {
[123] Fix | Delete
p 10
[124] Fix | Delete
sleep 1
[125] Fix | Delete
}
[126] Fix | Delete
}
[127] Fix | Delete
end
[128] Fix | Delete
[129] Fix | Delete
[130] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function