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)
56 e = assoc_find(&obarray, a, sizeof(entry), &f);
58 atom_pyobj *ao = PyObject_NEW(atom_pyobj, &atom_pytype);
60 e->a = (PyObject *)ao;
65 PyObject *atom_pyintern(PyObject *x)
74 a = atom_gensym(ATOM_GLOBAL);
76 if (PyObject_AsReadBuffer(x, &p, &n)) return (0);
77 a = atom_nintern(ATOM_GLOBAL, p, n);
79 return (atom_pywrap(a));
82 static void atom_pydealloc(PyObject *me)
83 { fprintf(stderr, "ouch! freeing atom\n"); abort(); }
85 static PyObject *atom_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
88 static char *kwlist[] = { "name", 0 };
90 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O:new", kwlist, &name))
92 return (atom_pyintern(name));
95 static PyObject *aget_name(PyObject *me, void *hunoz)
97 return (PyString_FromStringAndSize(ATOM_NAME(ATOM_A(me)),
98 ATOM_LEN(ATOM_A(me))));
101 static PyObject *aget_internedp(PyObject *me, void *hunoz)
103 PyObject *rc = (ATOM_A(me)->f & ATOMF_GENSYM) ? Py_False : Py_True;
107 static PyGetSetDef atom_pygetset[] = {
108 #define GETSETNAME(op, name) a##op##_##name
109 GET (name, "A.name -> NAME")
110 GET (internedp, "A.internedp -> BOOL")
115 static PyObject *atom_pyrichcompare(PyObject *x, PyObject *y, int op)
120 case Py_EQ: rc = (x == y) ? Py_True : Py_False; break;
121 case Py_NE: rc = (x != y) ? Py_True : Py_False; break;
123 PyErr_SetString(PyExc_TypeError, "atoms are unordered");
129 static PyObject *atom_pyrepr(PyObject *me)
131 PyObject *s, *sr = 0;
137 if ((s = aget_name(me, 0)) == 0 ||
138 (sr = PyObject_Repr(s)) == 0 ||
139 PyString_AsStringAndSize(sr, &p, &n))
141 dstr_puts(&d, "Atom(");
144 rc = PyString_FromStringAndSize(d.buf, d.len);
152 static long atom_pyhash(PyObject *me)
153 { long h = ATOM_HASH(ATOM_A(me)); if (h == -1) h = -2; return (h); }
155 PyTypeObject atom_pytype = {
156 PyObject_HEAD_INIT(0) 0, /* Header */
157 "mLib.Atom", /* @tp_name@ */
158 sizeof(atom_pyobj), /* @tp_basicsize@ */
159 0, /* @tp_itemsize@ */
161 atom_pydealloc, /* @tp_dealloc@ */
163 0, /* @tp_getattr@ */
164 0, /* @tp_setattr@ */
165 0, /* @tp_compare@ */
166 atom_pyrepr, /* @tp_repr@ */
167 0, /* @tp_as_number@ */
168 0, /* @tp_as_sequence@ */
169 0, /* @tp_as_mapping@ */
170 atom_pyhash, /* @tp_hash@ */
172 atom_pyrepr, /* @tp_str@ */
173 0, /* @tp_getattro@ */
174 0, /* @tp_setattro@ */
175 0, /* @tp_as_buffer@ */
176 Py_TPFLAGS_DEFAULT, /* @tp_flags@ */
181 0, /* @tp_traverse@ */
183 atom_pyrichcompare, /* @tp_richcompare@ */
184 0, /* @tp_weaklistoffset@ */
186 0, /* @tp_iternexr@ */
187 0, /* @tp_methods@ */
188 0, /* @tp_members@ */
189 atom_pygetset, /* @tp_getset@ */
192 0, /* @tp_descr_get@ */
193 0, /* @tp_descr_set@ */
194 0, /* @tp_dictoffset@ */
196 PyType_GenericAlloc, /* @tp_alloc@ */
197 atom_pynew, /* @tp_new@ */
202 void atom_pysetup(void)
203 { assoc_create(&obarray); PyType_Ready(&atom_pytype); }
205 /*----- That's all, folks -------------------------------------------------*/