Skip to content

Commit 005090a

Browse files
fix(dbapi): ensure Cursor.__aexit__() is called with the required arguments [backport 1.14] (#6110)
Backport ce589b5 from #6002 to 1.14. ## 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 87e6623 commit 005090a

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
@@ -319,6 +319,31 @@ async def test_connection_execute(self):
319319
assert len(rows) == 1, rows
320320
assert rows[0][0] == "one"
321321

322+
@mark_asyncio
323+
async def test_connection_context_execute(self):
324+
"""Checks whether connection context manager works as normal."""
325+
326+
query = SQL("""select 'one' as x""")
327+
async with (await psycopg.AsyncConnection.connect(**POSTGRES_CONFIG)) as conn:
328+
cur = await conn.execute(query)
329+
rows = await cur.fetchall()
330+
331+
assert len(rows) == 1, rows
332+
assert rows[0][0] == "one"
333+
334+
@mark_asyncio
335+
async def test_cursor_context_execute(self):
336+
"""Checks whether cursor context manager works as normal."""
337+
338+
query = SQL("""select 'one' as x""")
339+
async with (await psycopg.AsyncConnection.connect(**POSTGRES_CONFIG)).cursor() as cur:
340+
breakpoint()
341+
await cur.execute(query)
342+
rows = await cur.fetchall()
343+
344+
assert len(rows) == 1, rows
345+
assert rows[0][0] == "one"
346+
322347
@mark_asyncio
323348
async def test_cursor_from_connection_shortcut(self):
324349
"""Checks whether connection execute shortcute method works as normal"""

0 commit comments

Comments
 (0)