Skip to content

Commit ebf0a5a

Browse files
sharedonly -> strictequiv
1 parent 8c25b2f commit ebf0a5a

File tree

2 files changed

+66
-46
lines changed

2 files changed

+66
-46
lines changed

Lib/test/support/interpreters/queues.py

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,15 @@ class QueueFull(_queues.QueueFull, queue.Full):
3535
_SHARED_ONLY = 0
3636
_PICKLED = 1
3737

38-
def create(maxsize=0, *, sharedonly=False):
38+
def create(maxsize=0, *, strictequiv=False):
3939
"""Return a new cross-interpreter queue.
4040
4141
The queue may be used to pass data safely between interpreters.
4242
43-
"sharedonly" sets the default for Queue.put() and Queue.put_nowait().
43+
"strictequiv" sets the default for Queue.put()
44+
and Queue.put_nowait().
4445
"""
45-
fmt = _SHARED_ONLY if sharedonly else _PICKLED
46+
fmt = _SHARED_ONLY if strictequiv else _PICKLED
4647
qid = _queues.create(maxsize, fmt)
4748
return Queue(qid, _fmt=fmt)
4849

@@ -114,22 +115,41 @@ def qsize(self):
114115
return _queues.get_count(self._id)
115116

116117
def put(self, obj, timeout=None, *,
117-
sharedonly=None,
118+
strictequiv=None,
118119
_delay=10 / 1000, # 10 milliseconds
119120
):
120121
"""Add the object to the queue.
121122
122123
This blocks while the queue is full.
123124
124-
If "sharedonly" is true then the object must be "shareable".
125-
It will be passed through the queue efficiently. If false then
126-
all objects are supported, at the expense of worse performance.
127-
If None (the default) then it uses the queue's default.
125+
If "strictequiv" is None (the default) then it uses the
126+
queue's default, set with create_queue()..
127+
128+
If "strictequiv" is false then all objects are supported,
129+
at the expense of worse performance.
130+
131+
If "strictequiv" is true then the corresponding object returned
132+
from Queue.get() will be strictly equivalent to the given obj.
133+
In other words, the two objects will be indistinguishable from
134+
each other, even if the object is mutable. The received object
135+
may actually be the same object, or a copy (immutable values
136+
only), or a proxy.
137+
138+
Regardless, the received object should be treated as though
139+
the original has been shared directly, whether or not it
140+
actually is. That’s a slightly different and stronger promise
141+
than just equality.
142+
143+
This stricter guarantee requires that the provided object
144+
must be "shareable". Examples of "shareable" types include
145+
the builtin singletons, str, and memoryview. An additional
146+
benefit is that such objects will be passed through the queue
147+
efficiently.
128148
"""
129-
if sharedonly is None:
149+
if strictequiv is None:
130150
fmt = self._fmt
131151
else:
132-
fmt = _SHARED_ONLY if sharedonly else _PICKLED
152+
fmt = _SHARED_ONLY if strictequiv else _PICKLED
133153
if timeout is not None:
134154
timeout = int(timeout)
135155
if timeout < 0:
@@ -148,11 +168,11 @@ def put(self, obj, timeout=None, *,
148168
else:
149169
break
150170

151-
def put_nowait(self, obj, *, sharedonly=None):
152-
if sharedonly is None:
171+
def put_nowait(self, obj, *, strictequiv=None):
172+
if strictequiv is None:
153173
fmt = self._fmt
154174
else:
155-
fmt = _SHARED_ONLY if sharedonly else _PICKLED
175+
fmt = _SHARED_ONLY if strictequiv else _PICKLED
156176
if fmt is _PICKLED:
157177
obj = pickle.dumps(obj)
158178
try:

Lib/test/test_interpreters/test_queues.py

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,13 @@ def test_shareable(self):
5858

5959
with self.subTest('same interpreter'):
6060
queue2 = queues.create()
61-
queue1.put(queue2, sharedonly=True)
61+
queue1.put(queue2, strictequiv=True)
6262
queue3 = queue1.get()
6363
self.assertIs(queue3, queue2)
6464

6565
with self.subTest('from current interpreter'):
6666
queue4 = queues.create()
67-
queue1.put(queue4, sharedonly=True)
67+
queue1.put(queue4, strictequiv=True)
6868
out = _run_output(interp, dedent("""
6969
queue4 = queue1.get()
7070
print(queue4.id)
@@ -75,7 +75,7 @@ def test_shareable(self):
7575
with self.subTest('from subinterpreter'):
7676
out = _run_output(interp, dedent("""
7777
queue5 = queues.create()
78-
queue1.put(queue5, sharedonly=True)
78+
queue1.put(queue5, strictequiv=True)
7979
print(queue5.id)
8080
"""))
8181
qid = int(out)
@@ -118,7 +118,7 @@ class TestQueueOps(TestBase):
118118
def test_empty(self):
119119
queue = queues.create()
120120
before = queue.empty()
121-
queue.put(None, sharedonly=True)
121+
queue.put(None, strictequiv=True)
122122
during = queue.empty()
123123
queue.get()
124124
after = queue.empty()
@@ -133,7 +133,7 @@ def test_full(self):
133133
queue = queues.create(3)
134134
for _ in range(3):
135135
actual.append(queue.full())
136-
queue.put(None, sharedonly=True)
136+
queue.put(None, strictequiv=True)
137137
actual.append(queue.full())
138138
for _ in range(3):
139139
queue.get()
@@ -147,16 +147,16 @@ def test_qsize(self):
147147
queue = queues.create()
148148
for _ in range(3):
149149
actual.append(queue.qsize())
150-
queue.put(None, sharedonly=True)
150+
queue.put(None, strictequiv=True)
151151
actual.append(queue.qsize())
152152
queue.get()
153153
actual.append(queue.qsize())
154-
queue.put(None, sharedonly=True)
154+
queue.put(None, strictequiv=True)
155155
actual.append(queue.qsize())
156156
for _ in range(3):
157157
queue.get()
158158
actual.append(queue.qsize())
159-
queue.put(None, sharedonly=True)
159+
queue.put(None, strictequiv=True)
160160
actual.append(queue.qsize())
161161
queue.get()
162162
actual.append(queue.qsize())
@@ -165,9 +165,9 @@ def test_qsize(self):
165165

166166
def test_put_get_main(self):
167167
expected = list(range(20))
168-
for sharedonly in (True, False):
169-
kwds = dict(sharedonly=sharedonly)
170-
with self.subTest(f'sharedonly={sharedonly}'):
168+
for strictequiv in (True, False):
169+
kwds = dict(strictequiv=strictequiv)
170+
with self.subTest(f'strictequiv={strictequiv}'):
171171
queue = queues.create()
172172
for i in range(20):
173173
queue.put(i, **kwds)
@@ -176,9 +176,9 @@ def test_put_get_main(self):
176176
self.assertEqual(actual, expected)
177177

178178
def test_put_timeout(self):
179-
for sharedonly in (True, False):
180-
kwds = dict(sharedonly=sharedonly)
181-
with self.subTest(f'sharedonly={sharedonly}'):
179+
for strictequiv in (True, False):
180+
kwds = dict(strictequiv=strictequiv)
181+
with self.subTest(f'strictequiv={strictequiv}'):
182182
queue = queues.create(2)
183183
queue.put(None, **kwds)
184184
queue.put(None, **kwds)
@@ -188,9 +188,9 @@ def test_put_timeout(self):
188188
queue.put(None, **kwds)
189189

190190
def test_put_nowait(self):
191-
for sharedonly in (True, False):
192-
kwds = dict(sharedonly=sharedonly)
193-
with self.subTest(f'sharedonly={sharedonly}'):
191+
for strictequiv in (True, False):
192+
kwds = dict(strictequiv=strictequiv)
193+
with self.subTest(f'strictequiv={strictequiv}'):
194194
queue = queues.create(2)
195195
queue.put_nowait(None, **kwds)
196196
queue.put_nowait(None, **kwds)
@@ -199,7 +199,7 @@ def test_put_nowait(self):
199199
queue.get()
200200
queue.put_nowait(None, **kwds)
201201

202-
def test_put_sharedonly(self):
202+
def test_put_strictequiv(self):
203203
for obj in [
204204
None,
205205
True,
@@ -210,7 +210,7 @@ def test_put_sharedonly(self):
210210
]:
211211
with self.subTest(repr(obj)):
212212
queue = queues.create()
213-
queue.put(obj, sharedonly=True)
213+
queue.put(obj, strictequiv=True)
214214
obj2 = queue.get()
215215
self.assertEqual(obj2, obj)
216216

@@ -221,9 +221,9 @@ def test_put_sharedonly(self):
221221
with self.subTest(repr(obj)):
222222
queue = queues.create()
223223
with self.assertRaises(interpreters.NotShareableError):
224-
queue.put(obj, sharedonly=True)
224+
queue.put(obj, strictequiv=True)
225225

226-
def test_put_not_sharedonly(self):
226+
def test_put_not_strictequiv(self):
227227
for obj in [
228228
None,
229229
True,
@@ -237,7 +237,7 @@ def test_put_not_sharedonly(self):
237237
]:
238238
with self.subTest(repr(obj)):
239239
queue = queues.create()
240-
queue.put(obj, sharedonly=False)
240+
queue.put(obj, strictequiv=False)
241241
obj2 = queue.get()
242242
self.assertEqual(obj2, obj)
243243

@@ -251,9 +251,9 @@ def test_get_nowait(self):
251251
with self.assertRaises(queues.QueueEmpty):
252252
queue.get_nowait()
253253

254-
def test_put_get_default_sharedonly(self):
254+
def test_put_get_default_strictequiv(self):
255255
expected = list(range(20))
256-
queue = queues.create(sharedonly=True)
256+
queue = queues.create(strictequiv=True)
257257
for i in range(20):
258258
queue.put(i)
259259
actual = [queue.get() for _ in range(20)]
@@ -264,9 +264,9 @@ def test_put_get_default_sharedonly(self):
264264
with self.assertRaises(interpreters.NotShareableError):
265265
queue.put(obj)
266266

267-
def test_put_get_default_not_sharedonly(self):
267+
def test_put_get_default_not_strictequiv(self):
268268
expected = list(range(20))
269-
queue = queues.create(sharedonly=False)
269+
queue = queues.create(strictequiv=False)
270270
for i in range(20):
271271
queue.put(i)
272272
actual = [queue.get() for _ in range(20)]
@@ -285,7 +285,7 @@ def test_put_get_same_interpreter(self):
285285
from test.support.interpreters import queues
286286
queue = queues.create()
287287
orig = b'spam'
288-
queue.put(orig, sharedonly=True)
288+
queue.put(orig, strictequiv=True)
289289
obj = queue.get()
290290
assert obj == orig, 'expected: obj == orig'
291291
assert obj is not orig, 'expected: obj is not orig'
@@ -298,7 +298,7 @@ def test_put_get_different_interpreters(self):
298298
self.assertEqual(len(queues.list_all()), 2)
299299

300300
obj1 = b'spam'
301-
queue1.put(obj1, sharedonly=True)
301+
queue1.put(obj1, strictequiv=True)
302302

303303
out = _run_output(
304304
interp,
@@ -315,7 +315,7 @@ def test_put_get_different_interpreters(self):
315315
obj2 = b'eggs'
316316
print(id(obj2))
317317
assert queue2.qsize() == 0, 'expected: queue2.qsize() == 0'
318-
queue2.put(obj2, sharedonly=True)
318+
queue2.put(obj2, strictequiv=True)
319319
assert queue2.qsize() == 1, 'expected: queue2.qsize() == 1'
320320
"""))
321321
self.assertEqual(len(queues.list_all()), 2)
@@ -337,8 +337,8 @@ def test_put_cleared_with_subinterpreter(self):
337337
queue = queues.Queue({queue.id})
338338
obj1 = b'spam'
339339
obj2 = b'eggs'
340-
queue.put(obj1, sharedonly=True)
341-
queue.put(obj2, sharedonly=True)
340+
queue.put(obj1, strictequiv=True)
341+
queue.put(obj2, strictequiv=True)
342342
"""))
343343
self.assertEqual(queue.qsize(), 2)
344344

@@ -360,12 +360,12 @@ def f():
360360
break
361361
except queues.QueueEmpty:
362362
continue
363-
queue2.put(obj, sharedonly=True)
363+
queue2.put(obj, strictequiv=True)
364364
t = threading.Thread(target=f)
365365
t.start()
366366

367367
orig = b'spam'
368-
queue1.put(orig, sharedonly=True)
368+
queue1.put(orig, strictequiv=True)
369369
obj = queue2.get()
370370
t.join()
371371

0 commit comments

Comments
 (0)