clueboy / pymssql_issues

0 stars 0 forks source link

Support "with" statement (PEP 343) #124

Closed clueboy closed 11 years ago

clueboy commented 11 years ago

From airdrik on August 13, 2013 11:05:51

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: http://code.google.com/p/pymssql/issues/detail?id=124

clueboy commented 11 years ago

From msabramo on August 15, 2013 09:37:22

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

+    def __enter__(self):
+        return self
+
+    def __exit__(self, exc_type, exc_value, traceback):
+        self.close()
+
     def close(self):
         """
         Close the connection to the databsae. Implicitly rolls back all

Status: Accepted
Owner: msabramo
Labels: -Type-Defect Type-Enhancement

clueboy commented 11 years ago

From msabramo on August 15, 2013 10:44:09

Issue 62 has been merged into this issue.

clueboy commented 11 years ago

From msabramo on August 18, 2013 10:42:15

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

+    def __enter__(self):
+        return self
+
+    def __exit__(self, exc_type, exc_value, traceback):
+        self.close()
+
     def close(self):
         """
         Close the connection to the databsae. Implicitly rolls back all
@@ -333,6 +339,12 @@
         """
         return self

+    def __enter__(self):
+        return self
+
+    def __exit__(self, exc_type, exc_value, traceback):
+        self.close()
+
     def callproc(self, bytes procname, parameters=()):
         """
         Call a stored procedure with the given name.

We should have tests for these.

clueboy commented 11 years ago

From msabramo on August 18, 2013 10:44:18

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.")
clueboy commented 11 years ago

From msabramo on August 19, 2013 09:56:54

Fixed by: https://code.google.com/p/pymssql/source/detail?r=5303a5a787e28a0c29f5400a314ee1a518a749ad It would be great if folks could verify...

Status: Fixed