chiark / gitweb /
bytestring.c (dowrap): Factor out allocating the bytestring object.
[catacomb-python] / bytestring.c
index 67eb500ec2dbffba733fd015a1e49688cdedcac5..8557e3bc8a05af1ef7fdae92f923e92641909eab 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)); }