*.c: Use `PyVarObject_HEAD_INIT' to initialize type object headers.
[catacomb-python] / bytestring.c
index 82d117f..8e1d19a 100644 (file)
@@ -30,7 +30,7 @@
 
 /*----- Main code ---------------------------------------------------------*/
 
-PyTypeObject *bytestring_pytype;
+static PyTypeObject *bytestring_pytype;
 
 static PyObject *empty, *bytev[256];
 
@@ -96,11 +96,11 @@ end:
   return (0);
 }
 
-static PyObject *meth__ByteString_zero(PyObject *me, PyObject *arg)
+static PyObject *bymeth_zero(PyObject *me, PyObject *arg)
 {
   size_t sz;
   PyObject *rc = 0;
-  if (!PyArg_ParseTuple(arg, "OO&:zero", &me, convszt, &sz)) goto end;
+  if (!PyArg_ParseTuple(arg, "O&:zero", convszt, &sz)) goto end;
   rc = bytestring_pywrap(0, sz);
   memset(PyString_AS_STRING(rc), 0, sz);
 end:
@@ -192,7 +192,7 @@ static PyObject *bytestring_pyslice(PyObject *me, Py_ssize_t i, Py_ssize_t j)
   if (j < 0) j = 0;
   else if (j > n) j = n;
   if (j < i) i = j = 0;
-  if (i == 0 && j == n && me->ob_type == bytestring_pytype)
+  if (i == 0 && j == n && Py_TYPE(me) == bytestring_pytype)
     { Py_INCREF(me); rc = me; goto end; }
   rc = bytestring_pywrap(PyString_AS_STRING(me) + i, j - i);
 end:
@@ -265,7 +265,14 @@ BINOP(xor, ^)
   }
 UNOP(not, ~)
 
-static PyNumberMethods bytestring_pynumber = {
+static const PyMethodDef bytestring_pymethods[] = {
+#define METHNAME(name) bymeth_##name
+  SMTH (zero,          "zero(N) -> 0000...00")
+#undef METHNAME
+  { 0 }
+};
+
+static const PyNumberMethods bytestring_pynumber = {
   0,                                   /* @nb_add@ */
   0,                                   /* @nb_subtract@ */
   0,                                   /* @nb_multiply@ */
@@ -291,7 +298,7 @@ static PyNumberMethods bytestring_pynumber = {
   0,                                   /* @nb_hex@ */
 };
 
-static PySequenceMethods bytestring_pysequence = {
+static const PySequenceMethods bytestring_pysequence = {
   0,                                   /* @sq_length@ */
   bytestring_pyconcat,                 /* @sq_concat@ */
   bytestring_pyrepeat,                 /* @sq_repeat@ */
@@ -304,16 +311,14 @@ static PySequenceMethods bytestring_pysequence = {
   0,                                   /* @sq_inplace_repeat@ */
 };
 
-static PyMappingMethods bytestring_pymapping = {
+static const PyMappingMethods bytestring_pymapping = {
   0,                                   /* @mp_length@ */
   bytestring_pysubscript,              /* @mp_subscript@ */
   0,                                   /* @mp_ass_subscript@ */
 };
 
-static PyBufferProcs bytestring_pybuffer;
-
-static PyTypeObject bytestring_pytype_skel = {
-  PyObject_HEAD_INIT(0) 0,             /* Header */
+static const PyTypeObject bytestring_pytype_skel = {
+  PyVarObject_HEAD_INIT(0, 0)          /* Header */
   "ByteString",                                /* @tp_name@ */
   0,                                   /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
@@ -324,21 +329,21 @@ static PyTypeObject bytestring_pytype_skel = {
   0,                                   /* @tp_setattr@ */
   0,                                   /* @tp_compare@ */
   0,                                   /* @tp_repr@ */
-  &bytestring_pynumber,                        /* @tp_as_number@ */
-  &bytestring_pysequence,              /* @tp_as_sequence@ */
-  &bytestring_pymapping,               /* @tp_as_mapping@ */
+  PYNUMBER(bytestring),                        /* @tp_as_number@ */
+  PYSEQUENCE(bytestring),              /* @tp_as_sequence@ */
+  PYMAPPING(bytestring),               /* @tp_as_mapping@ */
   0,                                   /* @tp_hash@ */
   0,                                   /* @tp_call@ */
   0,                                   /* @tp_str@ */
   0,                                   /* @tp_getattro@ */
   0,                                   /* @tp_setattro@ */
-  &bytestring_pybuffer,                        /* @tp_as_buffer@ */
+  0,                                   /* @tp_as_buffer@ */
   Py_TPFLAGS_DEFAULT |                 /* @tp_flags@ */
     Py_TPFLAGS_CHECKTYPES |
     Py_TPFLAGS_BASETYPE,
 
   /* @tp_doc@ */
-"ByteString(STR): byte string class.",
+  "ByteString(STR): byte string class.",
 
   0,                                   /* @tp_traverse@ */
   0,                                   /* @tp_clear@ */
@@ -346,7 +351,7 @@ static PyTypeObject bytestring_pytype_skel = {
   0,                                   /* @tp_weaklistoffset@ */
   0,                                   /* @tp_iter@ */
   0,                                   /* @tp_iternext@ */
-  0,                                   /* @tp_methods@ */
+  PYMETHODS(bytestring),               /* @tp_methods@ */
   0,                                   /* @tp_members@ */
   0,                                   /* @tp_getset@ */
   0,                                   /* @tp_base@ */
@@ -363,10 +368,9 @@ static PyTypeObject bytestring_pytype_skel = {
 
 /*----- Initialization ----------------------------------------------------*/
 
-static PyMethodDef methods[] = {
+static const PyMethodDef methods[] = {
 #define METHNAME(func) meth_##func
-  METH  (ctstreq,              "ctstreq(S, T) -> BOOL")
-  METH (_ByteString_zero,      "zero(N) -> 0000...00")
+  METH (ctstreq,       "ctstreq(S, T) -> BOOL")
 #undef METHNAME
   { 0 }
 };