File tree Expand file tree Collapse file tree 3 files changed +28
-1
lines changed Expand file tree Collapse file tree 3 files changed +28
-1
lines changed Original file line number Diff line number Diff line change @@ -332,6 +332,26 @@ class RLockTests(BaseLockTests):
332
332
"""
333
333
Tests for recursive locks.
334
334
"""
335
+ def test_repr_count (self ):
336
+ # see gh-134322: check that count values are correct:
337
+ # when a rlock is just created,
338
+ # in a second thread when rlock is acquired in the main thread.
339
+ lock = self .locktype ()
340
+ self .assertIn ("count=0" , repr (lock ))
341
+ self .assertIn ("<unlocked" , repr (lock ))
342
+ lock .acquire ()
343
+ lock .acquire ()
344
+ self .assertIn ("count=2" , repr (lock ))
345
+ self .assertIn ("<locked" , repr (lock ))
346
+
347
+ result = []
348
+ def call_repr ():
349
+ result .append (repr (lock ))
350
+ with Bunch (call_repr , 1 ):
351
+ pass
352
+ self .assertIn ("count=2" , result [0 ])
353
+ self .assertIn ("<locked" , result [0 ])
354
+
335
355
def test_reacquire (self ):
336
356
lock = self .locktype ()
337
357
lock .acquire ()
Original file line number Diff line number Diff line change @@ -34,6 +34,7 @@ class ModuleLockAsRLockTests:
34
34
# lock status in repr unsupported
35
35
test_repr = None
36
36
test_locked_repr = None
37
+ test_repr_count = None
37
38
38
39
def tearDown (self ):
39
40
for splitinit in init .values ():
Original file line number Diff line number Diff line change @@ -1208,7 +1208,13 @@ rlock_repr(PyObject *op)
1208
1208
rlockobject * self = rlockobject_CAST (op );
1209
1209
PyThread_ident_t owner = self -> lock .thread ;
1210
1210
int locked = rlock_locked_impl (self );
1211
- size_t count = self -> lock .level + 1 ;
1211
+ size_t count ;
1212
+ if (locked ) {
1213
+ count = self -> lock .level + 1 ;
1214
+ }
1215
+ else {
1216
+ count = 0 ;
1217
+ }
1212
1218
return PyUnicode_FromFormat (
1213
1219
"<%s %s object owner=%" PY_FORMAT_THREAD_IDENT_T " count=%zu at %p>" ,
1214
1220
locked ? "locked" : "unlocked" ,
You can’t perform that action at this time.
0 commit comments