-# -*-pyrex-*-
-#
-# $Id$
-#
-# Symbol table, using universal hashing
-#
-# (c) 2005 Straylight/Edgeware
-#
+### -*-pyrex-*-
+###
+### Symbol table, using universal hashing
+###
+### (c) 2005 Straylight/Edgeware
+###
-#----- Licensing notice -----------------------------------------------------
-#
-# This file is part of the Python interface to mLib.
-#
-# mLib/Python is free software; you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation; either version 2 of the License, or
-# (at your option) any later version.
-#
-# mLib/Python is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with mLib/Python; if not, write to the Free Software Foundation,
-# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+###----- Licensing notice ---------------------------------------------------
+###
+### This file is part of the Python interface to mLib.
+###
+### mLib/Python is free software; you can redistribute it and/or modify
+### it under the terms of the GNU General Public License as published by
+### the Free Software Foundation; either version 2 of the License, or
+### (at your option) any later version.
+###
+### mLib/Python is distributed in the hope that it will be useful,
+### but WITHOUT ANY WARRANTY; without even the implied warranty of
+### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+### GNU General Public License for more details.
+###
+### You should have received a copy of the GNU General Public License
+### along with mLib/Python; if not, write to the Free Software Foundation,
+### Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
cdef struct _sym_entry:
sym_base _b
PyObject *v
-cdef class SymTable (Mapping):
+cdef class SymTable (_Mapping):
+ """
+ SymTable([DICT], **KW)
+
+ A mapping keyed by strings.
+ """
cdef sym_table _t
- cdef int _init(me) except -1:
+ def __cinit__(me):
sym_create(&me._t)
+ cdef int _init(me) except -1:
return 0
cdef void *_find(me, object key, unsigned *f) except NULL:
- cdef void *p
- cdef int n
- cdef _sym_entry *e
- PyObject_AsReadBuffer(key, &p, &n)
- if f:
- f[0] = 0
+ cdef const char *p
+ cdef Py_ssize_t n
+ cdef _sym_entry *e = NULL
+ TEXT_PTRLEN(key, &p, &n)
+ if not f:
+ e = <_sym_entry *>sym_find(&me._t, <char *>p, n, 0, NULL)
+ if not e:
+ raise KeyError(key)
+ else:
e = <_sym_entry *>sym_find(&me._t, <char *>p, n, PSIZEOF(e), f)
if not f[0]:
e.v = NULL
- else:
- e = <_sym_entry *>sym_find(&me._t, <char *>p, n, 0, NULL)
return <void *>e
cdef object _key(me, void *e):
- return PyString_FromStringAndSize(SYM_NAME(e), SYM_LEN(e))
+ return TEXT_FROMSTRLEN(SYM_NAME(e), SYM_LEN(e))
cdef object _value(me, void *e):
- cdef _sym_entry *ee
- ee = <_sym_entry *>e
+ cdef _sym_entry *ee = <_sym_entry *>e
Py_INCREF(ee.v)
return <object>ee.v
cdef void _setval(me, void *e, object val):
- cdef _sym_entry *ee
- ee = <_sym_entry *>e
+ cdef _sym_entry *ee = <_sym_entry *>e
if ee.v:
Py_DECREF(ee.v)
- ee.v = <PyObject *>v
+ ee.v = <PyObject *>val
Py_INCREF(ee.v)
cdef void _del(me, void *e):
- cdef _sym_entry *ee
- ee = <_sym_entry *>e
+ cdef _sym_entry *ee = <_sym_entry *>e
if ee.v:
Py_DECREF(ee.v)
sym_remove(&me._t, <void *>ee)
cdef class _SymIter (_MapIterator):
cdef SymTable t
cdef sym_iter i
- def __new__(me, SymTable t):
+ def __cinit__(me, SymTable t not None):
me.t = t
sym_mkiter(&me.i, &me.t._t)
cdef void *_next(me):
return sym_next(&me.i)
-#----- That's all, folks ----------------------------------------------------
+###----- That's all, folks --------------------------------------------------