static PyObject * trie_has_key(trieobject *mp, PyObject *py_key) { unsigned char *key; int has_key; /* Make sure key is a string. */ if(!PyString_Check(py_key)) { PyErr_SetString(PyExc_TypeError, "key must be a string"); return NULL; } key = (unsigned char *)PyString_AS_STRING(py_key); has_key = Trie_has_key(mp->trie, key); return PyInt_FromLong((long)has_key); }
static PyObject * trie_subscript(trieobject *mp, PyObject *py_key) { const char *key; PyObject *py_value; /* Make sure key is a string. */ #ifdef IS_PY3K if(!PyUnicode_Check(py_key)) { #else if(!PyString_Check(py_key)) { #endif PyErr_SetString(PyExc_TypeError, "key must be a string"); return NULL; } #ifdef IS_PY3K /* TODO - Review next line for buffer usage */ key = PyBytes_AS_STRING(PyUnicode_AsASCIIString(py_key)); #else key = PyString_AS_STRING(py_key); #endif py_value = Trie_get(mp->trie, key); if(py_value == NULL) PyErr_SetString(PyExc_KeyError, key); else Py_INCREF(py_value); return py_value; } static int trie_ass_sub(trieobject *mp, PyObject *py_key, PyObject *py_value) { int result = -1; const char *key; PyObject *py_prev; #ifdef IS_PY3K PyObject* bytes; #endif /* Make sure key is a string. */ #ifdef IS_PY3K if(!PyUnicode_Check(py_key)) { #else if(!PyString_Check(py_key)) { #endif PyErr_SetString(PyExc_TypeError, "key must be a string"); return -1; } #ifdef IS_PY3K bytes = PyUnicode_AsASCIIString(py_key); if(!bytes) { PyErr_SetString(PyExc_TypeError, "key must be an ASCII string"); return -1; } key = PyBytes_AsString(bytes); #else key = PyString_AS_STRING(py_key); #endif /* Check to see whether something already exists at that key. If there's already an object there, then I will have to remove it. */ py_prev = Trie_get(mp->trie, key); if(py_prev) { Py_DECREF(py_prev); } /* The client wants to delete a key from a dictionary. The Trie API doesn't support this, so I will just overwrite it with NULL. */ if(!py_value) { /* If the key doesn't exist, raise a KeyError. */ if(!py_prev) PyErr_SetString(PyExc_KeyError, key); else { Trie_set(mp->trie, key, NULL); result = 0; } } /* The client wants to set a key in the dictionary. */ else { Py_INCREF(py_value); if(Trie_set(mp->trie, key, py_value)) PyErr_SetString(PyExc_AssertionError, "error setting trie"); else result = 0; } #ifdef IS_PY3K Py_DECREF(bytes); #endif return result; } static int trie_contains(trieobject *mp, PyObject* py_key) { int result; #ifdef IS_PY3K PyObject* bytes; #endif const char *key; /* Make sure key is a string. */ #ifdef IS_PY3K if(!PyUnicode_Check(py_key)) { #else if(!PyString_Check(py_key)) { #endif PyErr_SetString(PyExc_TypeError, "key must be a string"); return -1; } #ifdef IS_PY3K bytes = PyUnicode_AsASCIIString(py_key); if(!bytes) { PyErr_SetString(PyExc_TypeError, "key must be an ASCII string"); return -1; } key = PyBytes_AsString(bytes); #else key = PyString_AS_STRING(py_key); #endif result = Trie_has_key(mp->trie, key); #ifdef IS_PY3K Py_DECREF(bytes); #endif return result; } static char has_key__doc__[] = "D.has_key(k) -> 1 if D has a key k, else 0"; static PyObject * trie_has_key(trieobject *mp, PyObject *py_key) { int has_key = trie_contains(mp, py_key); if (has_key==-1) return NULL; #ifdef IS_PY3K return PyLong_FromLong((long)has_key); #else return PyInt_FromLong((long)has_key); #endif }