X-Git-Url: https://git.distorted.org.uk/~mdw/pyke/blobdiff_plain/d53428cdbb18e94ee2941ab44936584c9ac3f4dd..1c0e9c88b06e3422c7afa0cf4bd3fbe166b401da:/pyke.h diff --git a/pyke.h b/pyke.h index 64608d9..cd13507 100644 --- a/pyke.h +++ b/pyke.h @@ -62,6 +62,124 @@ 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 + +/* Plain octet strings. Python 2 calls these `str', while Python 3 calls + * them `bytes'. We call them `bin' here, and define the following. + * + * * `BINOBJ' is the C type of a `bin' object. + * * `BIN_TYPE' is the Python `type' object for `bin'. + * * `BIN_CHECK(OBJ)' is true if OBJ is a `bin' object. + * * `BIN_PTR(OBJ)' points to the first octet of OBJ, without checking! + * * `BIN_LEN(OBJ)' yields the length of OBJ in octets, without checking! + * * `BIN_FROMSTR(STR)' makes a `bin' object from a null-terminated string. + * * `BIN_FORMAT(FMT, ARGS...)' and `BIN_VFORMAT(FMT, AP)' make a `bin' + * object from a `printf'-like format string and arguments. + * * `BIN_PREPAREWRITE(OBJ, PTR, LEN)' prepares to make a `bin' object: it + * sets PTR to point to a buffer of LEN bytes; call `BIN_DONEWRITE' when + * finished. The variable OBJ will eventually be the resulting object, + * but until `BIN_DONEWRITE' is called, it may in fact be some different + * object. + * * `BIN_DONEWRITE(OBJ, LEN)' completes making a `bin' object: it adjusts + * its length to be LEN, which must not be larger than the LEN given to + * `BIN_PREPAREWRITE', and sets OBJ to point to the finished object. + * * `BIN_SETLEN(OBJ, LEN)' adjusts the length of OBJ downwards to LEN, + * without checking! + # * `Y' is a format character for `PyArg_ParseTuple...' for retrieving a + * null-terminated octet string from a `bin' object. + # * `YN' is a format character for `PyArg_ParseTuple...' for retrieving an + * octet string and length from any sort-of vaguely binary-ish object. + */ +#define BINOBJ PyStringObject +#define BIN_TYPE PyString_Type +#define BIN_CHECK(obj) PyString_Check(obj) +#define BIN_PTR(obj) PyString_AS_STRING(obj) +#define BIN_LEN(obj) PyString_GET_SIZE(obj) +#define BIN_FROMSTR(str) PyString_FromString(str) +#define BIN_FROMSTRLEN(str, len) PyString_FromStringAndSize(str, len) +#define BIN_FORMAT PyString_FromFormat +#define BIN_VFORMAT PyString_FromFormatV +#define BIN_PREPAREWRITE(obj, ptr, sz) do { \ + (obj) = PyString_FromStringAndSize(0, (sz)); \ + (ptr) = PyString_AS_STRING(obj); \ +} while (0) +#define BIN_DONEWRITE(obj, sz) do Py_SIZE(obj) = (sz); while (0) +#define BIN_SETLEN(obj, len) do Py_SIZE(obj) = (len); while (0) +#define Y "s" +#define YN "s#" + +/* Text strings. Both Python 2 and Python 3 call these `str', but they're + * very different because a Python 3 `str' is Unicode inside. When dealing + * with Python 3 text, the data is UTF-8 encoded. We call them `text' here, + * and define the following. + * + * * `TEXTOBJ' is the C type of a `text' object. + * * `TEXT_TYPE' is the Python `type' object for `text'. + * * `TEXT_CHECK(OBJ)' is true if OBJ is a `text' object. + * * `TEXT_STR(OBJ)' points to the first byte of a null-terminated string + * OBJ, or is null. + * * `TEXT_PTR(OBJ)' points to the first byte of OBJ, without checking! + * * `TEXT_LEN(OBJ)' yields the length of OBJ in octets, without checking! + * * `TEXT_FROMSTR(STR)' makes a `text' object from a null-terminated + * string. + * * `TEXT_FORMAT(FMT, ARGS...)' and `TEST_VFORMAT(FMT, AP)' make a `text' + * object from a `printf'-like format string and arguments. + * * `TEXT_PREPAREWRITE(OBJ, PTR, LEN)' prepares to make a `text' object: + * it sets PTR to point to a buffer of LEN bytes; call `TEXT_DONEWRITE' + * when finished. The variable OBJ will eventually be the resulting + * object, but until `TEXT_DONEWRITE' is called, it may in fact be some + * different object. + * * `TEXT_DONEWRITE(OBJ, LEN)' completes making a `text' object: it + * adjusts its length to be LEN, which must not be larger than the LEN + * given to `TEXT_PREPAREWRITE', and sets OBJ to point to the finished + * object. + * + * (Use `s' and `s#' in `PyArg_ParseTuple...'.) + */ +#define TEXTOBJ PyStringObject +#define TEXT_TYPE PyString_Type +#define TEXT_CHECK(obj) PyString_Check(obj) +#define TEXT_PTR(obj) PyString_AS_STRING(obj) +#define TEXT_STR(obj) PyString_AsString(obj) +#define TEXT_PTRLEN(obj, ptr, len) do { \ + (ptr) = PyString_AS_STRING(obj); \ + (len) = PyString_GET_SIZE(obj); \ +} while (0) +#define TEXT_FORMAT PyString_FromFormat +#define TEXT_VFORMAT PyString_FromFormatV +#define TEXT_PREPAREWRITE(obj, ptr, sz) do { \ + (obj) = PyString_FromStringAndSize(0, (sz)); \ + (ptr) = PyString_AS_STRING(obj); \ +} while (0) +#define TEXT_DONEWRITE(obj, sz) do { Py_SIZE(obj) = (sz); } while (0) +#define TEXT_FROMSTR(str) PyString_FromString(str) +#define TEXT_FROMSTRLEN(str, len) PyString_FromStringAndSize(str, len) + /*----- Utilities for returning values and exceptions ---------------------*/ /* Returning values. */ @@ -142,10 +260,12 @@ extern void restore_exception(struct excinfo *, const char *, ...); /* Input conversion functions for standard kinds of objects, with overflow * checking where applicable. */ +struct bin { const void *p; Py_ssize_t sz; }; extern int convulong(PyObject *, void *); /* unsigned long */ extern int convuint(PyObject *, void *); /* unsigned int */ extern int convszt(PyObject *, void *); /* size_t */ extern int convbool(PyObject *, void *); /* bool */ +extern int convbin(PyObject *, void *); /* read buffer holding bytes */ /* Output conversions. */ extern PyObject *getbool(int); /* bool */ @@ -153,13 +273,17 @@ extern PyObject *getulong(unsigned long); /* any kind of unsigned integer */ /*----- 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. */ +extern PyObject *enrich_compare(int /*op*/, int /*cmp*/); + /* Use a traditional compare-against-zero comparison result CMP to answer a + * modern Python `tp_richcompare' operation OP. + */ + #ifndef CONVERT_CAREFULLY # define CONVERT_CAREFULLY(newty, expty, obj) \ (!sizeof(*(expty *)0 = (obj)) + (/*unconst*/ newty)(obj)) @@ -298,7 +422,7 @@ extern PyTypeObject *inittype(const PyTypeObject */*skel*/, /*----- Populating modules ------------------------------------------------*/ extern PyObject *modname; - /* The overall module name. Set this with `PyString_FromString'. */ + /* The overall module name. Set this with `TEXT_FROMSTR'. */ extern PyObject *home_module; /* The overall module object. */ @@ -371,6 +495,55 @@ extern PyMethodDef *donemethods(void); /*----- 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_METMNAME(func) gmapmeth_##func #define GMAP_METH(func, doc) STD_METHOD(GMAP_METMNAME, func, 0, doc) @@ -399,7 +572,7 @@ extern PyMethodDef *donemethods(void); KWMETH(setdefault, "D.setdefault(K, [default = None]) -> VALUE") \ KWMETH(pop, "D.pop(KEY, [default = ]) -> VALUE") \ NAMETH(popitem, "D.popitem() -> (KEY, VALUE)") \ - METH (update, "D.update(MAP)") + KWMETH(update, "D.update(MAP)") GMAP_DOMETHODS(GMAP_METHDECL, GMAP_KWMETHDECL, GMAP_NAMETHDECL) #define GMAP_ROMETHODS GMAP_DOROMETHODS(GMAP_METH, GMAP_KWMETH, GMAP_NAMETH) @@ -407,7 +580,12 @@ GMAP_DOMETHODS(GMAP_METHDECL, GMAP_KWMETHDECL, GMAP_NAMETHDECL) /* Mapping protocol implementation. */ extern Py_ssize_t gmap_pysize(PyObject *); /* for `mp_length' */ +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 -------------------------------------------------*/