Skip to content

Commit 1c0ab31

Browse files
Added support for the asynchronous context manager protocol on the
AsyncCursor class as a convenience.
1 parent cc1e3ea commit 1c0ab31

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

doc/src/release_notes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ Thin Mode Changes
1515

1616
#) Added support for using alternative event loop implementations like uvloop
1717
(`issue 276 <https://github.com/oracle/python-oracledb/issues/276>`__).
18+
#) Added support for the asynchronous context manager protocol on the
19+
AsyncCursor class as a convenience.
1820
#) Fixed bug with intermittent hang on some versions of Oracle Database when
1921
using asyncio and the database raises an error and output variables are
2022
present

src/oracledb/cursor.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# -----------------------------------------------------------------------------
2-
# Copyright (c) 2021, 2023, Oracle and/or its affiliates.
2+
# Copyright (c) 2021, 2024, Oracle and/or its affiliates.
33
#
44
# This software is dual-licensed to you under the Universal Permissive License
55
# (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl and Apache License
@@ -911,6 +911,15 @@ def scroll(self, value: int = 0, mode: str = "relative") -> None:
911911
class AsyncCursor(BaseCursor):
912912
__module__ = MODULE_NAME
913913

914+
async def __aenter__(self):
915+
self._verify_open()
916+
return self
917+
918+
async def __aexit__(self, *exc_info):
919+
self._verify_open()
920+
self._impl.close(in_del=True)
921+
self._impl = None
922+
914923
def __aiter__(self):
915924
return self
916925

tests/test_5400_cursor_execute_async.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# -----------------------------------------------------------------------------
2-
# Copyright (c) 2023, Oracle and/or its affiliates.
2+
# Copyright (c) 2023, 2024, Oracle and/or its affiliates.
33
#
44
# This software is dual-licensed to you under the Universal Permissive License
55
# (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl and Apache License
@@ -583,6 +583,16 @@ async def test_5433_fetch_info_slice(self):
583583
(fetch_info,) = self.cursor.description
584584
self.assertEqual(fetch_info[1:3], (oracledb.DB_TYPE_NUMBER, 10))
585585

586+
async def test_5434_async_context_manager(self):
587+
"5434 - test async context manager"
588+
expected_value = test_env.get_main_user().upper()
589+
with self.conn.cursor() as cursor:
590+
await cursor.execute("select user from dual")
591+
self.assertEqual(await cursor.fetchone(), (expected_value,))
592+
async with self.conn.cursor() as cursor:
593+
await cursor.execute("select user from dual")
594+
self.assertEqual(await cursor.fetchone(), (expected_value,))
595+
586596

587597
if __name__ == "__main__":
588598
test_env.run_test_cases()

0 commit comments

Comments
 (0)