Skip to content

Commit 118abde

Browse files
committed
Fix test
1 parent 8e2e55e commit 118abde

File tree

1 file changed

+8
-16
lines changed

1 file changed

+8
-16
lines changed

Lib/test/test_winreg.py

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import gc
55
import os, sys, errno
6+
import itertools
67
import threading
78
import unittest
89
from platform import machine, win32_edition
@@ -322,42 +323,33 @@ def test_queryvalueex_race_condition(self):
322323
# gh-142282: QueryValueEx could read garbage buffer under race
323324
# condition when another thread changes the value size
324325
done = False
325-
error_found = None
326+
ready = threading.Event()
326327
values = [b'ham', b'spam']
327328

328329
class WriterThread(threading.Thread):
329330
def run(self):
330331
with CreateKey(HKEY_CURRENT_USER, test_key_name) as key:
331-
use_first = True
332+
values_iter = itertools.cycle(values)
332333
while not done:
333-
val = values[0] if use_first else values[1]
334-
use_first = not use_first
334+
val = next(values_iter)
335335
SetValueEx(key, 'test_value', 0, REG_BINARY, val)
336+
ready.set()
336337

337338
thread = WriterThread()
338339
thread.start()
339340
try:
341+
ready.wait()
340342
with CreateKey(HKEY_CURRENT_USER, test_key_name) as key:
341343
for _ in range(1000):
342-
try:
343-
result, typ = QueryValueEx(key, 'test_value')
344-
except FileNotFoundError:
345-
# Value not yet created
346-
continue
344+
result, typ = QueryValueEx(key, 'test_value')
347345
# The result must be one of the written values,
348346
# not garbage data from uninitialized buffer
349-
if result not in values:
350-
error_found = result
351-
break
347+
self.assertIn(result, values, f"QueryValueEx returned unexpected value: {result!r}")
352348
finally:
353349
done = True
354350
thread.join()
355351
DeleteKey(HKEY_CURRENT_USER, test_key_name)
356352

357-
if error_found is not None:
358-
self.fail(f"QueryValueEx returned unexpected value: {error_found!r}, "
359-
f"expected one of {values}")
360-
361353
def test_long_key(self):
362354
# Issue2810, in 2.6 and 3.1 when the key name was exactly 256
363355
# characters, EnumKey raised "WindowsError: More data is

0 commit comments

Comments
 (0)