bytestring.c (dowrap): Factor out allocating the bytestring object.
authorMark Wooding <mdw@distorted.org.uk>
Fri, 9 Nov 2018 12:27:07 +0000 (12:27 +0000)
committerMark Wooding <mdw@distorted.org.uk>
Sat, 10 Nov 2018 01:30:58 +0000 (01:30 +0000)
bytestring.c

index 67eb500..8557e3b 100644 (file)
 
 PyTypeObject *bytestring_pytype;
 
-static PyObject *dowrap(PyTypeObject *ty, const void *p, size_t n)
+static PyObject *allocate(PyTypeObject *ty, size_t n)
 {
-  PyStringObject *x = (PyStringObject *)ty->tp_alloc(ty, n);
-  if (p) memcpy(x->ob_sval, p, n);
+  PyStringObject *x;
+  x = (PyStringObject *)ty->tp_alloc(ty, n);
   x->ob_sval[n] = 0;
 #if defined(CACHE_HASH) || PY_VERSION_HEX >= 0x02030000
   x->ob_shash = -1;
@@ -44,6 +44,15 @@ static PyObject *dowrap(PyTypeObject *ty, const void *p, size_t n)
   return ((PyObject *)x);
 }
 
+static PyObject *dowrap(PyTypeObject *ty, const void *p, size_t n)
+{
+  PyObject *x;
+
+  x = allocate(ty, n);
+  if (p) memcpy(PyString_AS_STRING(x), p, n);
+  return (x);
+}
+
 PyObject *bytestring_pywrap(const void *p, size_t n)
   { return (dowrap(bytestring_pytype, p, n)); }