[DB-SIG] Standardized "with" block behavior?

Carsten Haese carsten at uniqsys.com
Fri Sep 22 22:00:46 CEST 2006


Hiya folks,

Now that Python 2.5 is offically out and has introduced "with" blocks
(http://docs.python.org/whatsnew/pep-343.html), I'm wondering if we
(i.e. the module authors) should standardize how DB-API compliant
modules leverage this functionality.

One obvious possibility is that connection and cursor objects could
return themselves in __enter__ and close themselves on __exit__. This
would allow the user to write something like this:

with module.connect(...) as conn:
   with conn.cursor() as cur:
      # ...

which is much easier to read than the equivalent:

conn = module.connect(...)
try:
   cur = conn.cursor()
   try:
      # ...
   finally:
      cur.close()
finally:
   conn.close()

I've already implemented this behavior for InformixDB in CVS, but if
people have better ideas, I'm definitely open to suggestions.

-Carsten




More information about the DB-SIG mailing list