Skip to content

Commit 2db2011

Browse files
fix(dbapi): ensure Cursor.__aexit__() is called with the required arguments [backport 1.13] (#6111)
Backport ce589b5 from #6002 to 1.13. ## Changes Updates the dbapi_async implementation to pass along the `exc_...` parameters to the `__wrapped__.__aexit__` calls. This should not introduce any breaking changes or cause any changes to performance. This resolves #6001 ## Checklist - [x] Change(s) are motivated and described in the PR description. - [x] Testing strategy is described if automated tests are not included in the PR. - [x] Risk is outlined (performance impact, potential for breakage, maintainability, etc). - [x] Change is maintainable (easy to change, telemetry, documentation). - [x] [Library release note guidelines](https://ddtrace.readthedocs.io/en/stable/contributing.html#Release-Note-Guidelines) are followed. - [x] Documentation is included (in-code, generated user docs, [public corp docs](https://github.com/DataDog/documentation/)). ## Reviewer Checklist - [x] Title is accurate. - [x] No unnecessary changes are introduced. - [x] Description motivates each change. - [x] Avoids breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes unless absolutely necessary. - [x] Testing strategy adequately addresses listed risk(s). - [x] Change is maintainable (easy to change, telemetry, documentation). - [x] Release note makes sense to a user of the library. - [x] Reviewer has explicitly acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment. Co-authored-by: Sean <[email protected]>
1 parent a56c5c0 commit 2db2011

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

ddtrace/contrib/dbapi_async/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ async def __aexit__(self, exc_type, exc_val, exc_tb):
3333
# previous versions of the dbapi didn't support context managers. let's
3434
# reference the func that would be called to ensure that error
3535
# messages will be the same.
36-
return await self.__wrapped__.__aexit__()
36+
return await self.__wrapped__.__aexit__(exc_type, exc_val, exc_tb)
3737

3838
async def _trace_method(self, method, name, resource, extra_tags, dbm_propagator, *args, **kwargs):
3939
"""
@@ -221,7 +221,7 @@ async def __aexit__(self, exc_type, exc_val, exc_tb):
221221
# previous versions of the dbapi didn't support context managers. let's
222222
# reference the func that would be called to ensure that errors
223223
# messages will be the same.
224-
return await self.__wrapped__.__aexit__()
224+
return await self.__wrapped__.__aexit__(exc_type, exc_val, exc_tb)
225225

226226
async def _trace_method(self, method, name, extra_tags, *args, **kwargs):
227227
pin = Pin.get_from(self)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
fixes:
3+
- |
4+
psycopg: Resolves ``TypeError`` raised when an async cursor object is traced. This fix ensures `exc_type`, `exc_val`, and `exc_tb` are passed down to the wrapped object on `__aexit__`.

tests/contrib/psycopg/test_psycopg_async.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,31 @@ async def test_connection_execute(self):
280280
assert len(rows) == 1, rows
281281
assert rows[0][0] == "one"
282282

283+
@mark_asyncio
284+
async def test_connection_context_execute(self):
285+
"""Checks whether connection context manager works as normal."""
286+
287+
query = SQL("""select 'one' as x""")
288+
async with (await psycopg.AsyncConnection.connect(**POSTGRES_CONFIG)) as conn:
289+
cur = await conn.execute(query)
290+
rows = await cur.fetchall()
291+
292+
assert len(rows) == 1, rows
293+
assert rows[0][0] == "one"
294+
295+
@mark_asyncio
296+
async def test_cursor_context_execute(self):
297+
"""Checks whether cursor context manager works as normal."""
298+
299+
query = SQL("""select 'one' as x""")
300+
async with (await psycopg.AsyncConnection.connect(**POSTGRES_CONFIG)).cursor() as cur:
301+
breakpoint()
302+
await cur.execute(query)
303+
rows = await cur.fetchall()
304+
305+
assert len(rows) == 1, rows
306+
assert rows[0][0] == "one"
307+
283308
@mark_asyncio
284309
async def test_cursor_from_connection_shortcut(self):
285310
"""Checks whether connection execute shortcute method works as normal"""

0 commit comments

Comments
 (0)