diff options
author | Adrian Chiris <adrianc@mellanox.com> | 2019-02-14 14:19:01 +0200 |
---|---|---|
committer | Adrian Chiris <adrianc@mellanox.com> | 2019-04-07 19:16:59 +0300 |
commit | 7c164cf9388340735c8615bd85412e4b3efd7334 (patch) | |
tree | 88be63360b56c6048b8200a59974a5fe6d97a984 | |
parent | 57333e902a3747715b2f13ea63477cc4aedc2129 (diff) | |
download | oslo-db-7c164cf9388340735c8615bd85412e4b3efd7334.tar.gz |
Support context function argument as keyword
Today, when a user uses enginefacade reader/writer decorators
he/she is forced to pass the context argument as the
first positional argument. performing a decorated function
call with this argument passed as a keyword would lead to
an out of range exception.
This patch proposes to add support for the context argument
to be passed as a keyword argument in decorated functions.
e.g
@enginefacade.reader
def foo(context):
...
foo(context=ctxt) <- will now be possible
Change-Id: Ief0b71bf9a7eb75935612431bdcc26d33bce852d
-rw-r--r-- | oslo_db/sqlalchemy/enginefacade.py | 5 | ||||
-rw-r--r-- | oslo_db/tests/sqlalchemy/test_enginefacade.py | 13 |
2 files changed, 17 insertions, 1 deletions
diff --git a/oslo_db/sqlalchemy/enginefacade.py b/oslo_db/sqlalchemy/enginefacade.py index 1cfff15..28b8596 100644 --- a/oslo_db/sqlalchemy/enginefacade.py +++ b/oslo_db/sqlalchemy/enginefacade.py @@ -1003,10 +1003,13 @@ class _TransactionContextManager(object): context_index = 1 else: context_index = 0 + context_kw = argspec.args[context_index] @functools.wraps(fn) def wrapper(*args, **kwargs): - context = args[context_index] + context = kwargs.get(context_kw, None) + if not context: + context = args[context_index] with self._transaction_scope(context): return fn(*args, **kwargs) diff --git a/oslo_db/tests/sqlalchemy/test_enginefacade.py b/oslo_db/tests/sqlalchemy/test_enginefacade.py index 12ccba6..5788fb3 100644 --- a/oslo_db/tests/sqlalchemy/test_enginefacade.py +++ b/oslo_db/tests/sqlalchemy/test_enginefacade.py @@ -519,6 +519,19 @@ class MockFacadeTest(oslo_test_base.BaseTestCase): with self._assert_reader_session(makers) as session: session.execute("test") + def test_session_reader_decorator_kwarg_call(self): + context = oslo_context.RequestContext() + + @enginefacade.reader + def go(context): + context.session.execute("test") + go(context=context) + + with self._assert_engines() as engines: + with self._assert_makers(engines) as makers: + with self._assert_reader_session(makers) as session: + session.execute("test") + def test_connection_reader_decorator(self): context = oslo_context.RequestContext() |