Reorganization: split out utilities; mapping methods
[pyke] / catacomb-python.h
index d71caa9..286dd9e 100644 (file)
 #define ZDIVERR(str) EXCERR(PyExc_ZeroDivisionError, str)
 #define SYNERR(str) EXCERR(PyExc_SyntaxError, str)
 #define SYSERR(str) EXCERR(PyExc_SystemError, str)
+#define NIERR(str) EXCERR(PyExc_NotImplementedError, str)
 #define INDEXERR(idx) do {                                             \
   PyErr_SetObject(PyExc_KeyError, idx);                                        \
   goto end;                                                            \
@@ -200,6 +201,82 @@ MODULES(DO)
 #define FREEOBJ(obj)                                                   \
   (((PyObject *)(obj))->ob_type->tp_free((PyObject *)(obj)))
 
+#define GEN(func, base)                                                        \
+  static PyObject *func(void)                                          \
+  {                                                                    \
+    PyObject *d = PyDict_New();                                                \
+    PyObject *o;                                                       \
+    int i;                                                             \
+                                                                       \
+    for (i = 0; g##base##tab[i]; i++) {                                        \
+      o = gc##base##_pywrap((/*unconst*/ gc##base *)g##base##tab[i]);  \
+      PyDict_SetItemString(d,                                          \
+                          (/*unconst*/ char *)g##base##tab[i]->name,   \
+                          o);                                          \
+      Py_DECREF(o);                                                    \
+    }                                                                  \
+    return (d);                                                                \
+  }
+
+struct nameval { const char *name; unsigned long value; };
+extern void setconstants(PyObject *, const struct nameval *);
+
+extern PyObject *mexp_common(PyObject *, PyObject *, size_t,
+                            PyObject *(*id)(PyObject *),
+                            int (*fill)(void *, PyObject *,
+                                        PyObject *, PyObject *),
+                            PyObject *(*exp)(PyObject *, void *, int),
+                            void (*drop)(void *));
+
+extern int convulong(PyObject *, void *);
+#define DECL_CONVU_(n) extern int convu##n(PyObject *, void *);
+DOUINTSZ(DECL_CONVU_)
+extern int convmpw(PyObject *, void *);
+extern int convuint(PyObject *, void *);
+extern int convszt(PyObject *, void *);
+extern int convbool(PyObject *, void *);
+extern PyObject *abstract_pynew(PyTypeObject *, PyObject *, PyObject *);
+extern PyObject *getbool(int);
+#define DECL_GETU_(n) extern PyObject *getu##n(uint##n);
+DOUINTSZ(DECL_GETU_)
+extern void *newtype(PyTypeObject *, const PyTypeObject *, const char *);
+
+extern PyObject * mkexc(PyObject *, PyObject *, const char *, PyMethodDef *);
+extern PyTypeObject *inittype(PyTypeObject *);
+extern void addmethods(const PyMethodDef *);
+extern PyMethodDef *donemethods(void);
+
+/*----- 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_METHDECL(func, doc)                                       \
+  extern PyObject *gmapmeth_##func(PyObject *, PyObject *);
+#define GMAP_KWMETHDECL(func, doc)                                     \
+  extern PyObject *gmapmeth_##func(PyObject *, PyObject *, PyObject *);
+
+#define GMAP_DOMETHODS(METH, KWMETH)                                   \
+  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")                        \
+  METH (clear,         "D.clear()")                                    \
+  KWMETH(get,          "D.get(KEY, [default = None]) -> VALUE")        \
+  KWMETH(setdefault,   "D.setdefault(K, [default = None]) -> VALUE")   \
+  KWMETH(pop,          "D.pop(KEY, [default = <error>]) -> VALUE")     \
+  METH (popitem,       "D.popitem() -> (KEY, VALUE)")                  \
+  METH (update,        "D.update(MAP)")
+
+GMAP_DOMETHODS(GMAP_METHDECL, GMAP_KWMETHDECL)
+#define GMAP_METHODS GMAP_DOMETHODS(GMAP_METH, GMAP_KWMETH)
+extern int gmap_pysize(PyObject *);
+extern PySequenceMethods gmap_pysequence;
+extern PyMethodDef gmap_pymethods[];
+
 /*----- Bytestrings -------------------------------------------------------*/
 
 PyTypeObject *bytestring_pyobj;
@@ -502,31 +579,6 @@ extern int convpgev(PyObject *, void *);
 extern void droppgev(pgev *);
 extern void pgenerr(void);
 
-/*----- Core utility functions --------------------------------------------*/
-
-extern PyObject *mexp_common(PyObject *, PyObject *, size_t,
-                            PyObject *(*id)(PyObject *),
-                            int (*fill)(void *, PyObject *,
-                                        PyObject *, PyObject *),
-                            PyObject *(*exp)(PyObject *, void *, int),
-                            void (*drop)(void *));
-
-extern int convulong(PyObject *, void *);
-#define DECL_CONVU_(n) extern int convu##n(PyObject *, void *);
-DOUINTSZ(DECL_CONVU_)
-extern int convmpw(PyObject *, void *);
-extern int convuint(PyObject *, void *);
-extern int convszt(PyObject *, void *);
-extern int convbool(PyObject *, void *);
-extern PyObject *abstract_pynew(PyTypeObject *, PyObject *, PyObject *);
-extern PyObject *getbool(int);
-#define DECL_GETU_(n) extern PyObject *getu##n(uint##n);
-DOUINTSZ(DECL_GETU_)
-extern PyObject * mkexc(PyObject *, PyObject *, const char *, PyMethodDef *);
-extern void *newtype(PyTypeObject *, const PyTypeObject *, const char *);
-extern PyTypeObject *inittype(PyTypeObject *);
-extern void addmethods(const PyMethodDef *);
-
 /*----- That's all, folks -------------------------------------------------*/
 
 #ifdef __cplusplus