multitrap 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +23 -0
- data/.rspec +1 -0
- data/CHANGELOG.md +6 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +98 -0
- data/Rakefile +2 -0
- data/lib/multitrap.rb +85 -0
- data/lib/multitrap/version.rb +3 -0
- data/multitrap.gemspec +24 -0
- data/spec/multitrap_spec.rb +102 -0
- data/spec/spec_helper.rb +3 -0
- metadata +115 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f34b3f14dfb6b3271c98561b1a9dd6bde507947a
|
4
|
+
data.tar.gz: df07859ab58e19de64a97b6ab417b408476e55ca
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 46b7369e7ce17076171ca335951f16f1b4d0f94d1ff5b6e641542d520ea512f7d7d71aa54de76c31f0c5228ac85f93e31f64319df4b1f76263bc1c250dbadb20
|
7
|
+
data.tar.gz: f2f5d7b4abfb0ea065967e7910f01d5e9d7c09838243c26371285d642b3bf0fbb40720156a9d394d0d480708171b7ffd8e76ca79bb63dc5989a50fa72a60df2a
|
data/.gitignore
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
23
|
+
*~
|
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Kyrylo Silin
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
# Multitrap
|
2
|
+
|
3
|
+
The idea is to be able to attach multiple callbacks for `Signal.trap`.
|
4
|
+
|
5
|
+
```ruby
|
6
|
+
require 'multitrap'
|
7
|
+
|
8
|
+
trap('INT') do
|
9
|
+
puts 111
|
10
|
+
end
|
11
|
+
|
12
|
+
trap('INT') do
|
13
|
+
puts 222
|
14
|
+
end
|
15
|
+
|
16
|
+
Process.kill('INT', $$)
|
17
|
+
|
18
|
+
# Outputs:
|
19
|
+
# 111
|
20
|
+
# 222
|
21
|
+
```
|
22
|
+
|
23
|
+
Currently, the library doesn't achieve the goal. I want to make the gem
|
24
|
+
unobtrusive: the user installs it and it "just works". However, it's not
|
25
|
+
possible due to a limitation of MRI. If you earlier had defined traps and then
|
26
|
+
required Multitrap, it would discard your previously defined callbacks.
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
trap('INT') do
|
30
|
+
puts 111
|
31
|
+
end
|
32
|
+
|
33
|
+
require 'multitrap'
|
34
|
+
|
35
|
+
trap('INT') do
|
36
|
+
puts 222
|
37
|
+
end
|
38
|
+
|
39
|
+
trap('INT') do
|
40
|
+
puts 333
|
41
|
+
end
|
42
|
+
|
43
|
+
Process.kill('INT', $$)
|
44
|
+
|
45
|
+
# Outputs:
|
46
|
+
# 222
|
47
|
+
# 333
|
48
|
+
```
|
49
|
+
|
50
|
+
However, it's possible to bypass this limitation. Just redefine your trap
|
51
|
+
when Multitrap is loaded.
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
trap_proc = trap('INT') do
|
55
|
+
puts 111
|
56
|
+
end
|
57
|
+
|
58
|
+
require 'multitrap'
|
59
|
+
|
60
|
+
trap('INT', trap_proc)
|
61
|
+
|
62
|
+
trap('INT') do
|
63
|
+
puts 222
|
64
|
+
end
|
65
|
+
|
66
|
+
Process.kill('INT', $$)
|
67
|
+
|
68
|
+
# Outputs:
|
69
|
+
# 111
|
70
|
+
# 222
|
71
|
+
# 333
|
72
|
+
```
|
73
|
+
|
74
|
+
## Installation
|
75
|
+
|
76
|
+
Add this line to your application's Gemfile:
|
77
|
+
|
78
|
+
gem 'multitrap'
|
79
|
+
|
80
|
+
And then execute:
|
81
|
+
|
82
|
+
$ bundle
|
83
|
+
|
84
|
+
Or install it yourself as:
|
85
|
+
|
86
|
+
$ gem install multitrap
|
87
|
+
|
88
|
+
## Limitations
|
89
|
+
|
90
|
+
### Rubies
|
91
|
+
|
92
|
+
* MRI 2.1
|
93
|
+
|
94
|
+
### Known bugs
|
95
|
+
|
96
|
+
* recursive traps are broken (fixable)
|
97
|
+
* overwrites traps defined before the loading of the library ([not fixable,
|
98
|
+
requires patching MRI](https://bugs.ruby-lang.org/issues/10211))
|
data/Rakefile
ADDED
data/lib/multitrap.rb
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
require 'multitrap/version'
|
2
|
+
require 'thread'
|
3
|
+
|
4
|
+
module Multitrap
|
5
|
+
class Trap
|
6
|
+
RESERVED_SIGNALS = {
|
7
|
+
'BUS' => 10,
|
8
|
+
'SEGV' => 11,
|
9
|
+
'ILL' => 4,
|
10
|
+
'FPE' => 8,
|
11
|
+
'VTALRM' => 26
|
12
|
+
}
|
13
|
+
|
14
|
+
def self.trap(sig, prc, old_trap, &block)
|
15
|
+
@multitrap ||= self.new(old_trap)
|
16
|
+
@multitrap.add_trap(sig, prc, &block)
|
17
|
+
end
|
18
|
+
|
19
|
+
def initialize(old_trap)
|
20
|
+
@old_trap = old_trap
|
21
|
+
@traps = {}
|
22
|
+
@mutex = Mutex.new
|
23
|
+
end
|
24
|
+
|
25
|
+
def recursion?
|
26
|
+
frame = caller.find do |f|
|
27
|
+
f =~ %r{multitrap/lib/multitrap\.rb.+`block in add_trap'}
|
28
|
+
end
|
29
|
+
|
30
|
+
true if frame
|
31
|
+
end
|
32
|
+
|
33
|
+
def add_trap(sig, prc, &block)
|
34
|
+
if RESERVED_SIGNALS.key?(sig)
|
35
|
+
raise ArgumentError, "can't trap reserved signal SIG#{sig}"
|
36
|
+
end
|
37
|
+
|
38
|
+
unless Signal.list.key?(sig)
|
39
|
+
raise ArgumentError, "unsupported signal SIG#{sig}"
|
40
|
+
end
|
41
|
+
|
42
|
+
prc ||= block
|
43
|
+
|
44
|
+
if prc.nil?
|
45
|
+
raise ArgumentError, "tried to create Proc object without a block"
|
46
|
+
end
|
47
|
+
|
48
|
+
@traps[sig] ||= []
|
49
|
+
|
50
|
+
@mutex.synchronize do
|
51
|
+
if recursion?
|
52
|
+
@traps[sig].pop
|
53
|
+
else
|
54
|
+
@traps[sig].push(prc || block)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
@old_trap.call(sig) do
|
59
|
+
@traps[sig].each do |trap_handler|
|
60
|
+
trap_handler.call(Signal.list[sig])
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
@traps
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
module Signal
|
70
|
+
class << self
|
71
|
+
alias_method :old_trap, :trap
|
72
|
+
protected :old_trap
|
73
|
+
|
74
|
+
def trap(sig, prc = nil, &block)
|
75
|
+
Multitrap::Trap.trap(sig, prc, method(:old_trap), &block)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
module Kernel
|
81
|
+
def trap(sig, prc = nil, &block)
|
82
|
+
Signal.trap(sig, prc, &block)
|
83
|
+
end
|
84
|
+
module_function :trap
|
85
|
+
end
|
data/multitrap.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'multitrap/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "multitrap"
|
8
|
+
spec.version = Multitrap::VERSION
|
9
|
+
spec.authors = ["Kyrylo Silin"]
|
10
|
+
spec.email = ["silin@kyrylo.org"]
|
11
|
+
spec.summary = %q{Allows Signal.trap to have multiple callbacks}
|
12
|
+
spec.homepage = ""
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
21
|
+
spec.add_development_dependency "rake"
|
22
|
+
spec.add_development_dependency "rspec"
|
23
|
+
spec.add_development_dependency "pry"
|
24
|
+
end
|
@@ -0,0 +1,102 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Multitrap::Trap do
|
4
|
+
describe "#trap" do
|
5
|
+
describe "recursive" do
|
6
|
+
it "unwinds the stack" do
|
7
|
+
number = nil
|
8
|
+
|
9
|
+
trap('INT') do
|
10
|
+
trap('INT') do
|
11
|
+
number = 42
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
Process.kill('INT', $$)
|
16
|
+
expect(number).to be_nil
|
17
|
+
|
18
|
+
Process.kill('INT', $$)
|
19
|
+
expect(number).to eq(42)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
it "adds multiple callbacks" do
|
24
|
+
shared = []
|
25
|
+
|
26
|
+
3.times do |i|
|
27
|
+
trap('INT') { shared << i }
|
28
|
+
end
|
29
|
+
|
30
|
+
Process.kill('INT', $$)
|
31
|
+
|
32
|
+
expect(shared).to eq([0, 1, 2])
|
33
|
+
end
|
34
|
+
|
35
|
+
it "supports proc syntax" do
|
36
|
+
shared = []
|
37
|
+
|
38
|
+
3.times do |i|
|
39
|
+
trap('INT', proc { shared << i })
|
40
|
+
end
|
41
|
+
|
42
|
+
Process.kill('INT', $$)
|
43
|
+
|
44
|
+
expect(shared).to eq([0, 1, 2])
|
45
|
+
end
|
46
|
+
|
47
|
+
it "ignores block if proc is given" do
|
48
|
+
shared = []
|
49
|
+
|
50
|
+
3.times do |i|
|
51
|
+
trap('INT', proc { shared << i }) do
|
52
|
+
shared << i+100
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
Process.kill('INT', $$)
|
57
|
+
|
58
|
+
expect(shared).to eq([0, 1, 2])
|
59
|
+
end
|
60
|
+
|
61
|
+
it "binds to multiple signals" do
|
62
|
+
shared_int = []
|
63
|
+
shared_info = []
|
64
|
+
|
65
|
+
3.times do |i|
|
66
|
+
trap('INT') { shared_int << i }
|
67
|
+
end
|
68
|
+
|
69
|
+
3.times do |i|
|
70
|
+
trap('INFO') { shared_info << i+100 }
|
71
|
+
end
|
72
|
+
|
73
|
+
Process.kill('INT', $$)
|
74
|
+
Process.kill('INFO', $$)
|
75
|
+
|
76
|
+
expect(shared_int).to eq([0, 1, 2])
|
77
|
+
expect(shared_info).to eq([100, 101, 102])
|
78
|
+
end
|
79
|
+
|
80
|
+
it "yields signal's number" do
|
81
|
+
number = nil
|
82
|
+
|
83
|
+
trap('INT') { |signo| number = signo }
|
84
|
+
|
85
|
+
Process.kill('INT', $$)
|
86
|
+
|
87
|
+
expect(number).to eq(2)
|
88
|
+
end
|
89
|
+
|
90
|
+
it "raises ArgumentError if signal doesn't exist" do
|
91
|
+
expect{
|
92
|
+
trap('DONUTS') { }
|
93
|
+
}.to raise_error(ArgumentError, /unsupported signal SIGDONUTS/)
|
94
|
+
end
|
95
|
+
|
96
|
+
it "raises ArgumentError if signal is reserved" do
|
97
|
+
expect{
|
98
|
+
trap('ILL') {}
|
99
|
+
}.to raise_error(ArgumentError, /can't trap reserved signal SIGILL/)
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: multitrap
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kyrylo Silin
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-09-08 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- silin@kyrylo.org
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- CHANGELOG.md
|
79
|
+
- Gemfile
|
80
|
+
- LICENSE.txt
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- lib/multitrap.rb
|
84
|
+
- lib/multitrap/version.rb
|
85
|
+
- multitrap.gemspec
|
86
|
+
- spec/multitrap_spec.rb
|
87
|
+
- spec/spec_helper.rb
|
88
|
+
homepage: ''
|
89
|
+
licenses:
|
90
|
+
- MIT
|
91
|
+
metadata: {}
|
92
|
+
post_install_message:
|
93
|
+
rdoc_options: []
|
94
|
+
require_paths:
|
95
|
+
- lib
|
96
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
requirements: []
|
107
|
+
rubyforge_project:
|
108
|
+
rubygems_version: 2.2.2
|
109
|
+
signing_key:
|
110
|
+
specification_version: 4
|
111
|
+
summary: Allows Signal.trap to have multiple callbacks
|
112
|
+
test_files:
|
113
|
+
- spec/multitrap_spec.rb
|
114
|
+
- spec/spec_helper.rb
|
115
|
+
has_rdoc:
|