title: Using with statement on cursor in cx_Oracle
body: <p>cx_Oracle contains <code>__enter__</code> and <code>__exit__</code> on Connection objects, but not on Cursor objects. Thus, I use this everywhere to wrap cursors :</p>
  
  <pre><code>class CursorWrapper(object):
      def __init__(self, connection):
          self.connection = connection
          self.cursor = None
  
      def __enter__(self):
          self.cursor = self.connection.cursor()
          return self.cursor
  
      def __exit__(self, exc_type, exc_value, traceback):
         self.cursor.close()
  </code></pre>
  
  <p>then, when I want a cursor</p>
  
  <pre><code>with CursorWrapper(cnx) as cursor:
      cursor.execute("whatever sql statement")
  </code></pre>
  
  <p>It suits my needs fairly well.</p>
  
  <p>However, I was then wondering what could prevent <code>__enter__</code> and <code>__exit__</code> methods to be added directly in cx_Oracle ?</p>
  
  <p>Or is there a better way to use cursors with context managements, which would explain why it is not defined in the module ?</p>
  
  <p>EDIT:
  Sorry, I just found the answer. I can just use contextlib.closing.</p>
  
  <pre><code>import contextlib
  with contextlib.closing(cnx.cursor()) as cursor:
  </code></pre>