From: Kouhei S. <nu...@co...> - 2017-11-09 14:15:50
|
Kouhei Sutou 2017-11-09 23:13:22 +0900 (Thu, 09 Nov 2017) New Revision: 514c9e274dd7b03711102629c9d5962b8f23e7be https://github.com/ruby-gnome2/ruby-gnome2/commit/514c9e274dd7b03711102629c9d5962b8f23e7be Message: gio2: support Gio::InputStream#read_all GitHub: fix #1110 Reported by Paul van Tilburg. Thanks!!! read() uses #read_all to improve compatibility with Ruby's #read. Modified files: gio2/lib/gio2/input-stream.rb gio2/lib/gio2/loader.rb gio2/test/test-input-stream.rb Modified: gio2/lib/gio2/input-stream.rb (+17 -1) =================================================================== --- gio2/lib/gio2/input-stream.rb 2017-11-08 09:10:37 +0900 (e5ba19fcd) +++ gio2/lib/gio2/input-stream.rb 2017-11-09 23:13:22 +0900 (1042c4574) @@ -23,7 +23,7 @@ module Gio buffer_size = 8192 buffer = " ".force_encoding("ASCII-8BIT") * buffer_size loop do - read_bytes = read_raw_compatible(buffer) + _, read_bytes = read_all_raw_compatible(buffer) all << buffer.byteslice(0, read_bytes) break if read_bytes != buffer_size end @@ -36,6 +36,14 @@ module Gio end end + alias_method :read_all_raw, :read_all + def read_all(size) + buffer = " " * size + _, read_bytes = read_all_raw_compatible(buffer) + buffer.replace(buffer.byteslice(0, read_bytes)) + buffer + end + private def read_raw_compatible(buffer) if (GLib::VERSION <=> [2, 36, 0]) >= 0 @@ -44,5 +52,13 @@ module Gio read_raw(buffer, buffer.bytesize) end end + + def read_all_raw_compatible(buffer) + if (GLib::VERSION <=> [2, 36, 0]) >= 0 + read_all_raw(buffer) + else + read_all_raw(buffer, buffer.bytesize) + end + end end end Modified: gio2/lib/gio2/loader.rb (+14 -0) =================================================================== --- gio2/lib/gio2/loader.rb 2017-11-08 09:10:37 +0900 (374f78f02) +++ gio2/lib/gio2/loader.rb 2017-11-09 23:13:22 +0900 (f7bdf0d33) @@ -187,5 +187,19 @@ module Gio def error_parent_class(info) Error end + + def should_unlock_gvl?(function_info, klass) + case klass.name + when "Gio::InputStream" + case function_info.name + when "read", "read_all" + true + else + false + end + else + false + end + end end end Modified: gio2/test/test-input-stream.rb (+28 -4) =================================================================== --- gio2/test/test-input-stream.rb 2017-11-08 09:10:37 +0900 (8ca385656) +++ gio2/test/test-input-stream.rb 2017-11-09 23:13:22 +0900 (065cd59f9) @@ -26,11 +26,35 @@ class TestInputStream < Test::Unit::TestCase teardown_socket_client end - def test_read - data = "Hello\n" + sub_test_case("#read") do + def test_with_size + data = "Hello\n" + client = @server.accept + client.write(data) + client.flush + assert_equal("Hell", @stream.read(4)) + end + + def test_without_size + data = "Hello\n" + client = @server.accept + client.write(data) + client.flush + client.close + assert_equal(data, @stream.read) + end + end + + def test_read_all client = @server.accept - client.write(data) + client.write("He") client.flush - assert_equal(data, @stream.read) + thread = Thread.new do + sleep(0.1) + client.write("llo") + client.flush + end + assert_equal("Hell", @stream.read_all(4)) + thread.join end end |