Another optimization, probably of negligeable effect: instead of

calling self.tk.getint() and self.tk.getdouble(), call the globals
getint() and getdouble(), which in turn are just names for the Python
builtins int() and double().  (Making them globals actually save a
dict lookup compared to using the built-in.)  The corresponding
methods of class Misc have been changed similarly.  (Note that
getboolean() hasn't been changed because there's no Python
equivalent.)

The use of int() and float() has another advantage: if/when Tcl calls
can actually return Tcl objects with other types than string, use of
int() and float() is essential.
This commit is contained in:
Guido van Rossum 1998-04-30 17:50:36 +00:00
parent cc8e1a4ed7
commit 0132f69c2e
1 changed files with 78 additions and 82 deletions

View File

@ -111,14 +111,14 @@ class IntVar(Variable):
def __init__(self, master=None): def __init__(self, master=None):
Variable.__init__(self, master) Variable.__init__(self, master)
def get(self): def get(self):
return self._tk.getint(self._tk.globalgetvar(self._name)) return getint(self._tk.globalgetvar(self._name))
class DoubleVar(Variable): class DoubleVar(Variable):
_default = 0.0 _default = 0.0
def __init__(self, master=None): def __init__(self, master=None):
Variable.__init__(self, master) Variable.__init__(self, master)
def get(self): def get(self):
return self._tk.getdouble(self._tk.globalgetvar(self._name)) return getdouble(self._tk.globalgetvar(self._name))
class BooleanVar(Variable): class BooleanVar(Variable):
_default = "false" _default = "false"
@ -130,11 +130,9 @@ class BooleanVar(Variable):
def mainloop(n=0): def mainloop(n=0):
_default_root.tk.mainloop(n) _default_root.tk.mainloop(n)
def getint(s): getint = int
return _default_root.tk.getint(s)
def getdouble(s): getdouble = float
return _default_root.tk.getdouble(s)
def getboolean(s): def getboolean(s):
return _default_root.tk.getboolean(s) return _default_root.tk.getboolean(s)
@ -181,10 +179,8 @@ class Misc:
self.tk.setvar(name, value) self.tk.setvar(name, value)
def getvar(self, name='PY_VAR'): def getvar(self, name='PY_VAR'):
return self.tk.getvar(name) return self.tk.getvar(name)
def getint(self, s): getint = int
return self.tk.getint(s) getdouble = float
def getdouble(self, s):
return self.tk.getdouble(s)
def getboolean(self, s): def getboolean(self, s):
return self.tk.getboolean(s) return self.tk.getboolean(s)
def focus_set(self): def focus_set(self):
@ -299,13 +295,13 @@ class Misc:
return self.tk.call('tk', 'colormodel', self._w, value) return self.tk.call('tk', 'colormodel', self._w, value)
def winfo_atom(self, name, displayof=0): def winfo_atom(self, name, displayof=0):
args = ('winfo', 'atom') + self._displayof(displayof) + (name,) args = ('winfo', 'atom') + self._displayof(displayof) + (name,)
return self.tk.getint(self.tk.call(args)) return getint(self.tk.call(args))
def winfo_atomname(self, id, displayof=0): def winfo_atomname(self, id, displayof=0):
args = ('winfo', 'atomname') \ args = ('winfo', 'atomname') \
+ self._displayof(displayof) + (id,) + self._displayof(displayof) + (id,)
return self.tk.call(args) return self.tk.call(args)
def winfo_cells(self): def winfo_cells(self):
return self.tk.getint( return getint(
self.tk.call('winfo', 'cells', self._w)) self.tk.call('winfo', 'cells', self._w))
def winfo_children(self): def winfo_children(self):
return map(self._nametowidget, return map(self._nametowidget,
@ -323,26 +319,26 @@ class Misc:
if not name: return None if not name: return None
return self._nametowidget(name) return self._nametowidget(name)
def winfo_depth(self): def winfo_depth(self):
return self.tk.getint(self.tk.call('winfo', 'depth', self._w)) return getint(self.tk.call('winfo', 'depth', self._w))
def winfo_exists(self): def winfo_exists(self):
return self.tk.getint( return getint(
self.tk.call('winfo', 'exists', self._w)) self.tk.call('winfo', 'exists', self._w))
def winfo_fpixels(self, number): def winfo_fpixels(self, number):
return self.tk.getdouble(self.tk.call( return getdouble(self.tk.call(
'winfo', 'fpixels', self._w, number)) 'winfo', 'fpixels', self._w, number))
def winfo_geometry(self): def winfo_geometry(self):
return self.tk.call('winfo', 'geometry', self._w) return self.tk.call('winfo', 'geometry', self._w)
def winfo_height(self): def winfo_height(self):
return self.tk.getint( return getint(
self.tk.call('winfo', 'height', self._w)) self.tk.call('winfo', 'height', self._w))
def winfo_id(self): def winfo_id(self):
return self.tk.getint( return getint(
self.tk.call('winfo', 'id', self._w)) self.tk.call('winfo', 'id', self._w))
def winfo_interps(self, displayof=0): def winfo_interps(self, displayof=0):
args = ('winfo', 'interps') + self._displayof(displayof) args = ('winfo', 'interps') + self._displayof(displayof)
return self.tk.splitlist(self.tk.call(args)) return self.tk.splitlist(self.tk.call(args))
def winfo_ismapped(self): def winfo_ismapped(self):
return self.tk.getint( return getint(
self.tk.call('winfo', 'ismapped', self._w)) self.tk.call('winfo', 'ismapped', self._w))
def winfo_manager(self): def winfo_manager(self):
return self.tk.call('winfo', 'manager', self._w) return self.tk.call('winfo', 'manager', self._w)
@ -355,53 +351,53 @@ class Misc:
+ self._displayof(displayof) + (id,) + self._displayof(displayof) + (id,)
return self.tk.call(args) return self.tk.call(args)
def winfo_pixels(self, number): def winfo_pixels(self, number):
return self.tk.getint( return getint(
self.tk.call('winfo', 'pixels', self._w, number)) self.tk.call('winfo', 'pixels', self._w, number))
def winfo_pointerx(self): def winfo_pointerx(self):
return self.tk.getint( return getint(
self.tk.call('winfo', 'pointerx', self._w)) self.tk.call('winfo', 'pointerx', self._w))
def winfo_pointerxy(self): def winfo_pointerxy(self):
return self._getints( return self._getints(
self.tk.call('winfo', 'pointerxy', self._w)) self.tk.call('winfo', 'pointerxy', self._w))
def winfo_pointery(self): def winfo_pointery(self):
return self.tk.getint( return getint(
self.tk.call('winfo', 'pointery', self._w)) self.tk.call('winfo', 'pointery', self._w))
def winfo_reqheight(self): def winfo_reqheight(self):
return self.tk.getint( return getint(
self.tk.call('winfo', 'reqheight', self._w)) self.tk.call('winfo', 'reqheight', self._w))
def winfo_reqwidth(self): def winfo_reqwidth(self):
return self.tk.getint( return getint(
self.tk.call('winfo', 'reqwidth', self._w)) self.tk.call('winfo', 'reqwidth', self._w))
def winfo_rgb(self, color): def winfo_rgb(self, color):
return self._getints( return self._getints(
self.tk.call('winfo', 'rgb', self._w, color)) self.tk.call('winfo', 'rgb', self._w, color))
def winfo_rootx(self): def winfo_rootx(self):
return self.tk.getint( return getint(
self.tk.call('winfo', 'rootx', self._w)) self.tk.call('winfo', 'rootx', self._w))
def winfo_rooty(self): def winfo_rooty(self):
return self.tk.getint( return getint(
self.tk.call('winfo', 'rooty', self._w)) self.tk.call('winfo', 'rooty', self._w))
def winfo_screen(self): def winfo_screen(self):
return self.tk.call('winfo', 'screen', self._w) return self.tk.call('winfo', 'screen', self._w)
def winfo_screencells(self): def winfo_screencells(self):
return self.tk.getint( return getint(
self.tk.call('winfo', 'screencells', self._w)) self.tk.call('winfo', 'screencells', self._w))
def winfo_screendepth(self): def winfo_screendepth(self):
return self.tk.getint( return getint(
self.tk.call('winfo', 'screendepth', self._w)) self.tk.call('winfo', 'screendepth', self._w))
def winfo_screenheight(self): def winfo_screenheight(self):
return self.tk.getint( return getint(
self.tk.call('winfo', 'screenheight', self._w)) self.tk.call('winfo', 'screenheight', self._w))
def winfo_screenmmheight(self): def winfo_screenmmheight(self):
return self.tk.getint( return getint(
self.tk.call('winfo', 'screenmmheight', self._w)) self.tk.call('winfo', 'screenmmheight', self._w))
def winfo_screenmmwidth(self): def winfo_screenmmwidth(self):
return self.tk.getint( return getint(
self.tk.call('winfo', 'screenmmwidth', self._w)) self.tk.call('winfo', 'screenmmwidth', self._w))
def winfo_screenvisual(self): def winfo_screenvisual(self):
return self.tk.call('winfo', 'screenvisual', self._w) return self.tk.call('winfo', 'screenvisual', self._w)
def winfo_screenwidth(self): def winfo_screenwidth(self):
return self.tk.getint( return getint(
self.tk.call('winfo', 'screenwidth', self._w)) self.tk.call('winfo', 'screenwidth', self._w))
def winfo_server(self): def winfo_server(self):
return self.tk.call('winfo', 'server', self._w) return self.tk.call('winfo', 'server', self._w)
@ -409,7 +405,7 @@ class Misc:
return self._nametowidget(self.tk.call( return self._nametowidget(self.tk.call(
'winfo', 'toplevel', self._w)) 'winfo', 'toplevel', self._w))
def winfo_viewable(self): def winfo_viewable(self):
return self.tk.getint( return getint(
self.tk.call('winfo', 'viewable', self._w)) self.tk.call('winfo', 'viewable', self._w))
def winfo_visual(self): def winfo_visual(self):
return self.tk.call('winfo', 'visual', self._w) return self.tk.call('winfo', 'visual', self._w)
@ -420,28 +416,28 @@ class Misc:
self.tk.call('winfo', 'visualsavailable', self._w, self.tk.call('winfo', 'visualsavailable', self._w,
includeids and 'includeids' or None)) includeids and 'includeids' or None))
def parseitem(x, self=self): def parseitem(x, self=self):
return x[:1] + tuple(map(self.tk.getint, x[1:])) return x[:1] + tuple(map(getint, x[1:]))
return map(parseitem, data) return map(parseitem, data)
def winfo_vrootheight(self): def winfo_vrootheight(self):
return self.tk.getint( return getint(
self.tk.call('winfo', 'vrootheight', self._w)) self.tk.call('winfo', 'vrootheight', self._w))
def winfo_vrootwidth(self): def winfo_vrootwidth(self):
return self.tk.getint( return getint(
self.tk.call('winfo', 'vrootwidth', self._w)) self.tk.call('winfo', 'vrootwidth', self._w))
def winfo_vrootx(self): def winfo_vrootx(self):
return self.tk.getint( return getint(
self.tk.call('winfo', 'vrootx', self._w)) self.tk.call('winfo', 'vrootx', self._w))
def winfo_vrooty(self): def winfo_vrooty(self):
return self.tk.getint( return getint(
self.tk.call('winfo', 'vrooty', self._w)) self.tk.call('winfo', 'vrooty', self._w))
def winfo_width(self): def winfo_width(self):
return self.tk.getint( return getint(
self.tk.call('winfo', 'width', self._w)) self.tk.call('winfo', 'width', self._w))
def winfo_x(self): def winfo_x(self):
return self.tk.getint( return getint(
self.tk.call('winfo', 'x', self._w)) self.tk.call('winfo', 'x', self._w))
def winfo_y(self): def winfo_y(self):
return self.tk.getint( return getint(
self.tk.call('winfo', 'y', self._w)) self.tk.call('winfo', 'y', self._w))
def update(self): def update(self):
self.tk.call('update') self.tk.call('update')
@ -487,11 +483,11 @@ class Misc:
def quit(self): def quit(self):
self.tk.quit() self.tk.quit()
def _getints(self, string): def _getints(self, string):
if not string: return None if string:
return tuple(map(self.tk.getint, self.tk.splitlist(string))) return tuple(map(getint, self.tk.splitlist(string)))
def _getdoubles(self, string): def _getdoubles(self, string):
if not string: return None if string:
return tuple(map(self.tk.getdouble, self.tk.splitlist(string))) return tuple(map(getdouble, self.tk.splitlist(string)))
def _getboolean(self, string): def _getboolean(self, string):
if string: if string:
return self.tk.getboolean(string) return self.tk.getboolean(string)
@ -557,39 +553,40 @@ class Misc:
'%s', '%t', '%w', '%x', '%y', '%s', '%t', '%w', '%x', '%y',
'%A', '%E', '%K', '%N', '%W', '%T', '%X', '%Y') '%A', '%E', '%K', '%N', '%W', '%T', '%X', '%Y')
def _substitute(self, *args): def _substitute(self, *args):
tk = self.tk
if len(args) != len(self._subst_format): return args if len(args) != len(self._subst_format): return args
getboolean = self.tk.getboolean
getint = int
nsign, b, f, h, k, s, t, w, x, y, A, E, K, N, W, T, X, Y = args nsign, b, f, h, k, s, t, w, x, y, A, E, K, N, W, T, X, Y = args
# Missing: (a, c, d, m, o, v, B, R) # Missing: (a, c, d, m, o, v, B, R)
e = Event() e = Event()
e.serial = tk.getint(nsign) e.serial = getint(nsign)
e.num = tk.getint(b) e.num = getint(b)
try: e.focus = tk.getboolean(f) try: e.focus = getboolean(f)
except TclError: pass except TclError: pass
e.height = tk.getint(h) e.height = getint(h)
e.keycode = tk.getint(k) e.keycode = getint(k)
# For Visibility events, event state is a string and # For Visibility events, event state is a string and
# not an integer: # not an integer:
try: try:
e.state = tk.getint(s) e.state = getint(s)
except TclError: except TclError:
e.state = s e.state = s
e.time = tk.getint(t) e.time = getint(t)
e.width = tk.getint(w) e.width = getint(w)
e.x = tk.getint(x) e.x = getint(x)
e.y = tk.getint(y) e.y = getint(y)
e.char = A e.char = A
try: e.send_event = tk.getboolean(E) try: e.send_event = getboolean(E)
except TclError: pass except TclError: pass
e.keysym = K e.keysym = K
e.keysym_num = tk.getint(N) e.keysym_num = getint(N)
e.type = T e.type = T
try: try:
e.widget = self._nametowidget(W) e.widget = self._nametowidget(W)
except KeyError: except KeyError:
e.widget = W e.widget = W
e.x_root = tk.getint(X) e.x_root = getint(X)
e.y_root = tk.getint(Y) e.y_root = getint(Y)
return (e,) return (e,)
def _report_exception(self): def _report_exception(self):
import sys import sys
@ -672,9 +669,9 @@ class Misc:
if not value: if not value:
value = None value = None
elif '.' in value: elif '.' in value:
value = self.tk.getdouble(value) value = getdouble(value)
else: else:
value = self.tk.getint(value) value = getint(value)
dict[key] = value dict[key] = value
return dict return dict
res = self.tk.call( res = self.tk.call(
@ -683,8 +680,8 @@ class Misc:
if len(options) == 1: if len(options) == 1:
if not res: return None if not res: return None
# In Tk 7.5, -width can be a float # In Tk 7.5, -width can be a float
if '.' in res: return self.tk.getdouble(res) if '.' in res: return getdouble(res)
return self.tk.getint(res) return getint(res)
def grid_columnconfigure(self, index, cnf={}, **kw): def grid_columnconfigure(self, index, cnf={}, **kw):
return self._grid_configure('columnconfigure', index, cnf, kw) return self._grid_configure('columnconfigure', index, cnf, kw)
columnconfigure = grid_columnconfigure columnconfigure = grid_columnconfigure
@ -1141,14 +1138,14 @@ class Canvas(Widget):
return self._bind((self._w, 'bind', tagOrId), return self._bind((self._w, 'bind', tagOrId),
sequence, func, add) sequence, func, add)
def canvasx(self, screenx, gridspacing=None): def canvasx(self, screenx, gridspacing=None):
return self.tk.getdouble(self.tk.call( return getdouble(self.tk.call(
self._w, 'canvasx', screenx, gridspacing)) self._w, 'canvasx', screenx, gridspacing))
def canvasy(self, screeny, gridspacing=None): def canvasy(self, screeny, gridspacing=None):
return self.tk.getdouble(self.tk.call( return getdouble(self.tk.call(
self._w, 'canvasy', screeny, gridspacing)) self._w, 'canvasy', screeny, gridspacing))
def coords(self, *args): def coords(self, *args):
# XXX Should use _flatten on args # XXX Should use _flatten on args
return map(self.tk.getdouble, return map(getdouble,
self.tk.splitlist(self._do('coords', args))) self.tk.splitlist(self._do('coords', args)))
def _create(self, itemType, args, kw): # Args: (val, val, ..., cnf={}) def _create(self, itemType, args, kw): # Args: (val, val, ..., cnf={})
args = _flatten(args) args = _flatten(args)
@ -1157,7 +1154,7 @@ class Canvas(Widget):
args = args[:-1] args = args[:-1]
else: else:
cnf = {} cnf = {}
return self.tk.getint(apply( return getint(apply(
self.tk.call, self.tk.call,
(self._w, 'create', itemType) (self._w, 'create', itemType)
+ args + self._options(cnf, kw))) + args + self._options(cnf, kw)))
@ -1208,7 +1205,7 @@ class Canvas(Widget):
def icursor(self, *args): def icursor(self, *args):
self._do('icursor', args) self._do('icursor', args)
def index(self, *args): def index(self, *args):
return self.tk.getint(self._do('index', args)) return getint(self._do('index', args))
def insert(self, *args): def insert(self, *args):
self._do('insert', args) self._do('insert', args)
def itemcget(self, tagOrId, option): def itemcget(self, tagOrId, option):
@ -1287,7 +1284,7 @@ class Entry(Widget):
def icursor(self, index): def icursor(self, index):
self.tk.call(self._w, 'icursor', index) self.tk.call(self._w, 'icursor', index)
def index(self, index): def index(self, index):
return self.tk.getint(self.tk.call( return getint(self.tk.call(
self._w, 'index', index)) self._w, 'index', index))
def insert(self, index, string): def insert(self, index, string):
self.tk.call(self._w, 'insert', index, string) self.tk.call(self._w, 'insert', index, string)
@ -1359,7 +1356,7 @@ class Listbox(Widget):
def insert(self, index, *elements): def insert(self, index, *elements):
self.tk.call((self._w, 'insert', index) + elements) self.tk.call((self._w, 'insert', index) + elements)
def nearest(self, y): def nearest(self, y):
return self.tk.getint(self.tk.call( return getint(self.tk.call(
self._w, 'nearest', y)) self._w, 'nearest', y))
def scan_mark(self, x, y): def scan_mark(self, x, y):
self.tk.call(self._w, 'scan', 'mark', x, y) self.tk.call(self._w, 'scan', 'mark', x, y)
@ -1370,7 +1367,7 @@ class Listbox(Widget):
def index(self, index): def index(self, index):
i = self.tk.call(self._w, 'index', index) i = self.tk.call(self._w, 'index', index)
if i == 'none': return None if i == 'none': return None
return self.tk.getint(i) return getint(i)
def select_anchor(self, index): def select_anchor(self, index):
self.tk.call(self._w, 'selection', 'anchor', index) self.tk.call(self._w, 'selection', 'anchor', index)
selection_anchor = select_anchor selection_anchor = select_anchor
@ -1386,7 +1383,7 @@ class Listbox(Widget):
self.tk.call(self._w, 'selection', 'set', first, last) self.tk.call(self._w, 'selection', 'set', first, last)
selection_set = select_set selection_set = select_set
def size(self): def size(self):
return self.tk.getint(self.tk.call(self._w, 'size')) return getint(self.tk.call(self._w, 'size'))
def xview(self, *what): def xview(self, *what):
if not what: if not what:
return self._getdoubles(self.tk.call(self._w, 'xview')) return self._getdoubles(self.tk.call(self._w, 'xview'))
@ -1472,7 +1469,7 @@ class Menu(Widget):
def index(self, index): def index(self, index):
i = self.tk.call(self._w, 'index', index) i = self.tk.call(self._w, 'index', index)
if i == 'none': return None if i == 'none': return None
return self.tk.getint(i) return getint(i)
def invoke(self, index): def invoke(self, index):
return self.tk.call(self._w, 'invoke', index) return self.tk.call(self._w, 'invoke', index)
def post(self, x, y): def post(self, x, y):
@ -1482,7 +1479,7 @@ class Menu(Widget):
def unpost(self): def unpost(self):
self.tk.call(self._w, 'unpost') self.tk.call(self._w, 'unpost')
def yposition(self, index): def yposition(self, index):
return self.tk.getint(self.tk.call( return getint(self.tk.call(
self._w, 'yposition', index)) self._w, 'yposition', index))
class Menubutton(Widget): class Menubutton(Widget):
@ -1511,9 +1508,9 @@ class Scale(Widget):
def get(self): def get(self):
value = self.tk.call(self._w, 'get') value = self.tk.call(self._w, 'get')
try: try:
return self.tk.getint(value) return getint(value)
except TclError: except TclError:
return self.tk.getdouble(value) return getdouble(value)
def set(self, value): def set(self, value):
self.tk.call(self._w, 'set', value) self.tk.call(self._w, 'set', value)
@ -1523,11 +1520,10 @@ class Scrollbar(Widget):
def activate(self, index): def activate(self, index):
self.tk.call(self._w, 'activate', index) self.tk.call(self._w, 'activate', index)
def delta(self, deltax, deltay): def delta(self, deltax, deltay):
return self.getdouble(self.tk.call( return getdouble(
self._w, 'delta', deltax, deltay)) self.tk.call(self._w, 'delta', deltax, deltay))
def fraction(self, x, y): def fraction(self, x, y):
return self.getdouble(self.tk.call( return getdouble(self.tk.call(self._w, 'fraction', x, y))
self._w, 'fraction', x, y))
def identify(self, x, y): def identify(self, x, y):
return self.tk.call(self._w, 'identify', x, y) return self.tk.call(self._w, 'identify', x, y)
def get(self): def get(self):
@ -1747,12 +1743,12 @@ class Image:
self.tk.call((self.name, 'config') + res) self.tk.call((self.name, 'config') + res)
config = configure config = configure
def height(self): def height(self):
return self.tk.getint( return getint(
self.tk.call('image', 'height', self.name)) self.tk.call('image', 'height', self.name))
def type(self): def type(self):
return self.tk.call('image', 'type', self.name) return self.tk.call('image', 'type', self.name)
def width(self): def width(self):
return self.tk.getint( return getint(
self.tk.call('image', 'width', self.name)) self.tk.call('image', 'width', self.name))
class PhotoImage(Image): class PhotoImage(Image):