[PyGreSQL] Update to bytea escape patch, bugfix, enhancement

Charlie Dyson charlie at charliedyson.net
Thu Sep 9 14:47:05 EDT 2004


Thanks to Kavous Bojnourdi for pointing out that the pg client library
has functions that do what I was trying to do - this means more code
reuse (which is nice). Here is my version of his version of my patch,
with two changes incorporated. Firstly the unnecessary output variable
and the memory copying were removed from both functions. Secondly the
pgbytea_unescape called Py_BuildValue with an "s", rather than an "s#"
argument, leaving it unable to cope with any "premature" zero bytes,
which it would have assumed to be terminators.

Here then is the third revision of the patch - once again thanks to
Kavous for his input.
== CUT ==
diff -ur PyGreSQL-3.6-pre040830/pgmodule.c
PyGreSQL-3.6-pre040830_mod/pgmodule.c
--- PyGreSQL-3.6-pre040830/pgmodule.c	2004-08-30 09:54:27.000000000
+0100
+++ PyGreSQL-3.6-pre040830_mod/pgmodule.c	2004-09-09 19:40:50.037533519
+0100
@@ -1550,6 +1550,48 @@
 
 	return (PyObject *) npgobj;
 }
+static PyObject *pgbytea_escape(PyObject *self, PyObject *args) {
+	char *str; /* Our argument */
+	int strlength; /* Length of string */
+	int tolen=0;
+	char * tmp;
+	PyObject *ret; /* String object to return */
+
+	if (!PyArg_ParseTuple(args, "s#", &str, &strlength))
+		return NULL;
+
+	tmp = PQescapeBytea(str,strlength,&tolen);
+	ret = Py_BuildValue("s", tmp);
+	PQfreemem(tmp);
+
+	if(!ret)
+		/* Pass on exception */
+		return NULL;
+
+	return ret;
+}
+
+static PyObject *pgbytea_unescape(PyObject *self, PyObject *args) {
+	char *str; /* Our argument */
+	int strlength; /* Length of string */
+	int tolen=0;
+	char * tmp;
+	
+	PyObject *ret; /* String object to return */
+
+	if (!PyArg_ParseTuple(args, "s#", &str, &strlength))
+		return NULL;
+	
+	tmp = PQunescapeBytea(str,&tolen);
+	ret = Py_BuildValue("s#", tmp, tolen);
+	PQfreemem(tmp);
+
+	if(!ret)
+		/* Pass on exception */
+		return NULL;
+
+	return ret;
+}
 
 /* pgobject methods */
 
@@ -3175,6 +3217,9 @@
 
 static struct PyMethodDef pg_methods[] = {
 	{"connect", (PyCFunction) pgconnect, 3, connect__doc__},
+	{"pgbytea_escape",pgbytea_escape,METH_VARARGS,"Escape bytea"},
+	{"pgbytea_unescape",pgbytea_unescape,METH_VARARGS,"UnEscape bytea"},
+	
 
 #ifdef DEFAULT_VARS
 	{"get_defhost", pggetdefhost, 1, getdefhost__doc__},
==CUT==

Regards to all concerned,

  _______            ___       ___                   
 / ___/ /  ___ _____/ (_)__   / _ \__ _____ ___  ___ 
/ /__/ _ \/ _ `/ __/ / / -_) / // / // (_-</ _ \/ _ \
\___/_//_/\_,_/_/ /_/_/\__/ /____/\_, /___/\___/_//_/
                                 /___/               
	charlie at charlie dyson dot net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.vex.net/pipermail/pygresql/attachments/20040909/03605d52/attachment.html


More information about the PyGreSQL mailing list