X-Git-Url: https://git.distorted.org.uk/~mdw/catacomb-python/blobdiff_plain/be17c8c27ee7ae2e14fe34bb517484a11b1cf300..3317491a2bc28732f805c8521777aafa52038793:/key.c diff --git a/key.c b/key.c index 91f8920..2d1e2ed 100644 --- a/key.c +++ b/key.c @@ -51,7 +51,7 @@ static PyObject *kxmeth___init__(PyObject *me, PyObject *arg) if (PyObject_SetAttrString(me, "err", x)) goto end; Py_DECREF(x); x = 0; - x = PyString_FromString(key_strerror(err)); if (!x) goto end; + x = TEXT_FROMSTR(key_strerror(err)); if (!x) goto end; if (PyObject_SetAttrString(me, "errstring", x)) goto end; Py_DECREF(x); x = 0; @@ -87,9 +87,9 @@ static PyObject *kxmeth___str__(PyObject *me, PyObject *arg) if (err >= 0 && err < N(tab)) errtag = tab[err]; else errtag = ""; if ((x = PyObject_GetAttrString(me, "errstring")) == 0 || - (errstr = PyString_AsString(x)) == 0) + (errstr = TEXT_STR(x)) == 0) goto done; - rc = PyString_FromFormat("%s (%ld): %s", errtag, -err, errstr); + rc = TEXT_FORMAT("%s (%ld): %s", errtag, -err, errstr); done: Py_XDECREF(x); @@ -181,7 +181,7 @@ static int convfilter(PyObject *x, void *p) int err; int rc = 0; - if ((fs = PyString_AsString(x)) != 0) { + if ((fs = TEXT_STR(x)) != 0) { if ((err = key_readflags(fs, &end, &f->f, &f->m)) != 0) KEYERR(err); if (*end) @@ -220,7 +220,7 @@ static int convflags(PyObject *x, void *p) return (1); else { PyErr_Clear(); - if ((fs = PyString_AsString(x)) != 0) { + if ((fs = TEXT_STR(x)) != 0) { if ((err = key_readflags(fs, &end, f, 0)) != 0) KEYERR(err); if (*end) @@ -261,7 +261,7 @@ static PyObject *kdmeth_writeflags(PyObject *me, PyObject *arg) if (!PyArg_ParseTuple(arg, "O&:key_writeflags", convuint, &f)) return (0); key_writeflags(f, &d); - rc = PyString_FromStringAndSize(d.buf, d.len); + rc = TEXT_FROMSTRLEN(d.buf, d.len); dstr_destroy(&d); return (rc); } @@ -338,7 +338,7 @@ static PyObject *kdmeth_write(PyObject *me, PyObject *arg, PyObject *kw) convfilter, &f)) return (0); key_write(KEYDATA_KD(me), &d, &f); - rc = PyString_FromStringAndSize(d.buf, d.len); + rc = TEXT_FROMSTRLEN(d.buf, d.len); dstr_destroy(&d); return (rc); } @@ -795,7 +795,7 @@ end: } static PyObject *kdsget_str(PyObject *me, void *hunoz) - { return (PyString_FromString(KEYDATA_KD(me)->u.p)); } + { return (TEXT_FROMSTR(KEYDATA_KD(me)->u.p)); } static const PyGetSetDef keydatastr_pygetset[] = { #define GETSETNAME(op, name) kds##op##_##name @@ -939,7 +939,7 @@ static void *keydatastruct_gmlookup(PyObject *me, PyObject *k, unsigned *f) key_struct *ks; assert((kd->e&KF_ENCMASK) == KENC_STRUCT); - if ((tag = PyString_AsString(k)) == 0) return (0); + if ((tag = TEXT_STR(k)) == 0) return (0); if (f) { key_split(&kd); KEYDATA_KD(me) = kd; } ks = sym_find(&kd->u.s, tag, -1, f ? sizeof(key_struct) : 0, f); if (ks && f && !*f) ks->k = 0; @@ -958,7 +958,7 @@ static void *keydatastruct_gmiternext(PyObject *me, void *i) { return (sym_next(i)); } static PyObject *keydatastruct_gmentrykey(PyObject *me, void *e) - { key_struct *ks = e; return (PyString_FromString(SYM_NAME(ks))); } + { key_struct *ks = e; return (TEXT_FROMSTR(SYM_NAME(ks))); } static PyObject *keydatastruct_gmentryvalue(PyObject *me, void *e) { @@ -1035,7 +1035,7 @@ static PyObject *keydatastruct_pynew(PyTypeObject *ty, if ((it = PyObject_GetIter(sub)) == 0) goto end; while ((name = PyIter_Next(it)) != 0) { - if ((p = PyString_AsString(name)) == 0 || + if ((p = TEXT_STR(name)) == 0 || (val = PyObject_GetItem(sub, name)) == 0) goto end; if (!KEYDATA_PYCHECK(val)) @@ -1119,7 +1119,7 @@ static const PyTypeObject keydatastruct_pytype_skel = { static void *keyattrs_gmlookup(PyObject *me, PyObject *k, unsigned *f) { - char *name = PyString_AsString(k); + char *name = TEXT_STR(k); key_attr *a = 0; if (!name) goto end; @@ -1137,10 +1137,10 @@ static void *keyattrs_gmiternext(PyObject *me, void *i) { return (sym_next(i)); } static PyObject *keyattrs_gmentrykey(PyObject *me, void *e) - { return (PyString_FromString(SYM_NAME(e))); } + { return (TEXT_FROMSTR(SYM_NAME(e))); } static PyObject *keyattrs_gmentryvalue(PyObject *me, void *e) - { return (PyString_FromString(((key_attr *)e)->p)); } + { return (TEXT_FROMSTR(((key_attr *)e)->p)); } static int keyattrs_gmsetentry(PyObject *me, void *e, PyObject *val) { @@ -1149,8 +1149,8 @@ static int keyattrs_gmsetentry(PyObject *me, void *e, PyObject *val) Py_ssize_t n; int rc = -1; - if (!PyString_Check(val)) TYERR("expected string"); - p = PyString_AS_STRING(val); n = PyString_GET_SIZE(val); + if (!TEXT_CHECK(val)) TYERR("expected string"); + TEXT_PTRLEN(val, p, n); if (n > 255) VALERR("attribute too long"); if (memchr(p, 0, n)) VALERR("attribute must not contain nul"); if (a->p) xfree(a->p); @@ -1299,6 +1299,21 @@ static void key_pydealloc(PyObject *me) FREEOBJ(me); } +static Py_hash_t key_pyhash(PyObject *me) + { return ((Py_hash_t)KEY_K(me)); } + +static PyObject *key_pyrichcompare(PyObject *me, PyObject *you, int op) +{ + if (!KEY_PYCHECK(you)) RETURN_NOTIMPL; + switch (op) { + case Py_EQ: return (getbool(KEY_K(me) == KEY_K(you))); + case Py_NE: return (getbool(KEY_K(me) == KEY_K(you))); + default: TYERR("ordering makes no sense"); + } +end: + return (0); +} + static PyObject *kmeth_delete(PyObject *me) { int err; @@ -1331,27 +1346,21 @@ end: return (0); } -static PyObject *kmeth_extract(PyObject *me, PyObject *arg, PyObject *kw) +static PyObject *kmeth_extractline(PyObject *me, PyObject *arg, PyObject *kw) { key_filter f = { 0, 0 }; - PyObject *file; - PyObject *nameobj; - char *name; - FILE *fp; - static const char *const kwlist[] = { "file", "filter", 0 }; - - if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!|O&:extract", KWLIST, - &PyFile_Type, &file, - convfilter, &f) || - (fp = PyFile_AsFile(file)) == 0 || - (nameobj = PyFile_Name(file)) == 0 || - (name = PyString_AsString(nameobj)) == 0) + dstr d = DSTR_INIT; + PyObject *rc = 0; + static const char *const kwlist[] = { "filter", 0 }; + + if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:extract", KWLIST, + convfilter, &f)) goto end; - if (key_extract(KEY_KF(me), KEY_K(me), fp, &f)) - OSERR(name); - RETURN_ME; + key_extractline(KEY_KF(me), KEY_K(me), &d, &f); + rc = TEXT_FROMSTRLEN(d.buf, d.len); end: - return (0); + dstr_destroy(&d); + return (rc); } static PyObject *kmeth_fingerprint(PyObject *me, @@ -1372,7 +1381,7 @@ static PyObject *kget_id(PyObject *me, void *hunoz) static PyObject *kget_file(PyObject *me, void *hunoz) { RETURN_OBJ(KEY_KFOBJ(me)); } static PyObject *kget_type(PyObject *me, void *hunoz) - { return (PyString_FromString(KEY_K(me)->type)); } + { return (TEXT_FROMSTR(KEY_K(me)->type)); } static PyObject *kget_exptime(PyObject *me, void *hunoz) { return (getulong(KEY_K(me)->exp)); } static PyObject *kget_deltime(PyObject *me, void *hunoz) @@ -1443,7 +1452,7 @@ static PyObject *kget_fulltag(PyObject *me, void *hunoz) PyObject *rc; key_fulltag(KEY_K(me), &d); - rc = PyString_FromStringAndSize(d.buf, d.len); + rc = TEXT_FROMSTRLEN(d.buf, d.len); dstr_destroy(&d); return (rc); } @@ -1451,7 +1460,7 @@ static PyObject *kget_fulltag(PyObject *me, void *hunoz) static PyObject *kget_tag(PyObject *me, void *hunoz) { if (!KEY_K(me)->tag) RETURN_NONE; - return (PyString_FromString(KEY_K(me)->tag)); + return (TEXT_FROMSTR(KEY_K(me)->tag)); } static int kset_tag(PyObject *me, PyObject *x, void *hunoz) { @@ -1459,7 +1468,7 @@ static int kset_tag(PyObject *me, PyObject *x, void *hunoz) char *tag; if (!x || x == Py_None) tag = 0; - else if ((tag = PyString_AsString(x)) == 0) goto end; + else if ((tag = TEXT_STR(x)) == 0) goto end; if ((err = key_settag(KEY_KF(me), KEY_K(me), tag)) != 0) KEYERR(err); return (0); end: @@ -1469,7 +1478,7 @@ end: static PyObject *kget_comment(PyObject *me, void *hunoz) { if (!KEY_K(me)->c) RETURN_NONE; - return (PyString_FromString(KEY_K(me)->c)); + return (TEXT_FROMSTR(KEY_K(me)->c)); } static int kset_comment(PyObject *me, PyObject *x, void *hunoz) { @@ -1477,7 +1486,7 @@ static int kset_comment(PyObject *me, PyObject *x, void *hunoz) char *c; if (!x || x == Py_None) c = 0; - else if ((c = PyString_AsString(x)) == 0) goto end; + else if ((c = TEXT_STR(x)) == 0) goto end; if ((err = key_setcomment(KEY_KF(me), KEY_K(me), c)) != 0) KEYERR(err); return (0); end: @@ -1489,7 +1498,7 @@ static const PyMethodDef key_pymethods[] = { NAMETH(delete, "KEY.delete()") NAMETH(expire, "KEY.expire()") METH (used, "KEY.used(TIME)") - KWMETH(extract, "KEY.extract(FILE, [filter = ])") + KWMETH(extractline, "KEY.extractline([filter = ])") KWMETH(fingerprint, "KEY.fingerprint(HASH, [filter = '-secret'])") #undef METHNAME { 0 } @@ -1527,7 +1536,7 @@ static const PyTypeObject key_pytype_skel = { 0, /* @tp_as_number@ */ 0, /* @tp_as_sequence@ */ 0, /* @tp_as_mapping@ */ - 0, /* @tp_hash@ */ + key_pyhash, /* @tp_hash@ */ 0, /* @tp_call@ */ 0, /* @tp_str@ */ 0, /* @tp_getattro@ */ @@ -1541,7 +1550,7 @@ static const PyTypeObject key_pytype_skel = { 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ - 0, /* @tp_richcompare@ */ + key_pyrichcompare, /* @tp_richcompare@ */ 0, /* @tp_weaklistoffset@ */ 0, /* @tp_iter@ */ 0, /* @tp_iternext@ */ @@ -1572,7 +1581,7 @@ static key *bytag(PyObject *me, PyObject *tagobj) k = key_byid(KEYFILE_KF(me), id); else { PyErr_Clear(); - if ((tag = PyString_AsString(tagobj)) == 0) + if ((tag = TEXT_STR(tagobj)) == 0) goto end; k = key_bytag(KEYFILE_KF(me), tag); } @@ -1619,7 +1628,7 @@ static void pythonreporter(const char *file, int line, if (ri->stop) return; - if (!ri->func) + if (ri->func == Py_None) key_moan(file, line, msg, 0); else if ((res = PyObject_CallFunction(ri->func, "sis", file, line, msg)) == 0) @@ -1631,7 +1640,7 @@ static void pythonreporter(const char *file, int line, static PyObject *keyfile_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) { - struct reportinfo ri = { 0, 0 }; + struct reportinfo ri = { Py_None, 0 }; char *file = 0; unsigned how = KOPEN_READ; keyfile_pyobj *rc = 0; @@ -1641,7 +1650,7 @@ static PyObject *keyfile_pynew(PyTypeObject *ty, if (!PyArg_ParseTupleAndKeywords(arg, kw, "s|iO:new", KWLIST, &file, &how, &ri.func)) goto end; - if (ri.func && !PyCallable_Check(ri.func)) + if (ri.func != Py_None && !PyCallable_Check(ri.func)) TYERR("reporter function not callable"); if ((rc = (keyfile_pyobj *)ty->tp_alloc(ty, 0)) == 0) goto end; @@ -1686,27 +1695,20 @@ end: return (0); } -static PyObject *kfmeth_merge(PyObject *me, PyObject *arg, PyObject *kw) +static PyObject *kfmeth_mergeline(PyObject *me, PyObject *arg, PyObject *kw) { - struct reportinfo ri = { 0, 0 }; - char *name; - PyObject *x = 0; - FILE *fp = 0; - int rc; - static const char *const kwlist[] = { "file", "report", 0 }; + struct reportinfo ri = { Py_None, 0 }; + const char *file, *line; + int lno, rc; + static const char *const kwlist[] = { "name", "lno", "line", "report", 0 }; Py_XINCREF(arg); Py_XINCREF(kw); - if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!|O:merge", KWLIST, - &PyFile_Type, &x, &ri.func)) + if (!PyArg_ParseTupleAndKeywords(arg, kw, "sis|O:merge", KWLIST, + &file, &lno, &line, &ri.func)) goto end; - if (ri.func && !PyCallable_Check(ri.func)) + if (ri.func != Py_None && !PyCallable_Check(ri.func)) TYERR("reporter function not callable"); - if ((fp = PyFile_AsFile(x)) == 0) - goto end; - x = PyFile_Name(x); - if ((name = PyString_AsString(x)) == 0) - goto end; - rc = key_merge(KEYFILE_KF(me), name, fp, pythonreporter, &ri); + rc = key_mergeline(KEYFILE_KF(me), file, lno, line, pythonreporter, &ri); if (ri.stop) goto end; if (rc != 0) @@ -1806,7 +1808,7 @@ end: } static PyObject *kfget_name(PyObject *me, void *hunoz) - { return (PyString_FromString(KEYFILE_KF(me)->name)); } + { return (TEXT_FROMSTR(KEYFILE_KF(me)->name)); } static PyObject *kfget_modifiedp(PyObject *me, void *hunoz) { return (getbool(KEYFILE_KF(me)->f & KF_MODIFIED)); } static PyObject *kfget_writep(PyObject *me, void *hunoz) @@ -1817,7 +1819,8 @@ static PyObject *kfget_filep(PyObject *me, void *hunoz) static const PyMethodDef keyfile_pymethods[] = { #define METHNAME(func) kfmeth_##func NAMETH(save, "KF.save()") - KWMETH(merge, "KF.merge(FILE, [report = ])") + KWMETH(mergeline, "KF.mergeline(NAME, LNO, LINE, " + "[report = ])") KWMETH(newkey, "KF.newkey(ID, TYPE, [exptime = KEXP_FOREVER]) " "-> KEY") METH (byid, "KF.byid(KEYID) -> KEY|None")