pyke/pyke.[ch]: Make type skeleton structures be read-only.
[pyke] / pyke.h
diff --git a/pyke.h b/pyke.h
index 66438a4..138a9fb 100644 (file)
--- a/pyke.h
+++ b/pyke.h
@@ -208,11 +208,11 @@ extern void *newtype(PyTypeObject */*meta*/,
 extern void typeready(PyTypeObject *);
   /* The type object is now ready to be used. */
 
-extern PyTypeObject *inittype(PyTypeObject */*skel*/,
+extern PyTypeObject *inittype(const PyTypeObject */*skel*/,
+                             PyTypeObject */*base*/,
                              PyTypeObject */*meta*/);
   /* All-in-one function to construct a working type from a type skeleton
-   * SKEL, with metaclass META.  The caller is expected to have filled in the
-   * direct superclass in SKEL->tp_base.
+   * SKEL, with known base type BASE (null for `object') and metaclass.
    */
 
 /* Alias for built-in types, to fit in with Pyke naming conventions. */
@@ -220,8 +220,7 @@ extern PyTypeObject *inittype(PyTypeObject */*skel*/,
 #define type_pytype &PyType_Type
 
 #define INITTYPE_META(ty, base, meta) do {                             \
-  ty##_pytype_skel.tp_base = base##_pytype;                            \
-  ty##_pytype = inittype(&ty##_pytype_skel, meta##_pytype);            \
+  ty##_pytype = inittype(&ty##_pytype_skel, base##_pytype, meta##_pytype); \
 } while (0)
 #define INITTYPE(ty, base) INITTYPE_META(ty, base, type)
   /* Macros to initialize a type from its skeleton. */
@@ -236,6 +235,11 @@ extern PyTypeObject *inittype(PyTypeObject */*skel*/,
     CONVERT_CAREFULLY(PyCFunction, PyCFunctionWithKeywords, decor(func)), \
     METH_VARARGS | METH_KEYWORDS | flags,                              \
     doc },
+#define NOARG_METHOD(decor, func, flags, doc)                          \
+  { #func,                                                             \
+    CONVERT_CAREFULLY(PyCFunction, PyNoArgsFunction, decor(func)),     \
+    METH_NOARGS | flags,                                               \
+    doc },
 
 /* Convenience wrappers for filling in `PyMethodDef' tables, following
  * Pyke naming convention.  Define `METHNAME' locally as
@@ -246,10 +250,13 @@ extern PyTypeObject *inittype(PyTypeObject */*skel*/,
  */
 #define METH(func, doc) STD_METHOD(METHNAME, func, 0, doc)
 #define KWMETH(func, doc) KEYWORD_METHOD(METHNAME, func, 0, doc)
+#define NAMETH(func, doc) NOARG_METHOD(METHNAME, func, 0, doc)
 #define CMTH(func, doc) STD_METHOD(METHNAME, func, METH_CLASS, doc)
 #define KWCMTH(func, doc) KEYWORD_METHOD(METHNAME, func, METH_CLASS, doc)
+#define NACMTH(func, doc) NOARG_METHOD(METHNAME, func, METH_CLASS, doc)
 #define SMTH(func, doc) STD_METHOD(METHNAME, func, METH_STATIC, doc)
 #define KWSMTH(func, doc) KEYWORD_METHOD(METHNAME, func, METH_STATIC, doc)
+#define NASMTH(func, doc) NOARG_METHOD(METHNAME, func, METH_STATIC, doc)
 
 /* Convenience wrappers for filling in `PyGetSetDef' tables, following Pyke
  * naming convention.  Define `GETSETNAME' locally as
@@ -263,15 +270,16 @@ extern PyTypeObject *inittype(PyTypeObject */*skel*/,
 #define GETSET(func, doc)                                              \
   { #func, GETSETNAME(get, func), GETSETNAME(set, func), doc },
 
-/* Convenience wrapper for filling in `PyMemberDef' tables.  Define
+/* Convenience wrappers for filling in `PyMemberDef' tables.  Define
  * `MEMBERSTRUCT' locally as
  *
  *     #define MEMBERSTRUCT foo_pyobj
  *
  * around the member table.
  */
-#define MEMBER(name, ty, f, doc)                                       \
-  { #name, ty, offsetof(MEMBERSTRUCT, name), f, doc },
+#define MEMRNM(name, ty, mem, f, doc)                                  \
+  { #name, ty, offsetof(MEMBERSTRUCT, mem), f, doc },
+#define MEMBER(name, ty, f, doc) MEMRNM(name, ty, name, f, doc)
 
 /* Wrappers for filling in pointers in a `PyTypeObject' structure, (a)
  * following Pyke naming convention, and (b) stripping `const' from the types
@@ -365,32 +373,35 @@ extern PyMethodDef *donemethods(void);
 #define GMAP_METMNAME(func) gmapmeth_##func
 #define GMAP_METH(func, doc) STD_METHOD(GMAP_METMNAME, func, 0, doc)
 #define GMAP_KWMETH(func, doc) KEYWORD_METHOD(GMAP_METMNAME, func, 0, doc)
+#define GMAP_NAMETH(func, doc) NOARG_METHOD(GMAP_METMNAME, func, 0, doc)
 #define GMAP_METHDECL(func, doc)                                       \
   extern PyObject *gmapmeth_##func(PyObject *, PyObject *);
 #define GMAP_KWMETHDECL(func, doc)                                     \
   extern PyObject *gmapmeth_##func(PyObject *, PyObject *, PyObject *);
+#define GMAP_NAMETHDECL(func, doc)                                     \
+  extern PyObject *gmapmeth_##func(PyObject *);
 
-#define GMAP_DOROMETHODS(METH, KWMETH)                                 \
+#define GMAP_DOROMETHODS(METH, KWMETH, NAMETH)                         \
   METH (has_key,       "D.has_key(KEY) -> BOOL")                       \
-  METH (keys,          "D.keys() -> LIST")                             \
-  METH (values,        "D.values() -> LIST")                           \
-  METH (items,         "D.items() -> LIST")                            \
-  METH (iterkeys,      "D.iterkeys() -> ITER")                         \
-  METH (itervalues,    "D.itervalues() -> ITER")                       \
-  METH (iteritems,     "D.iteritems() -> ITER")                        \
+  NAMETH(keys,         "D.keys() -> LIST")                             \
+  NAMETH(values,       "D.values() -> LIST")                           \
+  NAMETH(items,                "D.items() -> LIST")                            \
+  NAMETH(iterkeys,     "D.iterkeys() -> ITER")                         \
+  NAMETH(itervalues,   "D.itervalues() -> ITER")                       \
+  NAMETH(iteritems,    "D.iteritems() -> ITER")                        \
   KWMETH(get,          "D.get(KEY, [default = None]) -> VALUE")
 
-#define GMAP_DOMETHODS(METH, KWMETH)                                   \
-  GMAP_DOROMETHODS(METH, KWMETH)                                       \
-  METH (clear,         "D.clear()")                                    \
+#define GMAP_DOMETHODS(METH, KWMETH, NAMETH)                           \
+  GMAP_DOROMETHODS(METH, KWMETH, NAMETH)                               \
+  NAMETH(clear,                "D.clear()")                                    \
   KWMETH(setdefault,   "D.setdefault(K, [default = None]) -> VALUE")   \
   KWMETH(pop,          "D.pop(KEY, [default = <error>]) -> VALUE")     \
-  METH (popitem,       "D.popitem() -> (KEY, VALUE)")                  \
+  NAMETH(popitem,      "D.popitem() -> (KEY, VALUE)")                  \
   METH (update,        "D.update(MAP)")
 
-GMAP_DOMETHODS(GMAP_METHDECL, GMAP_KWMETHDECL)
-#define GMAP_ROMETHODS GMAP_DOROMETHODS(GMAP_METH, GMAP_KWMETH)
-#define GMAP_METHODS GMAP_DOMETHODS(GMAP_METH, GMAP_KWMETH)
+GMAP_DOMETHODS(GMAP_METHDECL, GMAP_KWMETHDECL, GMAP_NAMETHDECL)
+#define GMAP_ROMETHODS GMAP_DOROMETHODS(GMAP_METH, GMAP_KWMETH, GMAP_NAMETH)
+#define GMAP_METHODS GMAP_DOMETHODS(GMAP_METH, GMAP_KWMETH, GMAP_NAMETH)
 
 /* Mapping protocol implementation. */
 extern Py_ssize_t gmap_pysize(PyObject *); /* for `mp_length' */