5 * (c) 2005 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the Python interface to mLib.
12 * mLib/Python is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * mLib/Python is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with mLib/Python; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 /*----- Header files ------------------------------------------------------*/
31 #include <mLib/atom.h>
32 #include <mLib/assoc.h>
33 #include <mLib/dstr.h>
38 /*----- Data structures ---------------------------------------------------*/
40 typedef struct entry {
45 /*----- Static variables --------------------------------------------------*/
47 static assoc_table obarray;
49 /*----- Main code ---------------------------------------------------------*/
51 PyObject *atom_pywrap(atom *a)
57 e = assoc_find(&obarray, a, sizeof(entry), &f);
59 ao = PyObject_NEW(atom_pyobj, &atom_pytype); ao->a = a;
60 e->a = (PyObject *)ao;
63 /* If we just created the new `Atom' object, we get a reference, which
64 * belongs to the `obarray' table. So when we return it to the caller, we
65 * always need to increment it.
70 PyObject *atom_pyintern(PyObject *x)
79 a = atom_gensym(ATOM_GLOBAL);
80 else if (!TEXT_CHECK(x))
81 { PyErr_SetString(PyExc_TypeError, "string wanted"); return (0); }
83 TEXT_PTRLEN(x, &p, &n); if (!p) return (0);
84 a = atom_nintern(ATOM_GLOBAL, p, n);
86 return (atom_pywrap(a));
89 static void atom_pydealloc(PyObject *me)
90 { fprintf(stderr, "ouch! freeing atom\n"); abort(); }
92 static PyObject *atom_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
94 PyObject *name = Py_None;
95 static const char *const kwlist[] = { "name", 0 };
97 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O:new", KWLIST, &name))
99 return (atom_pyintern(name));
102 static PyObject *aget_name(PyObject *me, void *hunoz)
103 { return (TEXT_FROMSTRLEN(ATOM_NAME(ATOM_A(me)), ATOM_LEN(ATOM_A(me)))); }
105 static PyObject *aget_internedp(PyObject *me, void *hunoz)
107 PyObject *rc = (ATOM_A(me)->f & ATOMF_GENSYM) ? Py_False : Py_True;
111 static PyGetSetDef atom_pygetset[] = {
112 #define GETSETNAME(op, name) a##op##_##name
113 GET (name, "A.name -> NAME")
114 GET (internedp, "A.internedp -> BOOL")
119 static PyObject *atom_pyrichcompare(PyObject *x, PyObject *y, int op)
124 case Py_EQ: rc = (x == y) ? Py_True : Py_False; break;
125 case Py_NE: rc = (x != y) ? Py_True : Py_False; break;
127 PyErr_SetString(PyExc_TypeError, "atoms are unordered");
133 static PyObject *atom_pyrepr(PyObject *me)
135 PyObject *s, *sr = 0;
141 if ((s = aget_name(me, 0)) == 0 ||
142 (sr = PyObject_Repr(s)) == 0)
145 { PyErr_SetString(PyExc_TypeError, "string wanted"); goto done; }
146 TEXT_PTRLEN(sr, &p, &n); if (!p) goto done;
147 dstr_puts(&d, "Atom(");
150 rc = TEXT_FROMSTRLEN(d.buf, d.len);
158 static Py_hash_t atom_pyhash(PyObject *me)
159 { Py_hash_t h = ATOM_HASH(ATOM_A(me)); if (h == -1) h = -2; return (h); }
161 PyTypeObject atom_pytype = {
162 PyVarObject_HEAD_INIT(0, 0) /* Header */
163 "mLib.Atom", /* @tp_name@ */
164 sizeof(atom_pyobj), /* @tp_basicsize@ */
165 0, /* @tp_itemsize@ */
167 atom_pydealloc, /* @tp_dealloc@ */
169 0, /* @tp_getattr@ */
170 0, /* @tp_setattr@ */
171 0, /* @tp_compare@ */
172 atom_pyrepr, /* @tp_repr@ */
173 0, /* @tp_as_number@ */
174 0, /* @tp_as_sequence@ */
175 0, /* @tp_as_mapping@ */
176 atom_pyhash, /* @tp_hash@ */
178 atom_pyrepr, /* @tp_str@ */
179 0, /* @tp_getattro@ */
180 0, /* @tp_setattro@ */
181 0, /* @tp_as_buffer@ */
182 Py_TPFLAGS_DEFAULT, /* @tp_flags@ */
187 0, /* @tp_traverse@ */
189 atom_pyrichcompare, /* @tp_richcompare@ */
190 0, /* @tp_weaklistoffset@ */
192 0, /* @tp_iternexr@ */
193 0, /* @tp_methods@ */
194 0, /* @tp_members@ */
195 atom_pygetset, /* @tp_getset@ */
198 0, /* @tp_descr_get@ */
199 0, /* @tp_descr_set@ */
200 0, /* @tp_dictoffset@ */
202 PyType_GenericAlloc, /* @tp_alloc@ */
203 atom_pynew, /* @tp_new@ */
208 void atom_pysetup(void)
209 { assoc_create(&obarray); PyType_Ready(&atom_pytype); }
211 /*----- That's all, folks -------------------------------------------------*/