From: Hidetoshi NAGAI Date: 2005-08-03T13:43:57+09:00 Subject: [ruby-dev:26725] Re: TkVariable.new_hash 経由だと trace が発生しない 永井@知能.九工大です. From: H.Yamamoto Subject: [ruby-dev:26721] TkVariable.new_hash 経由だと trace が発生しない Date: Wed, 3 Aug 2005 08:55:04 +0900 Message-ID: <20050803085503.DAD1AA40.ocean@m2.ccsnet.ne.jp> > いじっているうちに気づいたのですが、TkVariable.new_hash 経由で > 取得した TkVariable では trace が使用できないようです。これは > 意図された挙動でしょうか? 意図したものではないです. 次の patch ではいかがでしょう? Index: ext/tk/lib/tk/variable.rb =================================================================== RCS file: /var/cvs/src/ruby/ext/tk/lib/tk/variable.rb,v retrieving revision 1.1.2.17 diff -u -r1.1.2.17 variable.rb --- ext/tk/lib/tk/variable.rb 28 Jul 2005 09:15:36 -0000 1.1.2.17 +++ ext/tk/lib/tk/variable.rb 3 Aug 2005 04:42:58 -0000 @@ -41,13 +41,13 @@ EOL #def TkVariable.callback(args) - def TkVariable.callback(name1, name2, op) + def TkVariable.callback(id, name1, name2, op) #name1,name2,op = tk_split_list(args) #name1,name2,op = tk_split_simplelist(args) - if TkVar_CB_TBL[name1] + if TkVar_CB_TBL[id] #_get_eval_string(TkVar_CB_TBL[name1].trace_callback(name2,op)) begin - _get_eval_string(TkVar_CB_TBL[name1].trace_callback(name2, op)) + _get_eval_string(TkVar_CB_TBL[id].trace_callback(name2, op)) rescue SystemExit exit(0) rescue Interrupt @@ -270,6 +270,9 @@ Tk_VARIABLE_ID[1].succ! TkVar_ID_TBL[@id] = self + @var = @id + @elem = nil + @def_default = false @default_val = nil @@ -805,6 +808,7 @@ end def variable + # keeps a Tcl's variable name TkVarAccess.new(self._value) end def variable_element(*idxs) @@ -1264,12 +1268,14 @@ def trace(opts, cmd = Proc.new) @trace_var = [] if @trace_var == nil #opts = ['r','w','u'].find_all{|c| opts.index(c)}.join('') - opts = ['r','w','u'].find_all{|c| opts.to_s.index(c)}.join('') + opts = opts.to_s + opts = ['r','w','u'].find_all{|c| opts.index(c)}.join('') @trace_var.unshift([opts,cmd]) if @trace_opts == nil TkVar_CB_TBL[@id] = self @trace_opts = opts.dup - Tk.tk_call_without_enc('trace', 'variable', @id, @trace_opts, 'rb_var') + Tk.tk_call_without_enc('trace', 'variable', @id, @trace_opts, + 'rb_var ' << @id) =begin if /^(8\.([4-9]|[1-9][0-9])|9\.|[1-9][0-9])/ =~ Tk::TCL_VERSION # TCL_VERSION >= 8.4 @@ -1285,9 +1291,11 @@ #opts.each_byte{|c| newopts += c.chr unless newopts.index(c)} opts.each_byte{|c| newopts.concat(c.chr) unless newopts.index(c)} if newopts != @trace_opts - Tk.tk_call_without_enc('trace', 'vdelete', @id, @trace_opts, 'rb_var') + Tk.tk_call_without_enc('trace', 'vdelete', @id, @trace_opts, + 'rb_var ' << @id) @trace_opts.replace(newopts) - Tk.tk_call_without_enc('trace', 'variable', @id, @trace_opts, 'rb_var') + Tk.tk_call_without_enc('trace', 'variable', @id, @trace_opts, + 'rb_var ' << @id) =begin if /^(8\.([4-9]|[1-9][0-9])|9\.|[1-9][0-9])/ =~ Tk::TCL_VERSION # TCL_VERSION >= 8.4 @@ -1311,15 +1319,20 @@ end def trace_element(elem, opts, cmd = Proc.new) + if @elem + fail(RuntimeError, + "invalid for a TkVariable which denotes an element of Tcl's array") + end @trace_elem = {} if @trace_elem == nil @trace_elem[elem] = [] if @trace_elem[elem] == nil - #opts = ['r','w','u'].find_all{|c| opts.index(c)}.join('') - opts = ['r','w','u'].find_all{|c| opts.to_s.index(c)}.join('') + opts = opts.to_s + opts = ['r','w','u'].find_all{|c| opts.index(c)}.join('') @trace_elem[elem].unshift([opts,cmd]) if @trace_opts == nil TkVar_CB_TBL[@id] = self @trace_opts = opts.dup - Tk.tk_call_without_enc('trace', 'variable', @id, @trace_opts, 'rb_var') + Tk.tk_call_without_enc('trace', 'variable', @id, @trace_opts, + 'rb_var ' << @id) =begin if /^(8\.([4-9]|[1-9][0-9])|9\.|[1-9][0-9])/ =~ Tk::TCL_VERSION # TCL_VERSION >= 8.4 @@ -1336,9 +1349,11 @@ # opts.each_byte{|c| newopts += c.chr unless newopts.index(c)} opts.each_byte{|c| newopts.concat(c.chr) unless newopts.index(c)} if newopts != @trace_opts - Tk.tk_call_without_enc('trace', 'vdelete', @id, @trace_opts, 'rb_var') + Tk.tk_call_without_enc('trace', 'vdelete', @id, @trace_opts, + 'rb_var ' << @id) @trace_opts.replace(newopts) - Tk.tk_call_without_enc('trace', 'variable', @id, @trace_opts, 'rb_var') + Tk.tk_call_without_enc('trace', 'variable', @id, @trace_opts, + 'rb_var ' << @id) =begin if /^(8\.([4-9]|[1-9][0-9])|9\.|[1-9][0-9])/ =~ Tk::TCL_VERSION # TCL_VERSION >= 8.4 @@ -1365,7 +1380,12 @@ return [] unless @trace_var @trace_var.dup end - def trace_vinfo_for_element(elem) + + def _trace_vinfo_for_element(elem) + if @elem + fail(RuntimeError, + "invalid for a TkVariable which denotes an element of Tcl's array") + end return [] unless @trace_elem return [] unless @trace_elem[elem] @trace_elem[elem].dup @@ -1373,8 +1393,8 @@ def trace_vdelete(opts,cmd) return self unless @trace_var.kind_of? Array - #opts = ['r','w','u'].find_all{|c| opts.index(c)}.join('') - opts = ['r','w','u'].find_all{|c| opts.to_s.index(c)}.join('') + opts = opts.to_s + opts = ['r','w','u'].find_all{|c| opts.index(c)}.join('') idx = -1 newopts = '' @trace_var.each_with_index{|e,i| @@ -1398,10 +1418,11 @@ } } - #newopts = ['r','w','u'].find_all{|c| newopts.index(c)}.join('') - newopts = ['r','w','u'].find_all{|c| newopts.to_s.index(c)}.join('') + newopts = newopts.to_s + newopts = ['r','w','u'].find_all{|c| newopts.index(c)}.join('') if newopts != @trace_opts - Tk.tk_call_without_enc('trace', 'vdelete', @id, @trace_opts, 'rb_var') + Tk.tk_call_without_enc('trace', 'vdelete', @id, @trace_opts, + 'rb_var ' << @id) =begin if /^(8\.([4-9]|[1-9][0-9])|9\.|[1-9][0-9])/ =~ Tk::TCL_VERSION # TCL_VERSION >= 8.4 @@ -1415,7 +1436,8 @@ =end @trace_opts.replace(newopts) if @trace_opts != '' - Tk.tk_call_without_enc('trace', 'variable', @id, @trace_opts, 'rb_var') + Tk.tk_call_without_enc('trace', 'variable', @id, @trace_opts, + 'rb_var ' << @id) =begin if /^(8\.([4-9]|[1-9][0-9])|9\.|[1-9][0-9])/ =~ Tk::TCL_VERSION # TCL_VERSION >= 8.4 @@ -1434,10 +1456,14 @@ end def trace_vdelete_for_element(elem,opts,cmd) + if @elem + fail(RuntimeError, + "invalid for a TkVariable which denotes an element of Tcl's array") + end return self unless @trace_elem.kind_of? Hash return self unless @trace_elem[elem].kind_of? Array - # opts = ['r','w','u'].find_all{|c| opts.index(c)}.join('') - opts = ['r','w','u'].find_all{|c| opts.to_s.index(c)}.join('') + opts = opts.to_s + opts = ['r','w','u'].find_all{|c| opts.index(c)}.join('') idx = -1 @trace_elem[elem].each_with_index{|e,i| if idx < 0 && e[0] == opts && e[1] == cmd @@ -1463,10 +1489,11 @@ } } - #newopts = ['r','w','u'].find_all{|c| newopts.index(c)}.join('') - newopts = ['r','w','u'].find_all{|c| newopts.to_s.index(c)}.join('') + newopts = newopts.to_s + newopts = ['r','w','u'].find_all{|c| newopts.index(c)}.join('') if newopts != @trace_opts - Tk.tk_call_without_enc('trace', 'vdelete', @id, @trace_opts, 'rb_var') + Tk.tk_call_without_enc('trace', 'vdelete', @id, @trace_opts, + 'rb_var ' << @id) =begin if /^(8\.([4-9]|[1-9][0-9])|9\.|[1-9][0-9])/ =~ Tk::TCL_VERSION # TCL_VERSION >= 8.4 @@ -1480,7 +1507,8 @@ =end @trace_opts.replace(newopts) if @trace_opts != '' - Tk.tk_call_without_enc('trace', 'variable', @id, @trace_opts, 'rb_var') + Tk.tk_call_without_enc('trace', 'variable', @id, @trace_opts, + 'rb_var ' << @id) =begin if /^(8\.([4-9]|[1-9][0-9])|9\.|[1-9][0-9])/ =~ Tk::TCL_VERSION # TCL_VERSION >= 8.4 @@ -1519,6 +1547,9 @@ @id = varname TkVar_ID_TBL[@id] = self + @var = @id + @elem = nil + @def_default = false @default_val = nil @@ -1530,7 +1561,16 @@ var = self @element_type = Hash.new{|k,v| var.default_value_type } + # is an element? + if @id =~ /^([^(]+)\((.+)\)$/ + # is an element --> var == $1, elem == $2 + @var = $1 + @elem = $2 + end + # teach Tk-ip that @id is global var + INTERP._invoke_without_enc('global', @var) +=begin begin INTERP._invoke_without_enc('global', @id) rescue => e @@ -1541,6 +1581,7 @@ fail e end end +=end if val if val.kind_of?(Hash) -- 永井 秀利 (九工大 知能情報) nagai@ai.kyutech.ac.jp