From 41259144473470601897a4741b3d73225b2f307b Mon Sep 17 00:00:00 2001 From: Anthony Tuininga Date: Mon, 7 Mar 2016 08:09:54 -0700 Subject: [PATCH] Added support for displaying a string representation of an Oracle object as a convenience. --- Object.c | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/Object.c b/Object.c index 262177f..cf1d2f1 100644 --- a/Object.c +++ b/Object.c @@ -20,6 +20,7 @@ typedef struct { //----------------------------------------------------------------------------- static void Object_Free(udt_Object*); static PyObject *Object_GetAttr(udt_Object*, PyObject*); +static PyObject *Object_Repr(udt_Object*); static int Object_SetAttr(udt_Object*, PyObject*, PyObject*); static PyObject *Object_ConvertToPython(udt_Environment*, OCITypeCode, dvoid*, dvoid*, udt_ObjectType*); @@ -83,7 +84,7 @@ static PyTypeObject g_ObjectType = { 0, // tp_getattr 0, // tp_setattr 0, // tp_compare - 0, // tp_repr + (reprfunc) Object_Repr, // tp_repr 0, // tp_as_number 0, // tp_as_sequence 0, // tp_as_mapping @@ -247,6 +248,38 @@ static void Object_Free( } +//----------------------------------------------------------------------------- +// Object_Repr() +// Return a string representation of the object. +//----------------------------------------------------------------------------- +static PyObject *Object_Repr( + udt_Object *self) // object type to return the string for +{ + PyObject *module, *name, *result, *format, *formatArgs; + + if (GetModuleAndName(Py_TYPE(self), &module, &name) < 0) + return NULL; + format = cxString_FromAscii("<%s.%s %s.%s>"); + if (!format) { + Py_DECREF(module); + Py_DECREF(name); + return NULL; + } + formatArgs = PyTuple_Pack(4, module, name, self->objectType->schema, + self->objectType->name); + Py_DECREF(module); + Py_DECREF(name); + if (!formatArgs) { + Py_DECREF(format); + return NULL; + } + result = cxString_Format(format, formatArgs); + Py_DECREF(format); + Py_DECREF(formatArgs); + return result; +} + + //----------------------------------------------------------------------------- // Object_ConvertFromPython() // Convert a Python value to an Oracle value.