Skip to content

Commit 5ced0a9

Browse files
committed
cleanup
1 parent 6313b3d commit 5ced0a9

5 files changed

Lines changed: 32 additions & 37 deletions

File tree

Lib/test/test_free_threading/test_threading_iter_locked.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ def work(it):
4141

4242
data = tuple(range(400))
4343
for it in range(number_of_iterations):
44+
print(f'test_iter_locked {it=}')
4445
iter_locked_iterator = iter_locked(non_atomic_iterator(data,))
4546
worker_threads = []
4647
for ii in range(number_of_threads):

Lib/test/test_threading.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2418,18 +2418,13 @@ def run_last():
24182418

24192419
class LockedIterTests(unittest.TestCase):
24202420

2421-
def test_locked_iter(self):
2422-
for s in ("123", "", range(1000), ('do', 1.2), range(2000,2200,5)):
2423-
for g in (G, I, Ig, S, L, R):
2424-
seq = list(g(s))
2425-
expected = seq
2426-
actual = list(serialize(g(s)))
2427-
self.assertEqual(actual, expected)
2428-
self.assertRaises(TypeError, serialize, X(s))
2429-
self.assertRaises(TypeError, serialize, N(s))
2430-
self.assertRaises(ZeroDivisionError, list, serialize(E(s)))
2431-
for arg in [1, True, sys]:
2432-
self.assertRaises(TypeError, serialize, arg)
2421+
def test_iter_locked(self):
2422+
for s in ("123", [], [1, 2, 3], tuple(), (1, 2, 3)):
2423+
expected = list(s)
2424+
actual = list(threading.iter_locked(s))
2425+
self.assertEqual(actual, expected)
2426+
for arg in [1, None, True, sys]:
2427+
self.assertRaises(TypeError, threading.iter_locked, arg)
24332428

24342429
if __name__ == "__main__":
24352430
unittest.main()

Lib/threading.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
'Barrier', 'BrokenBarrierError', 'Timer', 'ThreadError',
3030
'setprofile', 'settrace', 'local', 'stack_size',
3131
'excepthook', 'ExceptHookArgs', 'gettrace', 'getprofile',
32-
'setprofile_all_threads','settrace_all_threads']
32+
'setprofile_all_threads','settrace_all_threads', 'iter_locked']
3333

3434
# Rename some stuff so "from threading import *" is safe
3535
_start_joinable_thread = _thread.start_joinable_thread
@@ -42,6 +42,7 @@
4242
get_ident = _thread.get_ident
4343
_get_main_thread_ident = _thread._get_main_thread_ident
4444
_is_main_interpreter = _thread._is_main_interpreter
45+
iter_locked = _thread.iter_locked
4546
try:
4647
get_native_id = _thread.get_native_id
4748
_HAVE_THREAD_NATIVE_ID = True

Modules/_threadmodule.c

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,7 @@ find_state_by_type(PyTypeObject *tp)
5656
return get_thread_state(mod);
5757
}
5858

59-
#define clinic_state() (find_state_by_type(type))
6059
#include "clinic/_threadmodule.c.h"
61-
#undef clinic_state
6260

6361
#ifdef MS_WINDOWS
6462
typedef HRESULT (WINAPI *PF_GET_THREAD_DESCRIPTION)(HANDLE, PCWSTR*);
@@ -70,10 +68,10 @@ static PF_SET_THREAD_DESCRIPTION pSetThreadDescription = NULL;
7068

7169
/*[clinic input]
7270
module _thread
73-
class _thread.iter_locked "iter_locked_object *" "clinic_state()->iter_locked_type"
71+
class _thread.iter_locked "iter_locked_object *" "find_state_by_type(type)->iter_locked_type"
7472
7573
[clinic start generated code]*/
76-
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=6c78d729dec7bf7e]*/
74+
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=cc495aee1743488d]*/
7775

7876

7977
// _ThreadHandle type
@@ -770,20 +768,24 @@ _thread_iter_locked_impl(PyTypeObject *type, PyObject *iterable)
770768
if (it == NULL)
771769
return NULL;
772770

773-
iter_locked_object *lz = (iter_locked_object *)type->tp_alloc(type, 0);
774-
lz->it = it;
771+
iter_locked_object *il = (iter_locked_object *)type->tp_alloc(type, 0);
772+
if (il == NULL) {
773+
Py_DECREF(it);
774+
return NULL;
775+
}
776+
il->it = it;
775777

776-
return (PyObject *)lz;
778+
return (PyObject *)il;
777779
}
778780

779781
static void
780782
iter_locked_dealloc(PyObject *op)
781783
{
782-
iter_locked_object *lz = iter_locked_object_CAST(op);
783-
PyTypeObject *tp = Py_TYPE(lz);
784-
PyObject_GC_UnTrack(lz);
785-
Py_XDECREF(lz->it);
786-
tp->tp_free(lz);
784+
iter_locked_object *il = iter_locked_object_CAST(op);
785+
PyTypeObject *tp = Py_TYPE(il);
786+
PyObject_GC_UnTrack(il);
787+
Py_DECREF(il->it);
788+
tp->tp_free(il);
787789
Py_DECREF(tp);
788790
}
789791

@@ -802,16 +804,12 @@ iter_locked_next(PyObject *op)
802804
iter_locked_object *lz = iter_locked_object_CAST(op);
803805
PyObject *result = NULL;
804806

805-
Py_BEGIN_CRITICAL_SECTION(lz->it); // or lock on op?
807+
Py_BEGIN_CRITICAL_SECTION(op); // lock on op or lz->it?
806808
PyObject *it = lz->it;
807-
if (it != NULL) {
808-
result = PyIter_Next(lz->it);
809-
if (result == NULL) {
810-
/* Note: StopIteration is already cleared by PyIter_Next() */
811-
if (PyErr_Occurred())
812-
return NULL;
813-
Py_CLEAR(lz->it);
814-
}
809+
result = PyIter_Next(it);
810+
if (result == NULL) {
811+
/* Note: StopIteration is already cleared by PyIter_Next() */
812+
/* If PyErr_Occurred() we will also return NULL*/
815813
}
816814
Py_END_CRITICAL_SECTION();
817815
return result;
@@ -2854,7 +2852,7 @@ thread_module_traverse(PyObject *module, visitproc visit, void *arg)
28542852
{
28552853
thread_module_state *state = get_thread_state(module);
28562854
Py_VISIT(state->excepthook_type);
2857-
Py_CLEAR(state->iter_locked_type);
2855+
Py_VISIT(state->iter_locked_type);
28582856
Py_VISIT(state->lock_type);
28592857
Py_VISIT(state->local_type);
28602858
Py_VISIT(state->local_dummy_type);

Modules/clinic/_threadmodule.c.h

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)