Skip to content

Commit 0b1a8b6

Browse files
committed
code style
1 parent 534c57d commit 0b1a8b6

File tree

2 files changed

+46
-34
lines changed

2 files changed

+46
-34
lines changed

Lib/tkinter/test/test_tkinter/test_threads.py

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,18 @@
77
import time
88

99
import unittest
10+
11+
1012
class TestThreads(unittest.TestCase):
1113
def test_threads(self):
1214
import subprocess
1315
p = subprocess.Popen([sys.executable, __file__, "run"],
14-
stdout=subprocess.PIPE,
15-
stderr=subprocess.PIPE)
16+
stdout=subprocess.PIPE,
17+
stderr=subprocess.PIPE)
1618
self.addCleanup(p.stdout.close)
1719
self.addCleanup(p.stderr.close)
1820
try:
19-
#Test code is designed to complete in a few seconds
21+
# Test code is designed to complete in a few seconds
2022
stdout, stderr = p.communicate(timeout=10)
2123
except subprocess.TimeoutExpired:
2224
p.kill()
@@ -28,60 +30,69 @@ def test_threads(self):
2830
self.assertTrue(len(stderr) == 0, "stderr: " + repr(stderr))
2931

3032

31-
3233
running = True
3334

35+
3436
class EventThread(threading.Thread):
35-
def __init__(self,target):
36-
super(EventThread,self).__init__()
37+
def __init__(self, target):
38+
super(EventThread, self).__init__()
3739
self.target = target
40+
3841
def run(self):
3942
while running:
4043
time.sleep(0.02)
4144
c = random.choice(string.ascii_letters)
4245
self.target.event_generate(c)
4346

47+
4448
class Main(object):
4549
def __init__(self):
4650
self.root = tkinter.Tk()
47-
self.root.bind('<Key>',dummy_handler)
48-
self.threads=[]
51+
self.root.bind('<Key>', dummy_handler)
52+
self.threads = []
4953

5054
self.t_cleanup = threading.Thread(target=self.tf_stop)
5155

5256
def go(self):
53-
self.root.after(0,self.add_threads)
54-
self.root.after(500,self.stop)
55-
self.root.protocol("WM_DELETE_WINDOW",self.stop)
57+
self.root.after(0, self.add_threads)
58+
self.root.after(500, self.stop)
59+
self.root.protocol("WM_DELETE_WINDOW", self.stop)
5660
self.root.mainloop()
5761
self.t_cleanup.join()
62+
5863
def stop(self):
5964
self.t_cleanup.start()
65+
6066
def tf_stop(self):
6167
global running
6268
running = False
6369
for t in self.threads: t.join()
64-
self.root.after(0,self.root.destroy)
70+
self.root.after(0, self.root.destroy)
71+
6572
def add_threads(self):
6673
for _ in range(20):
6774
t = EventThread(self.root)
6875
self.threads.append(t)
6976
t.start()
7077

78+
7179
def dummy_handler(event):
7280
pass
7381

7482

7583
tests_gui = (TestThreads,)
7684

77-
if __name__=='__main__':
78-
import sys,os
79-
if sys.argv[1:]==['run']:
85+
if __name__ == '__main__':
86+
import sys, os
87+
88+
if sys.argv[1:] == ['run']:
8089
if os.name == 'nt':
8190
import ctypes
82-
#the bug causes crashes
91+
92+
# the bug causes crashes
8393
ctypes.windll.kernel32.SetErrorMode(3)
8494
Main().go()
8595
else:
8696
from test.support import run_unittest
97+
8798
run_unittest(*tests_gui)

Modules/_tkinter.c

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -234,25 +234,25 @@ static PyThreadState *tcl_tstate = NULL;
234234

235235

236236
#define ACQUIRE_TCL_LOCK \
237-
if (tcl_lock) {\
237+
if (tcl_lock) { \
238238
if (tcl_lock_thread_ident == PyThread_get_thread_ident()) { \
239239
tcl_lock_reentry_count++; \
240240
if(!tcl_lock_reentry_count) \
241241
Py_FatalError("Tcl lock reentry count overflow"); \
242242
} else { \
243243
PyThread_acquire_lock(tcl_lock, 1); \
244244
tcl_lock_thread_ident = PyThread_get_thread_ident(); \
245-
}\
245+
} \
246246
}
247247

248248
#define RELEASE_TCL_LOCK \
249-
if (tcl_lock){\
249+
if (tcl_lock) { \
250250
if (tcl_lock_reentry_count) { \
251251
tcl_lock_reentry_count--; \
252252
} else { \
253253
tcl_lock_thread_ident = 0; \
254254
PyThread_release_lock(tcl_lock); \
255-
}\
255+
} \
256256
}
257257

258258

@@ -261,18 +261,19 @@ if (tcl_lock){\
261261
ENTER_TCL_CUSTOM_TSTATE(tstate)
262262

263263
#define ENTER_TCL_CUSTOM_TSTATE(tstate) \
264-
Py_BEGIN_ALLOW_THREADS \
265-
ACQUIRE_TCL_LOCK; tcl_tstate = tstate;
264+
Py_BEGIN_ALLOW_THREADS \
265+
ACQUIRE_TCL_LOCK \
266+
tcl_tstate = tstate;
266267

267268
#define LEAVE_TCL \
268269
tcl_tstate = NULL; \
269270
RELEASE_TCL_LOCK; Py_END_ALLOW_THREADS}
270271

271272
#define LEAVE_TCL_WITH_BUSYWAIT(result) \
272273
tcl_tstate = NULL; \
273-
RELEASE_TCL_LOCK;\
274-
if (result == 0)\
275-
Sleep(Tkinter_busywaitinterval);\
274+
RELEASE_TCL_LOCK \
275+
if (result == 0) \
276+
Sleep(Tkinter_busywaitinterval); \
276277
Py_END_ALLOW_THREADS
277278

278279

@@ -283,7 +284,7 @@ if (tcl_lock){\
283284
Py_BEGIN_ALLOW_THREADS
284285

285286
#define LEAVE_OVERLAP_TCL \
286-
tcl_tstate = NULL; RELEASE_TCL_LOCK; }
287+
tcl_tstate = NULL; RELEASE_TCL_LOCK }
287288

288289
#define ENTER_PYTHON \
289290
{ PyThreadState *tstate = tcl_tstate; tcl_tstate = NULL; PyEval_RestoreThread((tstate)); }
@@ -1844,7 +1845,7 @@ SetVar(PyObject *self, PyObject *args, int flags)
18441845
if (newval) {
18451846
LEAVE_OVERLAP
18461847
ok = Tcl_SetVar2Ex(Tkapp_Interp(self), name1, NULL,
1847-
newval, flags);
1848+
newval, flags);
18481849
ENTER_OVERLAP
18491850
if (!ok)
18501851
Tkinter_Error(self);
@@ -1867,7 +1868,7 @@ SetVar(PyObject *self, PyObject *args, int flags)
18671868
if (newval) {
18681869
LEAVE_OVERLAP
18691870
ok = Tcl_SetVar2Ex(Tkapp_Interp(self), name1, name2,
1870-
newval, flags);
1871+
newval, flags);
18711872
ENTER_OVERLAP
18721873
if (!ok)
18731874
Tkinter_Error(self);
@@ -2398,8 +2399,8 @@ PythonCmd(ClientData clientData, Tcl_Interp *interp, int argc, const char *argv[
23982399
ENTER_PYTHON
23992400

24002401
/* TBD: no error checking here since we know, via the
2401-
* Tkapp_CreateCommand() that the client data is a two-tuple
2402-
*/
2402+
* Tkapp_CreateCommand() that the client data is a two-tuple
2403+
*/
24032404
func = data->func;
24042405

24052406
/* Create argument list (argv1, ..., argvN) */
@@ -2421,14 +2422,14 @@ PythonCmd(ClientData clientData, Tcl_Interp *interp, int argc, const char *argv[
24212422
return PythonCmd_Error();
24222423

24232424
obj_res = AsObj(res);
2424-
if (obj_res) {
2425-
Tcl_SetObjResult(interp, obj_res);
2426-
rv = TCL_OK;
2427-
}
24282425
if (obj_res == NULL) {
24292426
Py_DECREF(res);
24302427
return PythonCmd_Error();
24312428
}
2429+
else {
2430+
Tcl_SetObjResult(interp, obj_res);
2431+
rv = TCL_OK;
2432+
}
24322433

24332434
Py_DECREF(res);
24342435

0 commit comments

Comments
 (0)