New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Better repr for threading.Lock() #65336
Comments
It is really nice to have the open/closed status in the __repr__ of file objects: <open file 'data.txt', mode 'r' at 0x102c8ec90> I would like to have the same for locks: <unlocked thread.lock object at 0x1002b1330> This would be nice in logs and tracebacks for example. |
Here's a patch with a test. Example repr of threading.Lock(): $ ./python -c "import threading; print(threading.Lock())"
<unlocked _thread.lock object> |
The repr of threading._RLock contains owner and count, but not lock/unlock status. The repr of locks from _dummy_thread also should contain lock/unlock status. And it would be nice to have the open/closed status in the repr of other synchronization primitives: Condition, Semaphore, etc. As for tests, I think assertRegex() will produce more useful error report. |
And of course we should keep "at 0x..." part, because it is the way to distinguish different lock objects. |
I think it's ok to stay focussed and restrict this issue to threading.Lock (and perhaps RLock). Berker, do you want to provide an updated patch? |
Here's an updated patch. Thanks for the reviews.
Done. $ ./python -c "import threading; l = threading.Lock(); print(l)"
<unlocked _thread.lock object at 0x7f0a19e7b1f8>
Done. $ ./python -c "import threading; rl = threading.RLock(); rl.acquire(); print(rl)"
<locked _thread.RLock object owner=139769600231168 count=1>
Done. $ ./python -c "import dummy_threading as threading; l = threading.Lock(); print(l)"
<unlocked _dummy_thread.LockType object at 0x7fb334245400>
$ ./python -c "import dummy_threading as threading; l = threading.RLock(); print(l)"
<unlocked threading._RLock object owner=None count=0 at 0x7f524d0138e0>
Done. |
The patch is just about ready to go but needs to address some failing tests: ====================================================================== Traceback (most recent call last):
File "/Users/raymond/cpython/Lib/test/lock_tests.py", line 91, in test_locked_repr
self.assertRegex(repr(lock), "<locked .* object (.*)?at .*>")
AssertionError: Regex didn't match: '<locked .* object (.*)?at .*>' not found in "_ModuleLock('some_lock') at 4438138048" ====================================================================== Traceback (most recent call last):
File "/Users/raymond/cpython/Lib/test/lock_tests.py", line 85, in test_repr
self.assertRegex(repr(lock), "<unlocked .* object (.*)?at .*>")
AssertionError: Regex didn't match: '<unlocked .* object (.*)?at .*>' not found in "_ModuleLock('some_lock') at 4439151784" ====================================================================== Traceback (most recent call last):
File "/Users/raymond/cpython/Lib/test/lock_tests.py", line 91, in test_locked_repr
self.assertRegex(repr(lock), "<locked .* object (.*)?at .*>")
AssertionError: Regex didn't match: '<locked .* object (.*)?at .*>' not found in "_ModuleLock('some_lock') at 4438192312" ====================================================================== Traceback (most recent call last):
File "/Users/raymond/cpython/Lib/test/lock_tests.py", line 85, in test_repr
self.assertRegex(repr(lock), "<unlocked .* object (.*)?at .*>")
AssertionError: Regex didn't match: '<unlocked .* object (.*)?at .*>' not found in "_ModuleLock('some_lock') at 4438192480" Ran 970 tests in 0.549s FAILED (failures=4, skipped=7, expected failures=1) |
Here is a new patch. I've disabled test_repr and test_locked_repr tests in Lib/test/test_importlib/test_locks.py. |
New changeset 7574f94b1068 by Raymond Hettinger in branch 'default': |
Thanks Berker. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: