Commits

Mike Bayer committed 63c2bdc Merge

Merged in msabramo/sqlalchemy/trac_ticket_2683 (pull request #46)

dialects/mssql/pymssql.py: Modify module.Binary to work properly with byte strings in Python 3

Comments (0)

Files changed (2)

lib/sqlalchemy/dialects/mssql/pymssql.py

         module = __import__('pymssql')
         # pymmsql doesn't have a Binary method.  we use string
         # TODO: monkeypatching here is less than ideal
-        module.Binary = str
+        module.Binary = lambda x: x if hasattr(x, 'decode') else str(x)
 
         client_ver = tuple(int(x) for x in module.__version__.split("."))
         if client_ver < (1, ):

test/dialect/test_mssql.py

                         not in list(engine.execute(tbl.select()).first())
                 engine.execute(tbl.delete())
 
+class MonkeyPatchedBinaryTest(fixtures.TestBase):
+    def test_unicode(self):
+        module = __import__('pymssql')
+        result = module.Binary(u'foo')
+        eq_(result, u'foo')
+
+    def test_bytes(self):
+        module = __import__('pymssql')
+        input = b'\x80\x03]q\x00X\x03\x00\x00\x00oneq\x01a.'
+        expected_result = input
+        result = module.Binary(input)
+        eq_(result, expected_result)
+
 class BinaryTest(fixtures.TestBase, AssertsExecutionResults):
     """Test the Binary and VarBinary types"""