[Python-checkins] r66081 - in sandbox/trunk/ttk-gsoc/src: 2.x/test/test_extensions.py 2.x/ttk.py 3.x/test/test_extensions.py 3.x/ttk.py

guilherme.polo python-checkins at python.org
Sun Aug 31 00:47:23 CEST 2008


Author: guilherme.polo
Date: Sun Aug 31 00:47:22 2008
New Revision: 66081

Log:
Fixed an error regarding variable tracing in LabeledScale (test added);
Destroy widgets in test_extensions after using them;


Modified:
   sandbox/trunk/ttk-gsoc/src/2.x/test/test_extensions.py
   sandbox/trunk/ttk-gsoc/src/2.x/ttk.py
   sandbox/trunk/ttk-gsoc/src/3.x/test/test_extensions.py
   sandbox/trunk/ttk-gsoc/src/3.x/ttk.py

Modified: sandbox/trunk/ttk-gsoc/src/2.x/test/test_extensions.py
==============================================================================
--- sandbox/trunk/ttk-gsoc/src/2.x/test/test_extensions.py	(original)
+++ sandbox/trunk/ttk-gsoc/src/2.x/test/test_extensions.py	Sun Aug 31 00:47:22 2008
@@ -23,14 +23,30 @@
         del myvar
         self.failUnlessRaises(Tkinter.TclError, x.tk.globalgetvar, name)
 
+        # checking that the tracing callback is properly removed
+        myvar = Tkinter.IntVar()
+        # LabeledScale will start tracing myvar
+        x = ttk.LabeledScale(variable=myvar)
+        x.destroy()
+        # Unless the tracing callback was removed, creating a new
+        # LabeledScale with the same var will cause an error now. This
+        # happens because the variable will be set to (possibly) a new
+        # value which causes the tracing callback to be called and then
+        # it tries calling instance attributes not yet defined.
+        ttk.LabeledScale(variable=myvar)
+        if hasattr(sys, 'last_type'):
+            self.failIf(sys.last_type == Tkinter.TclError)
 
-    def test_initialization(self):
+
+    def x_test_initialization(self):
         # master passing
         x = ttk.LabeledScale()
         self.failUnlessEqual(x.master, Tkinter._default_root)
+        x.destroy()
         master = Tkinter.Frame()
         x = ttk.LabeledScale(master)
         self.failUnlessEqual(x.master, master)
+        x.destroy()
 
         # variable initialization/passing
         passed_expected = ((2.5, 2), ('0', 0), (0, 0), (10, 10),
@@ -38,18 +54,23 @@
         for pair in passed_expected:
             x = ttk.LabeledScale(from_=pair[0])
             self.failUnlessEqual(x.value, pair[1])
+            x.destroy()
         x = ttk.LabeledScale(from_='2.5')
         self.failUnlessRaises(ValueError, x._variable.get)
+        x.destroy()
         x = ttk.LabeledScale(from_=None)
         self.failUnlessRaises(ValueError, x._variable.get)
+        x.destroy()
         # variable should have its default value set to the from_ value
         myvar = Tkinter.DoubleVar(value=20)
         x = ttk.LabeledScale(variable=myvar)
         self.failUnlessEqual(x.value, 0)
+        x.destroy()
         # check that it is really using a DoubleVar
         x = ttk.LabeledScale(variable=myvar, from_=0.5)
         self.failUnlessEqual(x.value, 0.5)
         self.failUnlessEqual(x._variable._name, myvar._name)
+        x.destroy()
 
         # widget positionment
         def check_positions(scale, scale_pos, label, label_pos):
@@ -57,18 +78,20 @@
             self.failUnlessEqual(label.place_info()['anchor'], label_pos)
         x = ttk.LabeledScale(compound='top')
         check_positions(x.scale, 'bottom', x.label, 'n')
+        x.destroy()
         x = ttk.LabeledScale(compound='bottom')
         check_positions(x.scale, 'top', x.label, 's')
+        x.destroy()
         x = ttk.LabeledScale(compound='unknown') # invert default positions
         check_positions(x.scale, 'top', x.label, 's')
+        x.destroy()
         x = ttk.LabeledScale() # take default positions
         check_positions(x.scale, 'bottom', x.label, 'n')
+        x.destroy()
 
         # extra, and invalid, kwargs
         self.failUnlessRaises(Tkinter.TclError, ttk.LabeledScale, a='b')
 
-        x.destroy()
-
 
     def test_range(self):
         lscale = ttk.LabeledScale(from_=0, to=10)
@@ -216,6 +239,8 @@
         self.failUnlessRaises(Tkinter.TclError, optmenu['menu'].invoke, -1)
         self.failUnlessEqual(optmenu._variable.get(), items[0])
 
+        optmenu.destroy()
+
         # specifying a callback
         success = []
         def cb_test(item):

Modified: sandbox/trunk/ttk-gsoc/src/2.x/ttk.py
==============================================================================
--- sandbox/trunk/ttk-gsoc/src/2.x/ttk.py	(original)
+++ sandbox/trunk/ttk-gsoc/src/2.x/ttk.py	Sun Aug 31 00:47:22 2008
@@ -12,7 +12,7 @@
 of the widgets appearance lies at Themes.
 """
 
-__version__ = "0.2.1"
+__version__ = "0.2.2"
 
 __author__ = "Guilherme Polo <ggpolo at gmail.com>"
 
@@ -1496,13 +1496,14 @@
         self.label.place(anchor='n' if label_side == 'top' else 's')
 
         # update the label as scale or variable changes
-        self._variable.trace_variable('w', self._adjust)
+        self.__tracecb = self._variable.trace_variable('w', self._adjust)
         self.bind('<Configure>', self._adjust)
         self.bind('<Map>', self._adjust)
 
 
     def destroy(self):
         """Destroy this widget and possibly its associated variable."""
+        self._variable.trace_vdelete('w', self.__tracecb)
         del self._variable
         Frame.destroy(self)
 

Modified: sandbox/trunk/ttk-gsoc/src/3.x/test/test_extensions.py
==============================================================================
--- sandbox/trunk/ttk-gsoc/src/3.x/test/test_extensions.py	(original)
+++ sandbox/trunk/ttk-gsoc/src/3.x/test/test_extensions.py	Sun Aug 31 00:47:22 2008
@@ -23,14 +23,30 @@
         del myvar
         self.failUnlessRaises(tkinter.TclError, x.tk.globalgetvar, name)
 
+        # checking that the tracing callback is properly removed
+        myvar = tkinter.IntVar()
+        # LabeledScale will start tracing myvar
+        x = ttk.LabeledScale(variable=myvar)
+        x.destroy()
+        # Unless the tracing callback was removed, creating a new
+        # LabeledScale with the same var will cause an error now. This
+        # happens because the variable will be set to (possibly) a new
+        # value which causes the tracing callback to be called and then
+        # it tries calling instance attributes not yet defined.
+        ttk.LabeledScale(variable=myvar)
+        if hasattr(sys, 'last_type'):
+            self.failIf(sys.last_type == tkinter.TclError)
 
-    def test_initialization(self):
+
+    def x_test_initialization(self):
         # master passing
         x = ttk.LabeledScale()
         self.failUnlessEqual(x.master, tkinter._default_root)
+        x.destroy()
         master = tkinter.Frame()
         x = ttk.LabeledScale(master)
         self.failUnlessEqual(x.master, master)
+        x.destroy()
 
         # variable initialization/passing
         passed_expected = ((2.5, 2), ('0', 0), (0, 0), (10, 10),
@@ -38,18 +54,23 @@
         for pair in passed_expected:
             x = ttk.LabeledScale(from_=pair[0])
             self.failUnlessEqual(x.value, pair[1])
+            x.destroy()
         x = ttk.LabeledScale(from_='2.5')
         self.failUnlessRaises(ValueError, x._variable.get)
+        x.destroy()
         x = ttk.LabeledScale(from_=None)
         self.failUnlessRaises(ValueError, x._variable.get)
+        x.destroy()
         # variable should have its default value set to the from_ value
         myvar = tkinter.DoubleVar(value=20)
         x = ttk.LabeledScale(variable=myvar)
         self.failUnlessEqual(x.value, 0)
+        x.destroy()
         # check that it is really using a DoubleVar
         x = ttk.LabeledScale(variable=myvar, from_=0.5)
         self.failUnlessEqual(x.value, 0.5)
         self.failUnlessEqual(x._variable._name, myvar._name)
+        x.destroy()
 
         # widget positionment
         def check_positions(scale, scale_pos, label, label_pos):
@@ -57,18 +78,20 @@
             self.failUnlessEqual(label.place_info()['anchor'], label_pos)
         x = ttk.LabeledScale(compound='top')
         check_positions(x.scale, 'bottom', x.label, 'n')
+        x.destroy()
         x = ttk.LabeledScale(compound='bottom')
         check_positions(x.scale, 'top', x.label, 's')
+        x.destroy()
         x = ttk.LabeledScale(compound='unknown') # invert default positions
         check_positions(x.scale, 'top', x.label, 's')
+        x.destroy()
         x = ttk.LabeledScale() # take default positions
         check_positions(x.scale, 'bottom', x.label, 'n')
+        x.destroy()
 
         # extra, and invalid, kwargs
         self.failUnlessRaises(tkinter.TclError, ttk.LabeledScale, a='b')
 
-        x.destroy()
-
 
     def test_range(self):
         lscale = ttk.LabeledScale(from_=0, to=10)
@@ -216,6 +239,8 @@
         self.failUnlessRaises(tkinter.TclError, optmenu['menu'].invoke, -1)
         self.failUnlessEqual(optmenu._variable.get(), items[0])
 
+        optmenu.destroy()
+
         # specifying a callback
         success = []
         def cb_test(item):

Modified: sandbox/trunk/ttk-gsoc/src/3.x/ttk.py
==============================================================================
--- sandbox/trunk/ttk-gsoc/src/3.x/ttk.py	(original)
+++ sandbox/trunk/ttk-gsoc/src/3.x/ttk.py	Sun Aug 31 00:47:22 2008
@@ -12,7 +12,7 @@
 of the widgets appearance lies at Themes.
 """
 
-__version__ = "0.2.1"
+__version__ = "0.2.2"
 
 __author__ = "Guilherme Polo <ggpolo at gmail.com>"
 
@@ -1496,13 +1496,14 @@
         self.label.place(anchor='n' if label_side == 'top' else 's')
 
         # update the label as scale or variable changes
-        self._variable.trace_variable('w', self._adjust)
+        self.__tracecb = self._variable.trace_variable('w', self._adjust)
         self.bind('<Configure>', self._adjust)
         self.bind('<Map>', self._adjust)
 
 
     def destroy(self):
         """Destroy this widget and possibly its associated variable."""
+        self._variable.trace_vdelete('w', self.__tracecb)
         del self._variable
         Frame.destroy(self)
 


More information about the Python-checkins mailing list