pyke/mapping.c, key.c: Make the mapping code more intrusive and complete.
[pyke] / pyke.h
CommitLineData
c1756f78
MW
1/* -*-c-*-
2 *
3 * Pyke: the Python Kit for Extensions
4 *
5 * (c) 2019 Straylight/Edgeware
6 */
7
8/*----- Licensing notice --------------------------------------------------*
9 *
10 * This file is part of Pyke: the Python Kit for Extensions.
11 *
12 * Pyke is free software: you can redistribute it and/or modify it under
13 * the terms of the GNU General Public License as published by the Free
14 * Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
16 *
17 * Pyke is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with Pyke. If not, write to the Free Software Foundation, Inc.,
24 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 */
26
27#ifndef PYKE_H
28#define PYKE_H
29
30#ifdef __cplusplus
31 extern "C" {
32#endif
33
34/*----- Header files ------------------------------------------------------*/
35
36#define PY_SSIZE_T_CLEAN
37
38#include <Python.h>
39#include <structmember.h>
40
41/*----- Other preliminaries -----------------------------------------------*/
42
43#define NOTHING
44#define COMMA ,
45
46/*----- Symbol visibility -------------------------------------------------*
47 *
48 * This library is very messy regarding symbol namespace. Keep this mess
49 * within our shared-object.
50 */
51
52#define GOBBLE_SEMI extern int notexist
53#if defined(__GNUC__) && defined(__ELF__)
54# define PRIVATE_SYMBOLS _Pragma("GCC visibility push(hidden)") GOBBLE_SEMI
55# define PUBLIC_SYMBOLS _Pragma("GCC visibility pop") GOBBLE_SEMI
56# define EXPORT __attribute__((__visibility__("default")))
57#else
58# define PRIVATE_SYMBOLS GOBBLE_SEMI
59# define PUBLIC_SYMBOLS GOBBLE_SEMI
60# define EXPORT
61#endif
62
63PRIVATE_SYMBOLS;
64
65/*----- Utilities for returning values and exceptions ---------------------*/
66
67/* Returning values. */
68#define RETURN_OBJ(obj) do { Py_INCREF(obj); return (obj); } while (0)
69#define RETURN_NONE RETURN_OBJ(Py_None)
70#define RETURN_NOTIMPL RETURN_OBJ(Py_NotImplemented)
71#define RETURN_TRUE RETURN_OBJ(Py_True)
72#define RETURN_FALSE RETURN_OBJ(Py_False)
73#define RETURN_ME RETURN_OBJ(me)
74
75/* Returning exceptions. (Note that `KeyError' is `MAPERR' here, because
76 * Catacomb has its own kind of `KeyError'.)
77 */
78#define EXCERR(exc, str) do { \
79 PyErr_SetString(exc, str); \
80 goto end; \
81} while (0)
82#define VALERR(str) EXCERR(PyExc_ValueError, str)
83#define OVFERR(str) EXCERR(PyExc_OverflowError, str)
84#define TYERR(str) EXCERR(PyExc_TypeError, str)
85#define IXERR(str) EXCERR(PyExc_IndexError, str)
86#define ZDIVERR(str) EXCERR(PyExc_ZeroDivisionError, str)
87#define SYSERR(str) EXCERR(PyExc_SystemError, str)
88#define NIERR(str) EXCERR(PyExc_NotImplementedError, str)
fcfa1c86 89#define MAPERR(idx) do { \
c1756f78
MW
90 PyErr_SetObject(PyExc_KeyError, idx); \
91 goto end; \
92} while (0)
93#define OSERR(name) do { \
94 PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); \
95 goto end; \
96} while (0)
97
98/* Saving and restoring exceptions. */
99struct excinfo { PyObject *ty, *val, *tb; };
100#define EXCINFO_INIT { 0, 0, 0 }
101#define INIT_EXCINFO(exc) do { \
102 struct excinfo *_exc = (exc); _exc->ty = _exc->val = _exc->tb = 0; \
103} while (0)
104#define RELEASE_EXCINFO(exc) do { \
105 struct excinfo *_exc = (exc); \
106 Py_XDECREF(_exc->ty); _exc->ty = 0; \
107 Py_XDECREF(_exc->val); _exc->val = 0; \
108 Py_XDECREF(_exc->tb); _exc->tb = 0; \
109} while (0)
110#define STASH_EXCINFO(exc) do { \
111 struct excinfo *_exc = (exc); \
112 PyErr_Fetch(&_exc->ty, &_exc->val, &_exc->tb); \
113 PyErr_NormalizeException(&_exc->ty, &_exc->val, &_exc->tb); \
114} while (0)
115#define RESTORE_EXCINFO(exc) do { \
116 struct excinfo *_exc = (exc); \
117 PyErr_Restore(_exc->ty, _exc->val, _exc->tb); \
118 _exc->ty = _exc->val = _exc->tb = 0; \
119} while (0)
120extern void report_lost_exception(struct excinfo *, const char *, ...);
121extern void report_lost_exception_v(struct excinfo *, const char *, va_list);
122extern void stash_exception(struct excinfo *, const char *, ...);
123extern void restore_exception(struct excinfo *, const char *, ...);
124
125/*----- Conversions -------------------------------------------------------*/
126
127/* Define an input conversion (`O&') function: check that the object has
128 * Python type TY, and extract a C pointer to CTY by calling EXT on the
129 * object (which may well be a macro).
130 */
131#define CONVFUNC(ty, cty, ext) \
132 int conv##ty(PyObject *o, void *p) \
133 { \
134 if (!PyObject_TypeCheck(o, ty##_pytype)) \
135 TYERR("wanted a " #ty); \
136 *(cty *)p = ext(o); \
137 return (1); \
138 end: \
139 return (0); \
140 }
141
142/* Input conversion functions for standard kinds of objects, with overflow
143 * checking where applicable.
144 */
145extern int convulong(PyObject *, void *); /* unsigned long */
146extern int convuint(PyObject *, void *); /* unsigned int */
147extern int convszt(PyObject *, void *); /* size_t */
148extern int convbool(PyObject *, void *); /* bool */
149
150/* Output conversions. */
151extern PyObject *getbool(int); /* bool */
152extern PyObject *getulong(unsigned long); /* any kind of unsigned integer */
153
154/*----- Miscellaneous utilities -------------------------------------------*/
155
156#define FREEOBJ(obj) \
157 (((PyObject *)(obj))->ob_type->tp_free((PyObject *)(obj)))
158 /* Actually free OBJ, e.g., in a deallocation function. */
159
160extern PyObject *abstract_pynew(PyTypeObject *, PyObject *, PyObject *);
161 /* A `tp_new' function which refuses to make the object. */
162
dce47d50
MW
163#ifndef CONVERT_CAREFULLY
164# define CONVERT_CAREFULLY(newty, expty, obj) \
165 (!sizeof(*(expty *)0 = (obj)) + (/*unconst*/ newty)(obj))
166 /* Convert OBJ to the type NEWTY, having previously checked that it is
167 * convertible to the expected type EXPTY.
168 *
169 * Because of the way we set up types, we can make many kinds of tables be
170 * `const' which can't usually be so (because Python will want to fiddle
171 * with their reference counts); and, besides, Python's internals are
172 * generally quite bad at being `const'-correct about tables. One frequent
173 * application of this macro, then, is in removing `const' from a type
174 * without sacrificing all type safety. The other common use is in
175 * checking that method function types match up with the signatures
176 * expected in their method definitions.
177 */
178#endif
179
180#define KWLIST CONVERT_CAREFULLY(char **, const char *const *, kwlist)
c1756f78
MW
181 /* Strip `const' qualifiers from the keyword list `kwlist'. Useful when
182 * calling `PyArg_ParseTupleAndKeywords', which isn't `const'-correct.
183 */
184
185/*----- Type definitions --------------------------------------------------*
186 *
187 * Pyke types are defined in a rather unusual way.
188 *
189 * The main code defines a `type skeleton' of type `PyTypeObject',
190 * conventionally named `TY_pytype_skel'. Unlike typical Python type
191 * definitions in extensions, this can (and should) be read-only. Also,
192 * there's no point in setting the `tp_base' pointer here, because the actual
193 * runtime base type object won't, in general, be known at compile time.
194 * Instead, the type skeletons are converted into Python `heap types' by the
195 * `INITTYPE' macro. The main difference is that Python code can add
196 * attributes to heap types, and we make extensive use of this ability.
197 */
198
199extern void *newtype(PyTypeObject */*meta*/,
200 const PyTypeObject */*skel*/, const char */*name*/);
201 /* Make and return a new Python type object, of type META (typically
202 * `PyType_Type', but may be a subclass), filled in from the skeleton SKEL
203 * (null to inherit everything), and named NAME. The caller can mess with
204 * the type object further at this time: call `typeready' when it's set up
205 * properly.
206 */
207
208extern void typeready(PyTypeObject *);
209 /* The type object is now ready to be used. */
210
747ddb1b
MW
211extern PyTypeObject *inittype(const PyTypeObject */*skel*/,
212 PyTypeObject */*base*/,
c1756f78
MW
213 PyTypeObject */*meta*/);
214 /* All-in-one function to construct a working type from a type skeleton
747ddb1b 215 * SKEL, with known base type BASE (null for `object') and metaclass.
c1756f78
MW
216 */
217
218/* Alias for built-in types, to fit in with Pyke naming conventions. */
219#define root_pytype 0
220#define type_pytype &PyType_Type
221
222#define INITTYPE_META(ty, base, meta) do { \
747ddb1b 223 ty##_pytype = inittype(&ty##_pytype_skel, base##_pytype, meta##_pytype); \
c1756f78
MW
224} while (0)
225#define INITTYPE(ty, base) INITTYPE_META(ty, base, type)
226 /* Macros to initialize a type from its skeleton. */
227
dce47d50
MW
228/* Macros for filling in `PyMethodDef' tables, ensuring that functions have
229 * the expected signatures.
230 */
7bc403cd
MW
231#define STD_METHOD(decor, func, flags, doc) \
232 { #func, decor(func), METH_VARARGS | flags, doc },
233#define KEYWORD_METHOD(decor, func, flags, doc) \
dce47d50
MW
234 { #func, \
235 CONVERT_CAREFULLY(PyCFunction, PyCFunctionWithKeywords, decor(func)), \
7bc403cd 236 METH_VARARGS | METH_KEYWORDS | flags, \
dce47d50 237 doc },
138563a5
MW
238#define NOARG_METHOD(decor, func, flags, doc) \
239 { #func, \
240 CONVERT_CAREFULLY(PyCFunction, PyNoArgsFunction, decor(func)), \
241 METH_NOARGS | flags, \
242 doc },
dce47d50 243
c1756f78
MW
244/* Convenience wrappers for filling in `PyMethodDef' tables, following
245 * Pyke naming convention. Define `METHNAME' locally as
246 *
247 * #define METHNAME(name) foometh_##func
248 *
249 * around the method table.
250 */
7bc403cd
MW
251#define METH(func, doc) STD_METHOD(METHNAME, func, 0, doc)
252#define KWMETH(func, doc) KEYWORD_METHOD(METHNAME, func, 0, doc)
138563a5 253#define NAMETH(func, doc) NOARG_METHOD(METHNAME, func, 0, doc)
7bc403cd
MW
254#define CMTH(func, doc) STD_METHOD(METHNAME, func, METH_CLASS, doc)
255#define KWCMTH(func, doc) KEYWORD_METHOD(METHNAME, func, METH_CLASS, doc)
138563a5 256#define NACMTH(func, doc) NOARG_METHOD(METHNAME, func, METH_CLASS, doc)
7bc403cd
MW
257#define SMTH(func, doc) STD_METHOD(METHNAME, func, METH_STATIC, doc)
258#define KWSMTH(func, doc) KEYWORD_METHOD(METHNAME, func, METH_STATIC, doc)
138563a5 259#define NASMTH(func, doc) NOARG_METHOD(METHNAME, func, METH_STATIC, doc)
c1756f78
MW
260
261/* Convenience wrappers for filling in `PyGetSetDef' tables, following Pyke
262 * naming convention. Define `GETSETNAME' locally as
263 *
264 * #define GETSETNAME(op, name) foo##op##_##func
265 *
266 * around the get/set table.
267 */
268#define GET(func, doc) \
269 { #func, GETSETNAME(get, func), 0, doc },
270#define GETSET(func, doc) \
271 { #func, GETSETNAME(get, func), GETSETNAME(set, func), doc },
272
ee4a6b1c 273/* Convenience wrappers for filling in `PyMemberDef' tables. Define
c1756f78
MW
274 * `MEMBERSTRUCT' locally as
275 *
276 * #define MEMBERSTRUCT foo_pyobj
277 *
278 * around the member table.
279 */
ee4a6b1c
MW
280#define MEMRNM(name, ty, mem, f, doc) \
281 { #name, ty, offsetof(MEMBERSTRUCT, mem), f, doc },
282#define MEMBER(name, ty, f, doc) MEMRNM(name, ty, name, f, doc)
c1756f78 283
87fa2d56
MW
284/* Wrappers for filling in pointers in a `PyTypeObject' structure, (a)
285 * following Pyke naming convention, and (b) stripping `const' from the types
286 * without losing type safety.
287 */
288#define UNCONST_TYPE_SLOT(type, suffix, op, ty) \
289 CONVERT_CAREFULLY(type *, const type *, op ty##_py##suffix)
290#define PYGETSET(ty) UNCONST_TYPE_SLOT(PyGetSetDef, getset, NOTHING, ty)
291#define PYMETHODS(ty) UNCONST_TYPE_SLOT(PyMethodDef, methods, NOTHING, ty)
292#define PYMEMBERS(ty) UNCONST_TYPE_SLOT(PyMemberDef, members, NOTHING, ty)
293#define PYNUMBER(ty) UNCONST_TYPE_SLOT(PyNumberMethods, number, &, ty)
294#define PYSEQUENCE(ty) UNCONST_TYPE_SLOT(PySequenceMethods, sequence, &, ty)
295#define PYMAPPING(ty) UNCONST_TYPE_SLOT(PyMappingMethods, mapping, &, ty)
296#define PYBUFFER(ty) UNCONST_TYPE_SLOT(PyBufferProcs, buffer, &, ty)
297
c1756f78
MW
298/*----- Populating modules ------------------------------------------------*/
299
300extern PyObject *modname;
301 /* The overall module name. Set this with `PyString_FromString'. */
302
303extern PyObject *home_module;
304 /* The overall module object. */
305
306extern PyObject *mkexc(PyObject */*mod*/, PyObject */*base*/,
87fa2d56 307 const char */*name*/, const PyMethodDef */*methods*/);
c1756f78
MW
308 /* Make and return an exception class called NAME, which will end up in
309 * module MOD (though it is not added at this time). The new class is a
310 * subclass of BASE. Attach the METHODS to it.
311 */
312
313#define INSERT(name, ob) do { \
314 PyObject *_o = (PyObject *)(ob); \
315 Py_INCREF(_o); \
316 PyModule_AddObject(mod, name, _o); \
317} while (0)
318 /* Insert a Python object OB into the module `mod' under the given NAME. */
319
320/* Numeric constants. */
321struct nameval { const char *name; unsigned f; unsigned long value; };
322#define CF_SIGNED 1u
323extern void setconstants(PyObject *, const struct nameval *);
d53428cd
MW
324#define CONST(x) { #x, (x) >= 0 ? 0 : CF_SIGNED, x }
325#define CONSTFLAG(f, x) { #x, f, x }
c1756f78
MW
326
327#define INSEXC(name, var, base, meth) \
328 INSERT(name, var = mkexc(mod, base, name, meth))
329 /* Insert an exception class into the module `mod'; other arguments are as
330 * for `mkexc'.
331 */
332
333/*----- Submodules --------------------------------------------------------*
334 *
335 * It's useful to split the Python module up into multiple source files, and
336 * have each one contribute its definitions into the main module.
337 *
338 * Define a list-macro `MODULES' in the master header file naming the
339 * submodules to be processed, and run
340 *
341 * MODULES(DECLARE_MODINIT)
342 *
343 * to declare the interface functions.
344 *
345 * Each submodule FOO defines two functions: `FOO_pyinit' initializes types
346 * (see `INITTYPE' above) and accumulates methods (`addmethods' below), while
347 * `FOO_pyinsert' populates the module with additional definitions
348 * (especially types, though also constants).
349 *
350 * The top-level module initialization should call `INIT_MODULES' before
351 * creating the Python module, and `INSERT_MODULES' afterwards to make
352 * everything work.
353 */
354
355extern void addmethods(const PyMethodDef *);
356extern PyMethodDef *donemethods(void);
357 /* Accumulate method-table fragments, and return the combined table of all
358 * of the fragments.
359 */
360
361#define DECLARE_MODINIT(m) \
362 extern void m##_pyinit(void); \
363 extern void m##_pyinsert(PyObject *);
364 /* Declare submodule interface functions. */
365
366#define DOMODINIT(m) m##_pyinit();
367#define DOMODINSERT(m) m##_pyinsert(mod);
368#define INIT_MODULES do { MODULES(DOMODINIT) } while (0)
369#define INSERT_MODULES do { MODULES(DOMODINSERT) } while (0)
370 /* Top-level dispatch to the various submodules. */
371
372/*----- Generic mapping support -------------------------------------------*/
373
78daa0e0
MW
374/* Operations table. ME is the mapping object throughout. */
375typedef struct gmap_ops {
376 size_t isz; /* iterator size */
377
378 void *(*lookup)(PyObject *me, PyObject *key, unsigned *f);
379 /* Lookup the KEY. If it is found, return an entry pointer for it; if F
380 * is not null, set *F nonzero. Otherwise, if F is null, return a null
381 * pointer (without setting a pending exception); if F is not null, then
382 * set *F zero and return a fresh entry pointer. Return null on a Python
383 * exception (the caller will notice the difference.)
384 */
385
386 void (*iter_init)(PyObject *me, void *i);
387 /* Initialize an iterator at I. */
388
389 void *(*iter_next)(PyObject *me, void *i);
390 /* Return an entry pointer for a different item, or null if all have been
391 * visited.
392 */
393
394 PyObject *(*entry_key)(PyObject *me, void *e);
395 /* Return the key object for a mapping entry. */
396
397 PyObject *(*entry_value)(PyObject *me, void *e);
398 /* Return the value object for a mapping entry. */
399
400 int (*set_entry)(PyObject *me, void *e, PyObject *val);
401 /* Modify the entry by storing VAL in its place. Return 0 on success,
402 * or -1 on a Python error.
403 */
404
405 int (*del_entry)(PyObject *me, void *e);
406 /* Delete the entry. (It may be necessary to delete a freshly allocated
407 * entry, e.g., if `set_entry' failed.) Return 0 on success, or -1 on a
408 * Python error.
409 */
410} gmap_ops;
411
412/* The intrusion at the head of a mapping object. */
413#define GMAP_PYOBJ_HEAD \
414 PyObject_HEAD \
415 const gmap_ops *gmops;
416
417typedef struct gmap_pyobj {
418 GMAP_PYOBJ_HEAD
419} gmap_pyobj;
420#define GMAP_OPS(obj) (((gmap_pyobj *)(obj))->gmops)
421 /* Discover the operations from a mapping object. */
422
c1756f78 423/* Mapping methods. */
dce47d50 424#define GMAP_METMNAME(func) gmapmeth_##func
7bc403cd
MW
425#define GMAP_METH(func, doc) STD_METHOD(GMAP_METMNAME, func, 0, doc)
426#define GMAP_KWMETH(func, doc) KEYWORD_METHOD(GMAP_METMNAME, func, 0, doc)
138563a5 427#define GMAP_NAMETH(func, doc) NOARG_METHOD(GMAP_METMNAME, func, 0, doc)
c1756f78
MW
428#define GMAP_METHDECL(func, doc) \
429 extern PyObject *gmapmeth_##func(PyObject *, PyObject *);
430#define GMAP_KWMETHDECL(func, doc) \
431 extern PyObject *gmapmeth_##func(PyObject *, PyObject *, PyObject *);
138563a5
MW
432#define GMAP_NAMETHDECL(func, doc) \
433 extern PyObject *gmapmeth_##func(PyObject *);
c1756f78 434
138563a5 435#define GMAP_DOROMETHODS(METH, KWMETH, NAMETH) \
c1756f78 436 METH (has_key, "D.has_key(KEY) -> BOOL") \
138563a5
MW
437 NAMETH(keys, "D.keys() -> LIST") \
438 NAMETH(values, "D.values() -> LIST") \
439 NAMETH(items, "D.items() -> LIST") \
440 NAMETH(iterkeys, "D.iterkeys() -> ITER") \
441 NAMETH(itervalues, "D.itervalues() -> ITER") \
442 NAMETH(iteritems, "D.iteritems() -> ITER") \
c1756f78
MW
443 KWMETH(get, "D.get(KEY, [default = None]) -> VALUE")
444
138563a5
MW
445#define GMAP_DOMETHODS(METH, KWMETH, NAMETH) \
446 GMAP_DOROMETHODS(METH, KWMETH, NAMETH) \
447 NAMETH(clear, "D.clear()") \
c1756f78
MW
448 KWMETH(setdefault, "D.setdefault(K, [default = None]) -> VALUE") \
449 KWMETH(pop, "D.pop(KEY, [default = <error>]) -> VALUE") \
138563a5 450 NAMETH(popitem, "D.popitem() -> (KEY, VALUE)") \
78daa0e0 451 KWMETH(update, "D.update(MAP)")
c1756f78 452
138563a5
MW
453GMAP_DOMETHODS(GMAP_METHDECL, GMAP_KWMETHDECL, GMAP_NAMETHDECL)
454#define GMAP_ROMETHODS GMAP_DOROMETHODS(GMAP_METH, GMAP_KWMETH, GMAP_NAMETH)
455#define GMAP_METHODS GMAP_DOMETHODS(GMAP_METH, GMAP_KWMETH, GMAP_NAMETH)
c1756f78
MW
456
457/* Mapping protocol implementation. */
458extern Py_ssize_t gmap_pysize(PyObject *); /* for `mp_length' */
78daa0e0
MW
459extern PyObject *gmap_pyiter(PyObject *); /* for `tp_iter' */
460extern PyObject *gmap_pylookup(PyObject *, PyObject *); /* for `mp_subscript' */
461extern int gmap_pystore(PyObject *, PyObject *, PyObject *); /* for `mp_ass_subscript' */
462extern int gmap_pyhaskey(PyObject *, PyObject *); /* for `sq_contains' */
87fa2d56 463extern const PySequenceMethods gmap_pysequence; /* for `tp_as_sequence' */
78daa0e0 464extern const PyMethodDef gmapro_pymethods[]; /* read-only methods */
87fa2d56 465extern const PyMethodDef gmap_pymethods[]; /* all the standard methods */
c1756f78
MW
466
467/*----- That's all, folks -------------------------------------------------*/
468
469#ifdef __cplusplus
470 }
471#endif
472
473#endif