@@@ cython and python3
[mLib-python] / atom-base.c
index e1e38f9..1078e2e 100644 (file)
@@ -1,7 +1,5 @@
 /* -*-c-*-
  *
- * $Id$
- *
  * Atom stuff
  *
  * (c) 2005 Straylight/Edgeware
@@ -52,15 +50,20 @@ static assoc_table obarray;
 
 PyObject *atom_pywrap(atom *a)
 {
-  entry *e;
+  entry *e = 0;
+  atom_pyobj *ao;
   unsigned f = 0;
 
   e = assoc_find(&obarray, a, sizeof(entry), &f);
   if (!f) {
-    atom_pyobj *ao = PyObject_NEW(atom_pyobj, &atom_pytype);
-    ao->a = a;
+    ao = PyObject_NEW(atom_pyobj, &atom_pytype); ao->a = a;
     e->a = (PyObject *)ao;
   }
+
+  /* If we just created the new `Atom' object, we get a reference, which
+   * belongs to the `obarray' table.  So when we return it to the caller, we
+   * always need to increment it.
+   */
   RETURN_OBJ(e->a);
 }
 
@@ -68,14 +71,16 @@ PyObject *atom_pyintern(PyObject *x)
 {
   atom *a;
   const void *p;
-  int n;
+  Py_ssize_t n;
 
   if (ATOM_PYCHECK(x))
     RETURN_OBJ(x);
   if (x == Py_None)
     a = atom_gensym(ATOM_GLOBAL);
+  else if (!TEXT_CHECK(x))
+    { PyErr_SetString(PyExc_TypeError, "string wanted"); return (0); }
   else {
-    if (PyObject_AsReadBuffer(x, &p, &n)) return (0);
+    TEXT_PTRLEN(x, &p, &n); if (!p) return (0);
     a = atom_nintern(ATOM_GLOBAL, p, n);
   }
   return (atom_pywrap(a));
@@ -86,19 +91,16 @@ static void atom_pydealloc(PyObject *me)
 
 static PyObject *atom_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
 {
-  PyObject *name;
-  static char *kwlist[] = { "name", 0 };
+  PyObject *name = Py_None;
+  static const char *const kwlist[] = { "name", 0 };
 
-  if (!PyArg_ParseTupleAndKeywords(arg, kw, "O:new", kwlist, &name))
+  if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O:new", KWLIST, &name))
     return (0);
   return (atom_pyintern(name));
 }
 
 static PyObject *aget_name(PyObject *me, void *hunoz)
-{
-  return (PyString_FromStringAndSize(ATOM_NAME(ATOM_A(me)),
-                                    ATOM_LEN(ATOM_A(me))));
-}
+  { return (TEXT_FROMSTRLEN(ATOM_NAME(ATOM_A(me)), ATOM_LEN(ATOM_A(me)))); }
 
 static PyObject *aget_internedp(PyObject *me, void *hunoz)
 {
@@ -133,17 +135,19 @@ static PyObject *atom_pyrepr(PyObject *me)
   PyObject *s, *sr = 0;
   PyObject *rc = 0;
   char *p;
-  int n;
+  Py_ssize_t n;
   dstr d = DSTR_INIT;
 
   if ((s = aget_name(me, 0)) == 0 ||
-      (sr = PyObject_Repr(s)) == 0 ||
-      PyString_AsStringAndSize(sr, &p, &n))
+      (sr = PyObject_Repr(s)) == 0)
     goto done;
+  if (!TEXT_CHECK(sr))
+    { PyErr_SetString(PyExc_TypeError, "string wanted"); goto done; }
+  TEXT_PTRLEN(sr, &p, &n); if (!p) goto done;
   dstr_puts(&d, "Atom(");
   dstr_putm(&d, p, n);
   dstr_puts(&d, ")");
-  rc = PyString_FromStringAndSize(d.buf, d.len);
+  rc = TEXT_FROMSTRLEN(d.buf, d.len);
 done:
   Py_XDECREF(s);
   Py_XDECREF(sr);
@@ -151,11 +155,11 @@ done:
   return (rc);
 }
 
-static long atom_pyhash(PyObject *me)
-  { long h = ATOM_HASH(ATOM_A(me)); if (h == -1) h = -2; return (h); }
+static Py_hash_t atom_pyhash(PyObject *me)
+  { Py_hash_t h = ATOM_HASH(ATOM_A(me)); if (h == -1) h = -2; return (h); }
 
 PyTypeObject atom_pytype = {
-  PyObject_HEAD_INIT(0) 0,             /* Header */
+  PyVarObject_HEAD_INIT(0, 0)          /* Header */
   "mLib.Atom",                         /* @tp_name@ */
   sizeof(atom_pyobj),                  /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */