*.c: Separate string function calls according to text/binary usage.
[catacomb-python] / mp.c
diff --git a/mp.c b/mp.c
index 0da3a5e..a664627 100644 (file)
--- a/mp.c
+++ b/mp.c
@@ -102,11 +102,11 @@ mp *mp_frompyobject(PyObject *o, int radix)
 {
   mp *x;
 
-  if (PyString_Check(o)) {
+  if (TEXT_CHECK(o)) {
     mptext_stringctx sc;
     mp *x;
-    sc.buf = PyString_AS_STRING(o);
-    sc.lim = sc.buf + PyString_GET_SIZE(o);
+    size_t sz;
+    TEXT_PTRLEN(o, sc.buf, sz); sc.lim = sc.buf + sz;
     x = mp_read(MP_NEW, radix, &mptext_stringops, &sc);
     if (!x) return (0);
     if (sc.buf < sc.lim) { MP_DROP(x); return (0); }
@@ -128,8 +128,7 @@ PyObject *mp_topystring(mp *x, int radix, const char *xpre,
   size_t postlen = post ? strlen(post) : 0;
   char *p;
   MP_COPY(x);
-  o = PyString_FromStringAndSize(0, len + 1 + xprelen + prelen + postlen);
-  p = PyString_AS_STRING(o);
+  TEXT_PREPAREWRITE(o, p, len + 1 + xprelen + prelen + postlen);
   sc.buf = p;
   if (xpre) { memcpy(sc.buf, xpre, xprelen); sc.buf += xprelen; }
   if (MP_NEGP(x)) { *sc.buf++ = '-'; x = mp_neg(x, x); }
@@ -138,7 +137,7 @@ PyObject *mp_topystring(mp *x, int radix, const char *xpre,
   mp_write(x, radix, &mptext_stringops, &sc);
   if (post) { memcpy(sc.buf, post, postlen); sc.buf += postlen; }
   MP_DROP(x);
-  _PyString_Resize(&o, sc.buf - p);
+  TEXT_DONEWRITE(o, sc.buf - p);
   return (o);
 }
 
@@ -709,7 +708,7 @@ end:
       if (!len) len = 1;                                               \
     }                                                                  \
     rc = bytestring_pywrap(0, len);                                    \
-    mp_##name(MP_X(me), PyString_AS_STRING(rc), len);                  \
+    mp_##name(MP_X(me), BIN_PTR(rc), len);                             \
   end:                                                                 \
     return (rc);                                                       \
   }
@@ -749,10 +748,10 @@ static PyObject *mpmeth_tobuf(PyObject *me)
   x = MP_X(me);
   n = mp_octets(x) + 3;
   rc = bytestring_pywrap(0, n);
-  buf_init(&b, PyString_AS_STRING(rc), n);
+  buf_init(&b, BIN_PTR(rc), n);
   buf_putmp(&b, x);
   assert(BOK(&b));
-  _PyString_Resize(&rc, BLEN(&b));
+  BIN_SETLEN(rc, BLEN(&b));
   return (rc);
 }