Skip to content

Commit f91c3d6

Browse files
committed
Use with-statements to acquire locks.
We can use with-statements now that we've dropped support for Python 2.4.
1 parent 6b978c9 commit f91c3d6

File tree

4 files changed

+18
-31
lines changed

4 files changed

+18
-31
lines changed

bson/objectid.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,9 @@ def __generate(self):
158158
oid += struct.pack(">H", os.getpid() % 0xFFFF)
159159

160160
# 3 bytes inc
161-
ObjectId._inc_lock.acquire()
162-
oid += struct.pack(">i", ObjectId._inc)[1:4]
163-
ObjectId._inc = (ObjectId._inc + 1) % 0xFFFFFF
164-
ObjectId._inc_lock.release()
161+
with ObjectId._inc_lock:
162+
oid += struct.pack(">i", ObjectId._inc)[1:4]
163+
ObjectId._inc = (ObjectId._inc + 1) % 0xFFFFFF
165164

166165
self.__id = oid
167166

pymongo/pool.py

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -339,15 +339,8 @@ def reset(self):
339339
self.pool_id += 1
340340
self.pid = os.getpid()
341341

342-
sockets = None
343-
try:
344-
# Swapping variables is not atomic. We need to ensure no other
345-
# thread is modifying self.sockets, or replacing it, in this
346-
# critical section.
347-
self.lock.acquire()
342+
with self.lock:
348343
sockets, self.sockets = self.sockets, set()
349-
finally:
350-
self.lock.release()
351344

352345
for sock_info in sockets:
353346
sock_info.close()
@@ -477,15 +470,11 @@ def _get_socket_no_auth(self):
477470

478471
# We've now acquired the semaphore and must release it on error.
479472
try:
480-
sock_info, from_pool = None, None
481473
try:
482-
try:
483-
# set.pop() isn't atomic in Jython less than 2.7, see
484-
# http://bugs.jython.org/issue1854
485-
self.lock.acquire()
474+
# set.pop() isn't atomic in Jython less than 2.7, see
475+
# http://bugs.jython.org/issue1854
476+
with self.lock:
486477
sock_info, from_pool = self.sockets.pop(), True
487-
finally:
488-
self.lock.release()
489478
except KeyError:
490479
sock_info, from_pool = self.connect(), False
491480

@@ -551,8 +540,7 @@ def maybe_return_socket(self, sock_info):
551540
def _return_socket(self, sock_info):
552541
"""Return socket to the pool. If pool is full the socket is discarded.
553542
"""
554-
try:
555-
self.lock.acquire()
543+
with self.lock:
556544
max_size = self.opts.max_pool_size
557545
too_many_sockets = (max_size is not None
558546
and len(self.sockets) >= max_size)
@@ -561,8 +549,6 @@ def _return_socket(self, sock_info):
561549
self.sockets.add(sock_info)
562550
else:
563551
sock_info.close()
564-
finally:
565-
self.lock.release()
566552

567553
self._socket_semaphore.release()
568554

pymongo/thread_util.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ def acquire(self):
3737
def release(self):
3838
pass
3939

40+
def __enter__(self):
41+
pass
42+
43+
def __exit__(self, t, v, tb):
44+
pass
45+
4046

4147
class ThreadIdent(object):
4248
def __init__(self):
@@ -70,13 +76,10 @@ def _make_vigil(self):
7076
# Threadlocals in Python <= 2.7.0 have race conditions when setting
7177
# attributes and possibly when getting them, too, leading to weakref
7278
# callbacks not getting called later.
73-
self._lock.acquire()
74-
try:
79+
with self._lock:
7580
vigil = getattr(self._local, 'vigil', None)
7681
if not vigil:
7782
self._local.vigil = vigil = ThreadIdent.ThreadVigil()
78-
finally:
79-
self._lock.release()
8083

8184
return vigil
8285

test/mod_wsgi_test/test_client.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,9 @@ def run(self):
104104

105105
self.errors += 1
106106

107-
URLGetterThread.counter_lock.acquire()
108-
URLGetterThread.counter += 1
109-
counter = URLGetterThread.counter
110-
URLGetterThread.counter_lock.release()
107+
with URLGetterThread.counter_lock:
108+
URLGetterThread.counter += 1
109+
counter = URLGetterThread.counter
111110

112111
should_print = options.verbose and not counter % 1000
113112

0 commit comments

Comments
 (0)