[Zope3-checkins] SVN: Zope3/trunk/src/zope/hookable/_ hookable_init(): Two bugs.

Tim Peters tim.one at comcast.net
Wed May 26 14:22:19 EDT 2004


Log message for revision 25018:
hookable_init():  Two bugs.

+ This should be declared int, and return ints, instead of
  pointer-to-PyObject.  This probably accounts for "impossible"
  SystemErrors under Mac OS X when creating a hookable object.

+ Minor.  The name of the function should have been embedded
  in the arg format list, not in the keyword argument list.

Added more tests for this stuff.


-=-
Modified: Zope3/trunk/src/zope/hookable/__init__.py
===================================================================
--- Zope3/trunk/src/zope/hookable/__init__.py	2004-05-26 18:17:58 UTC (rev 25017)
+++ Zope3/trunk/src/zope/hookable/__init__.py	2004-05-26 18:22:18 UTC (rev 25018)
@@ -19,7 +19,7 @@
 
    The idea is you create a function that does some default thing and
    make it hookable. Later, someone can modify what it does by calling
-   it's sethook method and changing it's implementation.  All users of
+   its sethook method and changing its implementation.  All users of
    the function, including tose that imported it, will see the change.
 
    >>> def f41():
@@ -54,7 +54,25 @@
    ...
    TypeError: readonly attribute
 
-   
+   Some error cases.
+
+   >>> g = hookable()  # not enough args
+   Traceback (most recent call last):
+      ...
+   TypeError: hookable() takes exactly 1 argument (0 given)
+
+   >>> g = hookable(f, f)  # too many args
+   Traceback (most recent call last):
+      ...
+   TypeError: hookable() takes exactly 1 argument (2 given)
+
+   >>> g = hookable(implementation=f)  # OK, but not advertised
+
+   >>> g = hookable(f, madeupkeywordname=f)  # bad kw name
+   Traceback (most recent call last):
+      ...
+   TypeError: 'madeupkeywordname' is an invalid keyword argument for this function
+
 $Id$
 """
 from _zope_hookable import *

Modified: Zope3/trunk/src/zope/hookable/_zope_hookable.c
===================================================================
--- Zope3/trunk/src/zope/hookable/_zope_hookable.c	2004-05-26 18:17:58 UTC (rev 25017)
+++ Zope3/trunk/src/zope/hookable/_zope_hookable.c	2004-05-26 18:22:18 UTC (rev 25018)
@@ -29,15 +29,15 @@
         PyObject *implementation;
 } hookable;
 
-static PyObject *
+static int
 hookable_init(hookable *self, PyObject *args, PyObject *kwds)
 {
-  static char *kwlist[] = {"implementation:hookable", NULL};
+  static char *kwlist[] = {"implementation", NULL};
   PyObject *implementation;
 
-  if (! PyArg_ParseTupleAndKeywords(args, kwds, "O", kwlist, 
+  if (! PyArg_ParseTupleAndKeywords(args, kwds, "O:hookable", kwlist,
                                     &implementation))
-    return NULL; 
+    return -1;
 
   Py_INCREF(implementation);
   Py_INCREF(implementation);
@@ -46,8 +46,7 @@
   Py_XDECREF(self->implementation);
   self->implementation = implementation;
 
-  Py_INCREF(Py_None);
-  return Py_None;
+  return 0;
 }
 
 static int
@@ -55,12 +54,12 @@
 {
   if (self->implementation != NULL && visit(self->implementation, arg) < 0)
     return -1;
-  if (self->old != NULL 
-      && self->old != self->implementation 
+  if (self->old != NULL
+      && self->old != self->implementation
       && visit(self->old, arg) < 0
       )
     return -1;
-      
+
   return 0;
 }
 
@@ -90,9 +89,9 @@
   static char *kwlist[] = {"implementation:sethook", NULL};
   PyObject *implementation, *old;
 
-  if (! PyArg_ParseTupleAndKeywords(args, kwds, "O", kwlist, 
+  if (! PyArg_ParseTupleAndKeywords(args, kwds, "O", kwlist,
                                     &implementation))
-    return NULL; 
+    return NULL;
 
   old = self->implementation;
   Py_INCREF(implementation);
@@ -142,7 +141,7 @@
 };
 
 
-static char Hookabletype__doc__[] = 
+static char Hookabletype__doc__[] =
 "Callable objects that support being overridden"
 ;
 
@@ -169,7 +168,7 @@
         /* tp_setattro       */ (setattrofunc)0,
         /* tp_as_buffer      */ 0,
         /* tp_flags          */ Py_TPFLAGS_DEFAULT
-				| Py_TPFLAGS_BASETYPE 
+				| Py_TPFLAGS_BASETYPE
                                 | Py_TPFLAGS_HAVE_GC,
 	/* tp_doc            */ Hookabletype__doc__,
         /* tp_traverse       */ (traverseproc)hookable_traverse,
@@ -189,7 +188,7 @@
         /* tp_init           */ (initproc)hookable_init,
         /* tp_alloc          */ (allocfunc)0,
         /* tp_new            */ (newfunc)0 /*PyType_GenericNew*/,
-	/* tp_free           */ 0/*_PyObject_GC_Del*/, 
+	/* tp_free           */ 0/*_PyObject_GC_Del*/,
 };
 
 static struct PyMethodDef zch_methods[] = {
@@ -207,17 +206,17 @@
 
   hookabletype.tp_new = PyType_GenericNew;
   hookabletype.tp_free = _PyObject_GC_Del;
-  
+
   if (PyType_Ready(&hookabletype) < 0)
     return;
-        
+
   m = Py_InitModule3("_zope_hookable", zch_methods,
                      "Provide an efficient implementation for hookable objects"
                      );
 
   if (m == NULL)
     return;
-        
+
   if (PyModule_AddObject(m, "hookable", (PyObject *)&hookabletype) < 0)
     return;
 }




More information about the Zope3-Checkins mailing list