| 1 |
from pypy.module.thread.test.support import GenericTestThread |
| 2 |
|
| 3 |
|
| 4 |
class AppTestLocal(GenericTestThread): |
| 5 |
|
| 6 |
def test_local_1(self): |
| 7 |
import _thread |
| 8 |
import gc |
| 9 |
from _thread import _local as tlsobject |
| 10 |
freed = [] |
| 11 |
class X: |
| 12 |
def __del__(self): |
| 13 |
freed.append(1) |
| 14 |
|
| 15 |
ok = [] |
| 16 |
TLS1 = tlsobject() |
| 17 |
TLS2 = tlsobject() |
| 18 |
TLS1.aa = "hello" |
| 19 |
def f(i): |
| 20 |
success = False |
| 21 |
try: |
| 22 |
a = TLS1.aa = i |
| 23 |
b = TLS1.bbb = X() |
| 24 |
c = TLS2.cccc = i*3 |
| 25 |
d = TLS2.ddddd = X() |
| 26 |
self.busywait(0.05) |
| 27 |
assert TLS1.aa == a |
| 28 |
assert TLS1.bbb is b |
| 29 |
assert TLS2.cccc == c |
| 30 |
assert TLS2.ddddd is d |
| 31 |
success = True |
| 32 |
finally: |
| 33 |
ok.append(success) |
| 34 |
for i in range(20): |
| 35 |
_thread.start_new_thread(f, (i,)) |
| 36 |
self.waitfor(lambda: len(ok) == 20, delay=3) |
| 37 |
assert ok == 20*[True] |
| 38 |
gc.collect(); gc.collect(); gc.collect() |
| 39 |
|
| 40 |
self.waitfor(lambda: len(freed) >= 40, delay=20) |
| 41 |
assert len(freed) == 40 |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
assert TLS1.aa == "hello" |
| 49 |
|
| 50 |
|
| 51 |
def test_local_init(self): |
| 52 |
import _thread |
| 53 |
tags = ['???', 1, 2, 3, 4, 5, 54321] |
| 54 |
seen = [] |
| 55 |
|
| 56 |
raises(TypeError, _thread._local, a=1) |
| 57 |
raises(TypeError, _thread._local, 1) |
| 58 |
|
| 59 |
class X(_thread._local): |
| 60 |
def __init__(self, n): |
| 61 |
assert n == 42 |
| 62 |
self.tag = tags.pop() |
| 63 |
|
| 64 |
x = X(42) |
| 65 |
assert x.tag == 54321 |
| 66 |
assert x.tag == 54321 |
| 67 |
def f(): |
| 68 |
seen.append(x.tag) |
| 69 |
for i in range(5): |
| 70 |
_thread.start_new_thread(f, ()) |
| 71 |
self.waitfor(lambda: len(seen) == 5, delay=2) |
| 72 |
seen1 = seen[:] |
| 73 |
seen1.sort() |
| 74 |
assert seen1 == [1, 2, 3, 4, 5] |
| 75 |
assert tags == ['???'] |
| 76 |
|
| 77 |
def test_local_init2(self): |
| 78 |
import _thread |
| 79 |
|
| 80 |
class A(object): |
| 81 |
def __init__(self, n): |
| 82 |
assert n == 42 |
| 83 |
self.n = n |
| 84 |
class X(_thread._local, A): |
| 85 |
pass |
| 86 |
|
| 87 |
x = X(42) |
| 88 |
assert x.n == 42 |
| 89 |
|
| 90 |
def test_local_setdict(self): |
| 91 |
import _thread |
| 92 |
x = _thread._local() |
| 93 |
raises(AttributeError, "x.__dict__ = 42") |
| 94 |
raises(AttributeError, "x.__dict__ = {}") |
| 95 |
|
| 96 |
done = [] |
| 97 |
def f(n): |
| 98 |
x.spam = n |
| 99 |
assert x.__dict__["spam"] == n |
| 100 |
done.append(1) |
| 101 |
for i in range(5): |
| 102 |
_thread.start_new_thread(f, (i,)) |
| 103 |
self.waitfor(lambda: len(done) == 5, delay=2) |
| 104 |
assert len(done) == 5 |
| 105 |
|
| 106 |
def test_weakrefable(self): |
| 107 |
import _thread, weakref |
| 108 |
weakref.ref(_thread._local()) |
| 109 |
|
| 110 |
def test_local_is_not_immortal(self): |
| 111 |
import _thread, gc, time |
| 112 |
class Local(_thread._local): |
| 113 |
def __del__(self): |
| 114 |
done.append('del') |
| 115 |
done = [] |
| 116 |
def f(): |
| 117 |
assert not hasattr(l, 'foo') |
| 118 |
l.bar = 42 |
| 119 |
done.append('ok') |
| 120 |
self.waitfor(lambda: len(done) == 3, delay=8) |
| 121 |
l = Local() |
| 122 |
l.foo = 42 |
| 123 |
_thread.start_new_thread(f, ()) |
| 124 |
self.waitfor(lambda: len(done) == 1, delay=2) |
| 125 |
l = None |
| 126 |
gc.collect() |
| 127 |
assert done == ['ok', 'del'] |
| 128 |
done.append('shutdown') |
| 129 |
|
| 130 |
def test_local_caching(): |
| 131 |
from pypy.module.thread.os_local import Local |
| 132 |
class FakeSpace: |
| 133 |
def getexecutioncontext(self): |
| 134 |
return self.ec |
| 135 |
|
| 136 |
def getattr(*args): |
| 137 |
pass |
| 138 |
def call_obj_args(*args): |
| 139 |
pass |
| 140 |
def newdict(*args, **kwargs): |
| 141 |
return {} |
| 142 |
def wrap(self, obj): |
| 143 |
return obj |
| 144 |
newtext = wrap |
| 145 |
def type(self, obj): |
| 146 |
return type(obj) |
| 147 |
class config: |
| 148 |
class translation: |
| 149 |
rweakref = True |
| 150 |
|
| 151 |
class FakeEC: |
| 152 |
def __init__(self, space): |
| 153 |
self.space = space |
| 154 |
self._thread_local_objs = None |
| 155 |
space = FakeSpace() |
| 156 |
ec1 = FakeEC(space) |
| 157 |
space.ec = ec1 |
| 158 |
|
| 159 |
l = Local(space, None) |
| 160 |
assert l.last_dict is l.dicts[ec1] |
| 161 |
assert l.last_ec is ec1 |
| 162 |
d1 = l.getdict(space) |
| 163 |
assert d1 is l.last_dict |
| 164 |
|
| 165 |
ec2 = space.ec = FakeEC(space) |
| 166 |
d2 = l.getdict(space) |
| 167 |
assert l.last_dict is d2 |
| 168 |
assert d2 is l.dicts[ec2] |
| 169 |
assert l.last_ec is ec2 |
| 170 |
dicts = l.dicts |
| 171 |
l.dicts = "nope" |
| 172 |
assert l.getdict(space) is d2 |
| 173 |
l.dicts = dicts |
| 174 |
|
| 175 |
space.ec = ec1 |
| 176 |
assert l.getdict(space) is d1 |
| 177 |
l.dicts = "nope" |
| 178 |
assert l.getdict(space) is d1 |
| 179 |
l.dicts = dicts |
| 180 |
|