from ast import Or
from functools import partial
-from threading import Thread
+from threading import Barrier, Thread
from unittest import TestCase
try:
for ref in thread_list:
self.assertIsNone(ref())
+ def test_racing_get_set_dict(self):
+ """Races getting and setting a dict should be thread safe"""
+ THREAD_COUNT = 10
+ barrier = Barrier(THREAD_COUNT)
+ def work(d):
+ barrier.wait()
+ for _ in range(1000):
+ d[10] = 0
+ d.get(10, None)
+ _ = d[10]
+
+ d = {}
+ worker_threads = []
+ for ii in range(THREAD_COUNT):
+ worker_threads.append(Thread(target=work, args=[d]))
+ for t in worker_threads:
+ t.start()
+ for t in worker_threads:
+ t.join()
+
+
def test_racing_set_object_dict(self):
"""Races assigning to __dict__ should be thread safe"""
class C: pass
}
default_value = args[1];
skip_optional:
- Py_BEGIN_CRITICAL_SECTION(self);
return_value = dict_get_impl((PyDictObject *)self, key, default_value);
- Py_END_CRITICAL_SECTION();
exit:
return return_value;
{
return dict_values_impl((PyDictObject *)self);
}
-/*[clinic end generated code: output=4956c5b276ea652f input=a9049054013a1b77]*/
+/*[clinic end generated code: output=0f04bf0e7e6b130f input=a9049054013a1b77]*/
}
/*[clinic input]
-@critical_section
dict.get
key: object
static PyObject *
dict_get_impl(PyDictObject *self, PyObject *key, PyObject *default_value)
-/*[clinic end generated code: output=bba707729dee05bf input=a631d3f18f584c60]*/
+/*[clinic end generated code: output=bba707729dee05bf input=279ddb5790b6b107]*/
{
PyObject *val = NULL;
Py_hash_t hash;