| 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 | /*----- Header files ------------------------------------------------------*/ |
| 28 | |
| 29 | #include "pyke.h" |
| 30 | |
| 31 | /*----- External variables ------------------------------------------------*/ |
| 32 | |
| 33 | PyObject *modname; |
| 34 | PyObject *home_module; |
| 35 | |
| 36 | /*----- Conversions -------------------------------------------------------*/ |
| 37 | |
| 38 | PyObject *getulong(unsigned long w) |
| 39 | { |
| 40 | if (w <= LONG_MAX) return (PyInt_FromLong(w)); |
| 41 | else return (PyLong_FromUnsignedLong(w)); |
| 42 | } |
| 43 | |
| 44 | PyObject *getbool(int b) |
| 45 | { if (b) RETURN_TRUE; else RETURN_FALSE; } |
| 46 | |
| 47 | int convulong(PyObject *o, void *pp) |
| 48 | { |
| 49 | unsigned long *p = pp; |
| 50 | PyObject *t; |
| 51 | |
| 52 | if (!o) VALERR("can't delete"); |
| 53 | #ifdef PY2 |
| 54 | if (PyInt_Check(o)) { |
| 55 | long i = PyInt_AS_LONG(o); |
| 56 | if (i < 0) VALERR("must be nonnegative"); |
| 57 | *p = i; |
| 58 | } else |
| 59 | #endif |
| 60 | { |
| 61 | if ((t = PyNumber_Long(o)) == 0) goto end; |
| 62 | *p = PyLong_AsUnsignedLong(t); |
| 63 | Py_DECREF(t); |
| 64 | if (PyErr_Occurred()) goto end; |
| 65 | } |
| 66 | return (1); |
| 67 | end: |
| 68 | return (0); |
| 69 | } |
| 70 | |
| 71 | int convuint(PyObject *o, void *pp) |
| 72 | { |
| 73 | unsigned long u; |
| 74 | unsigned *p = pp; |
| 75 | |
| 76 | if (!convulong(o, &u)) goto end; |
| 77 | if (u > UINT_MAX) VALERR("out of range"); |
| 78 | *p = u; |
| 79 | return (1); |
| 80 | end: |
| 81 | return (0); |
| 82 | } |
| 83 | |
| 84 | int convszt(PyObject *o, void *pp) |
| 85 | { |
| 86 | unsigned long u; |
| 87 | size_t *p = pp; |
| 88 | |
| 89 | if (!convulong(o, &u)) goto end; |
| 90 | if (u > ~(size_t)0) VALERR("out of range"); |
| 91 | *p = u; |
| 92 | return (1); |
| 93 | end: |
| 94 | return (0); |
| 95 | } |
| 96 | |
| 97 | int convbool(PyObject *o, void *pp) |
| 98 | { |
| 99 | if (!o) VALERR("can't delete"); |
| 100 | *(int *)pp = PyObject_IsTrue(o); |
| 101 | return (1); |
| 102 | end: |
| 103 | return (0); |
| 104 | } |
| 105 | |
| 106 | int convbin(PyObject *o, void *pp) |
| 107 | { |
| 108 | struct bin *r = pp; |
| 109 | int rc; |
| 110 | |
| 111 | if (BIN_CHECK(o)) { |
| 112 | r->vw.obj = 0; |
| 113 | r->p = BIN_PTR(o); |
| 114 | r->sz = BIN_LEN(o); |
| 115 | return (1); |
| 116 | } |
| 117 | #ifdef PY2 |
| 118 | if (PyUnicode_Check(o)) { |
| 119 | o = _PyUnicode_AsDefaultEncodedString(o, 0); |
| 120 | if (!o) return (0); |
| 121 | r->vw.obj = 0; |
| 122 | r->p = PyString_AS_STRING(o); |
| 123 | r->sz = PyString_GET_SIZE(o); |
| 124 | return (1); |
| 125 | } |
| 126 | #endif |
| 127 | #if PY_VERSION_HEX < 0x02060000 |
| 128 | r->vw.buf = 0; |
| 129 | return (PyObject_AsReadBuffer(o, &r->p, &r->sz) ? 0 : 1); |
| 130 | #else |
| 131 | rc = PyObject_GetBuffer(o, &r->vw, PyBUF_SIMPLE); if (rc) return (0); |
| 132 | o->p = o->vw.buf; o->sz = o->vw.len; |
| 133 | return (1); |
| 134 | #endif |
| 135 | } |
| 136 | |
| 137 | void freebin(struct bin *r) |
| 138 | { if (r->vw.obj) PyBuffer_Release(&r->vw); } |
| 139 | |
| 140 | /*----- Miscellaneous utilities -------------------------------------------*/ |
| 141 | |
| 142 | PyObject *abstract_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) |
| 143 | { |
| 144 | PyErr_SetString(PyExc_TypeError, "can't instantiate this class"); |
| 145 | return (0); |
| 146 | } |
| 147 | |
| 148 | PyObject *enrich_compare(int op, int cmp) |
| 149 | { |
| 150 | int r = -1; |
| 151 | |
| 152 | switch (op) { |
| 153 | case Py_LT: r = cmp < 0; break; |
| 154 | case Py_LE: r = cmp <= 0; break; |
| 155 | case Py_EQ: r = cmp == 0; break; |
| 156 | case Py_NE: r = cmp != 0; break; |
| 157 | case Py_GE: r = cmp >= 0; break; |
| 158 | case Py_GT: r = cmp > 0; break; |
| 159 | default: assert(0); |
| 160 | } |
| 161 | return (getbool(r)); |
| 162 | } |
| 163 | |
| 164 | /*----- Saving and restoring exceptions ----------------------------------*/ |
| 165 | |
| 166 | void report_lost_exception_v(struct excinfo *exc, |
| 167 | const char *why, va_list ap) |
| 168 | { |
| 169 | PyObject *hookfn = 0; |
| 170 | PyObject *whyobj = 0; |
| 171 | PyObject *obj = 0; |
| 172 | |
| 173 | /* Make sure we start out without a pending exception, or this will get |
| 174 | * really confusing. |
| 175 | */ |
| 176 | assert(!PyErr_Occurred()); |
| 177 | |
| 178 | /* Format the explanation. */ |
| 179 | if (why) whyobj = TEXT_VFORMAT(why, ap); |
| 180 | else { whyobj = Py_None; Py_INCREF(whyobj); } |
| 181 | |
| 182 | /* Find our home module's `lostexchook' function. This won't work if |
| 183 | * there's no module, or the function isn't defined, or it's `None'. |
| 184 | */ |
| 185 | if (!home_module) goto sys; |
| 186 | hookfn = PyObject_GetAttrString(home_module, "lostexchook"); |
| 187 | if (hookfn == Py_None) goto sys; |
| 188 | else if (hookfn) ; |
| 189 | else if (!PyErr_ExceptionMatches(PyExc_AttributeError)) goto ouch; |
| 190 | else { PyErr_Clear(); goto sys; } |
| 191 | |
| 192 | /* Call the hook function. */ |
| 193 | obj = PyObject_CallFunction(hookfn, "(OOOO)", |
| 194 | whyobj, exc->ty, exc->val, exc->tb); |
| 195 | if (!obj) goto ouch; |
| 196 | goto end; |
| 197 | |
| 198 | /* Something went wrong reporting the problem. */ |
| 199 | ouch: |
| 200 | PySys_WriteStderr("\n!!! FAILURE REPORTING LOST EXCEPTION\n"); |
| 201 | PyErr_Print(); |
| 202 | /* drop through... */ |
| 203 | |
| 204 | /* There was no hook, so try to do something sensible using |
| 205 | * `sys.excepthook'. |
| 206 | */ |
| 207 | sys: |
| 208 | PySys_WriteStderr("\n!!! LOST EXCEPTION: %s\n", TEXT_PTR(whyobj)); |
| 209 | RESTORE_EXCINFO(exc); |
| 210 | PyErr_Print(); |
| 211 | /* drop through... */ |
| 212 | |
| 213 | /* Clean up afterwards. */ |
| 214 | end: |
| 215 | Py_XDECREF(hookfn); |
| 216 | Py_XDECREF(whyobj); |
| 217 | Py_XDECREF(obj); |
| 218 | } |
| 219 | |
| 220 | void report_lost_exception(struct excinfo *exc, const char *why, ...) |
| 221 | { |
| 222 | va_list ap; |
| 223 | |
| 224 | va_start(ap, why); |
| 225 | report_lost_exception_v(exc, why, ap); |
| 226 | va_end(ap); |
| 227 | } |
| 228 | |
| 229 | void stash_exception(struct excinfo *exc, const char *why, ...) |
| 230 | { |
| 231 | va_list ap; |
| 232 | struct excinfo stash; |
| 233 | |
| 234 | if (!exc->ty) |
| 235 | STASH_EXCINFO(exc); |
| 236 | else { |
| 237 | va_start(ap, why); |
| 238 | STASH_EXCINFO(&stash); |
| 239 | report_lost_exception_v(&stash, why, ap); |
| 240 | va_end(ap); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | void restore_exception(struct excinfo *exc, const char *why, ...) |
| 245 | { |
| 246 | va_list ap; |
| 247 | struct excinfo stash; |
| 248 | |
| 249 | if (!PyErr_Occurred()) |
| 250 | RESTORE_EXCINFO(exc); |
| 251 | else { |
| 252 | va_start(ap, why); |
| 253 | STASH_EXCINFO(&stash); |
| 254 | report_lost_exception_v(exc, why, ap); |
| 255 | RESTORE_EXCINFO(&stash); |
| 256 | va_end(ap); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | /*----- Type definitions --------------------------------------------------*/ |
| 261 | |
| 262 | static const PyTypeObject emptytype = { 0 }; |
| 263 | |
| 264 | void *newtype(PyTypeObject *metaty, |
| 265 | const PyTypeObject *skel, |
| 266 | const char *name) |
| 267 | { |
| 268 | PyHeapTypeObject *ty = |
| 269 | (PyHeapTypeObject *)_PyObject_GC_Malloc(_PyObject_VAR_SIZE(metaty, 0)); |
| 270 | if (!skel) skel = &emptytype; |
| 271 | memcpy(ty, skel, sizeof(*skel)); |
| 272 | #define COPY(blah) do { \ |
| 273 | if (ty->ht_type.tp_as_##blah) { \ |
| 274 | memcpy(&ty->as_##blah, \ |
| 275 | ty->ht_type.tp_as_##blah, \ |
| 276 | sizeof(ty->as_##blah)); \ |
| 277 | ty->ht_type.tp_as_##blah = &ty->as_##blah; \ |
| 278 | } \ |
| 279 | } while (0) |
| 280 | COPY(number); |
| 281 | COPY(sequence); |
| 282 | COPY(mapping); |
| 283 | COPY(buffer); |
| 284 | #undef COPY |
| 285 | if (name) |
| 286 | ty->ht_name = TEXT_FROMSTR(name); |
| 287 | else if (ty->ht_type.tp_name) |
| 288 | ty->ht_name = TEXT_FROMSTR(ty->ht_type.tp_name); |
| 289 | else |
| 290 | ty->ht_name = 0; |
| 291 | if (ty->ht_name) |
| 292 | ty->ht_type.tp_name = TEXT_STR(ty->ht_name); |
| 293 | ty->ht_slots = 0; |
| 294 | #ifdef PY3 |
| 295 | ty->ht_qualname = 0; |
| 296 | #endif |
| 297 | (void)PyObject_INIT(&ty->ht_type, metaty); |
| 298 | Py_INCREF(metaty); |
| 299 | return (ty); |
| 300 | } |
| 301 | |
| 302 | void typeready(PyTypeObject *ty) |
| 303 | { |
| 304 | #ifdef PY3 |
| 305 | PyHeapTypeObject *hty = (PyHeapTypeObject *)ty; |
| 306 | hty->ht_qualname = hty->ht_name; |
| 307 | #endif |
| 308 | PyType_Ready(ty); |
| 309 | PyDict_SetItemString(ty->tp_dict, "__module__", modname); |
| 310 | } |
| 311 | |
| 312 | PyTypeObject *inittype(const PyTypeObject *tyskel, |
| 313 | PyTypeObject *base, PyTypeObject *meta) |
| 314 | { |
| 315 | PyTypeObject *ty = newtype(meta, tyskel, 0); |
| 316 | if (base) { ty->tp_base = base; Py_INCREF(base); } |
| 317 | ty->tp_flags |= Py_TPFLAGS_HEAPTYPE; |
| 318 | typeready(ty); |
| 319 | return (ty); |
| 320 | } |
| 321 | |
| 322 | /*----- Populating modules ------------------------------------------------*/ |
| 323 | |
| 324 | PyObject *mkexc(PyObject *mod, PyObject *base, |
| 325 | const char *name, const PyMethodDef *mm) |
| 326 | { |
| 327 | PyObject *nameobj = 0; |
| 328 | PyObject *dict = 0; |
| 329 | PyObject *exc = 0; |
| 330 | PyObject *func = 0; |
| 331 | PyObject *meth = 0; |
| 332 | |
| 333 | if ((dict = PyDict_New()) == 0) goto fail; |
| 334 | |
| 335 | if (mm) { |
| 336 | while (mm->ml_name) { |
| 337 | if ((func = PyCFunction_NewEx((/*unconst*/ PyMethodDef *)mm, |
| 338 | 0, mod)) == 0 || |
| 339 | (meth = PY23(PyMethod_New(func, 0, exc), |
| 340 | PyInstanceMethod_New(func))) == 0 || |
| 341 | PyDict_SetItemString(dict, mm->ml_name, meth)) |
| 342 | goto fail; |
| 343 | Py_DECREF(func); func = 0; |
| 344 | Py_DECREF(meth); meth = 0; |
| 345 | mm++; |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | if ((nameobj = TEXT_FORMAT("%s.%s", PyModule_GetName(mod), name)) == 0 || |
| 350 | (exc = PyErr_NewException(TEXT_STR(nameobj), base, dict)) == 0) |
| 351 | goto fail; |
| 352 | |
| 353 | done: |
| 354 | Py_XDECREF(nameobj); |
| 355 | Py_XDECREF(dict); |
| 356 | return (exc); |
| 357 | |
| 358 | fail: |
| 359 | Py_XDECREF(exc); |
| 360 | Py_XDECREF(func); |
| 361 | Py_XDECREF(meth); |
| 362 | exc = 0; |
| 363 | goto done; |
| 364 | } |
| 365 | |
| 366 | void setconstants(PyObject *mod, const struct nameval *c) |
| 367 | { |
| 368 | PyObject *x; |
| 369 | unsigned long u; |
| 370 | |
| 371 | while (c->name) { |
| 372 | u = c->value; |
| 373 | if (u <= LONG_MAX) x = PyInt_FromLong(u); |
| 374 | else if (c->f&CF_SIGNED) x = PyInt_FromLong(-1 - (long)(ULONG_MAX - u)); |
| 375 | else x = PyLong_FromUnsignedLong(u); |
| 376 | PyModule_AddObject(mod, (/*unconst*/ char *)c->name, x); |
| 377 | c++; |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | /*----- Submodules --------------------------------------------------------*/ |
| 382 | |
| 383 | static PyMethodDef *global_methods; |
| 384 | static size_t nmethods, methodsz; |
| 385 | |
| 386 | void addmethods(const PyMethodDef *m) |
| 387 | { |
| 388 | size_t n, want, newsz; |
| 389 | |
| 390 | for (n = 0; m[n].ml_name; n++); |
| 391 | want = nmethods + n + 1; |
| 392 | if (want > methodsz) { |
| 393 | newsz = methodsz ? 2*methodsz : 16; |
| 394 | while (want > newsz) newsz *= 2; |
| 395 | if (!global_methods) |
| 396 | global_methods = PyObject_Malloc(newsz*sizeof(PyMethodDef)); |
| 397 | else |
| 398 | global_methods = PyObject_Realloc(global_methods, |
| 399 | newsz*sizeof(PyMethodDef)); |
| 400 | assert(global_methods); |
| 401 | methodsz = newsz; |
| 402 | } |
| 403 | memcpy(global_methods + nmethods, m, n*sizeof(PyMethodDef)); |
| 404 | nmethods += n; |
| 405 | global_methods[nmethods].ml_name = 0; |
| 406 | } |
| 407 | |
| 408 | PyMethodDef *donemethods(void) { return (global_methods); } |
| 409 | |
| 410 | /*----- Low-level Python interface ----------------------------------------*/ |
| 411 | |
| 412 | static PyObject *meth__set_home_module(PyObject *me, PyObject *arg) |
| 413 | { |
| 414 | PyObject *mod; |
| 415 | |
| 416 | if (!PyArg_ParseTuple(arg, "O!:_set_home_module", &PyModule_Type, &mod)) |
| 417 | return (0); |
| 418 | Py_XDECREF(home_module); home_module = mod; Py_INCREF(home_module); |
| 419 | RETURN_NONE; |
| 420 | } |
| 421 | |
| 422 | static const PyMethodDef methods[] = { |
| 423 | #define METHNAME(func) meth_##func |
| 424 | METH (_set_home_module, "_set_home_module(MOD)") |
| 425 | #undef METHNAME |
| 426 | { 0 } |
| 427 | }; |
| 428 | |
| 429 | void pyke_core_pyinit(void) { addmethods(methods); } |
| 430 | void pyke_core_pyinsert(PyObject *mod) { ; } |
| 431 | |
| 432 | /*----- That's all, folks -------------------------------------------------*/ |