From 54f5bcf99ff1594cf2d65067061bf2e8a160bb23 Mon Sep 17 00:00:00 2001 From: Mark Wooding Date: Sun, 24 Nov 2019 22:50:56 +0000 Subject: [PATCH] ec.c, catacomb/__init__.py: Return tail from `parse'; provide `fromstring'. `ECCurve.parse' was inconsistent with the established nomenclature. Generally, `parse' reads a prefix of some given string, returning the denoted value and the unprocessed remainder of the input string; `fromstring' does the same, except that it returns only the value, and raises an exception if the remainder of the string is not only whitespace. Fix `ECCurve.parse' (which unfortunately is an incompatible change), and add missing `fromstring' methods to `ECCurve' and `ECInfo'. --- catacomb/__init__.py | 4 ++++ ec.c | 2 +- t/t-ec.py | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/catacomb/__init__.py b/catacomb/__init__.py index 725f6d2..08ec3d7 100644 --- a/catacomb/__init__.py +++ b/catacomb/__init__.py @@ -444,6 +444,8 @@ class _tmp: pp.pretty(me.a); pp.text(','); pp.breakable() pp.pretty(me.b) pp.end_group(ind, ')') + def fromstring(str): return _checkend(ECCurve.parse(str)) + fromstring = staticmethod(fromstring) def frombuf(me, s): return ecpt.frombuf(me, s) def fromraw(me, s): @@ -508,6 +510,8 @@ class _tmp: h ^= hash(me.curve) h ^= 2*hash(me.G) & 0xffffffff return h + def fromstring(str): return _checkend(ECInfo.parse(str)) + fromstring = staticmethod(fromstring) def group(me): return ECGroup(me) _augment(ECInfo, _tmp) diff --git a/ec.c b/ec.c index 95b59ef..f5a3aca 100644 --- a/ec.c +++ b/ec.c @@ -986,7 +986,7 @@ static PyObject *ecmeth_parse(PyObject *me, PyObject *arg) if (!PyArg_ParseTuple(arg, "s:parse", &p)) goto end; qd.p = p; qd.e = 0; if ((c = ec_curveparse(&qd)) == 0) VALERR(qd.e); - rc = eccurve_pywrap(0, c); + rc = Py_BuildValue("(Ns)", eccurve_pywrap(0, c), qd.p); end: return (rc); } diff --git a/t/t-ec.py b/t/t-ec.py index 01b5f27..d854216 100644 --- a/t/t-ec.py +++ b/t/t-ec.py @@ -251,7 +251,7 @@ class TestCurves (T.GenericTestMixin): def test_tinycurves(me): me._test_curve(C.ECInfo(E, 2*P, 13, 2), checkfail = True) - ei, _ = C.ECInfo.parse("binpoly: 0x13; bin: 0x01, 0x08; 0x02, 0x0c: 5*4") + ei = C.ECInfo.fromstring("binpoly: 0x13; bin: 0x01, 0x08; 0x02, 0x0c: 5*4") me._test_curve(ei, checkfail = True) TestCurves.generate_testcases((name, C.eccurves[name]) for name in -- 2.11.0