PRIVATE_SYMBOLS;
+/*----- Python version compatibility hacks --------------------------------*/
+
+/* The handy `Py_TYPE' and `Py_SIZE' macros turned up in 2.6. Define them if
+ * they're not already here.
+ */
+#ifndef Py_TYPE
+# define Py_TYPE(obj) (((PyObject *)(obj))->ob_type)
+#endif
+#ifndef Py_SIZE
+# define Py_SIZE(obj) (((PyVarObject *)(obj))->ob_size)
+#endif
+
+/* Python 3 added internal structure to the various object headers, and
+ * defined a new macro `PyVarObject_HEAD_INIT' to initialize variable-length
+ * static instances correctly. Define it if it's not already here.
+ */
+#ifndef PyVarObject_HEAD_INIT
+# define PyVarObject_HEAD_INIT(super, sz) PyObject_HEAD_INIT(super) sz,
+#endif
+
+/* Python 3.2 changed the type of hash values, so paper over this annoying
+ * difference.
+ */
+#if PY_VERSION_HEX < 0x03020000
+ typedef long Py_hash_t;
+#endif
+
/*----- Utilities for returning values and exceptions ---------------------*/
/* Returning values. */
#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)
/*----- Miscellaneous utilities -------------------------------------------*/
-#define FREEOBJ(obj) \
- (((PyObject *)(obj))->ob_type->tp_free((PyObject *)(obj)))
+#define FREEOBJ(obj) (Py_TYPE(obj)->tp_free((PyObject *)(obj)))
/* Actually free OBJ, e.g., in a deallocation function. */
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.
*/
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. */
#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. */
+/* Macros for filling in `PyMethodDef' tables, ensuring that functions have
+ * the expected signatures.
+ */
+#define STD_METHOD(decor, func, flags, doc) \
+ { #func, decor(func), METH_VARARGS | flags, doc },
+#define KEYWORD_METHOD(decor, func, flags, doc) \
+ { #func, \
+ 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
*
*
* 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, 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
#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
+ * 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 ------------------------------------------------*/
/* 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.
struct nameval { const char *name; unsigned f; unsigned long value; };
#define CF_SIGNED 1u
extern void setconstants(PyObject *, const struct nameval *);
+#define CONST(x) { #x, 0, x }
+#define CONSTFLAG(f, x) { #x, f, x }
#define INSEXC(name, var, base, meth) \
INSERT(name, var = mkexc(mod, base, name, meth))
/*----- Generic mapping support -------------------------------------------*/
+/* Operations table. ME is the mapping object throughout. */
+typedef struct gmap_ops {
+ size_t isz; /* iterator size */
+
+ void *(*lookup)(PyObject *me, PyObject *key, unsigned *f);
+ /* Lookup the KEY. If it is found, return an entry pointer for it; if F
+ * is not null, set *F nonzero. Otherwise, if F is null, return a null
+ * pointer (without setting a pending exception); if F is not null, then
+ * set *F zero and return a fresh entry pointer. Return null on a Python
+ * exception (the caller will notice the difference.)
+ */
+
+ void (*iter_init)(PyObject *me, void *i);
+ /* Initialize an iterator at I. */
+
+ void *(*iter_next)(PyObject *me, void *i);
+ /* Return an entry pointer for a different item, or null if all have been
+ * visited.
+ */
+
+ PyObject *(*entry_key)(PyObject *me, void *e);
+ /* Return the key object for a mapping entry. */
+
+ PyObject *(*entry_value)(PyObject *me, void *e);
+ /* Return the value object for a mapping entry. */
+
+ int (*set_entry)(PyObject *me, void *e, PyObject *val);
+ /* Modify the entry by storing VAL in its place. Return 0 on success,
+ * or -1 on a Python error.
+ */
+
+ int (*del_entry)(PyObject *me, void *e);
+ /* Delete the entry. (It may be necessary to delete a freshly allocated
+ * entry, e.g., if `set_entry' failed.) Return 0 on success, or -1 on a
+ * Python error.
+ */
+} gmap_ops;
+
+/* The intrusion at the head of a mapping object. */
+#define GMAP_PYOBJ_HEAD \
+ PyObject_HEAD \
+ const gmap_ops *gmops;
+
+typedef struct gmap_pyobj {
+ GMAP_PYOBJ_HEAD
+} gmap_pyobj;
+#define GMAP_OPS(obj) (((gmap_pyobj *)(obj))->gmops)
+ /* Discover the operations from a mapping object. */
+
/* 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, 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)") \
- METH (update, "D.update(MAP)")
+ NAMETH(popitem, "D.popitem() -> (KEY, VALUE)") \
+ KWMETH(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' */
-extern PySequenceMethods gmap_pysequence; /* for `tp_as_sequence' */
-extern PyMethodDef gmap_pymethods[]; /* all the standard methods */
+extern PyObject *gmap_pyiter(PyObject *); /* for `tp_iter' */
+extern PyObject *gmap_pylookup(PyObject *, PyObject *); /* for `mp_subscript' */
+extern int gmap_pystore(PyObject *, PyObject *, PyObject *); /* for `mp_ass_subscript' */
+extern int gmap_pyhaskey(PyObject *, PyObject *); /* for `sq_contains' */
+extern const PySequenceMethods gmap_pysequence; /* for `tp_as_sequence' */
+extern const PyMethodDef gmapro_pymethods[]; /* read-only methods */
+extern const PyMethodDef gmap_pymethods[]; /* all the standard methods */
/*----- That's all, folks -------------------------------------------------*/