[Zope-Checkins] CVS: Zope3/lib/python/Zope/ContextWrapper - wrapper.c:1.12.2.12

Guido van Rossum guido@python.org
Wed, 20 Mar 2002 14:13:35 -0500


Update of /cvs-repository/Zope3/lib/python/Zope/ContextWrapper
In directory cvs.zope.org:/tmp/cvs-serv16559

Modified Files:
      Tag: Zope-3x-branch
	wrapper.c 
Log Message:
It turns out that Wrapper(None) was not usable as a truth value,
because the wrapper didn't implement nb_nonzero; hence,
PyObject_IsTrue() would call sq_length or mp_length which would raise
an exception.

To fix this, I added a PyNumberMethods structure with only one field
filled in: nb_nonzero.  (It might be interesting to add other numeric
operations, but not now.  See my post to zope3-dev.)


=== Zope3/lib/python/Zope/ContextWrapper/wrapper.c 1.12.2.11 => 1.12.2.12 ===
 
 /*
+ *   Number methods
+ */
+
+static int
+wrap_nonzero(PyObject *self)
+{
+    return PyObject_IsTrue(Wrapper_GetObject(self));
+}
+
+/*
  *   Sequence methods
  */
 
@@ -277,6 +287,21 @@
     return NULL;
 }
 
+static PyNumberMethods
+wrap_as_number = {
+    0, /* nb_add */
+    0, /* nb_subtract */
+    0, /* nb_multiply */
+    0, /* nb_divide */
+    0, /* nb_remainder */
+    0, /* nb_divmod */
+    0, /* nb_power */
+    0, /* nb_negative */
+    0, /* nb_positive */
+    0, /* nb_absolute */
+    wrap_nonzero, /* nb_nonzero */
+};
+
 static PySequenceMethods
 wrap_as_sequence = {
     wrap_length,			/* sq_length */
@@ -325,7 +350,7 @@
     0,					/* tp_setattr */
     wrap_compare,			/* tp_compare */
     wrap_repr,				/* tp_repr */
-    0,					/* tp_as_number */
+    &wrap_as_number,			/* tp_as_number */
     &wrap_as_sequence,			/* tp_as_sequence */
     &wrap_as_mapping,			/* tp_as_mapping */
     wrap_hash,				/* tp_hash */