diff options
Diffstat (limited to 'lib/sqlalchemy/interfaces.py')
-rw-r--r-- | lib/sqlalchemy/interfaces.py | 102 |
1 files changed, 53 insertions, 49 deletions
diff --git a/lib/sqlalchemy/interfaces.py b/lib/sqlalchemy/interfaces.py index 30698ea33..f352f7f26 100644 --- a/lib/sqlalchemy/interfaces.py +++ b/lib/sqlalchemy/interfaces.py @@ -80,17 +80,18 @@ class PoolListener(object): """ - listener = util.as_interface(listener, - methods=('connect', 'first_connect', - 'checkout', 'checkin')) - if hasattr(listener, 'connect'): - event.listen(self, 'connect', listener.connect) - if hasattr(listener, 'first_connect'): - event.listen(self, 'first_connect', listener.first_connect) - if hasattr(listener, 'checkout'): - event.listen(self, 'checkout', listener.checkout) - if hasattr(listener, 'checkin'): - event.listen(self, 'checkin', listener.checkin) + listener = util.as_interface( + listener, + methods=("connect", "first_connect", "checkout", "checkin"), + ) + if hasattr(listener, "connect"): + event.listen(self, "connect", listener.connect) + if hasattr(listener, "first_connect"): + event.listen(self, "first_connect", listener.first_connect) + if hasattr(listener, "checkout"): + event.listen(self, "checkout", listener.checkout) + if hasattr(listener, "checkin"): + event.listen(self, "checkin", listener.checkin) def connect(self, dbapi_con, con_record): """Called once for each new DB-API connection or Pool's ``creator()``. @@ -187,27 +188,20 @@ class ConnectionProxy(object): @classmethod def _adapt_listener(cls, self, listener): - def adapt_execute(conn, clauseelement, multiparams, params): - def execute_wrapper(clauseelement, *multiparams, **params): return clauseelement, multiparams, params - return listener.execute(conn, execute_wrapper, - clauseelement, *multiparams, - **params) + return listener.execute( + conn, execute_wrapper, clauseelement, *multiparams, **params + ) - event.listen(self, 'before_execute', adapt_execute) + event.listen(self, "before_execute", adapt_execute) - def adapt_cursor_execute(conn, cursor, statement, - parameters, context, executemany): - - def execute_wrapper( - cursor, - statement, - parameters, - context, - ): + def adapt_cursor_execute( + conn, cursor, statement, parameters, context, executemany + ): + def execute_wrapper(cursor, statement, parameters, context): return statement, parameters return listener.cursor_execute( @@ -217,46 +211,56 @@ class ConnectionProxy(object): parameters, context, executemany, - ) + ) - event.listen(self, 'before_cursor_execute', adapt_cursor_execute) + event.listen(self, "before_cursor_execute", adapt_cursor_execute) def do_nothing_callback(*arg, **kw): pass def adapt_listener(fn): - def go(conn, *arg, **kw): fn(conn, do_nothing_callback, *arg, **kw) return util.update_wrapper(go, fn) - event.listen(self, 'begin', adapt_listener(listener.begin)) - event.listen(self, 'rollback', - adapt_listener(listener.rollback)) - event.listen(self, 'commit', adapt_listener(listener.commit)) - event.listen(self, 'savepoint', - adapt_listener(listener.savepoint)) - event.listen(self, 'rollback_savepoint', - adapt_listener(listener.rollback_savepoint)) - event.listen(self, 'release_savepoint', - adapt_listener(listener.release_savepoint)) - event.listen(self, 'begin_twophase', - adapt_listener(listener.begin_twophase)) - event.listen(self, 'prepare_twophase', - adapt_listener(listener.prepare_twophase)) - event.listen(self, 'rollback_twophase', - adapt_listener(listener.rollback_twophase)) - event.listen(self, 'commit_twophase', - adapt_listener(listener.commit_twophase)) + event.listen(self, "begin", adapt_listener(listener.begin)) + event.listen(self, "rollback", adapt_listener(listener.rollback)) + event.listen(self, "commit", adapt_listener(listener.commit)) + event.listen(self, "savepoint", adapt_listener(listener.savepoint)) + event.listen( + self, + "rollback_savepoint", + adapt_listener(listener.rollback_savepoint), + ) + event.listen( + self, + "release_savepoint", + adapt_listener(listener.release_savepoint), + ) + event.listen( + self, "begin_twophase", adapt_listener(listener.begin_twophase) + ) + event.listen( + self, "prepare_twophase", adapt_listener(listener.prepare_twophase) + ) + event.listen( + self, + "rollback_twophase", + adapt_listener(listener.rollback_twophase), + ) + event.listen( + self, "commit_twophase", adapt_listener(listener.commit_twophase) + ) def execute(self, conn, execute, clauseelement, *multiparams, **params): """Intercept high level execute() events.""" return execute(clauseelement, *multiparams, **params) - def cursor_execute(self, execute, cursor, statement, parameters, - context, executemany): + def cursor_execute( + self, execute, cursor, statement, parameters, context, executemany + ): """Intercept low-level cursor execute() events.""" return execute(cursor, statement, parameters, context) |