Skip to content

Commit 451a07d

Browse files
committed
Fix converting a thread-local variable name into Symbol in Thread methods
1 parent 135ef31 commit 451a07d

13 files changed

+88
-51
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Compatibility:
3636
* Fix `Hash#to_h` called with a block and pass key and value to the block as separate arguments (#3607, @andrykonchin).
3737
* Fix `StringIO#initialize` and preserve initial string's encoding when mode is `w` so the initial string is truncated (#3599, @andrykonchin).
3838
* Fix `IO#{autoclose=,autoclose?}` and raise `IOError` when io is closed (@andrykonchin).
39+
* Fix `Thread#{thread_variable_get,thread_variable_set,thread_variable?,key?,[],[]=,fetch}` and convert a non-String/Symbol thread-local variable name to String using `#to_str` (@andrykonchin).
3940

4041
Performance:
4142

spec/ruby/core/thread/element_reference_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,17 @@
3737
t2["value"].should == 2
3838
end
3939

40+
it "converts a key that is neither String nor Symbol with #to_str" do
41+
key = mock('value')
42+
key.should_receive(:to_str).and_return('value')
43+
44+
th = Thread.new do
45+
Thread.current[:value] = 1
46+
end.join
47+
48+
th[key].should == 1
49+
end
50+
4051
it "raises exceptions on the wrong type of keys" do
4152
-> { Thread.current[nil] }.should raise_error(TypeError)
4253
-> { Thread.current[5] }.should raise_error(TypeError)

spec/ruby/core/thread/element_set_spec.rb

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,33 @@
1212
th.freeze
1313
-> {
1414
th[:foo] = "bar"
15-
}.should raise_error(FrozenError, /frozen/)
15+
}.should raise_error(FrozenError, "can't modify frozen thread locals")
1616
end.join
1717
end
1818

19+
it "accepts Strings and Symbols" do
20+
t1 = Thread.new do
21+
Thread.current[:value] = 1
22+
end.join
23+
t2 = Thread.new do
24+
Thread.current["value"] = 2
25+
end.join
26+
27+
t1[:value].should == 1
28+
t2[:value].should == 2
29+
end
30+
31+
it "converts a key that is neither String nor Symbol with #to_str" do
32+
key = mock('value')
33+
key.should_receive(:to_str).and_return('value')
34+
35+
th = Thread.new do
36+
Thread.current[key] = 1
37+
end.join
38+
39+
th[:value].should == 1
40+
end
41+
1942
it "raises exceptions on the wrong type of keys" do
2043
-> { Thread.current[nil] = true }.should raise_error(TypeError)
2144
-> { Thread.current[5] = true }.should raise_error(TypeError)

spec/ruby/core/thread/key_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@
1616
@th.key?(:stanley.to_s).should == false
1717
end
1818

19+
it "converts a key that is neither String nor Symbol with #to_str" do
20+
key = mock('key')
21+
key.should_receive(:to_str).and_return('oliver')
22+
23+
@th.key?(key).should == true
24+
end
25+
1926
it "raises exceptions on the wrong type of keys" do
2027
-> { Thread.current.key? nil }.should raise_error(TypeError)
2128
-> { Thread.current.key? 5 }.should raise_error(TypeError)

spec/ruby/core/thread/thread_variable_get_spec.rb

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,15 @@
4141
@t.thread_variable_get(:a).should be_nil
4242
end
4343

44-
it "does not raise a TypeError if the key is neither Symbol nor String, nor responds to #to_str" do
45-
@t.thread_variable_get(123).should be_nil
46-
end
47-
48-
it "does not try to convert the key with #to_sym" do
49-
key = mock('key')
50-
key.should_not_receive(:to_sym)
51-
@t.thread_variable_get(key).should be_nil
44+
ruby_bug "#20606", ""..."3.4" do
45+
it "raises a TypeError if the key is neither Symbol nor String, nor responds to #to_str" do
46+
-> { @t.thread_variable_get(123) }.should raise_error(TypeError, /123 is not a symbol/)
47+
end
48+
49+
it "does not try to convert the key with #to_sym" do
50+
key = mock('key')
51+
key.should_not_receive(:to_sym)
52+
-> { @t.thread_variable_get(key) }.should raise_error(TypeError)
53+
end
5254
end
5355
end

spec/ruby/core/thread/thread_variable_set_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@
5151
end
5252

5353
it "raises a TypeError if the key is neither Symbol nor String, nor responds to #to_str" do
54-
-> { @t.thread_variable_set(123, 1) }.should raise_error(TypeError, '123 is not a symbol')
54+
-> { @t.thread_variable_set(123, 1) }.should raise_error(TypeError, /123 is not a symbol/)
5555
end
5656

5757
it "does not try to convert the key with #to_sym" do
5858
key = mock('key')
5959
key.should_not_receive(:to_sym)
60-
-> { @t.thread_variable_set(key, 42) }.should raise_error(TypeError, "#{key.inspect} is not a symbol")
60+
-> { @t.thread_variable_set(key, 42) }.should raise_error(TypeError, /#{key.inspect} is not a symbol/)
6161
end
6262
end

spec/ruby/core/thread/thread_variable_spec.rb

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,15 @@
4141
@t.thread_variable?(:a).should be_false
4242
end
4343

44-
it "does not raise a TypeError if the key is neither Symbol nor String, nor responds to #to_str" do
45-
@t.thread_variable?(123).should be_false
46-
end
47-
48-
it "does not try to convert the key with #to_sym" do
49-
key = mock('key')
50-
key.should_not_receive(:to_sym)
51-
@t.thread_variable?(key).should be_false
44+
ruby_bug "#20606", ""..."3.4" do
45+
it "raises a TypeError if the key is neither Symbol nor String, nor responds to #to_str" do
46+
-> { @t.thread_variable?(123) }.should raise_error(TypeError, /123 is not a symbol/)
47+
end
48+
49+
it "does not try to convert the key with #to_sym" do
50+
key = mock('key')
51+
key.should_not_receive(:to_sym)
52+
-> { @t.thread_variable?(key) }.should raise_error(TypeError)
53+
end
5254
end
5355
end

spec/tags/core/thread/thread_variable_get_tags.txt

Lines changed: 0 additions & 3 deletions
This file was deleted.

spec/tags/core/thread/thread_variable_set_tags.txt

Lines changed: 0 additions & 5 deletions
This file was deleted.

spec/tags/core/thread/thread_variable_tags.txt

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)