[pypy-commit] pypy py3.5: _thread.Lock.__repr__; _thread.RLock.__repr__

arigo pypy.commits at gmail.com
Wed Jan 4 13:58:17 EST 2017


Author: Armin Rigo <arigo at tunes.org>
Branch: py3.5
Changeset: r89366:5a4234a11c85
Date: 2017-01-04 19:57 +0100
http://bitbucket.org/pypy/pypy/changeset/5a4234a11c85/

Log:	_thread.Lock.__repr__; _thread.RLock.__repr__

diff --git a/pypy/module/thread/os_lock.py b/pypy/module/thread/os_lock.py
--- a/pypy/module/thread/os_lock.py
+++ b/pypy/module/thread/os_lock.py
@@ -94,13 +94,16 @@
             raise oefmt(space.w_RuntimeError,
                         "cannot release un-acquired lock")
 
+    def _is_locked(self):
+        if self.lock.acquire(False):
+            self.lock.release()
+            return False
+        else:
+            return True
+
     def descr_lock_locked(self, space):
         """Return whether the lock is in the locked state."""
-        if self.lock.acquire(False):
-            self.lock.release()
-            return space.w_False
-        else:
-            return space.w_True
+        return space.newbool(self._is_locked())
 
     def descr__enter__(self, space):
         self.descr_lock_acquire(space)
@@ -116,6 +119,14 @@
     def __exit__(self, *args):
         self.descr_lock_release(self.space)
 
+    def descr__repr__(self, space):
+        classname = space.getfulltypename(self)
+        if self._is_locked():
+            locked = u"locked"
+        else:
+            locked = u"unlocked"
+        return self.getrepr(space, u'%s %s object' % (locked, classname))
+
 Lock.typedef = TypeDef(
     "_thread.lock",
     __doc__="""\
@@ -134,6 +145,7 @@
     locked=interp2app(Lock.descr_lock_locked),
     __enter__=interp2app(Lock.descr__enter__),
     __exit__=interp2app(Lock.descr__exit__),
+    __repr__ = interp2app(Lock.descr__repr__),
     # Obsolete synonyms
     acquire_lock=interp2app(Lock.descr_lock_acquire),
     release_lock=interp2app(Lock.descr_lock_release),
@@ -174,10 +186,14 @@
         W_RLock.__init__(self, space)
         return space.wrap(self)
 
-    def descr__repr__(self):
-        typename = space.type(self).getname(space)
-        return space.wrap(u"<%s owner=%d count=%d>" % (
-                typename, self.rlock_owner, self.rlock_count))
+    def descr__repr__(self, space):
+        classname = space.getfulltypename(self)
+        if self.rlock_count == 0:
+            locked = u"unlocked"
+        else:
+            locked = u"locked"
+        return self.getrepr(space, u'%s %s object owner=%d count=%d' % (
+            locked, classname, self.rlock_owner, self.rlock_count))
 
     @unwrap_spec(blocking=int, timeout=float)
     def acquire_w(self, space, blocking=True, timeout=-1.0):
@@ -285,4 +301,5 @@
     __enter__ = interp2app(W_RLock.descr__enter__),
     __exit__ = interp2app(W_RLock.descr__exit__),
     __weakref__ = make_weakref_descr(W_RLock),
+    __repr__ = interp2app(W_RLock.descr__repr__),
     )
diff --git a/pypy/module/thread/test/test_lock.py b/pypy/module/thread/test/test_lock.py
--- a/pypy/module/thread/test/test_lock.py
+++ b/pypy/module/thread/test/test_lock.py
@@ -302,3 +302,20 @@
         finally:
             signal.signal(signal.SIGALRM, oldalrm)
 
+    def test_lock_repr(self):
+        import _thread
+        lock = _thread.allocate_lock()
+        assert repr(lock).startswith("<unlocked _thread.lock object at ")
+        lock.acquire()
+        assert repr(lock).startswith("<locked _thread.lock object at ")
+
+    def test_rlock_repr(self):
+        import _thread
+        rlock = _thread.RLock()
+        assert repr(rlock).startswith(
+            "<unlocked _thread.RLock object owner=0 count=0 at ")
+        rlock.acquire()
+        rlock.acquire()
+        assert repr(rlock).startswith("<locked _thread.RLock object owner=")
+        assert 'owner=0' not in repr(rlock)
+        assert " count=2 at " in repr(rlock)
diff --git a/pypy/objspace/std/objectobject.py b/pypy/objspace/std/objectobject.py
--- a/pypy/objspace/std/objectobject.py
+++ b/pypy/objspace/std/objectobject.py
@@ -170,18 +170,7 @@
 
 
 def descr__repr__(space, w_obj):
-    w_type = space.type(w_obj)
-    classname = w_type.name.decode('utf-8')
-    if w_type.is_heaptype():
-        w_module = w_type.lookup("__module__")
-        if w_module is not None:
-            try:
-                modulename = space.unicode_w(w_module)
-            except OperationError as e:
-                if not e.match(space, space.w_TypeError):
-                    raise
-            else:
-                classname = u'%s.%s' % (modulename, classname)
+    classname = space.getfulltypename(w_obj)
     return w_obj.getrepr(space, u'%s object' % (classname,))
 
 
diff --git a/pypy/objspace/std/objspace.py b/pypy/objspace/std/objspace.py
--- a/pypy/objspace/std/objspace.py
+++ b/pypy/objspace/std/objspace.py
@@ -709,3 +709,18 @@
     def is_overloaded(self, w_obj, tp, method):
         return (self.lookup(w_obj, method) is not
                 self.lookup_in_type(tp, method))
+
+    def getfulltypename(self, w_obj):
+        w_type = self.type(w_obj)
+        classname = w_type.name.decode('utf-8')
+        if w_type.is_heaptype():
+            w_module = w_type.lookup("__module__")
+            if w_module is not None:
+                try:
+                    modulename = self.unicode_w(w_module)
+                except OperationError as e:
+                    if not e.match(self, self.w_TypeError):
+                        raise
+                else:
+                    classname = u'%s.%s' % (modulename, classname)
+        return classname


More information about the pypy-commit mailing list