Skip to content

Commit ed1deb0

Browse files
authored
bpo-36096: IDLE: Refactor class variables in colorizer (GH-12002)
1 parent 32f5fdd commit ed1deb0

File tree

3 files changed

+58
-21
lines changed

3 files changed

+58
-21
lines changed

Lib/idlelib/colorizer.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,26 +55,35 @@ def color_config(text):
5555
class ColorDelegator(Delegator):
5656
"""Delegator for syntax highlighting (text coloring).
5757
58-
Class variables:
59-
after_id: Identifier for scheduled after event.
58+
Instance variables:
59+
delegate: Delegator below this one in the stack, meaning the
60+
one this one delegates to.
61+
62+
Used to track state:
63+
after_id: Identifier for scheduled after event, which is a
64+
timer for colorizing the text.
6065
allow_colorizing: Boolean toggle for applying colorizing.
6166
colorizing: Boolean flag when colorizing is in process.
6267
stop_colorizing: Boolean flag to end an active colorizing
6368
process.
6469
close_when_done: Widget to destroy after colorizing process
6570
completes (doesn't seem to be used by IDLE).
66-
67-
Instance variables:
68-
delegate: Delegator below this one in the stack, meaning the
69-
one this one delegates to.
7071
"""
7172

7273
def __init__(self):
7374
Delegator.__init__(self)
75+
self.init_state()
7476
self.prog = prog
7577
self.idprog = idprog
7678
self.LoadTagDefs()
7779

80+
def init_state(self):
81+
"Initialize variables that track colorizing state."
82+
self.after_id = None
83+
self.allow_colorizing = True
84+
self.stop_colorizing = False
85+
self.colorizing = False
86+
7887
def setdelegate(self, delegate):
7988
"""Set the delegate for this instance.
8089
@@ -134,11 +143,6 @@ def delete(self, index1, index2=None):
134143
self.delegate.delete(index1, index2)
135144
self.notify_range(index1)
136145

137-
after_id = None
138-
allow_colorizing = True
139-
stop_colorizing = False
140-
colorizing = False
141-
142146
def notify_range(self, index1, index2=None):
143147
"Mark text changes for processing and restart colorizing, if active."
144148
self.tag_add("TODO", index1, index2)

Lib/idlelib/idle_test/test_colorizer.py

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -100,46 +100,78 @@ def test_color_config(self):
100100
eq(text['inactiveselectbackground'], 'gray')
101101

102102

103-
class ColorDelegatorTest(unittest.TestCase):
103+
class ColorDelegatorInstantiationTest(unittest.TestCase):
104104

105105
@classmethod
106106
def setUpClass(cls):
107107
requires('gui')
108108
root = cls.root = Tk()
109109
root.withdraw()
110110
text = cls.text = Text(root)
111-
cls.percolator = Percolator(text)
112-
# Delegator stack = [Delagator(text)]
113111

114112
@classmethod
115113
def tearDownClass(cls):
116-
cls.percolator.redir.close()
117-
del cls.percolator, cls.text
114+
del cls.text
118115
cls.root.update_idletasks()
119116
cls.root.destroy()
120117
del cls.root
121118

122119
def setUp(self):
123120
self.color = colorizer.ColorDelegator()
124-
self.percolator.insertfilter(self.color)
125-
# Calls color.setdelagate(Delagator(text)).
126121

127122
def tearDown(self):
128123
self.color.close()
129-
self.percolator.removefilter(self.color)
130124
self.text.delete('1.0', 'end')
131125
self.color.resetcache()
132126
del self.color
133127

134128
def test_init(self):
135129
color = self.color
136130
self.assertIsInstance(color, colorizer.ColorDelegator)
137-
# The following are class variables.
131+
132+
def test_init_state(self):
133+
# init_state() is called during the instantiation of
134+
# ColorDelegator in setUp().
135+
color = self.color
136+
self.assertIsNone(color.after_id)
138137
self.assertTrue(color.allow_colorizing)
139138
self.assertFalse(color.colorizing)
139+
self.assertFalse(color.stop_colorizing)
140+
141+
142+
class ColorDelegatorTest(unittest.TestCase):
143+
144+
@classmethod
145+
def setUpClass(cls):
146+
requires('gui')
147+
root = cls.root = Tk()
148+
root.withdraw()
149+
text = cls.text = Text(root)
150+
cls.percolator = Percolator(text)
151+
# Delegator stack = [Delegator(text)]
152+
153+
@classmethod
154+
def tearDownClass(cls):
155+
cls.percolator.redir.close()
156+
del cls.percolator, cls.text
157+
cls.root.update_idletasks()
158+
cls.root.destroy()
159+
del cls.root
160+
161+
def setUp(self):
162+
self.color = colorizer.ColorDelegator()
163+
self.percolator.insertfilter(self.color)
164+
# Calls color.setdelegate(Delegator(text)).
165+
166+
def tearDown(self):
167+
self.color.close()
168+
self.percolator.removefilter(self.color)
169+
self.text.delete('1.0', 'end')
170+
self.color.resetcache()
171+
del self.color
140172

141173
def test_setdelegate(self):
142-
# Called in setUp.
174+
# Called in setUp when filter is attached to percolator.
143175
color = self.color
144176
self.assertIsInstance(color.delegate, colorizer.Delegator)
145177
# It is too late to mock notify_range, so test side effect.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Refactor class variables to instance variables in colorizer.

0 commit comments

Comments
 (0)