1d4458b0239cdba71229241d77f6854b282f5db2
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 int convbin(PyObject
*o
, void *pp
)
113 if (PyUnicode_Check(o
)) {
114 o
= _PyUnicode_AsDefaultEncodedString(o
, 0);
116 r
->p
= PyString_AS_STRING(o
);
117 r
->sz
= PyString_GET_SIZE(o
);
120 return (PyObject_AsReadBuffer(o
, &r
->p
, &r
->sz
) ?
0 : 1);
123 /*----- Miscellaneous utilities -------------------------------------------*/
125 PyObject
*abstract_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
127 PyErr_SetString(PyExc_TypeError
, "can't instantiate this class");
131 PyObject
*enrich_compare(int op
, int cmp
)
136 case Py_LT
: r
= cmp
< 0; break;
137 case Py_LE
: r
= cmp
<= 0; break;
138 case Py_EQ
: r
= cmp
== 0; break;
139 case Py_NE
: r
= cmp
!= 0; break;
140 case Py_GE
: r
= cmp
>= 0; break;
141 case Py_GT
: r
= cmp
> 0; break;
147 /*----- Saving and restoring exceptions ----------------------------------*/
149 void report_lost_exception_v(struct excinfo
*exc
,
150 const char *why
, va_list ap
)
152 PyObject
*hookfn
= 0;
153 PyObject
*whyobj
= 0;
156 /* Make sure we start out without a pending exception, or this will get
159 assert(!PyErr_Occurred());
161 /* Format the explanation. */
162 if (why
) whyobj
= TEXT_VFORMAT(why
, ap
);
163 else { whyobj
= Py_None
; Py_INCREF(whyobj
); }
165 /* Find our home module's `lostexchook' function. This won't work if
166 * there's no module, or the function isn't defined, or it's `None'.
168 if (!home_module
) goto sys
;
169 hookfn
= PyObject_GetAttrString(home_module
, "lostexchook");
170 if (hookfn
== Py_None
) goto sys
;
172 else if (!PyErr_ExceptionMatches(PyExc_AttributeError
)) goto ouch
;
173 else { PyErr_Clear(); goto sys
; }
175 /* Call the hook function. */
176 obj
= PyObject_CallFunction(hookfn
, "(OOOO)",
177 whyobj
, exc
->ty
, exc
->val
, exc
->tb
);
181 /* Something went wrong reporting the problem. */
183 PySys_WriteStderr("\n!!! FAILURE REPORTING LOST EXCEPTION\n");
185 /* drop through... */
187 /* There was no hook, so try to do something sensible using
191 PySys_WriteStderr("\n!!! LOST EXCEPTION: %s\n", TEXT_PTR(whyobj
));
192 RESTORE_EXCINFO(exc
);
194 /* drop through... */
196 /* Clean up afterwards. */
203 void report_lost_exception(struct excinfo
*exc
, const char *why
, ...)
208 report_lost_exception_v(exc
, why
, ap
);
212 void stash_exception(struct excinfo
*exc
, const char *why
, ...)
215 struct excinfo stash
;
221 STASH_EXCINFO(&stash
);
222 report_lost_exception_v(&stash
, why
, ap
);
227 void restore_exception(struct excinfo
*exc
, const char *why
, ...)
230 struct excinfo stash
;
232 if (!PyErr_Occurred())
233 RESTORE_EXCINFO(exc
);
236 STASH_EXCINFO(&stash
);
237 report_lost_exception_v(exc
, why
, ap
);
238 RESTORE_EXCINFO(&stash
);
243 /*----- Type definitions --------------------------------------------------*/
245 static const PyTypeObject emptytype
= { 0 };
247 void *newtype(PyTypeObject
*metaty
,
248 const PyTypeObject
*skel
,
251 PyHeapTypeObject
*ty
=
252 (PyHeapTypeObject
*)_PyObject_GC_Malloc(_PyObject_VAR_SIZE(metaty
, 0));
253 if (!skel
) skel
= &emptytype
;
254 memcpy(ty
, skel
, sizeof(*skel
));
255 #define COPY(blah) do { \
256 if (ty->ht_type.tp_as_##blah) { \
257 memcpy(&ty->as_##blah, \
258 ty->ht_type.tp_as_##blah, \
259 sizeof(ty->as_##blah)); \
260 ty->ht_type.tp_as_##blah = &ty->as_##blah; \
269 ty
->ht_name
= TEXT_FROMSTR(name
);
270 else if (ty
->ht_type
.tp_name
)
271 ty
->ht_name
= TEXT_FROMSTR(ty
->ht_type
.tp_name
);
275 ty
->ht_type
.tp_name
= TEXT_STR(ty
->ht_name
);
277 (void)PyObject_INIT(&ty
->ht_type
, metaty
);
282 void typeready(PyTypeObject
*ty
)
285 PyDict_SetItemString(ty
->tp_dict
, "__module__", modname
);
288 PyTypeObject
*inittype(const PyTypeObject
*tyskel
,
289 PyTypeObject
*base
, PyTypeObject
*meta
)
291 PyTypeObject
*ty
= newtype(meta
, tyskel
, 0);
292 if (base
) { ty
->tp_base
= base
; Py_INCREF(base
); }
293 ty
->tp_flags
|= Py_TPFLAGS_HEAPTYPE
;
298 /*----- Populating modules ------------------------------------------------*/
300 PyObject
*mkexc(PyObject
*mod
, PyObject
*base
,
301 const char *name
, const PyMethodDef
*mm
)
303 PyObject
*nameobj
= 0;
309 if ((dict
= PyDict_New()) == 0) goto fail
;
312 while (mm
->ml_name
) {
313 if ((func
= PyCFunction_NewEx((/*unconst*/ PyMethodDef
*)mm
,
315 (meth
= PyMethod_New(func
, 0, exc
)) == 0 ||
316 PyDict_SetItemString(dict
, mm
->ml_name
, meth
))
318 Py_DECREF(func
); func
= 0;
319 Py_DECREF(meth
); meth
= 0;
324 if ((nameobj
= TEXT_FORMAT("%s.%s", PyModule_GetName(mod
), name
)) == 0 ||
325 (exc
= PyErr_NewException(TEXT_STR(nameobj
), base
, dict
)) == 0)
341 void setconstants(PyObject
*mod
, const struct nameval
*c
)
348 if (u
<= LONG_MAX
) x
= PyInt_FromLong(u
);
349 else if (c
->f
&CF_SIGNED
) x
= PyInt_FromLong(-1 - (long)(ULONG_MAX
- u
));
350 else x
= PyLong_FromUnsignedLong(u
);
351 PyModule_AddObject(mod
, (/*unconst*/ char *)c
->name
, x
);
356 /*----- Submodules --------------------------------------------------------*/
358 static PyMethodDef
*global_methods
;
359 static size_t nmethods
, methodsz
;
361 void addmethods(const PyMethodDef
*m
)
363 size_t n
, want
, newsz
;
365 for (n
= 0; m
[n
].ml_name
; n
++);
366 want
= nmethods
+ n
+ 1;
367 if (want
> methodsz
) {
368 newsz
= methodsz ?
2*methodsz
: 16;
369 while (want
> newsz
) newsz
*= 2;
371 global_methods
= PyObject_Malloc(newsz
*sizeof(PyMethodDef
));
373 global_methods
= PyObject_Realloc(global_methods
,
374 newsz
*sizeof(PyMethodDef
));
375 assert(global_methods
);
378 memcpy(global_methods
+ nmethods
, m
, n
*sizeof(PyMethodDef
));
380 global_methods
[nmethods
].ml_name
= 0;
383 PyMethodDef
*donemethods(void) { return (global_methods
); }
385 /*----- Low-level Python interface ----------------------------------------*/
387 static PyObject
*meth__set_home_module(PyObject
*me
, PyObject
*arg
)
391 if (!PyArg_ParseTuple(arg
, "O!:_set_home_module", &PyModule_Type
, &mod
))
393 Py_XDECREF(home_module
); home_module
= mod
; Py_INCREF(home_module
);
397 static const PyMethodDef methods
[] = {
398 #define METHNAME(func) meth_##func
399 METH (_set_home_module
, "_set_home_module(MOD)")
404 void pyke_core_pyinit(void) { addmethods(methods
); }
405 void pyke_core_pyinsert(PyObject
*mod
) { ; }
407 /*----- That's all, folks -------------------------------------------------*/