*.c: Make all of the type-definition tables read-only.
[pyke] / pyke.h
diff --git a/pyke.h b/pyke.h
index 1f6232c..e200c09 100644 (file)
--- a/pyke.h
+++ b/pyke.h
@@ -86,7 +86,7 @@ PRIVATE_SYMBOLS;
 #define ZDIVERR(str) EXCERR(PyExc_ZeroDivisionError, str)
 #define SYSERR(str) EXCERR(PyExc_SystemError, str)
 #define NIERR(str) EXCERR(PyExc_NotImplementedError, str)
-#define INDEXERR(idx) do {                                             \
+#define MAPERR(idx) do {                                               \
   PyErr_SetObject(PyExc_KeyError, idx);                                        \
   goto end;                                                            \
 } while (0)
@@ -160,7 +160,24 @@ extern PyObject *getulong(unsigned long); /* any kind of unsigned integer */
 extern PyObject *abstract_pynew(PyTypeObject *, PyObject *, PyObject *);
   /* A `tp_new' function which refuses to make the object. */
 
-#define KWLIST (/*unconst*/ char **)kwlist
+#ifndef CONVERT_CAREFULLY
+#  define CONVERT_CAREFULLY(newty, expty, obj)                         \
+     (!sizeof(*(expty *)0 = (obj)) + (/*unconst*/ newty)(obj))
+  /* Convert OBJ to the type NEWTY, having previously checked that it is
+   * convertible to the expected type EXPTY.
+   *
+   * Because of the way we set up types, we can make many kinds of tables be
+   * `const' which can't usually be so (because Python will want to fiddle
+   * with their reference counts); and, besides, Python's internals are
+   * generally quite bad at being `const'-correct about tables.  One frequent
+   * application of this macro, then, is in removing `const' from a type
+   * without sacrificing all type safety.  The other common use is in
+   * checking that method function types match up with the signatures
+   * expected in their method definitions.
+   */
+#endif
+
+#define KWLIST CONVERT_CAREFULLY(char **, const char *const *, kwlist)
   /* Strip `const' qualifiers from the keyword list `kwlist'.  Useful when
    * calling `PyArg_ParseTupleAndKeywords', which isn't `const'-correct.
    */
@@ -209,6 +226,17 @@ extern PyTypeObject *inittype(PyTypeObject */*skel*/,
 #define INITTYPE(ty, base) INITTYPE_META(ty, base, type)
   /* Macros to initialize a type from its skeleton. */
 
+/* Macros for filling in `PyMethodDef' tables, ensuring that functions have
+ * the expected signatures.
+ */
+#define STD_METHOD(decor, func, doc)                                   \
+  { #func, decor(func), METH_VARARGS, doc },
+#define KEYWORD_METHOD(decor, func, doc)                               \
+  { #func,                                                             \
+    CONVERT_CAREFULLY(PyCFunction, PyCFunctionWithKeywords, decor(func)), \
+    METH_VARARGS | METH_KEYWORDS,                                      \
+    doc },
+
 /* Convenience wrappers for filling in `PyMethodDef' tables, following
  * Pyke naming convention.  Define `METHNAME' locally as
  *
@@ -216,11 +244,8 @@ extern PyTypeObject *inittype(PyTypeObject */*skel*/,
  *
  * around the method table.
  */
-#define METH(func, doc)                                                        \
-  { #func, METHNAME(func), METH_VARARGS, doc },
-#define KWMETH(func, doc)                                              \
-  { #func, (PyCFunction)METHNAME(func),                                        \
-    METH_VARARGS | METH_KEYWORDS, doc },
+#define METH(func, doc) STD_METHOD(METHNAME, func, doc)
+#define KWMETH(func, doc) KEYWORD_METHOD(METHNAME, func, doc)
 
 /* Convenience wrappers for filling in `PyGetSetDef' tables, following Pyke
  * naming convention.  Define `GETSETNAME' locally as
@@ -244,6 +269,20 @@ extern PyTypeObject *inittype(PyTypeObject */*skel*/,
 #define MEMBER(name, ty, f, doc)                                       \
   { #name, ty, offsetof(MEMBERSTRUCT, name), f, doc },
 
+/* Wrappers for filling in pointers in a `PyTypeObject' structure, (a)
+ * following Pyke naming convention, and (b) stripping `const' from the types
+ * without losing type safety.
+ */
+#define UNCONST_TYPE_SLOT(type, suffix, op, ty)                                \
+  CONVERT_CAREFULLY(type *, const type *, op ty##_py##suffix)
+#define PYGETSET(ty) UNCONST_TYPE_SLOT(PyGetSetDef, getset, NOTHING, ty)
+#define PYMETHODS(ty) UNCONST_TYPE_SLOT(PyMethodDef, methods, NOTHING, ty)
+#define PYMEMBERS(ty) UNCONST_TYPE_SLOT(PyMemberDef, members, NOTHING, ty)
+#define PYNUMBER(ty) UNCONST_TYPE_SLOT(PyNumberMethods, number, &, ty)
+#define PYSEQUENCE(ty) UNCONST_TYPE_SLOT(PySequenceMethods, sequence, &, ty)
+#define PYMAPPING(ty) UNCONST_TYPE_SLOT(PyMappingMethods, mapping, &, ty)
+#define PYBUFFER(ty) UNCONST_TYPE_SLOT(PyBufferProcs, buffer, &, ty)
+
 /*----- Populating modules ------------------------------------------------*/
 
 extern PyObject *modname;
@@ -253,7 +292,7 @@ extern PyObject *home_module;
   /* The overall module object. */
 
 extern PyObject *mkexc(PyObject */*mod*/, PyObject */*base*/,
-                      const char */*name*/, PyMethodDef */*methods*/);
+                      const char */*name*/, const PyMethodDef */*methods*/);
   /* Make and return an exception class called NAME, which will end up in
    * module MOD (though it is not added at this time).  The new class is a
    * subclass of BASE.  Attach the METHODS to it.
@@ -319,9 +358,9 @@ extern PyMethodDef *donemethods(void);
 /*----- Generic mapping support -------------------------------------------*/
 
 /* Mapping methods. */
-#define GMAP_METH(func, doc) { #func, gmapmeth_##func, METH_VARARGS, doc },
-#define GMAP_KWMETH(func, doc)                                         \
-  { #func, (PyCFunction)gmapmeth_##func, METH_VARARGS|METH_KEYWORDS, doc },
+#define GMAP_METMNAME(func) gmapmeth_##func
+#define GMAP_METH(func, doc) STD_METHOD(GMAP_METMNAME, func, doc)
+#define GMAP_KWMETH(func, doc) KEYWORD_METHOD(GMAP_METMNAME, func, doc)
 #define GMAP_METHDECL(func, doc)                                       \
   extern PyObject *gmapmeth_##func(PyObject *, PyObject *);
 #define GMAP_KWMETHDECL(func, doc)                                     \
@@ -351,8 +390,8 @@ GMAP_DOMETHODS(GMAP_METHDECL, GMAP_KWMETHDECL)
 
 /* Mapping protocol implementation. */
 extern Py_ssize_t gmap_pysize(PyObject *); /* for `mp_length' */
-extern PySequenceMethods gmap_pysequence; /* for `tp_as_sequence' */
-extern PyMethodDef gmap_pymethods[]; /* all the standard methods */
+extern const PySequenceMethods gmap_pysequence; /* for `tp_as_sequence' */
+extern const PyMethodDef gmap_pymethods[]; /* all the standard methods */
 
 /*----- That's all, folks -------------------------------------------------*/