Python missing __exit__ method
<p>Some background: I work in a large bank and I'm trying to re-use some Python modules, which I cannot change, only import. I also don't have the option of installing any new utilities/functions etc (running Python 2.6 on Linux).</p>

<p>I've got this at present:</p>

<p>In my module:</p>

<pre><code>from common.databaseHelper import BacktestingDatabaseHelper

class mfReportProcess(testingResource):
    def __init__(self):
        self.db = BacktestingDatabaseHelper.fromConfig('db_name')
</code></pre>

<p>One of the methods called within the 'testingResource' class has this:</p>

<pre><code> with self.db as handler:
</code></pre>

<p>which falls over with this:</p>

<pre><code>with self.db as handler:
AttributeError: 'BacktestingDatabaseHelper' object has no attribute '__exit__'
</code></pre>

<p>and, indeed, there is no <code>__exit__</code> method in the 'BacktestingDatabaseHelper' class, a class which I cannot change.</p>

<p>However, this code I'm trying to re-use works perfectly well for other apps - does anyone know why I get this error and no-one else? 
Is there some way of defining <code>__exit__</code> locally?</p>

<p>Many thanks in advance.</p>

<p>EDITED to add:</p>

<p>I've tried to add my own class to setup DB access but can't get it to work - added this to my module: </p>

<pre><code>class myDB(BacktestingDatabaseHelper): 
    def __enter__(self): 
        self.db = fromConfig('db_name') 
    def __exit__(self): 
        self.db.close() 
</code></pre>

<p>and added: </p>

<pre><code>self.db = myDB 
</code></pre>

<p>into my <strong>init</strong> attribute for my main class but I get this error:</p>

<pre><code>with self.db as handler:
TypeError: unbound method __enter__() must be called with myDB instance as first argument (got nothing instead)
</code></pre>

<p>Any suggestions as to how to do this properly?</p>
