+static HeapTuple
+PLyMapping_ToTuple(PLyTypeInfo * info, PyObject * mapping)
+{
+ TupleDesc desc;
+ HeapTuple tuple;
+ Datum *values;
+ bool *nulls;
+ volatile int i;
+
+ Assert(PyMapping_Check(mapping));
+
+ desc = lookup_rowtype_tupdesc(info->out.d.typoid, -1);
+ if (info->is_rowtype == 2)
+ PLy_output_tuple_funcs(info, desc);
+ Assert(info->is_rowtype == 1);
+
+ /* Build tuple */
+ values = palloc(sizeof(Datum) * desc->natts);
+ nulls = palloc(sizeof(bool) * desc->natts);
+ for (i = 0; i < desc->natts; ++i)
+ {
+ char *key;
+ PyObject *volatile value,
+ *volatile so;
+
+ key = NameStr(desc->attrs[i]->attname);
+ value = so = NULL;
+ PG_TRY();
+ {
+ value = PyMapping_GetItemString(mapping, key);
+ if (value == Py_None)
+ {
+ values[i] = (Datum) NULL;
+ nulls[i] = true;
+ }
+ else if (value)
+ {
+ char *valuestr;
+
+ so = PyObject_Str(value);
+ if (so == NULL)
+ PLy_elog(ERROR, "could not compute string representation of Python object");
+ valuestr = PyString_AsString(so);
+
+ values[i] = InputFunctionCall(&info->out.r.atts[i].typfunc
+ ,valuestr
+ ,info->out.r.atts[i].typioparam
+ ,-1);
+ Py_DECREF(so);
+ so = NULL;
+ nulls[i] = false;
+ }
+ else
+ ereport(ERROR,
+ (errcode(ERRCODE_UNDEFINED_COLUMN),
+ errmsg("key \"%s\" not found in mapping", key),
+ errhint("To return null in a column, "
+ "add the value None to the mapping with the key named after the column.")));
+
+ Py_XDECREF(value);
+ value = NULL;
+ }
+ PG_CATCH();
+ {
+ Py_XDECREF(so);
+ Py_XDECREF(value);
+ PG_RE_THROW();
+ }
+ PG_END_TRY();
+ }
+
+ tuple = heap_form_tuple(desc, values, nulls);
+ ReleaseTupleDesc(desc);
+ pfree(values);
+ pfree(nulls);
+
+ return tuple;
+}
+
+
+static HeapTuple
+PLySequence_ToTuple(PLyTypeInfo * info, PyObject * sequence)
+{
+ TupleDesc desc;
+ HeapTuple tuple;
+ Datum *values;
+ bool *nulls;
+ volatile int i;
+
+ Assert(PySequence_Check(sequence));
+
+ /*
+ * Check that sequence length is exactly same as PG tuple's. We actually
+ * can ignore exceeding items or assume missing ones as null but to avoid
+ * plpython developer's errors we are strict here
+ */
+ desc = lookup_rowtype_tupdesc(info->out.d.typoid, -1);
+ if (PySequence_Length(sequence) != desc->natts)
+ ereport(ERROR,
+ (errcode(ERRCODE_DATATYPE_MISMATCH),
+ errmsg("length of returned sequence did not match number of columns in row")));
+
+ if (info->is_rowtype == 2)
+ PLy_output_tuple_funcs(info, desc);
+ Assert(info->is_rowtype == 1);
+
+ /* Build tuple */
+ values = palloc(sizeof(Datum) * desc->natts);
+ nulls = palloc(sizeof(bool) * desc->natts);
+ for (i = 0; i < desc->natts; ++i)
+ {
+ PyObject *volatile value,
+ *volatile so;
+
+ value = so = NULL;
+ PG_TRY();
+ {
+ value = PySequence_GetItem(sequence, i);
+ Assert(value);
+ if (value == Py_None)
+ {
+ values[i] = (Datum) NULL;
+ nulls[i] = true;
+ }
+ else if (value)
+ {
+ char *valuestr;
+
+ so = PyObject_Str(value);
+ if (so == NULL)
+ PLy_elog(ERROR, "could not compute string representation of Python object");
+ valuestr = PyString_AsString(so);
+ values[i] = InputFunctionCall(&info->out.r.atts[i].typfunc
+ ,valuestr
+ ,info->out.r.atts[i].typioparam
+ ,-1);
+ Py_DECREF(so);
+ so = NULL;
+ nulls[i] = false;
+ }
+
+ Py_XDECREF(value);
+ value = NULL;
+ }
+ PG_CATCH();
+ {
+ Py_XDECREF(so);
+ Py_XDECREF(value);
+ PG_RE_THROW();
+ }
+ PG_END_TRY();
+ }
+
+ tuple = heap_form_tuple(desc, values, nulls);
+ ReleaseTupleDesc(desc);
+ pfree(values);
+ pfree(nulls);
+
+ return tuple;
+}
+
+
+static HeapTuple
+PLyObject_ToTuple(PLyTypeInfo * info, PyObject * object)
+{
+ TupleDesc desc;
+ HeapTuple tuple;
+ Datum *values;
+ bool *nulls;
+ volatile int i;
+
+ desc = lookup_rowtype_tupdesc(info->out.d.typoid, -1);
+ if (info->is_rowtype == 2)
+ PLy_output_tuple_funcs(info, desc);
+ Assert(info->is_rowtype == 1);
+
+ /* Build tuple */
+ values = palloc(sizeof(Datum) * desc->natts);
+ nulls = palloc(sizeof(bool) * desc->natts);
+ for (i = 0; i < desc->natts; ++i)
+ {
+ char *key;
+ PyObject *volatile value,
+ *volatile so;
+
+ key = NameStr(desc->attrs[i]->attname);
+ value = so = NULL;
+ PG_TRY();
+ {
+ value = PyObject_GetAttrString(object, key);
+ if (value == Py_None)
+ {
+ values[i] = (Datum) NULL;
+ nulls[i] = true;
+ }
+ else if (value)
+ {
+ char *valuestr;
+
+ so = PyObject_Str(value);
+ if (so == NULL)
+ PLy_elog(ERROR, "could not compute string representation of Python object");
+ valuestr = PyString_AsString(so);
+ values[i] = InputFunctionCall(&info->out.r.atts[i].typfunc
+ ,valuestr
+ ,info->out.r.atts[i].typioparam
+ ,-1);
+ Py_DECREF(so);
+ so = NULL;
+ nulls[i] = false;
+ }
+ else
+ ereport(ERROR,
+ (errcode(ERRCODE_UNDEFINED_COLUMN),
+ errmsg("attribute \"%s\" does not exist in Python object", key),
+ errhint("To return null in a column, "
+ "let the returned object have an attribute named "
+ "after column with value None.")));
+
+ Py_XDECREF(value);
+ value = NULL;
+ }
+ PG_CATCH();
+ {
+ Py_XDECREF(so);
+ Py_XDECREF(value);
+ PG_RE_THROW();
+ }
+ PG_END_TRY();
+ }
+
+ tuple = heap_form_tuple(desc, values, nulls);
+ ReleaseTupleDesc(desc);
+ pfree(values);
+ pfree(nulls);
+
+ return tuple;
+}
+
+
+/* initialization, some python variables function declared here */
+
+/* interface to postgresql elog */