]> granicus.if.org Git - python/commitdiff
Issue 21137: Better repr for threading.Lock()
authorRaymond Hettinger <python@rcn.com>
Mon, 26 May 2014 01:22:35 +0000 (18:22 -0700)
committerRaymond Hettinger <python@rcn.com>
Mon, 26 May 2014 01:22:35 +0000 (18:22 -0700)
Lib/_dummy_thread.py
Lib/test/lock_tests.py
Lib/test/test_importlib/test_locks.py
Lib/threading.py
Misc/NEWS
Modules/_threadmodule.c

index b67cfb95be8eb215770db8bbdbdf28f8e45de6bf..36e5f38ae0e8e276e4b3fb70305f6cb84e7cc0b9 100644 (file)
@@ -140,6 +140,14 @@ class LockType(object):
     def locked(self):
         return self.locked_status
 
+    def __repr__(self):
+        return "<%s %s.%s object at %s>" % (
+            "locked" if self.locked_status else "unlocked",
+            self.__class__.__module__,
+            self.__class__.__qualname__,
+            hex(id(self))
+        )
+
 # Used to signal that interrupt_main was called in a "thread"
 _interrupt = False
 # True when not executing in a "thread"
index 1cbcea2343968aca703a7f6f5e50de33fa4061b1..136f1c37c62b9f2220098e066db373986032fb70 100644 (file)
@@ -82,7 +82,13 @@ class BaseLockTests(BaseTestCase):
 
     def test_repr(self):
         lock = self.locktype()
-        repr(lock)
+        self.assertRegex(repr(lock), "<unlocked .* object (.*)?at .*>")
+        del lock
+
+    def test_locked_repr(self):
+        lock = self.locktype()
+        lock.acquire()
+        self.assertRegex(repr(lock), "<locked .* object (.*)?at .*>")
         del lock
 
     def test_acquire_destroy(self):
index 08050545cd1fa424d2ae45ce71648e267f9da02a..4c01177990d7a408683ddc28606d6486b5bcaa44 100644 (file)
@@ -31,6 +31,9 @@ if threading is not None:
         test_timeout = None
         # _release_save() unsupported
         test_release_save_unacquired = None
+        # lock status in repr unsupported
+        test_repr = None
+        test_locked_repr = None
 
     LOCK_TYPES = {kind: splitinit._bootstrap._ModuleLock
                   for kind, splitinit in init.items()}
index 34070830e78c82a29eacfe1ffc5b2c8f50b2db82..c7c4478178d90925748ebe87775bf241dc0741ee 100644 (file)
@@ -106,8 +106,14 @@ class _RLock:
             owner = _active[owner].name
         except KeyError:
             pass
-        return "<%s owner=%r count=%d>" % (
-                self.__class__.__name__, owner, self._count)
+        return "<%s %s.%s object owner=%r count=%d at %s>" % (
+            "locked" if self._block.locked() else "unlocked",
+            self.__class__.__module__,
+            self.__class__.__qualname__,
+            owner,
+            self._count,
+            hex(id(self))
+        )
 
     def acquire(self, blocking=True, timeout=-1):
         """Acquire a lock, blocking or non-blocking.
index 83fbeb8263f3ff65170f3f666dc9a66f4c6bcf06..ce98087a01a7e84bc14ef8afeb2d959e14aef6ed 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -97,6 +97,9 @@ Library
 - Issue #21513: Speedup some properties of IP addresses (IPv4Address,
   IPv6Address) such as .is_private or .is_multicast.
 
+- Issue #21137: Improve the repr for threading.Lock() and its variants
+  by showing the "locked" or "unlocked" status.  Patch by Berker Peksag.
+
 - Issue #21538: The plistlib module now supports loading of binary plist files
   when reference or offset size is not a power of two.
 
index 9925b0e7ec108577180069b177eff62fcaf7c6b2..b68c177f039ed0fd01bb77245a68b433233e15a3 100644 (file)
@@ -192,6 +192,13 @@ PyDoc_STRVAR(locked_doc,
 \n\
 Return whether the lock is in the locked state.");
 
+static PyObject *
+lock_repr(lockobject *self)
+{
+    return PyUnicode_FromFormat("<%s %s object at %p>",
+        self->locked ? "locked" : "unlocked", Py_TYPE(self)->tp_name, self);
+}
+
 static PyMethodDef lock_methods[] = {
     {"acquire_lock", (PyCFunction)lock_PyThread_acquire_lock,
      METH_VARARGS | METH_KEYWORDS, acquire_doc},
@@ -223,7 +230,7 @@ static PyTypeObject Locktype = {
     0,                                  /*tp_getattr*/
     0,                                  /*tp_setattr*/
     0,                                  /*tp_reserved*/
-    0,                                  /*tp_repr*/
+    (reprfunc)lock_repr,                /*tp_repr*/
     0,                                  /*tp_as_number*/
     0,                                  /*tp_as_sequence*/
     0,                                  /*tp_as_mapping*/
@@ -475,8 +482,10 @@ rlock_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
 static PyObject *
 rlock_repr(rlockobject *self)
 {
-    return PyUnicode_FromFormat("<%s owner=%ld count=%lu>",
-        Py_TYPE(self)->tp_name, self->rlock_owner, self->rlock_count);
+    return PyUnicode_FromFormat("<%s %s object owner=%ld count=%lu at %p>",
+        self->rlock_count ? "locked" : "unlocked",
+        Py_TYPE(self)->tp_name, self->rlock_owner,
+        self->rlock_count, self);
 }