Skip to content

Commit b0836c1

Browse files
committed
fix: fix comment problem
1 parent 437f14f commit b0836c1

2 files changed

Lines changed: 3 additions & 23 deletions

File tree

Lib/test/test_defaultdict.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -191,54 +191,42 @@ def test_union(self):
191191

192192
@threading_helper.requires_working_threading()
193193
def test_no_value_overwrite_race_condition(self):
194-
"""Test that concurrent access to missing keys doesn't overwrite values."""
195-
# Use a factory that returns unique objects so we can detect overwrites
196194
call_count = 0
197195

198196
def unique_factory():
199197
nonlocal call_count
200198
call_count += 1
201-
# Return a unique object that identifies this call
202199
return f"value_{call_count}_{threading.get_ident()}"
203200

204201
d = defaultdict(unique_factory)
205202
results = {}
206203

207204
def worker(thread_id):
208-
# Multiple threads access the same missing key
209205
for _ in range(5):
210206
value = d['shared_key']
211207
if 'shared_key' not in results:
212208
results['shared_key'] = value
213-
# Small delay to increase chance of race conditions
214209
time.sleep(0.001)
215210

216-
# Start multiple threads
217211
threads = []
218212
for i in range(3):
219213
t = threading.Thread(target=worker, args=(i,))
220214
threads.append(t)
221215
t.start()
222216

223-
# Wait for all threads to complete
224217
for t in threads:
225218
t.join()
226219

227-
# Key should exist in the dictionary
228220
self.assertIn('shared_key', d)
229221

230-
# All threads should see the same value (no overwrites occurred)
231222
final_value = d['shared_key']
232223
self.assertEqual(results['shared_key'], final_value)
233224

234-
# The value should be from the first successful factory call
235225
self.assertTrue(final_value.startswith('value_'))
236226

237-
# Factory should only be called once (since key only inserted once)
238227
self.assertEqual(call_count, 1)
239228

240229
def test_factory_called_only_when_key_missing(self):
241-
"""Test that factory is only called when key is truly missing."""
242230
factory_calls = []
243231

244232
def tracked_factory():
@@ -247,17 +235,14 @@ def tracked_factory():
247235

248236
d = defaultdict(tracked_factory)
249237

250-
# First access should call factory
251238
value1 = d['key']
252239
self.assertEqual(value1, [1, 2, 3])
253240
initial_call_count = len(factory_calls)
254241

255-
# Multiple subsequent accesses should not call factory
256242
for _ in range(10):
257243
value = d['key']
258244
self.assertEqual(value, [1, 2, 3])
259245

260-
# Factory call count should not have increased
261246
self.assertEqual(len(factory_calls), initial_call_count)
262247

263248

Modules/_collectionsmodule.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2218,6 +2218,7 @@ defdict_missing(PyObject *op, PyObject *key)
22182218
{
22192219
defdictobject *dd = defdictobject_CAST(op);
22202220
PyObject *factory = dd->default_factory;
2221+
PyObject *value;
22212222
if (factory == NULL || factory == Py_None) {
22222223
/* XXX Call dict.__missing__(key) */
22232224
PyObject *tup;
@@ -2228,10 +2229,9 @@ defdict_missing(PyObject *op, PyObject *key)
22282229
return NULL;
22292230
}
22302231

2231-
PyObject *value = _PyObject_CallNoArgs(factory);
2232-
if (value == NULL) {
2232+
value = _PyObject_CallNoArgs(factory);
2233+
if (value == NULL)
22332234
return NULL;
2234-
}
22352235

22362236
/* Use PyDict_SetDefaultRef to atomically insert the value only if the key is absent.
22372237
* This ensures we don't overwrite a value that another thread inserted
@@ -2240,11 +2240,6 @@ defdict_missing(PyObject *op, PyObject *key)
22402240
PyObject *result_value = NULL;
22412241
int res = PyDict_SetDefaultRef(op, key, value, &result_value);
22422242

2243-
if (res < 0) {
2244-
Py_DECREF(value);
2245-
return NULL;
2246-
}
2247-
22482243
if (res != 0) {
22492244
Py_DECREF(value);
22502245
}

0 commit comments

Comments
 (0)