X-Git-Url: https://git.distorted.org.uk/~mdw/pyke/blobdiff_plain/fcfa1c8692de00ec31dfedf5614428df571b7441..747ddb1b97d37fd57efae3c99c2bd188a14df308:/pyke.h diff --git a/pyke.h b/pyke.h index 4e67464..138a9fb 100644 --- a/pyke.h +++ b/pyke.h @@ -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. */ @@ -191,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. */ @@ -203,12 +220,27 @@ 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. */ +/* 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 * @@ -216,11 +248,15 @@ 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, 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 @@ -234,15 +270,30 @@ 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 + * 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 ------------------------------------------------*/ @@ -253,7 +304,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,40 +370,43 @@ 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, 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 = ]) -> 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' */ -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 -------------------------------------------------*/