From 438b163904f6566f07dd6d80e83e3d5e30691481 Mon Sep 17 00:00:00 2001 From: Mark Wooding Date: Fri, 9 Nov 2018 12:27:07 +0000 Subject: [PATCH] bytestring.c (dowrap): Factor out allocating the bytestring object. --- bytestring.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) 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)); } -- 2.11.0