Changeset 588


Ignore:
Timestamp:
Nov 21, 2015, 11:42:57 PM (4 years ago)
Author:
cito
Message:

Make putline() accept bytes as well

Location:
trunk/module
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/module/TEST_PyGreSQL_classic_connection.py

    r587 r588  
    823823        self.assertEqual(r, data)
    824824
     825    def testPutlineBytesAndUnicode(self):
     826        putline = self.c.putline
     827        query = self.c.query
     828        query("set client_encoding=utf8")
     829        query("copy test from stdin")
     830        try:
     831            putline(u"47\tkÀse\n".encode('utf8'))
     832            putline("35\twÃŒrstel\n")
     833            putline(b"\\.\n")
     834        finally:
     835            self.c.endcopy()
     836        r = query("select * from test").getresult()
     837        self.assertEqual(r, [(47, 'kÀse'), (35, 'wÃŒrstel')])
     838
    825839    def testGetline(self):
    826840        getline = self.c.getline
     
    839853                else:
    840854                    self.assertIsNone(v)
     855        finally:
     856            try:
     857                self.c.endcopy()
     858            except IOError:
     859                pass
     860
     861    def testGetlineBytesAndUnicode(self):
     862        getline = self.c.getline
     863        query = self.c.query
     864        data = [(54, u'kÀse'.encode('utf8')), (73, u'wÃŒrstel')]
     865        self.c.inserttable('test', data)
     866        query("copy test to stdout")
     867        try:
     868            v = getline()
     869            self.assertIsInstance(v, str)
     870            self.assertEqual(v, '54\tkÀse')
     871            v = getline()
     872            self.assertIsInstance(v, str)
     873            self.assertEqual(v, '73\twÃŒrstel')
     874            self.assertEqual(getline(), '\\.')
     875            self.assertIsNone(getline())
    841876        finally:
    842877            try:
  • trunk/module/pgmodule.c

    r585 r588  
    13011301"putline() -- sends a line directly to the backend";
    13021302
    1303 /* direct acces function : putline */
     1303/* direct access function: putline */
    13041304static PyObject *
    13051305connPutLine(connObject *self, PyObject *args)
    13061306{
    13071307        char *line;
     1308        int line_length;
    13081309
    13091310        if (!self->cnx)
     
    13141315
    13151316        /* reads args */
    1316         if (!PyArg_ParseTuple(args, "s", &line))
     1317        if (!PyArg_ParseTuple(args, "s#", &line, &line_length))
    13171318        {
    13181319                PyErr_SetString(PyExc_TypeError, "putline(line), with line (string).");
     
    13301331}
    13311332
    1332 /* direct access function : getline */
     1333/* direct access function: getline */
    13331334static char connGetLine__doc__[] =
    13341335"getline() -- gets a line directly from the backend.";
     
    13731374}
    13741375
    1375 /* direct access function : end copy */
     1376/* direct access function: end copy */
    13761377static char connEndCopy__doc__[] =
    13771378"endcopy() -- synchronizes client and server";
Note: See TracChangeset for help on using the changeset viewer.