*.c: Introduce a new input conversion for binary strings.
[catacomb-python] / mp.c
diff --git a/mp.c b/mp.c
index 99a84d1..0da3a5e 100644 (file)
--- a/mp.c
+++ b/mp.c
@@ -723,13 +723,12 @@ STOREOP(storeb2c, 2c)
   static PyObject *ty##meth_frombuf(PyObject *me, PyObject *arg)       \
   {                                                                    \
     buf b;                                                             \
-    char *p;                                                           \
-    Py_ssize_t sz;                                                     \
+    struct bin in;                                                     \
     PyObject *rc = 0;                                                  \
     mp *x;                                                             \
                                                                        \
-    if (!PyArg_ParseTuple(arg, "s#:frombuf", &p, &sz)) goto end;       \
-    buf_init(&b, p, sz);                                               \
+    if (!PyArg_ParseTuple(arg, "O&:frombuf", convbin, &in)) goto end;  \
+    buf_init(&b, (/*unconst*/ void *)in.p, in.sz);                     \
     if ((x = buf_getmp(&b)) == 0) VALERR("malformed data");            \
     rc = Py_BuildValue("(NN)", ty##_pywrap(x),                         \
                       bytestring_pywrapbuf(&b));                       \
@@ -815,10 +814,9 @@ static PyObject *mpmeth_fibonacci(PyObject *me, PyObject *arg)
 #define LOADOP(pre, name)                                              \
   static PyObject *pre##meth_##name(PyObject *me, PyObject *arg)       \
   {                                                                    \
-    char *p;                                                           \
-    Py_ssize_t len;                                                    \
-    if (!PyArg_ParseTuple(arg, "s#:" #name, &p, &len)) return (0);     \
-    return (pre##_pywrap(mp_##name(MP_NEW, p, len)));                  \
+    struct bin in;                                                     \
+    if (!PyArg_ParseTuple(arg, "O&:" #name, convbin, &in)) return (0); \
+    return (pre##_pywrap(mp_##name(MP_NEW, in.p, in.sz)));             \
   }
 LOADOP(mp, loadl)
 LOADOP(mp, loadb)