From: Mark Wooding Date: Fri, 9 Nov 2018 12:27:07 +0000 (+0000) Subject: bytestring.c (dowrap): Factor out allocating the bytestring object. X-Git-Tag: 1.3.0~18 X-Git-Url: https://git.distorted.org.uk/~mdw/catacomb-python/commitdiff_plain/438b163904f6566f07dd6d80e83e3d5e30691481 bytestring.c (dowrap): Factor out allocating the bytestring object. --- diff --git a/bytestring.c b/bytestring.c index 67eb500..8557e3b 100644 --- a/bytestring.c +++ b/bytestring.c @@ -32,10 +32,10 @@ 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)); }