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 typedef struct entry {
47 /*----- Static variables --------------------------------------------------*/
49 static assoc_table obarray;
51 /*----- Main code ---------------------------------------------------------*/
53 PyObject *atom_pywrap(atom *a)
58 e = assoc_find(&obarray, a, sizeof(entry), &f);
60 atom_pyobj *ao = PyObject_NEW(atom_pyobj, &atom_pytype);
62 e->a = (PyObject *)ao;
67 PyObject *atom_pyintern(PyObject *x)
76 a = atom_gensym(ATOM_GLOBAL);
78 if (PyObject_AsReadBuffer(x, &p, &n)) return (0);
79 a = atom_nintern(ATOM_GLOBAL, p, n);
81 return (atom_pywrap(a));
84 static void atom_pydealloc(PyObject *me)
85 { fprintf(stderr, "ouch! freeing atom\n"); abort(); }
87 static PyObject *atom_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
90 static char *kwlist[] = { "name", 0 };
92 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O:new", kwlist, &name))
94 return (atom_pyintern(name));
97 static PyObject *aget_name(PyObject *me, void *hunoz)
99 return (PyString_FromStringAndSize(ATOM_NAME(ATOM_A(me)),
100 ATOM_LEN(ATOM_A(me))));
103 static PyObject *aget_internedp(PyObject *me, void *hunoz)
105 PyObject *rc = (ATOM_A(me)->f & ATOMF_GENSYM) ? Py_False : Py_True;
109 static PyGetSetDef atom_pygetset[] = {
110 #define GETSETNAME(op, name) a##op##_##name
111 GET (name, "A.name -> NAME")
112 GET (internedp, "A.internedp -> BOOL")
117 static PyObject *atom_pyrichcompare(PyObject *x, PyObject *y, int op)
122 case Py_EQ: rc = (x == y) ? Py_True : Py_False; break;
123 case Py_NE: rc = (x != y) ? Py_True : Py_False; break;
125 PyErr_SetString(PyExc_TypeError, "atoms are unordered");
131 static PyObject *atom_pyrepr(PyObject *me)
133 PyObject *s, *sr = 0;
139 if ((s = aget_name(me, 0)) == 0 ||
140 (sr = PyObject_Repr(s)) == 0 ||
141 PyString_AsStringAndSize(sr, &p, &n))
143 dstr_puts(&d, "Atom(");
146 rc = PyString_FromStringAndSize(d.buf, d.len);
154 static long atom_pyhash(PyObject *me)
155 { long h = ATOM_HASH(ATOM_A(me)); if (h == -1) h = -2; return (h); }
157 PyTypeObject atom_pytype = {
158 PyObject_HEAD_INIT(0) 0, /* Header */
159 "mLib.Atom", /* @tp_name@ */
160 sizeof(atom_pyobj), /* @tp_basicsize@ */
161 0, /* @tp_itemsize@ */
163 atom_pydealloc, /* @tp_dealloc@ */
165 0, /* @tp_getattr@ */
166 0, /* @tp_setattr@ */
167 0, /* @tp_compare@ */
168 atom_pyrepr, /* @tp_repr@ */
169 0, /* @tp_as_number@ */
170 0, /* @tp_as_sequence@ */
171 0, /* @tp_as_mapping@ */
172 atom_pyhash, /* @tp_hash@ */
174 atom_pyrepr, /* @tp_str@ */
175 0, /* @tp_getattro@ */
176 0, /* @tp_setattro@ */
177 0, /* @tp_as_buffer@ */
178 Py_TPFLAGS_DEFAULT, /* @tp_flags@ */
183 0, /* @tp_traverse@ */
185 atom_pyrichcompare, /* @tp_richcompare@ */
186 0, /* @tp_weaklistoffset@ */
188 0, /* @tp_iternexr@ */
189 0, /* @tp_methods@ */
190 0, /* @tp_members@ */
191 atom_pygetset, /* @tp_getset@ */
194 0, /* @tp_descr_get@ */
195 0, /* @tp_descr_set@ */
196 0, /* @tp_dictoffset@ */
198 PyType_GenericAlloc, /* @tp_alloc@ */
199 atom_pynew, /* @tp_new@ */
204 void atom_pysetup(void)
205 { assoc_create(&obarray); PyType_Ready(&atom_pytype); }
207 /*----- That's all, folks -------------------------------------------------*/