summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>2010-12-15 03:07:13 +0000
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>2010-12-21 04:02:14 +0000
commitae06fb03e75c47f662606490203f365be26afd46 (patch)
treee9600a54a816ed64dd3f21eb847ef8a4b9ec58f3
parenta50a91fc7bbce34cca6db5d89cdcb0db7abd3ab6 (diff)
downloadpsycopg2-ae06fb03e75c47f662606490203f365be26afd46.tar.gz
Added psycopg_strdup utility function.
-rw-r--r--ChangeLog4
-rw-r--r--psycopg/psycopg.h2
-rw-r--r--psycopg/utils.c22
3 files changed, 28 insertions, 0 deletions
diff --git a/ChangeLog b/ChangeLog
index 676cded..ac6cc66 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2010-12-15 Daniele Varrazzo <daniele.varrazzo@gmail.com>
+
+ * psycopg/utils.c: Added psycopg_strdup function.
+
2010-12-14 Daniele Varrazzo <daniele.varrazzo@gmail.com>
* psycopg/connection_type.c: No need to put connection fields to zero.
diff --git a/psycopg/psycopg.h b/psycopg/psycopg.h
index d023e47..00cb98a 100644
--- a/psycopg/psycopg.h
+++ b/psycopg/psycopg.h
@@ -120,6 +120,8 @@ HIDDEN void psyco_set_error(PyObject *exc, PyObject *curs, const char *msg,
HIDDEN char *psycopg_escape_string(PyObject *conn,
const char *from, Py_ssize_t len, char *to, Py_ssize_t *tolen);
+HIDDEN char *psycopg_strdup(const char *from, Py_ssize_t len);
+
/* Exceptions docstrings */
#define Error_doc \
"Base class for error exceptions."
diff --git a/psycopg/utils.c b/psycopg/utils.c
index 22464e6..f2a0ab0 100644
--- a/psycopg/utils.c
+++ b/psycopg/utils.c
@@ -70,3 +70,25 @@ psycopg_escape_string(PyObject *obj, const char *from, Py_ssize_t len,
return to;
}
+
+/* Duplicate a string.
+ *
+ * Allocate a new buffer on the Python heap containing the new string.
+ * 'len' is optional: if 0 the length is calculated.
+ *
+ * Return NULL and set an exception in case of error.
+ */
+char *
+psycopg_strdup(const char *from, Py_ssize_t len)
+{
+ char *rv;
+
+ if (!len) { len = strlen(from); }
+ if (!(rv = PyMem_Malloc(len + 1))) {
+ PyErr_NoMemory();
+ return NULL;
+ }
+ strcpy(rv, from);
+ return rv;
+}
+