X-Git-Url: https://git.distorted.org.uk/~mdw/pyke/blobdiff_plain/68ec53f32a22b0c38e3b7b53e03edf747c49e416..43806eefdf9d9400425ff74ec25ec461b09dc0ef:/catacomb-python.h diff --git a/catacomb-python.h b/catacomb-python.h index 286dd9e..c272b53 100644 --- a/catacomb-python.h +++ b/catacomb-python.h @@ -1,13 +1,11 @@ /* -*-c-*- * - * $Id$ - * * Definitions for Catacomb bindings * * (c) 2004 Straylight/Edgeware */ -/*----- Licensing notice --------------------------------------------------* +/*----- Licensing notice --------------------------------------------------* * * This file is part of the Python interface to Catacomb. * @@ -15,12 +13,12 @@ * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. - * + * * Catacomb/Python is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * + * * You should have received a copy of the GNU General Public License * along with Catacomb/Python; if not, write to the Free Software Foundation, * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. @@ -35,19 +33,20 @@ /*----- Header files ------------------------------------------------------*/ +#define PY_SSIZE_T_CLEAN + #include #include #include -#undef ULLONG_MAX -#undef ULONG_LONG_MAX - #include #include #include #include +#include #include +#include #include #include @@ -66,9 +65,12 @@ #include #include #include +#include #include #include #include +#include +#include #include #include @@ -77,6 +79,7 @@ #include #include #include +#include #include #include @@ -86,6 +89,10 @@ #include #include #include +#include +#include +#include +#include #include #include @@ -128,7 +135,6 @@ #define VALERR(str) EXCERR(PyExc_ValueError, str) #define TYERR(str) EXCERR(PyExc_TypeError, str) #define ZDIVERR(str) EXCERR(PyExc_ZeroDivisionError, str) -#define SYNERR(str) EXCERR(PyExc_SyntaxError, str) #define SYSERR(str) EXCERR(PyExc_SystemError, str) #define NIERR(str) EXCERR(PyExc_NotImplementedError, str) #define INDEXERR(idx) do { \ @@ -139,7 +145,7 @@ PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); \ goto end; \ } while (0) -#define PGENERR do { pgenerr(); goto end; } while (0) +#define PGENERR(exc) do { pgenerr(exc); goto end; } while (0) #define CONVFUNC(ty, cty, ext) \ int conv##ty(PyObject *o, void *p) \ @@ -152,16 +158,24 @@ return (0); \ } +#if PY_VERSION_HEX < 0x02050000 /* Compatibility hack */ +# define ht_name name +# define ht_type type +#endif + #define root_pytype 0 #define type_pytype &PyType_Type -#define INITTYPE(ty, base) do { \ +#define INITTYPE_META(ty, base, meta) do { \ ty##_pytype_skel.tp_base = base##_pytype; \ - ty##_pytype = inittype(&ty##_pytype_skel); \ + ty##_pytype = inittype(&ty##_pytype_skel, meta##_pytype); \ } while (0) +#define INITTYPE(ty, base) INITTYPE_META(ty, base, type) + +extern PyObject *home_module; -#define INSERT(name, ob) do { \ +#define INSERT(name, ob) do { \ PyObject *_o = (PyObject *)(ob); \ - Py_INCREF(_o); \ + Py_INCREF(_o); \ PyModule_AddObject(mod, name, _o); \ } while (0) @@ -183,6 +197,7 @@ { #name, ty, offsetof(MEMBERSTRUCT, name), f, doc }, #define MODULES(_) \ + _(util) \ _(bytestring) _(buffer) \ _(rand) _(algorithms) _(pubkey) _(pgen) \ _(mp) _(field) _(ec) _(group) \ @@ -233,16 +248,45 @@ extern int convulong(PyObject *, void *); DOUINTSZ(DECL_CONVU_) extern int convmpw(PyObject *, void *); extern int convuint(PyObject *, void *); +extern int convk64(PyObject *, void *); extern int convszt(PyObject *, void *); extern int convbool(PyObject *, void *); extern PyObject *abstract_pynew(PyTypeObject *, PyObject *, PyObject *); extern PyObject *getbool(int); -#define DECL_GETU_(n) extern PyObject *getu##n(uint##n); -DOUINTSZ(DECL_GETU_) +extern PyObject *getulong(unsigned long); +extern PyObject *getk64(kludge64); extern void *newtype(PyTypeObject *, const PyTypeObject *, const char *); -extern PyObject * mkexc(PyObject *, PyObject *, const char *, PyMethodDef *); -extern PyTypeObject *inittype(PyTypeObject *); +struct excinfo { PyObject *ty, *val, *tb; }; +#define EXCINFO_INIT { 0, 0, 0 } + +extern PyObject *mkexc(PyObject *, PyObject *, const char *, PyMethodDef *); +#define INIT_EXCINFO(exc) do { \ + struct excinfo *_exc = (exc); _exc->ty = _exc->val = _exc->tb = 0; \ +} while (0) +#define RELEASE_EXCINFO(exc) do { \ + struct excinfo *_exc = (exc); \ + Py_XDECREF(_exc->ty); _exc->ty = 0; \ + Py_XDECREF(_exc->val); _exc->val = 0; \ + Py_XDECREF(_exc->tb); _exc->tb = 0; \ +} while (0) +#define STASH_EXCINFO(exc) do { \ + struct excinfo *_exc = (exc); \ + PyErr_Fetch(&_exc->ty, &_exc->val, &_exc->tb); \ + PyErr_NormalizeException(&_exc->ty, &_exc->val, &_exc->tb); \ +} while (0) +#define RESTORE_EXCINFO(exc) do { \ + struct excinfo *_exc = (exc); \ + PyErr_Restore(_exc->ty, _exc->val, _exc->tb); \ + _exc->ty = _exc->val = _exc->tb = 0; \ +} while (0) +extern void report_lost_exception(struct excinfo *, const char *, ...); +extern void report_lost_exception_v(struct excinfo *, const char *, va_list); +extern void stash_exception(struct excinfo *, const char *, ...); +extern void restore_exception(struct excinfo *, const char *, ...); + +extern void typeready(PyTypeObject *); +extern PyTypeObject *inittype(PyTypeObject *, PyTypeObject *); extern void addmethods(const PyMethodDef *); extern PyMethodDef *donemethods(void); @@ -256,24 +300,28 @@ extern PyMethodDef *donemethods(void); #define GMAP_KWMETHDECL(func, doc) \ extern PyObject *gmapmeth_##func(PyObject *, PyObject *, PyObject *); -#define GMAP_DOMETHODS(METH, KWMETH) \ +#define GMAP_DOROMETHODS(METH, KWMETH) \ METH (has_key, "D.has_key(KEY) -> BOOL") \ METH (keys, "D.keys() -> LIST") \ METH (values, "D.values() -> LIST") \ METH (items, "D.items() -> LIST") \ METH (iterkeys, "D.iterkeys() -> ITER") \ METH (itervalues, "D.itervalues() -> ITER") \ - METH (iteritems, "D.iteritems() -> ITER") \ - METH (clear, "D.clear()") \ + METH (iteritems, "D.iteritems() -> ITER") \ KWMETH(get, "D.get(KEY, [default = None]) -> VALUE") \ + +#define GMAP_DOMETHODS(METH, KWMETH) \ + GMAP_DOROMETHODS(METH, KWMETH) \ + METH (clear, "D.clear()") \ KWMETH(setdefault, "D.setdefault(K, [default = None]) -> VALUE") \ KWMETH(pop, "D.pop(KEY, [default = ]) -> VALUE") \ METH (popitem, "D.popitem() -> (KEY, VALUE)") \ METH (update, "D.update(MAP)") GMAP_DOMETHODS(GMAP_METHDECL, GMAP_KWMETHDECL) +#define GMAP_ROMETHODS GMAP_DOROMETHODS(GMAP_METH, GMAP_KWMETH) #define GMAP_METHODS GMAP_DOMETHODS(GMAP_METH, GMAP_KWMETH) -extern int gmap_pysize(PyObject *); +extern Py_ssize_t gmap_pysize(PyObject *); extern PySequenceMethods gmap_pysequence; extern PyMethodDef gmap_pymethods[]; @@ -296,8 +344,8 @@ extern PyTypeObject *gf_pytype; #define MP_PYCHECK(o) PyObject_TypeCheck((o), mp_pytype) #define GF_PYCHECK(o) PyObject_TypeCheck((o), gf_pytype) -extern mp *mp_frompylong(PyLongObject *); -extern PyLongObject *mp_topylong(mp *); +extern mp *mp_frompylong(PyObject *); +extern PyObject *mp_topylong(mp *); extern mp *tomp(PyObject *); extern mp *getmp(PyObject *); extern int convmp(PyObject *, void *); @@ -305,10 +353,11 @@ extern mp *getgf(PyObject *); extern int convgf(PyObject *, void *); extern PyObject *mp_pywrap(mp *); extern PyObject *gf_pywrap(mp *); +extern long mphash(mp *); extern mp *mp_frompyobject(PyObject *, int); extern PyObject *mp_topystring(mp *, int, const char *, const char *, const char *); -extern int mp_tolong_checked(mp *, long *); +extern int mp_tolong_checked(mp *, long *, int); /*----- Abstract fields ---------------------------------------------------*/ @@ -323,14 +372,13 @@ extern PyTypeObject *fe_pytype; #define FE_FOBJ(o) ((PyObject *)(o)->ob_type) #define FE_X(o) (((fe_pyobj *)(o))->x) extern PyObject *fe_pywrap(PyObject *, mp *); -extern mp *getfe(field *, PyObject *); typedef struct fe_pyobj { PyObject_HEAD field *f; mp *x; } fe_pyobj; - + extern PyTypeObject *field_pytype; extern PyTypeObject *primefield_pytype; extern PyTypeObject *niceprimefield_pytype; @@ -386,7 +434,7 @@ typedef struct ecinfo_pyobj { ec_info ei; PyObject *cobj; } ecinfo_pyobj; - + extern PyTypeObject *ecinfo_pytype; #define ECINFO_PYCHECK(o) PyObject_TypeCheck((o), ecinfo_pytype) #define ECINFO_EI(o) (&((ecinfo_pyobj *)(o))->ei) @@ -552,7 +600,7 @@ extern PyObject *gmac_pywrap(PyObject *, gmac *, unsigned); extern int convgmac(PyObject *, void *); /*----- Key generation ----------------------------------------------------*/ - + typedef struct pfilt_pyobj { PyObject_HEAD pfilt f; @@ -575,9 +623,15 @@ extern PyTypeObject *pgev_pytype; #define PGEV_PYCHECK(o) PyObject_TypeCheck(o, pgev_pytype) #define PGEV_PG(o) (&((pgev_pyobj *)(o))->pg) +typedef struct pypgev { + pgev ev; + PyObject *obj; + struct excinfo *exc; +} pypgev; + extern int convpgev(PyObject *, void *); -extern void droppgev(pgev *); -extern void pgenerr(void); +extern void droppgev(pypgev *); +extern void pgenerr(struct excinfo *exc); /*----- That's all, folks -------------------------------------------------*/