[ruby-dev:49716] [Ruby trunk Bug#12554] yamlに渡すファイルポインタを自分でcloseしないと動作が変

From: pt_charat@...
Date: 2016-07-05 13:47:24 UTC
List: ruby-dev #49716
Issue #12554 has been reported by 西行寺 うゆ.

----------------------------------------
Bug #12554: yamlに渡すファイルポインタを自分でcloseしないと動作が変
https://bugs.ruby-lang.org/issues/12554

* Author: 西行寺 うゆ
* Status: Open
* Priority: Normal
* Assignee: 
* ruby -v: ruby 2.3.1p112 (2016-04-26 revision 54768) [i386-mswin32_100]
* Backport: 2.1: UNKNOWN, 2.2: UNKNOWN, 2.3: UNKNOWN
----------------------------------------

rubyのファイルポインタは、スコープを抜けたら処理系側でcloseされているものと思ったんですが
yamlで読み書きを行う場合に動作がおかしいような気がします

このスクリプトを実行すると、それなりの確率で以下のエラーが出て、
test.yamlのデータが変です

~~~

require"yaml"
def func
file = "test.yaml"
data = YAML.load_file( file )
r = rand(99999)
data ||={}
data[r] ||=[]
data[r] << rand(99999)

# 問題個所
YAML.dump( data, open( file , 'w') )


# 自分でcloseすると平気
#
# f = open( file , 'w')
# YAML.dump( data , f ) ;
# f.close

end
1000.times do |n|
  print n , " "
  func
end

~~~




~~~
C:/Ruby/lib/ruby/2.3.0/psych.rb:377:in `parse': (test.yaml): did not find expected key while pa
rsing a block mapping at line 2 column 1 (Psych::SyntaxError)
        from C:/Ruby/lib/ruby/2.3.0/psych.rb:377:in `parse_stream'
        from C:/Ruby/lib/ruby/2.3.0/psych.rb:325:in `parse'
        from C:/Ruby/lib/ruby/2.3.0/psych.rb:252:in `load'
        from C:/Ruby/lib/ruby/2.3.0/psych.rb:471:in `block in load_file'
        from C:/Ruby/lib/ruby/2.3.0/psych.rb:471:in `open'
        from C:/Ruby/lib/ruby/2.3.0/psych.rb:471:in `load_file'
        from -:4:in `func'
        from -:11:in `block in <main>'
        from -:10:in `times'
        from -:10:in `<main>'
~~~




---Files--------------------------------
test.yaml (270 Bytes)


-- 
https://bugs.ruby-lang.org/

In This Thread

Prev Next