Update of /usr/cvs/Public/pygresql/module
In directory druid.net:/tmp/cvs-serv2853/module
Modified Files:
pg.py pgmodule.c test_pg.py
Log Message:
Support for PQescapeStringConn and PQescapeByteaConn.
To see the diffs for this commit:
http://www.druid.net/pygresql/viewcvs.cgi/cvs/pygresql/module/pg.py.diff?r1=1.61&r2=1.62
Index: pg.py
===================================================================
RCS file: /usr/cvs/Public/pygresql/module/pg.py,v
retrieving revision 1.61
retrieving revision 1.62
diff -u -r1.61 -r1.62
--- pg.py 21 Nov 2008 13:10:51 -0000 1.61
+++ pg.py 21 Nov 2008 19:25:27 -0000 1.62
@@ -5,7 +5,7 @@
# Written by D'Arcy J.M. Cain
# Improved by Christoph Zwerschke
#
-# $Id: pg.py,v 1.61 2008/11/21 13:10:51 cito Exp $
+# $Id: pg.py,v 1.62 2008/11/21 19:25:27 cito Exp $
#
"""PyGreSQL classic interface.
@@ -188,9 +188,9 @@
else:
raise InternalError('Connection is not valid')
- # For convenience, define some module functions as static methods also:
- escape_string, escape_bytea, unescape_bytea = map(staticmethod,
- (escape_string, escape_bytea, unescape_bytea))
+ # escape_string and escape_bytea exist as methods,
+ # so we define unescape_bytea as a method as well
+ unescape_bytea = staticmethod(unescape_bytea)
def _do_debug(self, s):
"""Print a debug message."""
http://www.druid.net/pygresql/viewcvs.cgi/cvs/pygresql/module/pgmodule.c.diff?r1=1.84&r2=1.85
Index: pgmodule.c
===================================================================
RCS file: /usr/cvs/Public/pygresql/module/pgmodule.c,v
retrieving revision 1.84
retrieving revision 1.85
diff -u -r1.84 -r1.85
--- pgmodule.c 21 Nov 2008 17:25:28 -0000 1.84
+++ pgmodule.c 21 Nov 2008 19:25:27 -0000 1.85
@@ -1,5 +1,5 @@
/*
- * $Id: pgmodule.c,v 1.84 2008/11/21 17:25:28 cito Exp $
+ * $Id: pgmodule.c,v 1.85 2008/11/21 19:25:27 cito Exp $
* PyGres, version 2.2 A Python interface for PostgreSQL database. Written by
* D'Arcy J.M. Cain, ([EMAIL PROTECTED]). Based heavily on code written by
* Pascal Andre, [EMAIL PROTECTED] Copyright (c) 1995, Pascal Andre
@@ -2724,6 +2724,59 @@
return Py_None;
}
+/* escape string */
+static char pg_escape_string__doc__[] =
+"pg_escape_string(str) -- escape a string for use within SQL.";
+
+static PyObject *
+pg_escape_string(pgobject *self, PyObject *args) {
+ char *from; /* our string argument */
+ char *to=NULL; /* the result */
+ int from_length; /* length of string */
+ int to_length; /* length of result */
+ PyObject *ret; /* string object to return */
+
+ if (!PyArg_ParseTuple(args, "s#", &from, &from_length))
+ return NULL;
+ to_length = 2*from_length + 1;
+ if (to_length < from_length) { /* overflow */
+ to_length = from_length;
+ from_length = (from_length - 1)/2;
+ }
+ to = (char *)malloc(to_length);
+ to_length = (int)PQescapeStringConn(self->cnx,
+ to, from, (size_t)from_length, NULL);
+ ret = Py_BuildValue("s#", to, to_length);
+ if (to)
+ free(to);
+ if (!ret) /* pass on exception */
+ return NULL;
+ return ret;
+}
+
+/* escape bytea */
+static char pg_escape_bytea__doc__[] =
+"pg_escape_bytea(data) -- escape binary data for use within SQL as type
bytea.";
+
+static PyObject *
+pg_escape_bytea(pgobject *self, PyObject *args) {
+ unsigned char *from; /* our string argument */
+ unsigned char *to; /* the result */
+ int from_length; /* length of string */
+ size_t to_length; /* length of result */
+ PyObject *ret; /* string object to return */
+
+ if (!PyArg_ParseTuple(args, "s#", &from, &from_length))
+ return NULL;
+ to = PQescapeByteaConn(self->cnx, from, (int)from_length, &to_length);
+ ret = Py_BuildValue("s", to);
+ if (to)
+ PQfreemem((void *)to);
+ if (!ret) /* pass on exception */
+ return NULL;
+ return ret;
+}
+
#ifdef LARGE_OBJECTS
/* creates large object */
static char pg_locreate__doc__[] =
@@ -2821,7 +2874,6 @@
}
#endif /* LARGE_OBJECTS */
-
/* connection object methods */
static struct PyMethodDef pgobj_methods[] = {
{"source", (PyCFunction) pg_source, METH_VARARGS, pg_source__doc__},
@@ -2838,6 +2890,10 @@
pg_transaction__doc__},
{"parameter", (PyCFunction) pg_parameter, METH_VARARGS,
pg_parameter__doc__},
+ {"escape_string", (PyCFunction) pg_escape_string, METH_VARARGS,
+ pg_escape_string__doc__},
+ {"escape_bytea", (PyCFunction) pg_escape_bytea, METH_VARARGS,
+ pg_escape_bytea__doc__},
#ifdef DIRECT_ACCESS
{"putline", (PyCFunction) pg_putline, 1, pg_putline__doc__},
http://www.druid.net/pygresql/viewcvs.cgi/cvs/pygresql/module/test_pg.py.diff?r1=1.15&r2=1.16
Index: test_pg.py
===================================================================
RCS file: /usr/cvs/Public/pygresql/module/test_pg.py,v
retrieving revision 1.15
retrieving revision 1.16
diff -u -r1.15 -r1.16
--- test_pg.py 21 Nov 2008 13:00:21 -0000 1.15
+++ test_pg.py 21 Nov 2008 19:25:27 -0000 1.16
@@ -4,7 +4,7 @@
#
# Written by Christoph Zwerschke
#
-# $Id: test_pg.py,v 1.15 2008/11/21 13:00:21 cito Exp $
+# $Id: test_pg.py,v 1.16 2008/11/21 19:25:27 cito Exp $
#
"""Test the classic PyGreSQL interface in the pg module.
@@ -354,18 +354,24 @@
""""Test pg escape and unescape functions."""
def testEscapeString(self):
- self.assertEqual(pg.escape_string('hello'), 'hello')
+ self.assertEqual(pg.escape_string('plain'), 'plain')
+ self.assertEqual(pg.escape_string(
+ "that's k\xe4se"), "that''s k\xe4se")
self.assertEqual(pg.escape_string(
r"It's fine to have a \ inside."),
r"It''s fine to have a \\ inside.")
def testEscapeBytea(self):
- self.assertEqual(pg.escape_bytea('hello'), 'hello')
+ self.assertEqual(pg.escape_bytea('plain'), 'plain')
+ self.assertEqual(pg.escape_bytea(
+ "that's k\xe4se"), "that''s k\\\\344se")
self.assertEqual(pg.escape_bytea(
'O\x00ps\xff!'), r'O\\000ps\\377!')
def testUnescapeBytea(self):
- self.assertEqual(pg.unescape_bytea('hello'), 'hello')
+ self.assertEqual(pg.unescape_bytea('plain'), 'plain')
+ self.assertEqual(pg.unescape_bytea(
+ "that's k\\344se"), "that's k\xe4se")
self.assertEqual(pg.unescape_bytea(
r'O\000ps\377!'), 'O\x00ps\xff!')
@@ -405,18 +411,13 @@
def testAllConnectMethods(self):
methods = ['cancel', 'close', 'endcopy',
+ 'escape_bytea', 'escape_string',
'fileno', 'getline', 'getlo', 'getnotify',
'inserttable', 'locreate', 'loimport',
'parameter', 'putline', 'query', 'reset',
'source', 'transaction']
connection_methods = [a for a in dir(self.connection)
if callable(eval("self.connection." + a))]
- if 'transaction' not in connection_methods:
- # this may be the case for PostgreSQL < 7.4
- connection_methods.append('transaction')
- if 'parameter' not in connection_methods:
- # this may be the case for PostgreSQL < 7.4
- connection_methods.append('parameter')
self.assertEqual(methods, connection_methods)
def testAttributeDb(self):
@@ -695,12 +696,6 @@
'update', 'user']
db_attributes = [a for a in dir(self.db)
if not a.startswith('_')]
- if 'transaction' not in db_attributes:
- # this may be the case for PostgreSQL < 7.4
- db_attributes.insert(-4, 'transaction')
- if 'parameter' not in db_attributes:
- # this may be the case for PostgreSQL < 7.4
- db_attributes.insert(-12, 'parameter')
self.assertEqual(attributes, db_attributes)
def testAttributeDb(self):
@@ -745,13 +740,26 @@
self.assertEqual(self.db.db.user, def_user)
def testMethodEscapeString(self):
- self.assertEqual(self.db.escape_string('hello'), 'hello')
+ self.assertEqual(self.db.escape_string("plain"), "plain")
+ self.assertEqual(self.db.escape_string(
+ "that's k\xe4se"), "that''s k\xe4se")
+ self.assertEqual(self.db.escape_string(
+ r"It's fine to have a \ inside."),
+ r"It''s fine to have a \\ inside.")
def testMethodEscapeBytea(self):
- self.assertEqual(self.db.escape_bytea('hello'), 'hello')
+ self.assertEqual(self.db.escape_bytea("plain"), "plain")
+ self.assertEqual(self.db.escape_bytea(
+ "that's k\xe4se"), "that''s k\\\\344se")
+ self.assertEqual(self.db.escape_bytea(
+ 'O\x00ps\xff!'), r'O\\000ps\\377!')
def testMethodUnescapeBytea(self):
- self.assertEqual(self.db.unescape_bytea('hello'), 'hello')
+ self.assertEqual(self.db.unescape_bytea("plain"), "plain")
+ self.assertEqual(self.db.unescape_bytea(
+ "that's k\\344se"), "that's k\xe4se")
+ self.assertEqual(pg.unescape_bytea(
+ r'O\000ps\377!'), 'O\x00ps\xff!')
def testMethodQuery(self):
self.db.query("select 1+1")
@@ -809,16 +817,25 @@
self.db.close()
def testEscapeString(self):
+ self.assertEqual(self.db.escape_string("plain"), "plain")
+ self.assertEqual(self.db.escape_string(
+ "that's k\xe4se"), "that''s k\xe4se")
self.assertEqual(self.db.escape_string(
r"It's fine to have a \ inside."),
r"It''s fine to have a \\ inside.")
def testEscapeBytea(self):
+ self.assertEqual(self.db.escape_bytea("plain"), "plain")
+ self.assertEqual(self.db.escape_bytea(
+ "that's k\xe4se"), "that''s k\\\\344se")
self.assertEqual(self.db.escape_bytea(
'O\x00ps\xff!'), r'O\\000ps\\377!')
def testUnescapeBytea(self):
+ self.assertEqual(self.db.unescape_bytea("plain"), "plain")
self.assertEqual(self.db.unescape_bytea(
+ "that's k\\344se"), "that's k\xe4se")
+ self.assertEqual(pg.unescape_bytea(
r'O\000ps\377!'), 'O\x00ps\xff!')
def testPkey(self):
_______________________________________________
PyGreSQL mailing list
[email protected]
http://mailman.vex.net/mailman/listinfo/pygresql