chiark / gitweb /
ec.c: Accept and discard parentheses around a point when parsing.
authorMark Wooding <mdw@distorted.org.uk>
Sun, 24 Nov 2019 22:46:33 +0000 (22:46 +0000)
committerMark Wooding <mdw@distorted.org.uk>
Sat, 11 Apr 2020 11:49:31 +0000 (12:49 +0100)
The `str' function returns parentheses around the coordinates, so it
seems unfair to reject parentheses on input.

ec.c
t/t-ec.py

diff --git a/ec.c b/ec.c
index 770e71d72969353b0827f4e43ed6498a14ff139c..094bd007e0657b52e53378a912418876d0326bbf 100644 (file)
--- a/ec.c
+++ b/ec.c
@@ -333,11 +333,15 @@ static PyObject *epmeth_parse(PyObject *me, PyObject *arg)
   char *p;
   qd_parse qd;
   PyObject *rc = 0;
+  int paren;
   ec pp = EC_INIT;
 
   if (!PyArg_ParseTuple(arg, "s:parse", &p)) goto end;
   qd.p = p; qd.e = 0;
+  qd_skipspc(&qd); paren = qd_delim(&qd, '(');
   if (!ec_ptparse(&qd, &pp)) VALERR(qd.e);
+  qd_skipspc(&qd); if (paren && !qd_delim(&qd, ')'))
+    { EC_DESTROY(&pp); VALERR("missing `)'"); }
   rc = Py_BuildValue("(Ns)", ecpt_pywrapout(me, &pp), qd.p);
 end:
   return (rc);
@@ -513,6 +517,7 @@ static int ecptxl_1(ec_curve *c, ec *p, PyObject *x)
   mp *xx = 0;
   Py_ssize_t n;
   qd_parse qd;
+  int paren;
 
   Py_XINCREF(x);
   if (!x || x == Py_None)
@@ -522,7 +527,10 @@ static int ecptxl_1(ec_curve *c, ec *p, PyObject *x)
     goto fix;
   } else if (TEXT_CHECK(x)) {
     qd.p = TEXT_PTR(x); qd.e = 0;
+    qd_skipspc(&qd); paren = qd_delim(&qd, '(');
     if (!ec_ptparse(&qd, p)) VALERR(qd.e);
+    qd_skipspc(&qd); if (paren && !qd_delim(&qd, ')'))
+      { EC_DESTROY(p); VALERR("missing `)'"); }
     qd_skipspc(&qd); if (!qd_eofp(&qd)) VALERR("junk at eof");
     goto fix;
   } else if (c && (xx = tomp(x)) != 0) {
index f11da7d211ed633db7fcc6100a89acbf8db57f81..01b5f274ff4a6d7b534d562282c01fa5d502e9de 100644 (file)
--- a/t/t-ec.py
+++ b/t/t-ec.py
@@ -93,7 +93,9 @@ class TestCurvelessPoints (U.TestCase):
     me.assertRaises(ValueError, C.ECPt.frombuf, C.bytes("0001fe000201"))
 
     ## String conversion and parsing.
+    me.assertEqual(str(P), "(254, 291)")
     me.assertEqual(C.ECPt.parse("254, 291)"), (P, ")"))
+    me.assertEqual(C.ECPt.parse("(254, 291)"), (P, ""))
     me.assertRaises(ValueError, C.ECPt.parse, "(254, 291")
 
 ###--------------------------------------------------------------------------