Commit 506acb5c authored by Jim Fulton's avatar Jim Fulton

Can now supply a string as the computed attribute function, in which

case, the ComputedAttribute object does a simple getattr. This is a
simple way to set up aliases. For example::

  class foo:

     spam=ComputedAttribute('eggs')

is equivalent to:

  class foo:

     spam=ComputedAttribute(lambda self: self.eggs)

Note that the simple alias version also avoids a function
call.
parent 14bc8871
...@@ -33,7 +33,7 @@ ...@@ -33,7 +33,7 @@
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE. DAMAGE.
$Id: ComputedAttribute.c,v 1.3 1999/10/16 14:30:31 jim Exp $ $Id: ComputedAttribute.c,v 1.4 2001/01/23 14:37:20 jim Exp $
If you have questions regarding this software, If you have questions regarding this software,
contact: contact:
...@@ -108,6 +108,15 @@ CA_of(CA *self, PyObject *args) ...@@ -108,6 +108,15 @@ CA_of(CA *self, PyObject *args)
return self->callable; return self->callable;
} }
if (PyString_Check(self->callable))
{
/* Special case string as simple alias. */
PyObject *o;
UNLESS (PyArg_ParseTuple(args,"O", &o)) return NULL;
return PyObject_GetAttr(o, self->callable);
}
return PyObject_CallObject(self->callable, args); return PyObject_CallObject(self->callable, args);
} }
...@@ -136,7 +145,7 @@ void ...@@ -136,7 +145,7 @@ void
initComputedAttribute() initComputedAttribute()
{ {
PyObject *m, *d; PyObject *m, *d;
char *rev="$Revision: 1.3 $"; char *rev="$Revision: 1.4 $";
UNLESS(ExtensionClassImported) return; UNLESS(ExtensionClassImported) return;
...@@ -146,7 +155,7 @@ initComputedAttribute() ...@@ -146,7 +155,7 @@ initComputedAttribute()
/* Create the module and add the functions */ /* Create the module and add the functions */
m = Py_InitModule4("ComputedAttribute", methods, m = Py_InitModule4("ComputedAttribute", methods,
"Provide Computed Attributes\n\n" "Provide Computed Attributes\n\n"
"$Id: ComputedAttribute.c,v 1.3 1999/10/16 14:30:31 jim Exp $\n", "$Id: ComputedAttribute.c,v 1.4 2001/01/23 14:37:20 jim Exp $\n",
OBJECT(NULL),PYTHON_API_VERSION); OBJECT(NULL),PYTHON_API_VERSION);
d = PyModule_GetDict(m); d = PyModule_GetDict(m);
......
...@@ -33,7 +33,7 @@ ...@@ -33,7 +33,7 @@
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE. DAMAGE.
$Id: ComputedAttribute.c,v 1.3 1999/10/16 14:30:31 jim Exp $ $Id: ComputedAttribute.c,v 1.4 2001/01/23 14:37:20 jim Exp $
If you have questions regarding this software, If you have questions regarding this software,
contact: contact:
...@@ -108,6 +108,15 @@ CA_of(CA *self, PyObject *args) ...@@ -108,6 +108,15 @@ CA_of(CA *self, PyObject *args)
return self->callable; return self->callable;
} }
if (PyString_Check(self->callable))
{
/* Special case string as simple alias. */
PyObject *o;
UNLESS (PyArg_ParseTuple(args,"O", &o)) return NULL;
return PyObject_GetAttr(o, self->callable);
}
return PyObject_CallObject(self->callable, args); return PyObject_CallObject(self->callable, args);
} }
...@@ -136,7 +145,7 @@ void ...@@ -136,7 +145,7 @@ void
initComputedAttribute() initComputedAttribute()
{ {
PyObject *m, *d; PyObject *m, *d;
char *rev="$Revision: 1.3 $"; char *rev="$Revision: 1.4 $";
UNLESS(ExtensionClassImported) return; UNLESS(ExtensionClassImported) return;
...@@ -146,7 +155,7 @@ initComputedAttribute() ...@@ -146,7 +155,7 @@ initComputedAttribute()
/* Create the module and add the functions */ /* Create the module and add the functions */
m = Py_InitModule4("ComputedAttribute", methods, m = Py_InitModule4("ComputedAttribute", methods,
"Provide Computed Attributes\n\n" "Provide Computed Attributes\n\n"
"$Id: ComputedAttribute.c,v 1.3 1999/10/16 14:30:31 jim Exp $\n", "$Id: ComputedAttribute.c,v 1.4 2001/01/23 14:37:20 jim Exp $\n",
OBJECT(NULL),PYTHON_API_VERSION); OBJECT(NULL),PYTHON_API_VERSION);
d = PyModule_GetDict(m); d = PyModule_GetDict(m);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment