Skip to content

Commit d408a0f

Browse files
Improve test and update RC due to refactored logic
1 parent e39f73f commit d408a0f

2 files changed

Lines changed: 30 additions & 14 deletions

File tree

Lib/test/test_using.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,20 @@ def test_thread_creation(self):
171171
from using import PyronaThread as T
172172

173173
class Mutable: pass
174-
self.assertRaises(RuntimeError, T, kwargs = { 'target' : print, 'args' : (Mutable(),) })
175-
self.assertRaises(RuntimeError, T, kwargs = { 'target' : print, 'kwargs' : {'a' : Mutable()} })
176-
self.assertRaises(RuntimeError, T, kwargs = { 'target' : print, 'args' : (Mutable(),), 'kwargs' : {'a' : Mutable()} })
174+
self.assertRaises(RuntimeError, lambda x: T(target=print, args=(Mutable(),)), None)
175+
self.assertRaises(RuntimeError, lambda x: T(target=print, kwargs={'a' : Mutable()}), None)
176+
self.assertRaises(RuntimeError, lambda x: T(target=print, args=(Mutable(),), kwargs={'a' : Mutable()}), None)
177+
self.assertRaises(RuntimeError, lambda x: T(target=print, args=(Mutable(), 42)), None)
178+
self.assertRaises(RuntimeError, lambda x: T(target=print, args=(Mutable(), Cown())), None)
179+
self.assertRaises(RuntimeError, lambda x: T(target=print, args=(Mutable(), Region())), None)
177180

178-
T(target=print, args=(42, Cown(), Region()))
179181
T(target=print, kwargs={'imm' : 42, 'cown' : Cown(), 'region' : Region()})
182+
T(target=print, kwargs={'a': 42})
183+
T(target=print, kwargs={'a': Cown()})
184+
T(target=print, kwargs={'a': Region()})
185+
186+
T(target=print, args=(42, Cown(), Region()))
187+
T(target=print, args=(42,))
188+
T(target=print, args=(Cown(),))
189+
T(target=print, args=(Region(),))
180190
self.assertTrue(True) # To make sure we got here correctly

Lib/using.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ def decorator(func):
4949
# TODO: this creates a normal Python thread and ensures that all its
5050
# arguments are moved to the new thread. Eventually we should revisit
5151
# this behaviour as we go multiple interpreters / multicore.
52+
# TODO: require RC to be one less when move is upstreamed
5253
def PyronaThread(group=None, target=None, name=None,
5354
args=(), kwargs=None, *, daemon=None):
5455
# Only check when a program uses pyrona
@@ -66,31 +67,36 @@ def ok_share(o):
6667
return False
6768
def ok_move(o):
6869
if isinstance(o, Region):
69-
if rc(o) != 4:
70+
if rc(o) != 5:
7071
# rc = 4 because:
7172
# 1. ref to o in rc
72-
# 2. ref to o on this frame
73-
# 3. ref to o on the calling frame
74-
# 4. ref to o from kwargs dictionary or args tuple/list
73+
# 2. ref to o on this frame (ok_move)
74+
# 3. ref to o on the calling frame (check)
75+
# 4. ref to o from iteration over kwargs dictionary or args tuple/list
76+
# 5. ref to o from kwargs dictionary or args tuple/list
7577
raise RuntimeError("Region passed to thread was not moved into thread")
7678
if o.is_open():
7779
raise RuntimeError("Region passed to thread was open")
7880
return True
7981
return False
8082

8183
def check(a, args):
82-
# rc(args) == 3 because we need to know that the args list is moved into the thread too
83-
# rc = 3 because:
84+
# rc(args) == 4 because we need to know that the args list is moved into the thread too
85+
# rc = 4 because:
8486
# 1. ref to args in rc
8587
# 2. ref to args on this frame
8688
# 3. ref to args on the calling framedef check(a, args):
87-
if not ok_share(a) or (ok_move(a) and rc(args) == 3):
89+
# 4. ref from frame calling PyronaThread -- FIXME: not valid; revisit after #45
90+
if not (ok_share(a) or (ok_move(a) and rc(args) == 4)):
8891
raise RuntimeError("Thread was passed an object which was neither immutable, a cown, or a unique region")
92+
8993
if kwargs is None:
9094
for a in args:
9195
check(a, args)
92-
return Thread(group, target, name, args, daemon)
96+
return Thread(group, target, name, args, daemon)
9397
else:
9498
for k in kwargs:
95-
check(k, kwargs)
96-
return Thread(group, target, name, kwargs, daemon)
99+
# Important to get matching RCs in both paths
100+
v = kwargs[k]
101+
check(v, kwargs)
102+
return Thread(group, target, name, kwargs, daemon)

0 commit comments

Comments
 (0)