Skip to content

Commit 04a33e3

Browse files
author
Anselm Kruis
committed
Stackless issue python#82: PEP-8 fixes and some IDE-warning fixes
This change does not change the code in any way. The change formats all tests and some demos according to PEP-8. Additionally I fixed some missing imports of stackles.test_*-functions, moved imports to the top of the file, removed some duplicate imports and added some magic comments to silence my IDE (Eclipse Pydev). https://bitbucket.org/stackless-dev/stackless/issues/82 (grafted from 43fcfbe017a1456c7285819eee6a2c4984e20045)
1 parent f3a3d97 commit 04a33e3

40 files changed

+975
-514
lines changed

Stackless/demo/autoscheduling.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
import sys, stackless
1+
import sys
2+
import stackless
3+
24

35
def schedule_cb(task):
46
if task:
57
task.insert()
68

9+
710
def autoschedule(bytecodes=None):
811
if bytecodes is None:
912
bytecodes = sys.getcheckinterval()
@@ -22,15 +25,12 @@ def run_atomic(fn, *args, **kwargs):
2225

2326
def print_name(name, count):
2427
print name, count
25-
28+
2629
# Helpers
2730
def runtask(name):
2831
for ii in xrange(1000):
2932
if ii % 50:
3033
run_atomic(print_name, name, ii)
31-
3234

3335
tasklets = [stackless.tasklet(runtask)(name) for name in ["one", "two", "three"]]
3436
autoschedule()
35-
36-

Stackless/demo/counter.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,17 @@
1515
print numbers
1616
# [16, 13, 12, 5, 6, 4, 7, 1, 9, 17, 15, 14, 10, 8, 0, 3, 11, 18, 2, 19]
1717

18+
1819
def counter(n, ch):
19-
for i in xrange(n):
20-
schedule()
21-
ch.send(n)
22-
23-
ch=stackless.channel()
20+
for i in xrange(n):
21+
schedule()
22+
ch.send(n)
23+
24+
ch = stackless.channel()
2425
for each in numbers:
25-
stackless.tasklet(counter)(each, ch)
26+
stackless.tasklet(counter)(each, ch)
2627

2728
stackless.run()
2829
# now we should have a sorted chain of results in ch
2930
while ch.queue:
30-
print ch.receive()
31+
print ch.receive()

Stackless/demo/fakechannel.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import stackless
22

3+
34
class MyChannel:
5+
46
def __init__(self):
57
self.queue = []
68
self.balance = 0
79
self.temp = None
10+
811
def send(self, data):
912
if self.balance < 0:
1013
receiver = self.queue.pop(0)
@@ -17,6 +20,7 @@ def send(self, data):
1720
self.queue.append((sender, data))
1821
self.balance += 1
1922
jump_off(sender)
23+
2024
def receive(self):
2125
if self.balance > 0:
2226
sender, retval = self.queue.pop(0)
@@ -30,16 +34,19 @@ def receive(self):
3034
jump_off(receiver)
3135
return self.temp
3236

37+
3338
def jump_off(task):
3439
stackless.tasklet().capture()
3540
task.remove()
3641
stackless.schedule()
3742

43+
3844
def f1(ch):
3945
for i in range(5):
4046
ch.send(i)
4147
print "done sending"
4248

49+
4350
def f2(ch):
4451
while 1:
4552
data = ch.receive()
@@ -48,13 +55,13 @@ def f2(ch):
4855
return
4956
print "received", data
5057

58+
5159
def test():
5260
ch = MyChannel()
5361
t2 = stackless.tasklet(f2)(ch)
5462
t1 = stackless.tasklet(f1)(ch)
5563
stackless.run()
5664
return ch
5765

58-
if __name__=="__main__":
66+
if __name__ == "__main__":
5967
test()
60-

Stackless/demo/fakechannel2.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import stackless
22

3+
34
class MyChannel:
5+
46
def __init__(self):
57
self.queue = []
68
self.balance = 0
9+
710
def send(self, data):
811
if self.balance < 0:
912
receiver = self.queue.pop(0)
@@ -16,6 +19,7 @@ def send(self, data):
1619
self.queue.append(sender)
1720
self.balance += 1
1821
jump_off(sender, data)
22+
1923
def receive(self):
2024
if self.balance > 0:
2125
sender = self.queue.pop(0)
@@ -28,19 +32,22 @@ def receive(self):
2832
receiver = stackless.current
2933
self.queue.append(receiver)
3034
self.balance -= 1
31-
retval =jump_off(receiver)
35+
retval = jump_off(receiver)
3236
return retval
3337

38+
3439
def jump_off(task, data=None):
3540
stackless.tasklet().capture(data)
3641
task.remove()
3742
stackless.schedule()
3843

44+
3945
def f1(ch):
4046
for i in range(5):
4147
ch.send(i)
4248
print "done sending"
4349

50+
4451
def f2(ch):
4552
while 1:
4653
data = ch.receive()
@@ -49,13 +56,13 @@ def f2(ch):
4956
return
5057
print "received", data
5158

59+
5260
def test():
5361
ch = MyChannel()
5462
t2 = stackless.tasklet(f2)(ch)
5563
t1 = stackless.tasklet(f1)(ch)
5664
stackless.run()
5765
return ch
5866

59-
if __name__=="__main__":
67+
if __name__ == "__main__":
6068
test()
61-

Stackless/demo/handledtasklet.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ def handler(self, *args,**kwds):
1616

1717
from stackless import *
1818

19+
1920
class HandledTasklet(tasklet):
20-
21+
2122
def __new__(self, func):
2223
def handler(*args, **kwds):
2324
try:

Stackless/demo/tracing.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from stackless import *
44
import traceback
55

6+
67
def _tasklet__repr__(self):
78
try:
89
return "<tasklet %s>" % ("main" if self.is_main else self.name,)
@@ -23,6 +24,7 @@ def __new__(self, func, name=None):
2324

2425

2526
class Mutex(object):
27+
2628
def __init__(self, capacity=1):
2729
self.queue = channel()
2830
self.capacity = capacity
@@ -72,7 +74,7 @@ def trace_function(frame, event, arg):
7274
if frame.f_code.co_name in ('schedule_cb', 'channel_cb'):
7375
return None
7476
print(" trace_function: %s %s in %s, line %s" %
75-
(stackless.current, event, frame.f_code.co_name, frame.f_lineno))
77+
(stackless.current, event, frame.f_code.co_name, frame.f_lineno))
7678
if event in ('call', 'line', 'exception'):
7779
return trace_function
7880
return None
@@ -83,7 +85,7 @@ def channel_cb(channel, tasklet, sending, willblock):
8385
try:
8486
tasklet.trace_function = None
8587
print("Channel CB, tasklet %r, %s%s" %
86-
(tasklet, ("recv", "send")[sending], ("", " will block")[willblock]))
88+
(tasklet, ("recv", "send")[sending], ("", " will block")[willblock]))
8789
finally:
8890
tasklet.trace_function = tf
8991

@@ -109,12 +111,12 @@ def schedule_cb(prev, next):
109111
print("%sjumping from %s to %s" % (current_info, prev, next))
110112

111113
# Inform about the installed trace functions
112-
113-
#
114+
115+
#
114116
prev_tf = current_tf if prev.frame is current_frame else prev.trace_function
115117
next_tf = current_tf if next.frame is current_frame else next.trace_function
116118
print(" Current trace functions: prev: %r, next: %r" %
117-
(prev_tf, next_tf))
119+
(prev_tf, next_tf))
118120

119121
# Eventually set a trace function
120122
if next is not None:
@@ -126,7 +128,7 @@ def schedule_cb(prev, next):
126128
# Set the "global" trace function for the tasklet
127129
next.trace_function = tf
128130
# Set the "local" trace function for each frame
129-
# This is required, if the tasklet is already running
131+
# This is required, if the tasklet is already running
130132
frame = next.frame
131133
if frame is current_frame:
132134
frame = frame.f_back

Stackless/test/bajo_crash.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import stackless
22

3+
34
class A:
5+
46
def T1(self):
57
print "T1"
68

@@ -9,6 +11,7 @@ def bug(self):
911
t1.remove()
1012
stackless.schedule()
1113

14+
1215
def Start():
1316
try:
1417
a = A()
@@ -21,4 +24,4 @@ def Start():
2124
while stackless.getruncount() > 1:
2225
this.next.kill()
2326

24-
Start()
27+
Start()

Stackless/test/bajo_crash2.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
import stackless
22

3+
34
class B:
5+
46
def __del__(self):
5-
1 << 4 # good breakpoint for ceval :-)
7+
1 << 4 # good breakpoint for ceval :-)
68
print "__del__"
79
# even this works now!
810
stackless.schedule()
911

12+
1013
class A:
14+
1115
def T2(self, b):
1216
pass
1317

1418
def bug(self):
1519
b = B()
16-
t=stackless.tasklet(self.T2)(b)
20+
t = stackless.tasklet(self.T2)(b)
1721
t.remove()
1822

1923
a = A()

Stackless/test/chantest.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import stackless
2+
import sys
3+
4+
15
def receiver(chan, name):
26
while 1:
37
try:
@@ -10,12 +14,14 @@ def receiver(chan, name):
1014
chan.send("%s says bye" % name)
1115
return
1216

13-
import stackless, sys
1417

1518
# the following two are here to check about the bad
1619
# situation that main is blocked and no sender is available.
20+
21+
1722
def test_recv():
1823
c = stackless.channel()
24+
1925
def foo(c):
2026
c.send(5)
2127
t = stackless.tasklet(foo)(c)
@@ -24,8 +30,10 @@ def foo(c):
2430
print 'second receive'
2531
c.receive()
2632

33+
2734
def test_send():
2835
c = stackless.channel()
36+
2937
def foo(c):
3038
c.receive()
3139
stackless.schedule()
@@ -40,11 +48,11 @@ def foo(c):
4048
t2 = stackless.tasklet(receiver)(chan, "dinky")
4149
stackless.run()
4250
try:
43-
for i in 2,3,5,7, 42:
51+
for i in 2, 3, 5, 7, 42:
4452
print "sending", i
4553
chan.send(i)
4654
chan.send(i)
47-
if i==7:
55+
if i == 7:
4856
print "sending Exception"
4957
chan.send_exception(ValueError, i)
5058
except ValueError:

Stackless/test/rectest.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
import sys
22
pr = 1
33

4+
45
def f(x):
5-
if x % 1000 == 0 and pr: print x
6+
if x % 1000 == 0 and pr:
7+
print x
68
if x:
7-
f(x-1)
8-
if x % 1000 == 0 and pr: print x
9+
f(x - 1)
10+
if x % 1000 == 0 and pr:
11+
print x
912

1013
# these are deprecated
11-
##sys.enable_stackless(1)
12-
##print "slicing interval =", sys.getslicinginterval()
14+
# sys.enable_stackless(1)
15+
# print "slicing interval =", sys.getslicinginterval()
1316

14-
sys.setrecursionlimit(sys.maxint)
17+
sys.setrecursionlimit(min(sys.maxint, 2**31 - 1))
1518
f(100000)

0 commit comments

Comments
 (0)