7 * (c) 2005 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the Python interface to mLib.
14 * mLib/Python is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * mLib/Python is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with mLib/Python; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Header files ------------------------------------------------------*/
33 #include <mLib/atom.h>
34 #include <mLib/assoc.h>
35 #include <mLib/dstr.h>
40 /*----- Data structures ---------------------------------------------------*/
42 static PyTypeObject atom_pytype;
44 typedef struct entry {
49 /*----- Static variables --------------------------------------------------*/
51 static assoc_table obarray;
53 /*----- Main code ---------------------------------------------------------*/
55 PyObject *atom_pywrap(atom *a)
60 e = assoc_find(&obarray, a, sizeof(entry), &f);
62 atom_pyobj *ao = PyObject_NEW(atom_pyobj, &atom_pytype);
64 e->a = (PyObject *)ao;
69 PyObject *atom_pyintern(PyObject *x)
78 a = atom_gensym(ATOM_GLOBAL);
80 if (PyObject_AsReadBuffer(x, &p, &n)) return (0);
81 a = atom_nintern(ATOM_GLOBAL, p, n);
83 return (atom_pywrap(a));
86 static void atom_pydealloc(PyObject *me)
87 { fprintf(stderr, "ouch! freeing atom\n"); abort(); }
89 static PyObject *atom_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
92 static char *kwlist[] = { "name", 0 };
94 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O:new", kwlist, &name))
96 return (atom_pyintern(name));
99 static PyObject *aget_name(PyObject *me, void *hunoz)
101 return (PyString_FromStringAndSize(ATOM_NAME(ATOM_A(me)),
102 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 ||
143 PyString_AsStringAndSize(sr, &p, &n))
145 dstr_puts(&d, "Atom(");
148 rc = PyString_FromStringAndSize(d.buf, d.len);
156 static long atom_pyhash(PyObject *me)
157 { long h = ATOM_HASH(ATOM_A(me)); if (h == -1) h = -2; return (h); }
159 static PyTypeObject atom_pytype = {
160 PyObject_HEAD_INIT(0) 0, /* Header */
161 "atom.Atom", /* @tp_name@ */
162 sizeof(atom_pyobj), /* @tp_basicsize@ */
163 0, /* @tp_itemsize@ */
165 atom_pydealloc, /* @tp_dealloc@ */
167 0, /* @tp_getattr@ */
168 0, /* @tp_setattr@ */
169 0, /* @tp_compare@ */
170 atom_pyrepr, /* @tp_repr@ */
171 0, /* @tp_as_number@ */
172 0, /* @tp_as_sequence@ */
173 0, /* @tp_as_mapping@ */
174 atom_pyhash, /* @tp_hash@ */
176 atom_pyrepr, /* @tp_str@ */
177 0, /* @tp_getattro@ */
178 0, /* @tp_setattro@ */
179 0, /* @tp_as_buffer@ */
180 Py_TPFLAGS_DEFAULT, /* @tp_flags@ */
185 0, /* @tp_traverse@ */
187 atom_pyrichcompare, /* @tp_richcompare@ */
188 0, /* @tp_weaklistoffset@ */
190 0, /* @tp_iternexr@ */
191 0, /* @tp_methods@ */
192 0, /* @tp_members@ */
193 atom_pygetset, /* @tp_getset@ */
196 0, /* @tp_descr_get@ */
197 0, /* @tp_descr_set@ */
198 0, /* @tp_dictoffset@ */
200 PyType_GenericAlloc, /* @tp_alloc@ */
201 atom_pynew, /* @tp_new@ */
206 PyObject *atom_pystartup(void)
208 assoc_create(&obarray);
209 PyType_Ready(&atom_pytype);
210 return ((PyObject *)&atom_pytype);
213 /*----- That's all, folks -------------------------------------------------*/