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
)
49 unsigned long *p
= pp
;
52 if (!o
) VALERR("can't delete");
55 long i
= PyInt_AS_LONG(o
);
56 if (i
< 0) VALERR("must be nonnegative");
61 if ((t
= PyNumber_Long(o
)) == 0) goto end
;
62 *p
= PyLong_AsUnsignedLong(t
);
64 if (PyErr_Occurred()) goto end
;
71 int convuint(PyObject
*o
, void *pp
)
76 if (!convulong(o
, &u
)) goto end
;
77 if (u
> UINT_MAX
) VALERR("out of range");
84 int convszt(PyObject
*o
, void *pp
)
89 if (!convulong(o
, &u
)) goto end
;
90 if (u
> ~(size_t)0) VALERR("out of range");
97 int convbool(PyObject
*o
, void *pp
)
99 if (!o
) VALERR("can't delete");
100 *(int *)pp
= PyObject_IsTrue(o
);
106 int convbin(PyObject
*o
, void *pp
)
116 if (PyUnicode_Check(o
)) {
117 o
= _PyUnicode_AsDefaultEncodedString(o
, 0);
119 r
->p
= PyString_AS_STRING(o
);
120 r
->sz
= PyString_GET_SIZE(o
);
124 return (PyObject_AsReadBuffer(o
, &r
->p
, &r
->sz
) ?
0 : 1);
127 /*----- Miscellaneous utilities -------------------------------------------*/
129 PyObject
*abstract_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
131 PyErr_SetString(PyExc_TypeError
, "can't instantiate this class");
135 PyObject
*enrich_compare(int op
, int cmp
)
140 case Py_LT
: r
= cmp
< 0; break;
141 case Py_LE
: r
= cmp
<= 0; break;
142 case Py_EQ
: r
= cmp
== 0; break;
143 case Py_NE
: r
= cmp
!= 0; break;
144 case Py_GE
: r
= cmp
>= 0; break;
145 case Py_GT
: r
= cmp
> 0; break;
151 /*----- Saving and restoring exceptions ----------------------------------*/
153 void report_lost_exception_v(struct excinfo
*exc
,
154 const char *why
, va_list ap
)
156 PyObject
*hookfn
= 0;
157 PyObject
*whyobj
= 0;
160 /* Make sure we start out without a pending exception, or this will get
163 assert(!PyErr_Occurred());
165 /* Format the explanation. */
166 if (why
) whyobj
= TEXT_VFORMAT(why
, ap
);
167 else { whyobj
= Py_None
; Py_INCREF(whyobj
); }
169 /* Find our home module's `lostexchook' function. This won't work if
170 * there's no module, or the function isn't defined, or it's `None'.
172 if (!home_module
) goto sys
;
173 hookfn
= PyObject_GetAttrString(home_module
, "lostexchook");
174 if (hookfn
== Py_None
) goto sys
;
176 else if (!PyErr_ExceptionMatches(PyExc_AttributeError
)) goto ouch
;
177 else { PyErr_Clear(); goto sys
; }
179 /* Call the hook function. */
180 obj
= PyObject_CallFunction(hookfn
, "(OOOO)",
181 whyobj
, exc
->ty
, exc
->val
, exc
->tb
);
185 /* Something went wrong reporting the problem. */
187 PySys_WriteStderr("\n!!! FAILURE REPORTING LOST EXCEPTION\n");
189 /* drop through... */
191 /* There was no hook, so try to do something sensible using
195 PySys_WriteStderr("\n!!! LOST EXCEPTION: %s\n", TEXT_PTR(whyobj
));
196 RESTORE_EXCINFO(exc
);
198 /* drop through... */
200 /* Clean up afterwards. */
207 void report_lost_exception(struct excinfo
*exc
, const char *why
, ...)
212 report_lost_exception_v(exc
, why
, ap
);
216 void stash_exception(struct excinfo
*exc
, const char *why
, ...)
219 struct excinfo stash
;
225 STASH_EXCINFO(&stash
);
226 report_lost_exception_v(&stash
, why
, ap
);
231 void restore_exception(struct excinfo
*exc
, const char *why
, ...)
234 struct excinfo stash
;
236 if (!PyErr_Occurred())
237 RESTORE_EXCINFO(exc
);
240 STASH_EXCINFO(&stash
);
241 report_lost_exception_v(exc
, why
, ap
);
242 RESTORE_EXCINFO(&stash
);
247 /*----- Type definitions --------------------------------------------------*/
249 static const PyTypeObject emptytype
= { 0 };
251 void *newtype(PyTypeObject
*metaty
,
252 const PyTypeObject
*skel
,
255 PyHeapTypeObject
*ty
=
256 (PyHeapTypeObject
*)_PyObject_GC_Malloc(_PyObject_VAR_SIZE(metaty
, 0));
257 if (!skel
) skel
= &emptytype
;
258 memcpy(ty
, skel
, sizeof(*skel
));
259 #define COPY(blah) do { \
260 if (ty->ht_type.tp_as_##blah) { \
261 memcpy(&ty->as_##blah, \
262 ty->ht_type.tp_as_##blah, \
263 sizeof(ty->as_##blah)); \
264 ty->ht_type.tp_as_##blah = &ty->as_##blah; \
273 ty
->ht_name
= TEXT_FROMSTR(name
);
274 else if (ty
->ht_type
.tp_name
)
275 ty
->ht_name
= TEXT_FROMSTR(ty
->ht_type
.tp_name
);
279 ty
->ht_type
.tp_name
= TEXT_STR(ty
->ht_name
);
284 (void)PyObject_INIT(&ty
->ht_type
, metaty
);
289 void typeready(PyTypeObject
*ty
)
292 PyHeapTypeObject
*hty
= (PyHeapTypeObject
*)ty
;
293 hty
->ht_qualname
= hty
->ht_name
;
296 PyDict_SetItemString(ty
->tp_dict
, "__module__", modname
);
299 PyTypeObject
*inittype(const PyTypeObject
*tyskel
,
300 PyTypeObject
*base
, PyTypeObject
*meta
)
302 PyTypeObject
*ty
= newtype(meta
, tyskel
, 0);
303 if (base
) { ty
->tp_base
= base
; Py_INCREF(base
); }
304 ty
->tp_flags
|= Py_TPFLAGS_HEAPTYPE
;
309 /*----- Populating modules ------------------------------------------------*/
311 PyObject
*mkexc(PyObject
*mod
, PyObject
*base
,
312 const char *name
, const PyMethodDef
*mm
)
314 PyObject
*nameobj
= 0;
320 if ((dict
= PyDict_New()) == 0) goto fail
;
323 while (mm
->ml_name
) {
324 if ((func
= PyCFunction_NewEx((/*unconst*/ PyMethodDef
*)mm
,
326 (meth
= PY23(PyMethod_New(func
, 0, exc
),
327 PyInstanceMethod_New(func
))) == 0 ||
328 PyDict_SetItemString(dict
, mm
->ml_name
, meth
))
330 Py_DECREF(func
); func
= 0;
331 Py_DECREF(meth
); meth
= 0;
336 if ((nameobj
= TEXT_FORMAT("%s.%s", PyModule_GetName(mod
), name
)) == 0 ||
337 (exc
= PyErr_NewException(TEXT_STR(nameobj
), base
, dict
)) == 0)
353 void setconstants(PyObject
*mod
, const struct nameval
*c
)
360 if (u
<= LONG_MAX
) x
= PyInt_FromLong(u
);
361 else if (c
->f
&CF_SIGNED
) x
= PyInt_FromLong(-1 - (long)(ULONG_MAX
- u
));
362 else x
= PyLong_FromUnsignedLong(u
);
363 PyModule_AddObject(mod
, (/*unconst*/ char *)c
->name
, x
);
368 /*----- Submodules --------------------------------------------------------*/
370 static PyMethodDef
*global_methods
;
371 static size_t nmethods
, methodsz
;
373 void addmethods(const PyMethodDef
*m
)
375 size_t n
, want
, newsz
;
377 for (n
= 0; m
[n
].ml_name
; n
++);
378 want
= nmethods
+ n
+ 1;
379 if (want
> methodsz
) {
380 newsz
= methodsz ?
2*methodsz
: 16;
381 while (want
> newsz
) newsz
*= 2;
383 global_methods
= PyObject_Malloc(newsz
*sizeof(PyMethodDef
));
385 global_methods
= PyObject_Realloc(global_methods
,
386 newsz
*sizeof(PyMethodDef
));
387 assert(global_methods
);
390 memcpy(global_methods
+ nmethods
, m
, n
*sizeof(PyMethodDef
));
392 global_methods
[nmethods
].ml_name
= 0;
395 PyMethodDef
*donemethods(void) { return (global_methods
); }
397 /*----- Low-level Python interface ----------------------------------------*/
399 static PyObject
*meth__set_home_module(PyObject
*me
, PyObject
*arg
)
403 if (!PyArg_ParseTuple(arg
, "O!:_set_home_module", &PyModule_Type
, &mod
))
405 Py_XDECREF(home_module
); home_module
= mod
; Py_INCREF(home_module
);
409 static const PyMethodDef methods
[] = {
410 #define METHNAME(func) meth_##func
411 METH (_set_home_module
, "_set_home_module(MOD)")
416 void pyke_core_pyinit(void) { addmethods(methods
); }
417 void pyke_core_pyinsert(PyObject
*mod
) { ; }
419 /*----- That's all, folks -------------------------------------------------*/