Implement context manager for Cursors · Issue #190 · oracle/python-cx_Oracle · GitHub
Skip to content

Implement context manager for Cursors #190

Description

@gvenzl

Implement context manager for Cursor object to enable with statement.
Currently the with statement can't be used for cursors because they don't implement a context manager, i.e. the __enter__ and __exit__ attributes meaning that the following code will fail:

with conn.cursor() as c:
    c.execute("SELECT 'test' from dual")
    result = c.fetchall()
    print(result)

  File/Users/gvenzl/test/test.py”, line 23, in test
    with conn.cursor() as c:
AttributeError: __exit__

It would make sense to enable cursor object to be used in with statements that take care of closing the cursors automatically.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions