Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Switch to using context managers for acquiring and releasing locks.

  • Loading branch information...
commit e0fce8706d31f104f781676b49e7fe0df83438e0 1 parent e1b74d0
@alex alex authored
View
5 django/core/handlers/wsgi.py
@@ -210,8 +210,7 @@ def __call__(self, environ, start_response):
# Set up middleware if needed. We couldn't do this earlier, because
# settings weren't available.
if self._request_middleware is None:
- self.initLock.acquire()
- try:
+ with self.initLock:
try:
# Check that middleware is still uninitialised.
if self._request_middleware is None:
@@ -220,8 +219,6 @@ def __call__(self, environ, start_response):
# Unload whatever middleware we got
self._request_middleware = None
raise
- finally:
- self.initLock.release()
set_script_prefix(base.get_script_name(environ))
signals.request_started.send(sender=self.__class__)
View
28 django/core/mail/backends/console.py
@@ -16,19 +16,17 @@ def send_messages(self, email_messages):
"""Write all messages to the stream in a thread-safe way."""
if not email_messages:
return
- self._lock.acquire()
- try:
- stream_created = self.open()
- for message in email_messages:
- self.stream.write('%s\n' % message.message().as_string())
- self.stream.write('-'*79)
- self.stream.write('\n')
- self.stream.flush() # flush after each message
- if stream_created:
- self.close()
- except:
- if not self.fail_silently:
- raise
- finally:
- self._lock.release()
+ with self._lock:
+ try:
+ stream_created = self.open()
+ for message in email_messages:
+ self.stream.write('%s\n' % message.message().as_string())
+ self.stream.write('-'*79)
+ self.stream.write('\n')
+ self.stream.flush() # flush after each message
+ if stream_created:
+ self.close()
+ except:
+ if not self.fail_silently:
+ raise
return len(email_messages)
View
5 django/core/mail/backends/smtp.py
@@ -80,8 +80,7 @@ def send_messages(self, email_messages):
"""
if not email_messages:
return
- self._lock.acquire()
- try:
+ with self._lock:
new_conn_created = self.open()
if not self.connection:
# We failed silently on open().
@@ -94,8 +93,6 @@ def send_messages(self, email_messages):
num_sent += 1
if new_conn_created:
self.close()
- finally:
- self._lock.release()
return num_sent
def _send(self, email_message):
View
10 django/db/models/loading.py
@@ -54,8 +54,7 @@ def _populate(self):
"""
if self.loaded:
return
- self.write_lock.acquire()
- try:
+ with self.write_lock:
if self.loaded:
return
for app_name in settings.INSTALLED_APPS:
@@ -66,8 +65,6 @@ def _populate(self):
for app_name in self.postponed:
self.load_app(app_name)
self.loaded = True
- finally:
- self.write_lock.release()
def _label_for(self, app_mod):
"""
@@ -138,8 +135,7 @@ def get_app(self, app_label, emptyOK=False):
the app has no models in it and 'emptyOK' is True, returns None.
"""
self._populate()
- self.write_lock.acquire()
- try:
+ with self.write_lock:
for app_name in settings.INSTALLED_APPS:
if app_label == app_name.split('.')[-1]:
mod = self.load_app(app_name, False)
@@ -150,8 +146,6 @@ def get_app(self, app_label, emptyOK=False):
else:
return mod
raise ImproperlyConfigured("App with label %s could not be found" % app_label)
- finally:
- self.write_lock.release()
def get_app_errors(self):
"Returns the map of known problems with the INSTALLED_APPS."
View
17 django/dispatch/dispatcher.py
@@ -99,15 +99,12 @@ def connect(self, receiver, sender=None, weak=True, dispatch_uid=None):
if weak:
receiver = saferef.safeRef(receiver, onDelete=self._remove_receiver)
- self.lock.acquire()
- try:
+ with self.lock:
for r_key, _ in self.receivers:
if r_key == lookup_key:
break
else:
self.receivers.append((lookup_key, receiver))
- finally:
- self.lock.release()
def disconnect(self, receiver=None, sender=None, weak=True, dispatch_uid=None):
"""
@@ -135,16 +132,13 @@ def disconnect(self, receiver=None, sender=None, weak=True, dispatch_uid=None):
lookup_key = (dispatch_uid, _make_id(sender))
else:
lookup_key = (_make_id(receiver), _make_id(sender))
-
- self.lock.acquire()
- try:
+
+ with self.lock:
for index in xrange(len(self.receivers)):
(r_key, _) = self.receivers[index]
if r_key == lookup_key:
del self.receivers[index]
break
- finally:
- self.lock.release()
def send(self, sender, **named):
"""
@@ -237,8 +231,7 @@ def _remove_receiver(self, receiver):
Remove dead receivers from connections.
"""
- self.lock.acquire()
- try:
+ with self.lock:
to_remove = []
for key, connected_receiver in self.receivers:
if connected_receiver == receiver:
@@ -250,8 +243,6 @@ def _remove_receiver(self, receiver):
for idx, (r_key, _) in enumerate(reversed(self.receivers)):
if r_key == key:
del self.receivers[last_idx-idx]
- finally:
- self.lock.release()
def receiver(signal, **kwargs):
View
20 tests/regressiontests/mail/tests.py
@@ -603,21 +603,16 @@ def process_message(self, peer, mailfrom, rcpttos, data):
maddr = email.Utils.parseaddr(m.get('from'))[1]
if mailfrom != maddr:
return "553 '%s' != '%s'" % (mailfrom, maddr)
- self.sink_lock.acquire()
- self._sink.append(m)
- self.sink_lock.release()
+ with self.sink_lock:
+ self._sink.append(m)
def get_sink(self):
- self.sink_lock.acquire()
- try:
+ with self.sink_lock:
return self._sink[:]
- finally:
- self.sink_lock.release()
def flush_sink(self):
- self.sink_lock.acquire()
- self._sink[:] = []
- self.sink_lock.release()
+ with self.sink_lock:
+ self._sink[:] = []
def start(self):
assert not self.active
@@ -629,9 +624,8 @@ def run(self):
self.active = True
self.__flag.set()
while self.active and asyncore.socket_map:
- self.active_lock.acquire()
- asyncore.loop(timeout=0.1, count=1)
- self.active_lock.release()
+ with self.active_lock:
+ asyncore.loop(timeout=0.1, count=1)
asyncore.close_all()
def stop(self):
Please sign in to comment.
Something went wrong with that request. Please try again.