[Checkins] SVN: zope.hookable/trunk/ Python 3 support

Lennart Regebro regebro at gmail.com
Mon Mar 14 11:41:12 EDT 2011


Log message for revision 120911:
  Python 3 support

Changed:
  U   zope.hookable/trunk/CHANGES.txt
  U   zope.hookable/trunk/setup.py
  U   zope.hookable/trunk/src/zope/hookable/__init__.py
  U   zope.hookable/trunk/src/zope/hookable/_zope_hookable.c

-=-
Modified: zope.hookable/trunk/CHANGES.txt
===================================================================
--- zope.hookable/trunk/CHANGES.txt	2011-03-14 15:39:44 UTC (rev 120910)
+++ zope.hookable/trunk/CHANGES.txt	2011-03-14 15:41:12 UTC (rev 120911)
@@ -6,6 +6,8 @@
 
 - Removed use of 'zope.testing.doctestunit' in favor of stdlib's 'doctest.
 
+- Python 3 support.
+
 3.4.1 (2009-04-05)
 ------------------
 

Modified: zope.hookable/trunk/setup.py
===================================================================
--- zope.hookable/trunk/setup.py	2011-03-14 15:39:44 UTC (rev 120910)
+++ zope.hookable/trunk/setup.py	2011-03-14 15:41:12 UTC (rev 120911)
@@ -31,18 +31,30 @@
       author='Zope Foundation and Contributors',
       author_email='zope-dev at zope.org',
       long_description="Hookable object support.",
+      classifiers=[
+        "Development Status :: 5 - Production/Stable",
+        "Intended Audience :: Developers",
+        "License :: OSI Approved :: Zope Public License",
+        "Operating System :: OS Independent",
+        "Programming Language :: Python :: 2.4",
+        "Programming Language :: Python :: 2.5",
+        "Programming Language :: Python :: 2.6",
+        "Programming Language :: Python :: 3.1",
+        "Topic :: Software Development :: Libraries :: Python Modules",
+      ],
 
       packages=find_packages('src'),
-      package_dir = {'': 'src'},
+      package_dir={'': 'src'},
       ext_modules=[Extension("zope.hookable._zope_hookable",
                              [os.path.join('src', 'zope', 'hookable',
                                            "_zope_hookable.c")
-                              ]),
+                              ], extra_compile_args=['-g']),
                    ],
       namespace_packages=['zope',],
       extras_require=dict(test=['zope.testing']),
       install_requires=['setuptools'],
-      include_package_data = True,
-
-      zip_safe = False,
+      include_package_data=True,
+      zip_safe=False,
+      
+      test_suite='zope.hookable.tests.test_hookable.test_suite',
       )

Modified: zope.hookable/trunk/src/zope/hookable/__init__.py
===================================================================
--- zope.hookable/trunk/src/zope/hookable/__init__.py	2011-03-14 15:39:44 UTC (rev 120910)
+++ zope.hookable/trunk/src/zope/hookable/__init__.py	2011-03-14 15:41:12 UTC (rev 120911)
@@ -45,18 +45,18 @@
    >>> f()
    41
 
-   >>> del f.original
-   Traceback (most recent call last):
-   ...
-   TypeError: readonly attribute
+   >>> try:
+   ...     del f.original
+   ...     raise "Could delete read only attribute"
+   ... except (TypeError, AttributeError):
+   ...     pass
 
-   >>> del f.implementation
-   Traceback (most recent call last):
-   ...
-   TypeError: readonly attribute
+   >>> try:
+   ...     del f.implementation
+   ...     raise "Could delete read only attribute"
+   ... except (TypeError, AttributeError):
+   ...     pass
 
-   Some error cases.
-
    >>> g = hookable()  # not enough args
    Traceback (most recent call last):
       ...
@@ -76,7 +76,7 @@
 """
 __docformat__ = 'restructuredtext'
 
-from _zope_hookable import *
+from zope.hookable._zope_hookable import *
 
 # DocTest:
 if __name__ == "__main__":

Modified: zope.hookable/trunk/src/zope/hookable/_zope_hookable.c
===================================================================
--- zope.hookable/trunk/src/zope/hookable/_zope_hookable.c	2011-03-14 15:39:44 UTC (rev 120910)
+++ zope.hookable/trunk/src/zope/hookable/_zope_hookable.c	2011-03-14 15:41:12 UTC (rev 120911)
@@ -21,6 +21,22 @@
 #include "Python.h"
 #include "structmember.h"
 
+/* Support for Python < 2.6 */
+
+#ifndef Py_TYPE
+  #define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
+#endif
+
+#ifndef cmpfunc
+  #define cmpfunc int
+#endif
+
+#ifndef PyVarObject_HEAD_INIT
+  #define PyVarObject_HEAD_INIT(type, size) \
+    PyObject_HEAD_INIT(type) size,
+#endif
+
+
 typedef struct {
 	PyObject_HEAD
         PyObject *old;
@@ -78,19 +94,14 @@
   PyObject_GC_UnTrack((PyObject *)self);
   Py_XDECREF(self->old);
   Py_XDECREF(self->implementation);
-  self->ob_type->tp_free((PyObject*)self);
+  Py_TYPE(self)->tp_free((PyObject*)self);
 }
 
 static PyObject *
-hookable_sethook(hookable *self, PyObject *args, PyObject *kwds)
+hookable_sethook(hookable *self, PyObject *implementation)
 {
-  static char *kwlist[] = {"implementation:sethook", NULL};
-  PyObject *implementation, *old;
+  PyObject *old;
 
-  if (! PyArg_ParseTupleAndKeywords(args, kwds, "O", kwlist,
-                                    &implementation))
-    return NULL;
-
   old = self->implementation;
   Py_INCREF(implementation);
   self->implementation = implementation;
@@ -115,7 +126,7 @@
 }
 
 static struct PyMethodDef hookable_methods[] = {
-  {"sethook",	(PyCFunction)hookable_sethook, METH_KEYWORDS,
+  {"sethook",	(PyCFunction)hookable_sethook, METH_O,
    "Set the hook implementation for the hookable object"},
   {"reset",	(PyCFunction)hookable_reset, METH_NOARGS,
    "Reset the hook to the original value"},
@@ -133,8 +144,8 @@
 }
 
 static PyMemberDef hookable_members[] = {
-  { "original", T_OBJECT_EX, offsetof(hookable, old), RO },
-  { "implementation", T_OBJECT_EX, offsetof(hookable, implementation), RO },
+  { "original", T_OBJECT_EX, offsetof(hookable, old), READONLY },
+  { "implementation", T_OBJECT_EX, offsetof(hookable, implementation), READONLY },
   {NULL}	/* Sentinel */
 };
 
@@ -144,8 +155,7 @@
 ;
 
 static PyTypeObject hookabletype = {
-	PyObject_HEAD_INIT(NULL)
-	/* ob_size           */ 0,
+	PyVarObject_HEAD_INIT(NULL, 0)
 	/* tp_name           */ "zope.hookable."
                                 "hookable",
 	/* tp_basicsize      */ sizeof(hookable),
@@ -154,7 +164,7 @@
 	/* tp_print          */ (printfunc)0,
 	/* tp_getattr        */ (getattrfunc)0,
 	/* tp_setattr        */ (setattrfunc)0,
-	/* tp_compare        */ (cmpfunc)0,
+	/* tp_compare        */ 0,
 	/* tp_repr           */ (reprfunc)0,
 	/* tp_as_number      */ 0,
 	/* tp_as_sequence    */ 0,
@@ -189,15 +199,28 @@
 	/* tp_free           */ 0/*_PyObject_GC_Del*/,
 };
 
-static struct PyMethodDef zch_methods[] = {
-	{NULL,	 (PyCFunction)NULL, 0, NULL}		/* sentinel */
+
+#if PY_MAJOR_VERSION >= 3
+  #define MOD_ERROR_VAL NULL
+  #define MOD_SUCCESS_VAL(val) val
+  #define MOD_INIT(name) PyMODINIT_FUNC PyInit_##name(void)
+  #define MOD_DEF(ob, name, doc, methods) \
+	  static struct PyModuleDef moduledef = { \
+	    PyModuleDef_HEAD_INIT, name, doc, -1, methods, }; \
+	  ob = PyModule_Create(&moduledef);
+#else
+  #define MOD_ERROR_VAL
+  #define MOD_SUCCESS_VAL(val)
+  #define MOD_INIT(name) void init##name(void)
+  #define MOD_DEF(ob, name, doc, methods) \
+          ob = Py_InitModule3(name, methods, doc);
+#endif
+
+static struct PyMethodDef module_methods[] = {
+	{NULL, NULL}
 };
 
-#ifndef PyMODINIT_FUNC	/* declarations for DLL import/export */
-#define PyMODINIT_FUNC void
-#endif
-PyMODINIT_FUNC
-init_zope_hookable(void)
+MOD_INIT(_zope_hookable)
 {
   PyObject *m;
 
@@ -206,16 +229,18 @@
   hookabletype.tp_free = _PyObject_GC_Del;
 
   if (PyType_Ready(&hookabletype) < 0)
-    return;
+    return MOD_ERROR_VAL;
 
-  m = Py_InitModule3("_zope_hookable", zch_methods,
-                     "Provide an efficient implementation for hookable objects"
-                     );
+  MOD_DEF(m, "_zope_hookable", 
+    "Provide an efficient implementation for hookable objects",
+    module_methods)
 
   if (m == NULL)
-    return;
+    return MOD_ERROR_VAL;
+  
+  if (PyModule_AddObject(m, "hookable", (PyObject *)&hookabletype) < 0)
+    return MOD_ERROR_VAL;
 
-  if (PyModule_AddObject(m, "hookable", (PyObject *)&hookabletype) < 0)
-    return;
+  return MOD_SUCCESS_VAL(m);
+
 }
-



More information about the checkins mailing list