Stamp the module name onto types properly.
[pyke] / util.c
diff --git a/util.c b/util.c
index 86b7aba..717c3d9 100644 (file)
--- a/util.c
+++ b/util.c
@@ -1,13 +1,11 @@
 /* -*-c-*-
  *
- * $Id$
- *
  * Miscellaneous utilities (not Catacomb-specific)
  *
  * (c) 2005 Straylight/Edgeware
  */
 
-/*----- Licensing notice --------------------------------------------------* 
+/*----- Licensing notice --------------------------------------------------*
  *
  * This file is part of the Python interface to Catacomb.
  *
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.
- * 
+ *
  * Catacomb/Python is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
- * 
+ *
  * You should have received a copy of the GNU General Public License
  * along with Catacomb/Python; if not, write to the Free Software Foundation,
  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
 /*----- Conversions -------------------------------------------------------*/
 
-#define GETU_(n)                                                       \
-  PyObject *getu##n(uint##n w)                                         \
-  {                                                                    \
-    if (w <= MASK##n)                                                  \
-      return (PyInt_FromLong(w));                                      \
-    else                                                               \
-      return (PyLong_FromUnsignedLong(w));                             \
-  }
-DOUINTSZ(GETU_)
+PyObject *getulong(unsigned long w)
+{
+  if (w <= LONG_MAX)
+    return (PyInt_FromLong(w));
+  else
+    return (PyLong_FromUnsignedLong(w));
+}
 
 PyObject *getbool(int b)
 {
@@ -62,7 +58,7 @@ int convulong(PyObject *o, void *pp)
 
   if (PyInt_Check(o)) {
     i = PyInt_AS_LONG(o);
-    if (i < 0) TYERR("must be nonnegative");
+    if (i < 0) VALERR("must be nonnegative");
     *p = i;
   } else {
     if ((t = PyNumber_Long(o)) == 0) goto end;
@@ -82,7 +78,7 @@ end:
     uint##n *p = pp;                                                   \
                                                                        \
     if (!convulong(o, &u)) goto end;                                   \
-    if (u > MASK##n) TYERR("out of range");                            \
+    if (u > MASK##n) VALERR("out of range");                           \
     *p = u;                                                            \
     return (1);                                                                \
   end:                                                                 \
@@ -96,7 +92,7 @@ int convuint(PyObject *o, void *pp)
   unsigned *p = pp;
 
   if (!convulong(o, &u)) goto end;
-  if (u > UINT_MAX) TYERR("out of range");
+  if (u > UINT_MAX) VALERR("out of range");
   *p = u;
   return (1);
 end:
@@ -109,7 +105,7 @@ int convmpw(PyObject *o, void *pp)
   unsigned *p = pp;
 
   if (!convulong(o, &u)) goto end;
-  if (u > MPW_MAX) TYERR("out of range");
+  if (u > MPW_MAX) VALERR("out of range");
   *p = u;
   return (1);
 end:
@@ -122,7 +118,7 @@ int convszt(PyObject *o, void *pp)
   size_t *p = pp;
 
   if (!convulong(o, &u)) goto end;
-  if (u > ~(size_t)0) TYERR("out of range");
+  if (u > ~(size_t)0) VALERR("out of range");
   *p = u;
   return (1);
 end:
@@ -147,13 +143,13 @@ void *newtype(PyTypeObject *metaty,
     (PyHeapTypeObject *)_PyObject_GC_Malloc(_PyObject_VAR_SIZE(metaty, 0));
   if (!skel) skel = &emptytype;
   memcpy(ty, skel, sizeof(*skel));
-  if (ty->type.tp_base) Py_INCREF(ty->type.tp_base);
+  if (ty->ht_type.tp_base) Py_INCREF(ty->ht_type.tp_base);
 #define COPY(blah) do {                                                        \
-    if (ty->type.tp_as_##blah) {                                       \
+    if (ty->ht_type.tp_as_##blah) {                                    \
       memcpy(&ty->as_##blah,                                           \
-            ty->type.tp_as_##blah,                                     \
+            ty->ht_type.tp_as_##blah,                                  \
             sizeof(ty->as_##blah));                                    \
-      ty->type.tp_as_##blah = &ty->as_##blah;                          \
+      ty->ht_type.tp_as_##blah = &ty->as_##blah;                       \
     }                                                                  \
   } while (0)
   COPY(number);
@@ -162,21 +158,24 @@ void *newtype(PyTypeObject *metaty,
   COPY(buffer);
 #undef COPY
   if (name)
-    ty->name = PyString_FromString(name);
-  else if (ty->type.tp_name)
-    ty->name = PyString_FromString(ty->type.tp_name);
-  if (ty->name)
-    ty->type.tp_name = PyString_AS_STRING(ty->name);
-  PyObject_INIT(&ty->type, metaty);
+    ty->ht_name = PyString_FromString(name);
+  else if (ty->ht_type.tp_name)
+    ty->ht_name = PyString_FromString(ty->ht_type.tp_name);
+  if (ty->ht_name)
+    ty->ht_type.tp_name = PyString_AS_STRING(ty->ht_name);
+  DISCARD(PyObject_INIT(&ty->ht_type, metaty));
   Py_INCREF(metaty);
   return (ty);
 }
 
 PyTypeObject *inittype(PyTypeObject *tyskel)
 {
+  static PyObject *modname = 0;
   PyTypeObject *ty = newtype(&PyType_Type, tyskel, 0);
+  if (!modname) modname = PyString_FromString("catacomb");
   ty->tp_flags |= Py_TPFLAGS_HEAPTYPE;
   PyType_Ready(ty);
+  PyDict_SetItemString(ty->tp_dict, "__module__", modname);
   return (ty);
 }
 
@@ -280,7 +279,7 @@ static void iter_pydealloc(PyObject *me)
 static PyObject *itemiter_pynext(PyObject *me)
 {
   PyObject *k = 0, *v = 0, *rc = 0;
-  
+
   if ((k = PyIter_Next(ITER_I(me))) != 0 &&
       (v = PyObject_GetItem(ITER_MAP(me), k)) != 0)
     rc = Py_BuildValue("(OO)", k, v);
@@ -339,7 +338,7 @@ static PyTypeObject itemiter_pytype_skel = {
 static PyObject *valiter_pynext(PyObject *me)
 {
   PyObject *k = 0, *rc = 0;
-  
+
   if ((k = PyIter_Next(ITER_I(me))) != 0)
     rc = PyObject_GetItem(ITER_MAP(me), k);
   Py_XDECREF(k);
@@ -405,7 +404,7 @@ PySequenceMethods gmap_pysequence = {
   PyMapping_HasKey,                    /* @sq_contains@ */
   0,                                   /* @sq_inplace_concat@ */
   0                                    /* @sq_inplace_repeat@ */
-};  
+};
 
 int gmap_pysize(PyObject *me)
 {
@@ -456,7 +455,7 @@ PyObject *gmapmeth_values(PyObject *me, PyObject *arg)
       (l = PyList_New(0)) == 0 ||
       (i = PyObject_GetIter(me)) == 0)
     goto done;
-  while ((k = PyIter_Next(i)) != 0) {  
+  while ((k = PyIter_Next(i)) != 0) {
     if ((v = PyObject_GetItem(me, k)) == 0 ||
        PyList_Append(l, v))
       err = -1;
@@ -636,13 +635,13 @@ PyMethodDef gmap_pymethods[] = {
 
 /*----- Initialization ----------------------------------------------------*/
 
-void util_init(void)
+void util_pyinit(void)
 {
   INITTYPE(itemiter, root);
   INITTYPE(valiter, root);
 }
 
-void util_insert(PyObject *mod)
+void util_pyinsert(PyObject *mod)
 {
   INSERT("ItemIter", itemiter_pytype);
   INSERT("ValueIter", valiter_pytype);