diff options
| author | Heckad <heckad@yandex.ru> | 2020-01-01 14:47:01 -0500 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2020-01-03 13:09:46 -0500 |
| commit | a076b1f30406cbb59a55e2c01ddd17a84636778e (patch) | |
| tree | bb4c5a5b88d45963040842427d73f9616465bdb0 /lib/sqlalchemy | |
| parent | 748bf710fdeeed327933a5e7c277c7f79f6053a6 (diff) | |
| download | sqlalchemy-a076b1f30406cbb59a55e2c01ddd17a84636778e.tar.gz | |
Use context managers for threading.Lock()
(zzzeek:) For some reason I thought that threading.Lock() still did
not support context managers, at least in Python 2, however this
does not seem to be the case.
Co-authored-by: Mike Bayer <mike_mp@zzzcomputing.com>
Closes: #5069
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/5069
Pull-request-sha: efeac06dda5afdbe33abcf9b27c8b5b5725c8444
Change-Id: Ic64fcd99cd587bc70b4ecc5b45d8205b5c76eff2
Diffstat (limited to 'lib/sqlalchemy')
| -rw-r--r-- | lib/sqlalchemy/ext/automap.py | 5 | ||||
| -rw-r--r-- | lib/sqlalchemy/ext/declarative/base.py | 5 | ||||
| -rw-r--r-- | lib/sqlalchemy/orm/__init__.py | 5 | ||||
| -rw-r--r-- | lib/sqlalchemy/orm/mapper.py | 10 | ||||
| -rw-r--r-- | lib/sqlalchemy/pool/dbapi_proxy.py | 5 | ||||
| -rw-r--r-- | lib/sqlalchemy/util/langhelpers.py | 10 | ||||
| -rw-r--r-- | lib/sqlalchemy/util/queue.py | 29 |
7 files changed, 17 insertions, 52 deletions
diff --git a/lib/sqlalchemy/ext/automap.py b/lib/sqlalchemy/ext/automap.py index 5fa4e8822..24c5117c8 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 622a83736..4247c79d7 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 e2eb93409..e826c0043 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 c23aaf9ef..1e4f1a2fa 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 d78d85d1f..77f567515 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 83f119660..8124764be 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 819d95684..78b7351d4 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. |
