Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dbapi2 patching: do respect with statement on cursor objects (aws#17)
Some database backends (including already-supported `django` and not-yet-supported `psycopg2`) implement non-standard ability to use cursor in `with` statement like this: conn = get_db_connection() with conn.cursor() as cur: cur.execute('MY QUERY') This syntax will automatically close cursor when leaving `with` statement. The problem is that `Cursor.__enter__` returns `self`, thus dropping our `XRayTracedCursor` wrapper. This PR aims to fix that issue by wrapping `__enter__` as well. If connection's `__enter__` returned itself then we return wrapper instead, else we return what cursor returned for compatibility. Probably something similar should also be implemented for `Connection.__enter__` because some backends support it as well (including `sqlite3`).
- Loading branch information