google-code-export / pymssql

Automatically exported from code.google.com/p/pymssql
GNU Lesser General Public License v2.1
0 stars 0 forks source link

Support "with" statement (PEP 343) #62

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
The connection object is an ideal use case for PEP 343's "with" statement.

http://www.python.org/dev/peps/pep-0343/

I think that all it would take is adding to the connection object:

1) an __enter__(self) method that does nothing

2) an __exit__(self) method that calls self.close()

Then we can write code like so:

with connection = pymssql.connect(**connect_args):
    cursor = connection.cursor()
    # do more stuff

and we will be confident that the connection will be cleanly closed no matter 
what.

Original issue reported on code.google.com by mhriv...@gmail.com on 20 Jul 2011 at 4:59

GoogleCodeExporter commented 9 years ago
Good idea.

Original comment by rsyr...@gmail.com on 20 Jul 2011 at 2:23

GoogleCodeExporter commented 9 years ago

Original comment by rsyr...@gmail.com on 3 Apr 2012 at 5:47

GoogleCodeExporter commented 9 years ago
It doesn't look like this was actually fixed - I'm using the 20130108 build and 
running the following (in python 2.7, should also work in 2.6) doesn't work:

with pymssql.connect( ... ) as conn:
    cur = conn.cursor()
    ...

I currently get an AttributeError: __exit__

Original comment by airdrik@gmail.com on 23 Apr 2013 at 6:09

GoogleCodeExporter commented 9 years ago
Reopening

Original comment by msabr...@gmail.com on 14 Aug 2013 at 4:41

GoogleCodeExporter commented 9 years ago
Dup of issue 124

Original comment by msabr...@gmail.com on 15 Aug 2013 at 5:44