summaryrefslogtreecommitdiff
path: root/psycopg/psycopgmodule.c
diff options
context:
space:
mode:
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>2011-02-19 00:05:43 +0000
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>2011-02-19 00:05:43 +0000
commitc620f18be14b994337a3bb939974e1fb769823fd (patch)
treefd450abdf6103a04ead99f9a7512ab45af8eb14d /psycopg/psycopgmodule.c
parente2cbc3411dbfa901422241f75cd5906aaa8181f4 (diff)
downloadpsycopg2-c620f18be14b994337a3bb939974e1fb769823fd.tar.gz
Provide cursor.description as named tuple if possible
If namedtuple() is not available, use regular tuples.
Diffstat (limited to 'psycopg/psycopgmodule.c')
-rw-r--r--psycopg/psycopgmodule.c42
1 files changed, 42 insertions, 0 deletions
diff --git a/psycopg/psycopgmodule.c b/psycopg/psycopgmodule.c
index 59562e2..82a6a2d 100644
--- a/psycopg/psycopgmodule.c
+++ b/psycopg/psycopgmodule.c
@@ -69,6 +69,9 @@ HIDDEN int psycopg_debug_enabled = 0;
/* Python representation of SQL NULL */
HIDDEN PyObject *psyco_null = NULL;
+/* The type of the cursor.description items */
+HIDDEN PyObject *psyco_DescriptionType = NULL;
+
/** connect module-level function **/
#define psyco_connect_doc \
"connect(dsn, ...) -- Create a new database connection.\n\n" \
@@ -685,6 +688,44 @@ psyco_GetDecimalType(void)
}
+/* Create a namedtuple for cursor.description items
+ *
+ * Return None in case of expected errors (e.g. namedtuples not available)
+ * NULL in case of errors to propagate.
+ */
+static PyObject *
+psyco_make_description_type(void)
+{
+ PyObject *nt = NULL;
+ PyObject *coll = NULL;
+ PyObject *rv = NULL;
+
+ /* Try to import collections.namedtuple */
+ if (!(coll = PyImport_ImportModule("collections"))) {
+ Dprintf("psyco_make_description_type: collections import failed");
+ PyErr_Clear();
+ rv = Py_None;
+ goto exit;
+ }
+ if (!(nt = PyObject_GetAttrString(coll, "namedtuple"))) {
+ Dprintf("psyco_make_description_type: no collections.namedtuple");
+ PyErr_Clear();
+ rv = Py_None;
+ goto exit;
+ }
+
+ /* Build the namedtuple */
+ rv = PyObject_CallFunction(nt, "ss", "Column",
+ "name type_code display_size internal_size precision scale null_ok");
+
+exit:
+ Py_XDECREF(coll);
+ Py_XDECREF(nt);
+
+ return rv;
+}
+
+
/** method table and module initialization **/
static PyMethodDef psycopgMethods[] = {
@@ -886,6 +927,7 @@ INIT_MODULE(_psycopg)(void)
psycoEncodings = PyDict_New();
psyco_encodings_fill(psycoEncodings);
psyco_null = Bytes_FromString("NULL");
+ psyco_DescriptionType = psyco_make_description_type();
/* set some module's parameters */
PyModule_AddStringConstant(module, "__version__", PSYCOPG_VERSION);