python - Using [clock format] through TKinter changes how proc returns "" -
i using tkinter access existing tcl library python. 1 of tcl procs looks values in list , returns "" if value not found. python code sees return value unicode , checks see if equal "". works until call [clock format] made in tcl code. after that, python code sees return value tuple. can add logic python code handle this, seems there larger issue going on might have other effect.
sample python program:
import tkinter _tclsh = tkinter.tcl() _tclsh.eval('proc returnblank { } { return "" }') _tclsh.eval('proc returnnotblank { } { return "not blank" }') print "before calling clock" _tclsh.eval('set shouldbeblank [returnblank]') shouldbeblank = _tclsh.getvar('shouldbeblank') print "shouldbeblank ", shouldbeblank, " type ", type(shouldbeblank) _tclsh.eval('set shouldnotbeblank [returnnotblank]') shouldnotbeblank = _tclsh.getvar('shouldnotbeblank') print "shouldnotbeblank ", shouldnotbeblank, " type ", type(shouldnotbeblank) print "\ncalling [clock seconds]" _tclsh.eval('puts [clock seconds]') _tclsh.eval('set shouldbeblank [returnblank]') shouldbeblank = _tclsh.getvar('shouldbeblank') print "shouldbeblank ", shouldbeblank, " type ", type(shouldbeblank) _tclsh.eval('set shouldnotbeblank [returnnotblank]') shouldnotbeblank = _tclsh.getvar('shouldnotbeblank') print "shouldnotbeblank ", shouldnotbeblank, " type ", type(shouldnotbeblank) print "\ncalling [clock format [clock seconds]]" _tclsh.eval('puts [clock format [clock seconds]]') _tclsh.eval('set shouldbeblank [returnblank]') shouldbeblank = _tclsh.getvar('shouldbeblank') print "shouldbeblank ", shouldbeblank, " type ", type(shouldbeblank) _tclsh.eval('set shouldnotbeblank [returnnotblank]') shouldnotbeblank = _tclsh.getvar('shouldnotbeblank') print "shouldnotbeblank ", shouldnotbeblank, " type ", type(shouldnotbeblank) the resulting output is:
before calling clock shouldbeblank type <type 'unicode'> shouldnotbeblank not blank type <type 'str'> calling [clock seconds] 1431623835 shouldbeblank type <type 'unicode'> shouldnotbeblank not blank type <type 'str'> calling [clock format [clock seconds]] thu may 14 13:17:15 edt 2015 shouldbeblank () type <type 'tuple'> shouldnotbeblank not blank type <type 'str'> as can see, empty string affected , calling [clock] not cause issue; [clock format].
any appreciated.
note: have tried different ways return empty string, did not affect output. tried:
_tclsh.eval('proc returnblank { } { return }') _tclsh.eval('proc returnblank { } { return {} }')
this problem way tkinter guesses type of string.
in tcl there no meaningful difference between empty list or empty string, bytecode compiler can change internal types @ will, convenient tcl in case.
tcl uses tcl_obj structure in c implementation can hold @ 2 type informations @ once (one string, other current view of tcl interpreter, depending on last usage of object).
so, if code tries 'guess' type tcl object, following (unreliable) trick, looks @ internal values of tcl object , assumes if type pointer set, object of type.
so watch this:
import tkinter tclsh = tkinter.tcl() tclsh.eval('set x ""') v = tclsh.getvar('x') print type(v) tclsh.eval('lindex $x 0') v = tclsh.getvar('x') print type(v) first 'str' , later 'tuple'. so, happens if use variable list, why modify procs return value?
this happens due aggressive literal sharing when byte compiling. empty string such common literal, there 1 object of (like pythons none or small integers).
you can use small trick work around issue, if need to, force type conversion (called 'shimmering' see http://wiki.tcl.tk/3033) explictly.
e.g. if want force integer internal representation, in tcl:
set x "2" incr x 0 # x has internal type integer lindex $x 0 # x has internal type tuple append x "" # x has internal type unicode and on. 1 of quirks when try map 2 languages type systems if vastly different, see similar effects in dbapi layers sqlite or when trying call functions via xmlrpc or json.
Comments
Post a Comment