Skip to content

Commit 8e1d679

Browse files
author
allendowney
committed
Adding while loops to Sync.py
1 parent 1303c7a commit 8e1d679

2 files changed

Lines changed: 22 additions & 24 deletions

File tree

‎python2/Sync.py‎

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -534,9 +534,15 @@ def create_thread(self):
534534
new = Thread(self)
535535
return new
536536

537-
# iterating through a Column is the same as iterating
538-
# through its list of rows.
539-
def __iter__(self): return self.rows.__iter__()
537+
def next_row(self, row):
538+
if row is None:
539+
return self.rows[0]
540+
541+
index = self.rows.index(row)
542+
try:
543+
return self.rows[index+1]
544+
except IndexError:
545+
return None
540546

541547

542548
class TopColumn(Column):
@@ -648,9 +654,6 @@ def __init__(self, column, name=None):
648654
self.sync.register(self)
649655
self.start()
650656

651-
def get_column(self):
652-
return self.column
653-
654657
def __str__(self):
655658
return '<' + self.name + '>'
656659

@@ -668,9 +671,11 @@ def dequeue(self):
668671

669672
def jump_to(self, row):
670673
"""Removes this thread from its current row and moves it to row."""
671-
self.row.remove_thread(self)
674+
if self.row:
675+
self.row.remove_thread(self)
672676
self.row = row
673-
self.row.add_thread(self)
677+
if self.row:
678+
self.row.add_thread(self)
674679

675680
def balk(self):
676681
self.row.remove_thread(self)
@@ -679,7 +684,6 @@ def balk(self):
679684
def start(self):
680685
"""Moves this thread to the top of the column."""
681686
self.queued = False
682-
self.iter = self.column.__iter__()
683687
self.row = None
684688
self.next_loop()
685689

@@ -694,17 +698,8 @@ def next_row(self):
694698
if self.queued:
695699
return
696700

697-
if self.row:
698-
self.row.remove_thread(self)
699-
else:
700-
# this probably means we executed balk
701-
self.iter = self.column.__iter__()
702-
703-
try:
704-
self.row = self.iter.next()
705-
self.row.add_thread(self)
706-
except StopIteration:
707-
self.row = None
701+
row = self.column.next_row(self.row)
702+
self.jump_to(row)
708703

709704
def skip_body(self):
710705
"""Skips the body of a conditional."""

‎python2/Sync_test.py‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111

1212
class Tests(unittest.TestCase):
1313

14-
def xtest_sync_mutex(self):
14+
def test_sync_mutex(self):
1515
sync = Sync.Sync(['mutex.py'])
1616

1717
threads = sync.get_threads()
1818
threadA = threads[0]
19-
column = threadA.get_column()
19+
column = threadA.column
2020

2121
source = threadA.step()
2222
self.assertEqual(source, 'mutex.wait()')
@@ -42,7 +42,7 @@ def xtest_sync_mutex(self):
4242
source = threadA.exec_line('pid = pid()', sync)
4343
self.assertEqual(sync.locals['pid'], 'A')
4444

45-
def xtest_sync_conditional(self):
45+
def test_sync_conditional(self):
4646
sync = Sync.Sync(['sync_code/conditional.py'])
4747
threads = sync.get_threads()
4848
threadA = threads[0]
@@ -90,7 +90,10 @@ def test_sync_while(self):
9090
self.assertEqual(source, ' counter += 1')
9191

9292
source = threadA.step()
93-
print source
93+
self.assertEqual(source, 'while counter < 1:')
94+
95+
source = threadA.step()
96+
self.assertEqual(source, 'pass')
9497

9598

9699
if __name__ == '__main__':

0 commit comments

Comments
 (0)