Author: cito
Date: Sat Nov 21 23:42:57 2015
New Revision: 588
Log:
Make putline() accept bytes as well
Modified:
trunk/module/TEST_PyGreSQL_classic_connection.py
trunk/module/pgmodule.c
Modified: trunk/module/TEST_PyGreSQL_classic_connection.py
==============================================================================
--- trunk/module/TEST_PyGreSQL_classic_connection.py Sat Nov 21 23:19:45
2015 (r587)
+++ trunk/module/TEST_PyGreSQL_classic_connection.py Sat Nov 21 23:42:57
2015 (r588)
@@ -822,6 +822,20 @@
r = query("select * from test").getresult()
self.assertEqual(r, data)
+ def testPutlineBytesAndUnicode(self):
+ putline = self.c.putline
+ query = self.c.query
+ query("set client_encoding=utf8")
+ query("copy test from stdin")
+ try:
+ putline(u"47\tkäse\n".encode('utf8'))
+ putline("35\twürstel\n")
+ putline(b"\\.\n")
+ finally:
+ self.c.endcopy()
+ r = query("select * from test").getresult()
+ self.assertEqual(r, [(47, 'käse'), (35, 'würstel')])
+
def testGetline(self):
getline = self.c.getline
query = self.c.query
@@ -844,6 +858,27 @@
except IOError:
pass
+ def testGetlineBytesAndUnicode(self):
+ getline = self.c.getline
+ query = self.c.query
+ data = [(54, u'käse'.encode('utf8')), (73, u'würstel')]
+ self.c.inserttable('test', data)
+ query("copy test to stdout")
+ try:
+ v = getline()
+ self.assertIsInstance(v, str)
+ self.assertEqual(v, '54\tkäse')
+ v = getline()
+ self.assertIsInstance(v, str)
+ self.assertEqual(v, '73\twürstel')
+ self.assertEqual(getline(), '\\.')
+ self.assertIsNone(getline())
+ finally:
+ try:
+ self.c.endcopy()
+ except IOError:
+ pass
+
def testParameterChecks(self):
self.assertRaises(TypeError, self.c.putline)
self.assertRaises(TypeError, self.c.getline, 'invalid')
Modified: trunk/module/pgmodule.c
==============================================================================
--- trunk/module/pgmodule.c Sat Nov 21 23:19:45 2015 (r587)
+++ trunk/module/pgmodule.c Sat Nov 21 23:42:57 2015 (r588)
@@ -1300,11 +1300,12 @@
static char connPutLine__doc__[] =
"putline() -- sends a line directly to the backend";
-/* direct acces function : putline */
+/* direct access function: putline */
static PyObject *
connPutLine(connObject *self, PyObject *args)
{
char *line;
+ int line_length;
if (!self->cnx)
{
@@ -1313,7 +1314,7 @@
}
/* reads args */
- if (!PyArg_ParseTuple(args, "s", &line))
+ if (!PyArg_ParseTuple(args, "s#", &line, &line_length))
{
PyErr_SetString(PyExc_TypeError, "putline(line), with line
(string).");
return NULL;
@@ -1329,7 +1330,7 @@
return Py_None;
}
-/* direct access function : getline */
+/* direct access function: getline */
static char connGetLine__doc__[] =
"getline() -- gets a line directly from the backend.";
@@ -1372,7 +1373,7 @@
return str;
}
-/* direct access function : end copy */
+/* direct access function: end copy */
static char connEndCopy__doc__[] =
"endcopy() -- synchronizes client and server";
_______________________________________________
PyGreSQL mailing list
[email protected]
https://mail.vex.net/mailman/listinfo.cgi/pygresql