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 convlong(PyObject *o, void *pp)
52 if (!o) VALERR("can't delete");
55 *p = PyInt_AS_LONG(o);
59 if ((t = PyNumber_Index(o)) == 0) goto end;
60 *p = PyLong_AsLong(t);
62 if (PyErr_Occurred()) goto end;
69 int convint(PyObject *o, void *pp)
74 if (!convlong(o, &l)) goto end;
75 if (INT_MIN > l || l > INT_MAX) VALERR("out of range");
82 int convulong(PyObject *o, void *pp)
84 unsigned long *p = pp;
87 if (!o) VALERR("can't delete");
90 long i = PyInt_AS_LONG(o);
91 if (i < 0) VALERR("must be nonnegative");
96 if ((t = PyNumber_Index(o)) == 0) goto end;
97 *p = PyLong_AsUnsignedLong(t);
99 if (PyErr_Occurred()) goto end;
106 int convuint(PyObject *o, void *pp)
111 if (!convulong(o, &u)) goto end;
112 if (u > UINT_MAX) VALERR("out of range");
119 int convszt(PyObject *o, void *pp)
124 if (!convulong(o, &u)) goto end;
125 if (u > ~(size_t)0) VALERR("out of range");
132 int convbool(PyObject *o, void *pp)
134 if (!o) VALERR("can't delete");
135 *(int *)pp = PyObject_IsTrue(o);
141 int convbin(PyObject *o, void *pp)
151 if (PyUnicode_Check(o)) {
152 o = _PyUnicode_AsDefaultEncodedString(o, 0);
154 r->p = PyString_AS_STRING(o);
155 r->sz = PyString_GET_SIZE(o);
159 return (PyObject_AsReadBuffer(o, &r->p, &r->sz) ? 0 : 1);
162 /*----- Miscellaneous utilities -------------------------------------------*/
164 PyObject *abstract_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
166 PyErr_SetString(PyExc_TypeError, "can't instantiate this class");
170 PyObject *enrich_compare(int op, int cmp)
175 case Py_LT: r = cmp < 0; break;
176 case Py_LE: r = cmp <= 0; break;
177 case Py_EQ: r = cmp == 0; break;
178 case Py_NE: r = cmp != 0; break;
179 case Py_GE: r = cmp >= 0; break;
180 case Py_GT: r = cmp > 0; break;
186 /*----- Saving and restoring exceptions ----------------------------------*/
188 void report_lost_exception_v(struct excinfo *exc,
189 const char *why, va_list ap)
191 PyObject *hookfn = 0;
192 PyObject *whyobj = 0;
195 /* Make sure we start out without a pending exception, or this will get
198 assert(!PyErr_Occurred());
200 /* Format the explanation. */
201 if (why) whyobj = TEXT_VFORMAT(why, ap);
202 else { whyobj = Py_None; Py_INCREF(whyobj); }
204 /* Find our home module's `lostexchook' function. This won't work if
205 * there's no module, or the function isn't defined, or it's `None'.
207 if (!home_module) goto sys;
208 hookfn = PyObject_GetAttrString(home_module, "lostexchook");
209 if (hookfn == Py_None) goto sys;
211 else if (!PyErr_ExceptionMatches(PyExc_AttributeError)) goto ouch;
212 else { PyErr_Clear(); goto sys; }
214 /* Call the hook function. */
215 obj = PyObject_CallFunction(hookfn, "(OOOO)",
216 whyobj, exc->ty, exc->val, exc->tb);
220 /* Something went wrong reporting the problem. */
222 PySys_WriteStderr("\n!!! FAILURE REPORTING LOST EXCEPTION\n");
224 /* drop through... */
226 /* There was no hook, so try to do something sensible using
230 PySys_WriteStderr("\n!!! LOST EXCEPTION: %s\n", TEXT_PTR(whyobj));
231 RESTORE_EXCINFO(exc);
233 /* drop through... */
235 /* Clean up afterwards. */
242 void report_lost_exception(struct excinfo *exc, const char *why, ...)
247 report_lost_exception_v(exc, why, ap);
251 void stash_exception(struct excinfo *exc, const char *why, ...)
254 struct excinfo stash;
260 STASH_EXCINFO(&stash);
261 report_lost_exception_v(&stash, why, ap);
266 void restore_exception(struct excinfo *exc, const char *why, ...)
269 struct excinfo stash;
271 if (!PyErr_Occurred())
272 RESTORE_EXCINFO(exc);
275 STASH_EXCINFO(&stash);
276 report_lost_exception_v(exc, why, ap);
277 RESTORE_EXCINFO(&stash);
282 /*----- Type definitions --------------------------------------------------*/
284 static const PyTypeObject emptytype = { 0 };
286 void *newtype(PyTypeObject *metaty,
287 const PyTypeObject *skel,
290 PyHeapTypeObject *ty =
291 (PyHeapTypeObject *)_PyObject_GC_Malloc(_PyObject_VAR_SIZE(metaty, 0));
292 if (!skel) skel = &emptytype;
293 memcpy(ty, skel, sizeof(*skel));
294 #define COPY(blah) do { \
295 if (ty->ht_type.tp_as_##blah) { \
296 memcpy(&ty->as_##blah, \
297 ty->ht_type.tp_as_##blah, \
298 sizeof(ty->as_##blah)); \
299 ty->ht_type.tp_as_##blah = &ty->as_##blah; \
308 ty->ht_name = TEXT_FROMSTR(name);
309 else if (ty->ht_type.tp_name)
310 ty->ht_name = TEXT_FROMSTR(ty->ht_type.tp_name);
314 ty->ht_type.tp_name = TEXT_STR(ty->ht_name);
319 (void)PyObject_INIT(&ty->ht_type, metaty);
324 void typeready(PyTypeObject *ty)
327 PyHeapTypeObject *hty = (PyHeapTypeObject *)ty;
328 hty->ht_qualname = hty->ht_name;
331 PyDict_SetItemString(ty->tp_dict, "__module__", modname);
334 PyTypeObject *inittype(const PyTypeObject *tyskel,
335 PyTypeObject *base, PyTypeObject *meta)
337 PyTypeObject *ty = newtype(meta, tyskel, 0);
338 if (base) { ty->tp_base = base; Py_INCREF(base); }
339 ty->tp_flags |= Py_TPFLAGS_HEAPTYPE;
344 /*----- Populating modules ------------------------------------------------*/
346 PyObject *mkexc(PyObject *mod, PyObject *base,
347 const char *name, const PyMethodDef *mm)
349 PyObject *nameobj = 0;
355 if ((dict = PyDict_New()) == 0) goto fail;
358 while (mm->ml_name) {
359 if ((func = PyCFunction_NewEx((/*unconst*/ PyMethodDef *)mm,
361 (meth = PY23(PyMethod_New(func, 0, exc),
362 PyInstanceMethod_New(func))) == 0 ||
363 PyDict_SetItemString(dict, mm->ml_name, meth))
365 Py_DECREF(func); func = 0;
366 Py_DECREF(meth); meth = 0;
371 if ((nameobj = TEXT_FORMAT("%s.%s", PyModule_GetName(mod), name)) == 0 ||
372 (exc = PyErr_NewException(TEXT_STR(nameobj), base, dict)) == 0)
388 void setconstants(PyObject *mod, const struct nameval *c)
395 if (u <= LONG_MAX) x = PyInt_FromLong(u);
396 else if (c->f&CF_SIGNED) x = PyInt_FromLong(-1 - (long)(ULONG_MAX - u));
397 else x = PyLong_FromUnsignedLong(u);
398 PyModule_AddObject(mod, (/*unconst*/ char *)c->name, x);
403 /*----- Submodules --------------------------------------------------------*/
405 static PyMethodDef *global_methods;
406 static size_t nmethods, methodsz;
408 void addmethods(const PyMethodDef *m)
410 size_t n, want, newsz;
412 for (n = 0; m[n].ml_name; n++);
413 want = nmethods + n + 1;
414 if (want > methodsz) {
415 newsz = methodsz ? 2*methodsz : 16;
416 while (want > newsz) newsz *= 2;
418 global_methods = PyObject_Malloc(newsz*sizeof(PyMethodDef));
420 global_methods = PyObject_Realloc(global_methods,
421 newsz*sizeof(PyMethodDef));
422 assert(global_methods);
425 memcpy(global_methods + nmethods, m, n*sizeof(PyMethodDef));
427 global_methods[nmethods].ml_name = 0;
430 PyMethodDef *donemethods(void) { return (global_methods); }
432 /*----- Low-level Python interface ----------------------------------------*/
434 static PyObject *meth__set_home_module(PyObject *me, PyObject *arg)
438 if (!PyArg_ParseTuple(arg, "O!:_set_home_module", &PyModule_Type, &mod))
440 Py_XDECREF(home_module); home_module = mod; Py_INCREF(home_module);
444 static const PyMethodDef methods[] = {
445 #define METHNAME(func) meth_##func
446 METH (_set_home_module, "_set_home_module(MOD)")
451 void pyke_core_pyinit(void) { addmethods(methods); }
452 void pyke_core_pyinsert(PyObject *mod) { ; }
454 /*----- That's all, folks -------------------------------------------------*/