Skip to content

Commit

Permalink
Stackless issue python#82: PEP-8 fixes and some IDE-warning fixes
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
Anselm Kruis committed Aug 24, 2016
1 parent f3a3d97 commit 04a33e3
Show file tree
Hide file tree
Showing 40 changed files with 975 additions and 514 deletions.
10 changes: 5 additions & 5 deletions Stackless/demo/autoscheduling.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import sys, stackless
import sys
import stackless


def schedule_cb(task):
if task:
task.insert()


def autoschedule(bytecodes=None):
if bytecodes is None:
bytecodes = sys.getcheckinterval()
Expand All @@ -22,15 +25,12 @@ def run_atomic(fn, *args, **kwargs):

def print_name(name, count):
print name, count

# Helpers
def runtask(name):
for ii in xrange(1000):
if ii % 50:
run_atomic(print_name, name, ii)


tasklets = [stackless.tasklet(runtask)(name) for name in ["one", "two", "three"]]
autoschedule()


15 changes: 8 additions & 7 deletions Stackless/demo/counter.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@
print numbers
# [16, 13, 12, 5, 6, 4, 7, 1, 9, 17, 15, 14, 10, 8, 0, 3, 11, 18, 2, 19]


def counter(n, ch):
for i in xrange(n):
schedule()
ch.send(n)
ch=stackless.channel()
for i in xrange(n):
schedule()
ch.send(n)

ch = stackless.channel()
for each in numbers:
stackless.tasklet(counter)(each, ch)
stackless.tasklet(counter)(each, ch)

stackless.run()
# now we should have a sorted chain of results in ch
while ch.queue:
print ch.receive()
print ch.receive()
11 changes: 9 additions & 2 deletions Stackless/demo/fakechannel.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import stackless


class MyChannel:

def __init__(self):
self.queue = []
self.balance = 0
self.temp = None

def send(self, data):
if self.balance < 0:
receiver = self.queue.pop(0)
Expand All @@ -17,6 +20,7 @@ def send(self, data):
self.queue.append((sender, data))
self.balance += 1
jump_off(sender)

def receive(self):
if self.balance > 0:
sender, retval = self.queue.pop(0)
Expand All @@ -30,16 +34,19 @@ def receive(self):
jump_off(receiver)
return self.temp


def jump_off(task):
stackless.tasklet().capture()
task.remove()
stackless.schedule()


def f1(ch):
for i in range(5):
ch.send(i)
print "done sending"


def f2(ch):
while 1:
data = ch.receive()
Expand All @@ -48,13 +55,13 @@ def f2(ch):
return
print "received", data


def test():
ch = MyChannel()
t2 = stackless.tasklet(f2)(ch)
t1 = stackless.tasklet(f1)(ch)
stackless.run()
return ch

if __name__=="__main__":
if __name__ == "__main__":
test()

13 changes: 10 additions & 3 deletions Stackless/demo/fakechannel2.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import stackless


class MyChannel:

def __init__(self):
self.queue = []
self.balance = 0

def send(self, data):
if self.balance < 0:
receiver = self.queue.pop(0)
Expand All @@ -16,6 +19,7 @@ def send(self, data):
self.queue.append(sender)
self.balance += 1
jump_off(sender, data)

def receive(self):
if self.balance > 0:
sender = self.queue.pop(0)
Expand All @@ -28,19 +32,22 @@ def receive(self):
receiver = stackless.current
self.queue.append(receiver)
self.balance -= 1
retval =jump_off(receiver)
retval = jump_off(receiver)
return retval


def jump_off(task, data=None):
stackless.tasklet().capture(data)
task.remove()
stackless.schedule()


def f1(ch):
for i in range(5):
ch.send(i)
print "done sending"


def f2(ch):
while 1:
data = ch.receive()
Expand All @@ -49,13 +56,13 @@ def f2(ch):
return
print "received", data


def test():
ch = MyChannel()
t2 = stackless.tasklet(f2)(ch)
t1 = stackless.tasklet(f1)(ch)
stackless.run()
return ch

if __name__=="__main__":
if __name__ == "__main__":
test()

3 changes: 2 additions & 1 deletion Stackless/demo/handledtasklet.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ def handler(self, *args,**kwds):

from stackless import *


class HandledTasklet(tasklet):

def __new__(self, func):
def handler(*args, **kwds):
try:
Expand Down
14 changes: 8 additions & 6 deletions Stackless/demo/tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from stackless import *
import traceback


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


class Mutex(object):

def __init__(self, capacity=1):
self.queue = channel()
self.capacity = capacity
Expand Down Expand Up @@ -72,7 +74,7 @@ def trace_function(frame, event, arg):
if frame.f_code.co_name in ('schedule_cb', 'channel_cb'):
return None
print(" trace_function: %s %s in %s, line %s" %
(stackless.current, event, frame.f_code.co_name, frame.f_lineno))
(stackless.current, event, frame.f_code.co_name, frame.f_lineno))
if event in ('call', 'line', 'exception'):
return trace_function
return None
Expand All @@ -83,7 +85,7 @@ def channel_cb(channel, tasklet, sending, willblock):
try:
tasklet.trace_function = None
print("Channel CB, tasklet %r, %s%s" %
(tasklet, ("recv", "send")[sending], ("", " will block")[willblock]))
(tasklet, ("recv", "send")[sending], ("", " will block")[willblock]))
finally:
tasklet.trace_function = tf

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

# Inform about the installed trace functions
#

#
prev_tf = current_tf if prev.frame is current_frame else prev.trace_function
next_tf = current_tf if next.frame is current_frame else next.trace_function
print(" Current trace functions: prev: %r, next: %r" %
(prev_tf, next_tf))
(prev_tf, next_tf))

# Eventually set a trace function
if next is not None:
Expand All @@ -126,7 +128,7 @@ def schedule_cb(prev, next):
# Set the "global" trace function for the tasklet
next.trace_function = tf
# Set the "local" trace function for each frame
# This is required, if the tasklet is already running
# This is required, if the tasklet is already running
frame = next.frame
if frame is current_frame:
frame = frame.f_back
Expand Down
5 changes: 4 additions & 1 deletion Stackless/test/bajo_crash.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import stackless


class A:

def T1(self):
print "T1"

Expand All @@ -9,6 +11,7 @@ def bug(self):
t1.remove()
stackless.schedule()


def Start():
try:
a = A()
Expand All @@ -21,4 +24,4 @@ def Start():
while stackless.getruncount() > 1:
this.next.kill()

Start()
Start()
8 changes: 6 additions & 2 deletions Stackless/test/bajo_crash2.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
import stackless


class B:

def __del__(self):
1 << 4 # good breakpoint for ceval :-)
1 << 4 # good breakpoint for ceval :-)
print "__del__"
# even this works now!
stackless.schedule()


class A:

def T2(self, b):
pass

def bug(self):
b = B()
t=stackless.tasklet(self.T2)(b)
t = stackless.tasklet(self.T2)(b)
t.remove()

a = A()
Expand Down
14 changes: 11 additions & 3 deletions Stackless/test/chantest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import stackless
import sys


def receiver(chan, name):
while 1:
try:
Expand All @@ -10,12 +14,14 @@ def receiver(chan, name):
chan.send("%s says bye" % name)
return

import stackless, sys

# the following two are here to check about the bad
# situation that main is blocked and no sender is available.


def test_recv():
c = stackless.channel()

def foo(c):
c.send(5)
t = stackless.tasklet(foo)(c)
Expand All @@ -24,8 +30,10 @@ def foo(c):
print 'second receive'
c.receive()


def test_send():
c = stackless.channel()

def foo(c):
c.receive()
stackless.schedule()
Expand All @@ -40,11 +48,11 @@ def foo(c):
t2 = stackless.tasklet(receiver)(chan, "dinky")
stackless.run()
try:
for i in 2,3,5,7, 42:
for i in 2, 3, 5, 7, 42:
print "sending", i
chan.send(i)
chan.send(i)
if i==7:
if i == 7:
print "sending Exception"
chan.send_exception(ValueError, i)
except ValueError:
Expand Down
15 changes: 9 additions & 6 deletions Stackless/test/rectest.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import sys
pr = 1


def f(x):
if x % 1000 == 0 and pr: print x
if x % 1000 == 0 and pr:
print x
if x:
f(x-1)
if x % 1000 == 0 and pr: print x
f(x - 1)
if x % 1000 == 0 and pr:
print x

# these are deprecated
##sys.enable_stackless(1)
##print "slicing interval =", sys.getslicinginterval()
# sys.enable_stackless(1)
# print "slicing interval =", sys.getslicinginterval()

sys.setrecursionlimit(sys.maxint)
sys.setrecursionlimit(min(sys.maxint, 2**31 - 1))
f(100000)
3 changes: 2 additions & 1 deletion Stackless/test/reduce_frame_issue61.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,21 @@

from __future__ import absolute_import, print_function, unicode_literals, division

import pickle
import stackless
from collections import namedtuple

FrameState = namedtuple("FrameState", ('f_code', 'valid', 'exec_name', 'f_globals', 'have_locals',
'f_locals', 'f_trace', 'exc_as_tuple', 'f_lasti', 'f_lineno',
'blockstack_as_tuple', 'localsplus_as_tuple'))


def reduce_current():
result = []

def reduce_current2():
"""This function has exactly one local variable (func), one cellvar (result2) and one freevar (result)"""
result2 = result

def func(current):
result2.append(stackless._wrap.frame.__reduce__(current.frame))
stackless.tasklet().bind(func, (stackless.current,)).run()
Expand Down
Loading

0 comments on commit 04a33e3

Please sign in to comment.