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