algorithms.c: Use `RETURN_ME' rather than doing the job by hand.
[catacomb-python] / catacomb.c
index 7d3cba7..d994347 100644 (file)
@@ -44,7 +44,6 @@ PyObject *mexp_common(PyObject *me, PyObject *arg,
 
   if (PyTuple_GET_SIZE(arg) == 1)
     arg = PyTuple_GET_ITEM(arg, 0);
-  Py_INCREF(arg);
   if (!PySequence_Check(arg)) TYERR("not a sequence");
   n = PySequence_Size(arg); if (n < 0) goto end;
   if (!n) { z = id(me); goto end; }
@@ -93,7 +92,6 @@ end:
       drop(vv);
     xfree(v);
   }
-  Py_DECREF(arg);
   return (z);
 }
 
@@ -128,7 +126,7 @@ static void *thingtab_gmlookup(PyObject *me, PyObject *key, unsigned *f)
 {
   const char *p;
 
-  p = PyString_AsString(key); if (!p) return (0);
+  p = TEXT_STR(key); if (!p) return (0);
   return (sym_find(THINGTAB_T(me), p, -1, 0, f));
 }
 
@@ -139,7 +137,7 @@ static void *thingtab_gmiternext(PyObject *me, void *i)
   { sym_iter *it = i; void *e; SYM_NEXT(it, e); return (e); }
 
 static PyObject *thingtab_gmentrykey(PyObject *me, void *e)
-  { return (PyString_FromString(SYM_NAME(e))); }
+  { return (TEXT_FROMSTR(SYM_NAME(e))); }
 
 static PyObject *thingtab_gmentryvalue(PyObject *me, void *e)
   { PyObject *rc = THING_VAL(e); RETURN_OBJ(rc); }
@@ -321,19 +319,42 @@ static void init_random(void)
 #endif
 }
 
-EXPORT void init_base(void)
+#ifdef PY3
+static PyModuleDef moddef = {
+  PyModuleDef_HEAD_INIT,
+  "catacomb._base",                    /* @m_name@ */
+  "Low-level module for Catacomb bindings.  Use `catacomb' instead.",
+                                       /* @m_doc@ */
+  0,                                   /* @m_size@ */
+  0,                                   /* @m_methods@ */
+  0,                                   /* @m_slots@ */
+  0,                                   /* @m_traverse@ */
+  0,                                   /* @m_clear@ */
+  0                                    /* @m_free@ */
+};
+#endif
+
+EXPORT PyMODINIT_FUNC PY23(init_base, PyInit__base)(void)
 {
   PyObject *mod;
 
-  modname = PyString_FromString("catacomb");
+  modname = TEXT_FROMSTR("catacomb");
   addmethods(methods);
   INIT_MODULES;
   INITTYPE(thingtab, root);
   init_random();
+#ifdef PY3
+  moddef.m_methods = donemethods();
+  mod = PyModule_Create(&moddef);
+#else
   mod = Py_InitModule("catacomb._base", donemethods());
+#endif
   INSERT_MODULES;
   INSERT("_MiscTable", thingtab_pytype);
   INSERT("smallprimes", smallprimes());
+#ifdef PY3
+  return (mod);
+#endif
 }
 
 /*----- That's all, folks -------------------------------------------------*/