djhenderson / 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) #124

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Add support to use Connection and Cursor objects in the "with" statement

This is a duplicate of issue 62 which was closed as fixed but no change was 
made and no such functionality currently exists.  
Using the 20130108 build and running the following (in python 2.7, should also 
work in 2.6, and ) fails:

with pymssql.connect( ... ) as conn:
    cur = conn.cursor()
    # run some commands

or

conn = pymssql.connect( ... )
with conn.cursor() as cur:
    # run some commands

I currently get an AttributeError: __exit__

The remedy is to add to Cursor and Connection (and other closable resources) an 
empty __enter__ method and an __exit__ method which closes the resource.

Original issue reported on code.google.com by airdrik@gmail.com on 13 Aug 2013 at 6:05

GoogleCodeExporter commented 9 years ago
Here's a patch that makes pymssql.Connection into a context manager:

vagrant@lucid64:~/dev/hg-repos/pymssql$ hg diff pymssql.pyx diff -r 54554641cc7e pymssql.pyx --- a/pymssql.pyx Mon Aug 12 22:12:54 2013 -0700 +++ b/pymssql.pyx Thu Aug 15 09:33:27 2013 -0700 @@ -213,6 +213,12 @@ self._conn.execute_non_query('%s TRAN' % tran_type) self._autocommit = status

Original comment by msabr...@gmail.com on 15 Aug 2013 at 4:37

GoogleCodeExporter commented 9 years ago
Issue 62 has been merged into this issue.

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

GoogleCodeExporter commented 9 years ago
This patch makes both pymssql.Connection and pymssql.Cursor into context 
managers:

diff -r 54554641cc7e pymssql.pyx --- a/pymssql.pyx Mon Aug 12 22:12:54 2013 -0700 +++ b/pymssql.pyx Thu Aug 15 09:43:39 2013 -0700 @@ -213,6 +213,12 @@ self._conn.execute_non_query('%s TRAN' % tran_type) self._autocommit = status


We should have tests for these.

Original comment by msabr...@gmail.com on 18 Aug 2013 at 5:42

GoogleCodeExporter commented 9 years ago
And here are some tests:

"""
Test context managers -- i.e.: the `with` statement
"""

try:
    import unittest2 as unittest
except ImportError:
    import unittest

from pymssql import InterfaceError
from .helpers import pymssqlconn

class TestContextManagers(unittest.TestCase):
    def test_pymssql_Connection_with(self):
        with pymssqlconn() as conn:
            cursor = conn.cursor()
            cursor.execute("SELECT @@version AS version")
            self.assertIsNotNone(conn._conn)

        with self.assertRaises(InterfaceError) as context:
            self.assertIsNotNone(conn._conn)

        self.assertEqual(str(context.exception), "Connection is closed.")

    def test_pymssql_Cursor_with(self):
        conn = pymssqlconn()
        with conn.cursor() as cursor:
            cursor.execute("SELECT @@version AS version")
            self.assertIsNotNone(conn._conn)

        self.assertIsNotNone(cursor)

        with self.assertRaises(InterfaceError) as context:
            cursor.execute("SELECT @@version AS version")

        self.assertEqual(str(context.exception), "Cursor is closed.")

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

GoogleCodeExporter commented 9 years ago
Fixed by:

https://code.google.com/p/pymssql/source/detail?r=5303a5a787e28a0c29f5400a314ee1
a518a749ad

It would be great if folks could verify...

Original comment by msabr...@gmail.com on 19 Aug 2013 at 4:56