File tree 3 files changed +28
-1
lines changed
3 files changed +28
-1
lines changed Original file line number Diff line number Diff line change @@ -337,6 +337,26 @@ class RLockTests(BaseLockTests):
337
337
"""
338
338
Tests for recursive locks.
339
339
"""
340
+ def test_repr_count (self ):
341
+ # see gh-134322: check that count values are correct:
342
+ # when a rlock is just created,
343
+ # in a second thread when rlock is acquired in the main thread.
344
+ lock = self .locktype ()
345
+ self .assertIn ("count=0" , repr (lock ))
346
+ self .assertIn ("<unlocked" , repr (lock ))
347
+ lock .acquire ()
348
+ lock .acquire ()
349
+ self .assertIn ("count=2" , repr (lock ))
350
+ self .assertIn ("<locked" , repr (lock ))
351
+
352
+ result = []
353
+ def call_repr ():
354
+ result .append (repr (lock ))
355
+ with Bunch (call_repr , 1 ):
356
+ pass
357
+ self .assertIn ("count=2" , result [0 ])
358
+ self .assertIn ("<locked" , result [0 ])
359
+
340
360
def test_reacquire (self ):
341
361
lock = self .locktype ()
342
362
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 @@ -1225,7 +1225,13 @@ rlock_repr(PyObject *op)
1225
1225
rlockobject * self = rlockobject_CAST (op );
1226
1226
PyThread_ident_t owner = self -> lock .thread ;
1227
1227
int locked = rlock_locked_impl (self );
1228
- size_t count = self -> lock .level + 1 ;
1228
+ size_t count ;
1229
+ if (locked ) {
1230
+ count = self -> lock .level + 1 ;
1231
+ }
1232
+ else {
1233
+ count = 0 ;
1234
+ }
1229
1235
return PyUnicode_FromFormat (
1230
1236
"<%s %s object owner=%" PY_FORMAT_THREAD_IDENT_T " count=%zu at %p>" ,
1231
1237
locked ? "locked" : "unlocked" ,
You can’t perform that action at this time.
0 commit comments