Author: cito
Date: Sat Nov 21 11:19:59 2015
New Revision: 572

Log:
Get the classic_functions tests running in Py 2/3

Modified:
   trunk/module/TEST_PyGreSQL_classic_functions.py
   trunk/module/pgmodule.c

Modified: trunk/module/TEST_PyGreSQL_classic_functions.py
==============================================================================
--- trunk/module/TEST_PyGreSQL_classic_functions.py     Sat Nov 21 09:19:33 
2015        (r571)
+++ trunk/module/TEST_PyGreSQL_classic_functions.py     Sat Nov 21 11:19:59 
2015        (r572)
@@ -17,14 +17,16 @@
 except ImportError:
     import unittest
 
+import re
+
+import pg  # the module under test
+
 try:
     long
 except NameError:  # Python >= 3.0
     long = int
 
-import re
-
-import pg  # the module under test
+unicode_strings = str is not bytes
 
 
 class TestAuxiliaryFunctions(unittest.TestCase):
@@ -264,22 +266,60 @@
 
     def testEscapeString(self):
         f = pg.escape_string
-        self.assertEqual(f('plain'), 'plain')
-        self.assertEqual(f("that's k\xe4se"), "that''s k\xe4se")
-        self.assertEqual(f(r"It's fine to have a \ inside."),
-            r"It''s fine to have a \\ inside.")
+        r = f('plain')
+        self.assertIsInstance(r, str)
+        self.assertEqual(r, 'plain')
+        if unicode_strings:
+            r = f(b'plain')
+            self.assertIsInstance(r, str)
+            self.assertEqual(r, 'plain')
+        r = f("das is' käse")
+        self.assertIsInstance(r, str)
+        self.assertEqual(r, "das is'' käse")
+        if unicode_strings:
+            r = f("das is' käse".encode('utf-8'))
+            self.assertIsInstance(r, str)
+            self.assertEqual(r, "das is'' käse")
+        r = f(r"It's fine to have a \ inside.")
+        self.assertEqual(r, r"It''s fine to have a \\ inside.")
 
     def testEscapeBytea(self):
         f = pg.escape_bytea
-        self.assertEqual(f('plain'), 'plain')
-        self.assertEqual(f("that's k\xe4se"), "that''s k\\\\344se")
-        self.assertEqual(f('O\x00ps\xff!'), r'O\\000ps\\377!')
+        r = f('plain')
+        self.assertIsInstance(r, str)
+        self.assertEqual(r, 'plain')
+        if unicode_strings:
+            r = f(b'plain')
+            self.assertIsInstance(r, str)
+            self.assertEqual(r, 'plain')
+        r = f("das is' käse")
+        self.assertIsInstance(r, str)
+        self.assertEqual(r, r"das is'' k\\303\\244se")
+        if unicode_strings:
+            r = f("das is' käse".encode('utf-8'))
+            self.assertIsInstance(r, str)
+            self.assertEqual(r, r"das is'' k\\303\\244se")
+        r = f(b'O\x00ps\xff!')
+        self.assertEqual(r, r'O\\000ps\\377!')
 
     def testUnescapeBytea(self):
         f = pg.unescape_bytea
-        self.assertEqual(f('plain'), 'plain')
-        self.assertEqual(f("that's k\\344se"), "that's k\xe4se")
-        self.assertEqual(f(r'O\000ps\377!'), 'O\x00ps\xff!')
+        r = f('plain')
+        self.assertIsInstance(r, str)
+        self.assertEqual(r, 'plain')
+        if unicode_strings:
+            r = f(b'plain')
+            self.assertIsInstance(r, str)
+            self.assertEqual(r, 'plain')
+        r = f(r"das is' k\303\244se")
+        self.assertIsInstance(r, str)
+        self.assertEqual(r, "das is' käse")
+        if unicode_strings:
+            r = f(u"das is' k\\303\\244se")
+            self.assertIsInstance(r, str)
+            self.assertEqual(r, "das is' käse")
+        r = f(r'O\\000ps\\377!')
+        self.assertEqual(r, r'O\000ps\377!')
 
 
 class TestConfigFunctions(unittest.TestCase):

Modified: trunk/module/pgmodule.c
==============================================================================
--- trunk/module/pgmodule.c     Sat Nov 21 09:19:33 2015        (r571)
+++ trunk/module/pgmodule.c     Sat Nov 21 11:19:59 2015        (r572)
@@ -3716,15 +3716,18 @@
 {
        unsigned char *from; /* our string argument */
        unsigned char *to; /* the result */
+       int from_length; /* length of string */
        size_t to_length; /* length of result string */
        PyObject *ret; /* string object to return */
 
-       if (!PyArg_ParseTuple(args, "s", &from))
+       if (!PyArg_ParseTuple(args, "s#", &from, &from_length))
                return NULL;
        to = PQunescapeBytea(from, &to_length);
+       if (!to)
+           return NULL;
        ret = Py_BuildValue("s#", to, (int)to_length);
        if (to)
-               PQfreemem((void *)to);
+           PQfreemem((void *)to);
        if (!ret) /* pass on exception */
                return NULL;
        return ret;
_______________________________________________
PyGreSQL mailing list
[email protected]
https://mail.vex.net/mailman/listinfo.cgi/pygresql

Reply via email to