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. */
/*----- 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 *);
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. */
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_METMNAME(func) gmapmeth_##func
#define GMAP_METH(func, doc) STD_METHOD(GMAP_METMNAME, func, 0, doc)
KWMETH(setdefault, "D.setdefault(K, [default = None]) -> VALUE") \
KWMETH(pop, "D.pop(KEY, [default = <error>]) -> 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)
/* 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 -------------------------------------------------*/