3 * Pyke: the Python Kit for Extensions
5 * (c) 2019 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Pyke: the Python Kit for Extensions.
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.
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
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.
27 /*----- Header files ------------------------------------------------------*/
31 /*----- External variables ------------------------------------------------*/
34 PyObject *home_module;
36 /*----- Conversions -------------------------------------------------------*/
38 PyObject *getulong(unsigned long w)
40 if (w <= LONG_MAX) return (PyInt_FromLong(w));
41 else return (PyLong_FromUnsignedLong(w));
44 PyObject *getbool(int b)
45 { if (b) RETURN_TRUE; else RETURN_FALSE; }
47 int convulong(PyObject *o, void *pp)
50 unsigned long *p = pp;
53 if (!o) VALERR("can't delete");
56 if (i < 0) VALERR("must be nonnegative");
59 if ((t = PyNumber_Long(o)) == 0) goto end;
60 *p = PyLong_AsUnsignedLong(t);
62 if (PyErr_Occurred()) goto end;
69 int convuint(PyObject *o, void *pp)
74 if (!convulong(o, &u)) goto end;
75 if (u > UINT_MAX) VALERR("out of range");
82 int convszt(PyObject *o, void *pp)
87 if (!convulong(o, &u)) goto end;
88 if (u > ~(size_t)0) VALERR("out of range");
95 int convbool(PyObject *o, void *pp)
97 if (!o) VALERR("can't delete");
98 *(int *)pp = PyObject_IsTrue(o);
104 /*----- Miscellaneous utilities -------------------------------------------*/
106 PyObject *abstract_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
108 PyErr_SetString(PyExc_TypeError, "can't instantiate this class");
112 /*----- Saving and restoring exceptions ----------------------------------*/
114 void report_lost_exception_v(struct excinfo *exc,
115 const char *why, va_list ap)
117 PyObject *hookfn = 0;
118 PyObject *whyobj = 0;
121 /* Make sure we start out without a pending exception, or this will get
124 assert(!PyErr_Occurred());
126 /* Format the explanation. */
127 if (why) whyobj = PyString_FromFormatV(why, ap);
128 else { whyobj = Py_None; Py_INCREF(whyobj); }
130 /* Find our home module's `lostexchook' function. This won't work if
131 * there's no module, or the function isn't defined, or it's `None'.
133 if (!home_module) goto sys;
134 hookfn = PyObject_GetAttrString(home_module, "lostexchook");
135 if (hookfn == Py_None) goto sys;
137 else if (!PyErr_ExceptionMatches(PyExc_AttributeError)) goto ouch;
138 else { PyErr_Clear(); goto sys; }
140 /* Call the hook function. */
141 obj = PyObject_CallFunction(hookfn, "(OOOO)",
142 whyobj, exc->ty, exc->val, exc->tb);
146 /* Something went wrong reporting the problem. */
148 PySys_WriteStderr("\n!!! FAILURE REPORTING LOST EXCEPTION\n");
150 /* drop through... */
152 /* There was no hook, so try to do something sensible using
156 PySys_WriteStderr("\n!!! LOST EXCEPTION: %s\n",
157 PyString_AS_STRING(whyobj));
158 RESTORE_EXCINFO(exc);
160 /* drop through... */
162 /* Clean up afterwards. */
169 void report_lost_exception(struct excinfo *exc, const char *why, ...)
174 report_lost_exception_v(exc, why, ap);
178 void stash_exception(struct excinfo *exc, const char *why, ...)
181 struct excinfo stash;
187 STASH_EXCINFO(&stash);
188 report_lost_exception_v(&stash, why, ap);
193 void restore_exception(struct excinfo *exc, const char *why, ...)
196 struct excinfo stash;
198 if (!PyErr_Occurred())
199 RESTORE_EXCINFO(exc);
202 STASH_EXCINFO(&stash);
203 report_lost_exception_v(exc, why, ap);
204 RESTORE_EXCINFO(&stash);
209 /*----- Type definitions --------------------------------------------------*/
211 static const PyTypeObject emptytype = { 0 };
213 void *newtype(PyTypeObject *metaty,
214 const PyTypeObject *skel,
217 PyHeapTypeObject *ty =
218 (PyHeapTypeObject *)_PyObject_GC_Malloc(_PyObject_VAR_SIZE(metaty, 0));
219 if (!skel) skel = &emptytype;
220 memcpy(ty, skel, sizeof(*skel));
221 #define COPY(blah) do { \
222 if (ty->ht_type.tp_as_##blah) { \
223 memcpy(&ty->as_##blah, \
224 ty->ht_type.tp_as_##blah, \
225 sizeof(ty->as_##blah)); \
226 ty->ht_type.tp_as_##blah = &ty->as_##blah; \
235 ty->ht_name = PyString_FromString(name);
236 else if (ty->ht_type.tp_name)
237 ty->ht_name = PyString_FromString(ty->ht_type.tp_name);
239 ty->ht_type.tp_name = PyString_AS_STRING(ty->ht_name);
241 (void)PyObject_INIT(&ty->ht_type, metaty);
246 void typeready(PyTypeObject *ty)
249 PyDict_SetItemString(ty->tp_dict, "__module__", modname);
252 PyTypeObject *inittype(const PyTypeObject *tyskel,
253 PyTypeObject *base, PyTypeObject *meta)
255 PyTypeObject *ty = newtype(meta, tyskel, 0);
256 if (base) { ty->tp_base = base; Py_INCREF(base); }
257 ty->tp_flags |= Py_TPFLAGS_HEAPTYPE;
262 /*----- Populating modules ------------------------------------------------*/
264 PyObject *mkexc(PyObject *mod, PyObject *base,
265 const char *name, const PyMethodDef *mm)
267 PyObject *nameobj = 0;
273 if ((dict = PyDict_New()) == 0) goto fail;
276 while (mm->ml_name) {
277 if ((func = PyCFunction_NewEx((/*unconst*/ PyMethodDef *)mm,
279 (meth = PyMethod_New(func, 0, exc)) == 0 ||
280 PyDict_SetItemString(dict, mm->ml_name, meth))
282 Py_DECREF(func); func = 0;
283 Py_DECREF(meth); meth = 0;
288 if ((nameobj = PyString_FromFormat("%s.%s",
289 PyModule_GetName(mod),
291 (exc = PyErr_NewException(PyString_AS_STRING(nameobj),
308 void setconstants(PyObject *mod, const struct nameval *c)
315 if (u <= LONG_MAX) x = PyInt_FromLong(u);
316 else if (c->f&CF_SIGNED) x = PyInt_FromLong(-1 - (long)(ULONG_MAX - u));
317 else x = PyLong_FromUnsignedLong(u);
318 PyModule_AddObject(mod, (/*unconst*/ char *)c->name, x);
323 /*----- Submodules --------------------------------------------------------*/
325 static PyMethodDef *global_methods;
326 static size_t nmethods, methodsz;
328 void addmethods(const PyMethodDef *m)
330 size_t n, want, newsz;
332 for (n = 0; m[n].ml_name; n++);
333 want = nmethods + n + 1;
334 if (want > methodsz) {
335 newsz = methodsz ? 2*methodsz : 16;
336 while (want > newsz) newsz *= 2;
338 global_methods = PyObject_Malloc(newsz*sizeof(PyMethodDef));
340 global_methods = PyObject_Realloc(global_methods,
341 newsz*sizeof(PyMethodDef));
342 assert(global_methods);
345 memcpy(global_methods + nmethods, m, n*sizeof(PyMethodDef));
347 global_methods[nmethods].ml_name = 0;
350 PyMethodDef *donemethods(void) { return (global_methods); }
352 /*----- Low-level Python interface ----------------------------------------*/
354 static PyObject *meth__set_home_module(PyObject *me, PyObject *arg)
358 if (!PyArg_ParseTuple(arg, "O!:_set_home_module", &PyModule_Type, &mod))
360 Py_XDECREF(home_module); home_module = mod; Py_INCREF(home_module);
364 static const PyMethodDef methods[] = {
365 #define METHNAME(func) meth_##func
366 METH (_set_home_module, "_set_home_module(MOD)")
371 void pyke_core_pyinit(void) { addmethods(methods); }
372 void pyke_core_pyinsert(PyObject *mod) { ; }
374 /*----- That's all, folks -------------------------------------------------*/