summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authormike bayer <mike_mp@zzzcomputing.com>2020-01-03 21:49:49 +0000
committerGerrit Code Review <gerrit@bbpush.zzzcomputing.com>2020-01-03 21:49:49 +0000
commitc0352b03e840ddf68116182b22c5fd4eac2059cf (patch)
tree46c46a3d151197f149348680fcbbc18bdc395e81 /lib
parent5881fd274015af3de37f2ff0f91ff6a7c61c1540 (diff)
parenta076b1f30406cbb59a55e2c01ddd17a84636778e (diff)
downloadsqlalchemy-c0352b03e840ddf68116182b22c5fd4eac2059cf.tar.gz
Merge "Use context managers for threading.Lock()"
Diffstat (limited to 'lib')
-rw-r--r--lib/sqlalchemy/ext/automap.py5
-rw-r--r--lib/sqlalchemy/ext/declarative/base.py5
-rw-r--r--lib/sqlalchemy/orm/__init__.py5
-rw-r--r--lib/sqlalchemy/orm/mapper.py10
-rw-r--r--lib/sqlalchemy/pool/dbapi_proxy.py5
-rw-r--r--lib/sqlalchemy/util/langhelpers.py10
-rw-r--r--lib/sqlalchemy/util/queue.py29
7 files changed, 17 insertions, 52 deletions
diff --git a/lib/sqlalchemy/ext/automap.py b/lib/sqlalchemy/ext/automap.py
index 6d458ab56..e409cf38c 100644
--- a/lib/sqlalchemy/ext/automap.py
+++ b/lib/sqlalchemy/ext/automap.py
@@ -762,8 +762,7 @@ class AutomapBase(object):
autoload_replace=False,
)
- _CONFIGURE_MUTEX.acquire()
- try:
+ with _CONFIGURE_MUTEX:
table_to_map_config = dict(
(m.local_table, m)
for m in _DeferredMapperConfig.classes_for_base(
@@ -818,8 +817,6 @@ class AutomapBase(object):
for map_config in _DeferredMapperConfig.classes_for_base(cls):
map_config.map()
- finally:
- _CONFIGURE_MUTEX.release()
_sa_decl_prepare = True
"""Indicate that the mapping of classes should be deferred.
diff --git a/lib/sqlalchemy/ext/declarative/base.py b/lib/sqlalchemy/ext/declarative/base.py
index d76e4ab70..51ba35b4b 100644
--- a/lib/sqlalchemy/ext/declarative/base.py
+++ b/lib/sqlalchemy/ext/declarative/base.py
@@ -179,8 +179,7 @@ class _MapperConfig(object):
self._scan_attributes()
- mapperlib._CONFIGURE_MUTEX.acquire()
- try:
+ with mapperlib._CONFIGURE_MUTEX:
clsregistry.add_class(self.classname, self.cls)
self._extract_mappable_attributes()
@@ -192,8 +191,6 @@ class _MapperConfig(object):
self._setup_inheritance()
self._early_mapping()
- finally:
- mapperlib._CONFIGURE_MUTEX.release()
def _early_mapping(self):
self.map()
diff --git a/lib/sqlalchemy/orm/__init__.py b/lib/sqlalchemy/orm/__init__.py
index 55b8ea536..45a777a56 100644
--- a/lib/sqlalchemy/orm/__init__.py
+++ b/lib/sqlalchemy/orm/__init__.py
@@ -231,8 +231,7 @@ def clear_mappers():
upon a fixed set of classes.
"""
- mapperlib._CONFIGURE_MUTEX.acquire()
- try:
+ with mapperlib._CONFIGURE_MUTEX:
while _mapper_registry:
try:
# can't even reliably call list(weakdict) in jython
@@ -240,8 +239,6 @@ def clear_mappers():
mapper.dispose()
except KeyError:
pass
- finally:
- mapperlib._CONFIGURE_MUTEX.release()
joinedload = strategy_options.joinedload._unbound_fn
diff --git a/lib/sqlalchemy/orm/mapper.py b/lib/sqlalchemy/orm/mapper.py
index 224092a00..82e68fd07 100644
--- a/lib/sqlalchemy/orm/mapper.py
+++ b/lib/sqlalchemy/orm/mapper.py
@@ -698,8 +698,7 @@ class Mapper(sql_base.HasCacheKey, InspectionAttr):
# prevent this mapper from being constructed
# while a configure_mappers() is occurring (and defer a
# configure_mappers() until construction succeeds)
- _CONFIGURE_MUTEX.acquire()
- try:
+ with _CONFIGURE_MUTEX:
self.dispatch._events._new_mapper_instance(class_, self)
self._configure_inheritance()
self._configure_class_instrumentation()
@@ -709,8 +708,6 @@ class Mapper(sql_base.HasCacheKey, InspectionAttr):
Mapper._new_mappers = True
self._log("constructed")
self._expire_memoizations()
- finally:
- _CONFIGURE_MUTEX.release()
# major attributes initialized at the classlevel so that
# they can be Sphinx-documented.
@@ -3167,8 +3164,7 @@ def configure_mappers():
if not Mapper._new_mappers:
return
- _CONFIGURE_MUTEX.acquire()
- try:
+ with _CONFIGURE_MUTEX:
global _already_compiling
if _already_compiling:
return
@@ -3225,8 +3221,6 @@ def configure_mappers():
Mapper._new_mappers = False
finally:
_already_compiling = False
- finally:
- _CONFIGURE_MUTEX.release()
Mapper.dispatch._for_class(Mapper).after_configured()
diff --git a/lib/sqlalchemy/pool/dbapi_proxy.py b/lib/sqlalchemy/pool/dbapi_proxy.py
index 95f207906..6e11d2e59 100644
--- a/lib/sqlalchemy/pool/dbapi_proxy.py
+++ b/lib/sqlalchemy/pool/dbapi_proxy.py
@@ -104,8 +104,7 @@ class _DBProxy(object):
try:
return self.pools[key]
except KeyError:
- self._create_pool_mutex.acquire()
- try:
+ with self._create_pool_mutex:
if key not in self.pools:
kw.pop("sa_pool_key", None)
pool = self.poolclass(
@@ -115,8 +114,6 @@ class _DBProxy(object):
return pool
else:
return self.pools[key]
- finally:
- self._create_pool_mutex.release()
def connect(self, *args, **kw):
"""Activate a connection to the database.
diff --git a/lib/sqlalchemy/util/langhelpers.py b/lib/sqlalchemy/util/langhelpers.py
index 1042aaac6..402f8bb09 100644
--- a/lib/sqlalchemy/util/langhelpers.py
+++ b/lib/sqlalchemy/util/langhelpers.py
@@ -1179,11 +1179,8 @@ def counter():
# avoid the 2to3 "next" transformation...
def _next():
- lock.acquire()
- try:
+ with lock:
return next(counter)
- finally:
- lock.release()
return _next
@@ -1362,14 +1359,11 @@ class symbol(object):
_lock = compat.threading.Lock()
def __new__(cls, name, doc=None, canonical=None):
- cls._lock.acquire()
- try:
+ with cls._lock:
sym = cls.symbols.get(name)
if sym is None:
cls.symbols[name] = sym = _symbol(name, doc, canonical)
return sym
- finally:
- symbol._lock.release()
@classmethod
def parse_user_argument(
diff --git a/lib/sqlalchemy/util/queue.py b/lib/sqlalchemy/util/queue.py
index 1b2d19524..3433657d6 100644
--- a/lib/sqlalchemy/util/queue.py
+++ b/lib/sqlalchemy/util/queue.py
@@ -66,28 +66,22 @@ class Queue:
def qsize(self):
"""Return the approximate size of the queue (not reliable!)."""
- self.mutex.acquire()
- n = self._qsize()
- self.mutex.release()
- return n
+ with self.mutex:
+ return self._qsize()
def empty(self):
"""Return True if the queue is empty, False otherwise (not
reliable!)."""
- self.mutex.acquire()
- n = self._empty()
- self.mutex.release()
- return n
+ with self.mutex:
+ return self._empty()
def full(self):
"""Return True if the queue is full, False otherwise (not
reliable!)."""
- self.mutex.acquire()
- n = self._full()
- self.mutex.release()
- return n
+ with self.mutex:
+ return self._full()
def put(self, item, block=True, timeout=None):
"""Put an item into the queue.
@@ -102,8 +96,7 @@ class Queue:
(`timeout` is ignored in that case).
"""
- self.not_full.acquire()
- try:
+ with self.not_full:
if not block:
if self._full():
raise Full
@@ -121,8 +114,6 @@ class Queue:
self.not_full.wait(remaining)
self._put(item)
self.not_empty.notify()
- finally:
- self.not_full.release()
def put_nowait(self, item):
"""Put an item into the queue without blocking.
@@ -142,9 +133,9 @@ class Queue:
available within that time. Otherwise (`block` is false),
return an item if one is immediately available, else raise the
``Empty`` exception (`timeout` is ignored in that case).
+
"""
- self.not_empty.acquire()
- try:
+ with self.not_empty:
if not block:
if self._empty():
raise Empty
@@ -163,8 +154,6 @@ class Queue:
item = self._get()
self.not_full.notify()
return item
- finally:
- self.not_empty.release()
def get_nowait(self):
"""Remove and return an item from the queue without blocking.