X-Git-Url: https://git.distorted.org.uk/~mdw/catacomb-python/blobdiff_plain/3aa33042dc760937bb9da54c09f7c668f00eb241..ecc5edeeffc8726f9b14708ae4cb6183a2e80da6:/bytestring.c diff --git a/bytestring.c b/bytestring.c index 61627f4..28d62b7 100644 --- a/bytestring.c +++ b/bytestring.c @@ -34,9 +34,9 @@ PyTypeObject *bytestring_pytype; -PyObject *bytestring_pywrap(const void *p, size_t n) +static PyObject *dowrap(PyTypeObject *ty, const void *p, size_t n) { - PyStringObject *x = PyObject_NewVar(PyStringObject, bytestring_pytype, n); + PyStringObject *x = (PyStringObject *)ty->tp_alloc(ty, n); if (p) memcpy(x->ob_sval, p, n); x->ob_sval[n] = 0; #ifdef CACHE_HASH @@ -46,8 +46,22 @@ PyObject *bytestring_pywrap(const void *p, size_t n) return ((PyObject *)x); } +PyObject *bytestring_pywrap(const void *p, size_t n) + { return (dowrap(bytestring_pytype, p, n)); } + PyObject *bytestring_pywrapbuf(buf *b) - { return bytestring_pywrap(BCUR(b), BLEFT(b)); } + { return (dowrap(bytestring_pytype, BCUR(b), BLEFT(b))); } + +static PyObject *bytestring_pynew(PyTypeObject *ty, + PyObject *arg, PyObject *kw) +{ + const char *p; + int n; + static char *kwlist[] = { "data", 0 }; + if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", kwlist, &p, &n)) + return (0); + return (dowrap(ty, p, n)); +} #define BINOP(name, op) \ static PyObject *bytestring_py##name(PyObject *x, PyObject *y) { \ @@ -117,7 +131,7 @@ static PyNumberMethods bytestring_pynumber = { static PyBufferProcs bytestring_pybuffer; static PyTypeObject bytestring_pytype_skel = { - PyObject_HEAD_INIT(&PyType_Type) 0, /* Header */ + PyObject_HEAD_INIT(0) 0, /* Header */ "catacomb.ByteString", /* @tp_name@ */ 0, /* @tp_basicsize@ */ 0, /* @tp_itemsize@ */ @@ -160,7 +174,7 @@ static PyTypeObject bytestring_pytype_skel = { 0, /* @tp_dictoffset@ */ 0, /* @tp_init@ */ PyType_GenericAlloc, /* @tp_alloc@ */ - 0, /* @tp_new@ */ + bytestring_pynew, /* @tp_new@ */ 0, /* @tp_free@ */ 0 /* @tp_is_gc@ */ };