Пример #1
0
static PyObject *
trie_get_approximate(trieobject *mp, PyObject *args)
{
    unsigned char *key;
    int k;
    PyObject *py_list;

    if (!PyArg_ParseTuple(args, "si:get_approximate", &key, &k))
	return NULL;

    if(!(py_list = PyList_New(0)))
	return NULL;
    Trie_get_approximate(mp->trie, key, k, 
			 _trie_get_approximate_helper, (void *)py_list);
    if(PyErr_Occurred()) {
	Py_DECREF(py_list);
	return NULL;
    }
    return py_list;
}
Пример #2
0
static void
_trie_get_approximate_helper(const char *key, const void *value,
                             const int mismatches, void *data)
{
    /* Append a tuple of (key, value) to data, which is a PyList. */
    PyObject *py_list = (PyObject *)data,
              *py_value = (PyObject *)value,
               *py_key,
               *py_tuple,
               *py_mismatches;

    if(PyErr_Occurred())
        return;

#ifdef IS_PY3K
    if(!(py_key = PyUnicode_FromFormat(key)))
#else
    if(!(py_key = PyString_FromString(key)))
#endif
        return;
#ifdef IS_PY3K
    if(!(py_mismatches = PyLong_FromLong(mismatches))) {
#else
    if(!(py_mismatches = PyInt_FromLong(mismatches))) {
#endif
        Py_DECREF(py_key);
        return;
    }
    Py_INCREF(py_value);

    if(!(py_tuple = PyTuple_New(3))) {
        Py_DECREF(py_key);
        Py_DECREF(py_mismatches);
        Py_DECREF(py_value);
        return;
    }
    PyTuple_SetItem(py_tuple, 0, py_key);
    PyTuple_SetItem(py_tuple, 1, py_value);
    PyTuple_SetItem(py_tuple, 2, py_mismatches);
    PyList_Append(py_list, py_tuple);
    Py_DECREF(py_tuple);
}

static PyObject *
trie_get_approximate(trieobject *mp, PyObject *args)
{
    const char *key;
    int k;
    PyObject *py_list;

    if (!PyArg_ParseTuple(args, "si:get_approximate", &key, &k))
        return NULL;

    if(!(py_list = PyList_New(0)))
        return NULL;
    Trie_get_approximate(mp->trie, key, k,
                         _trie_get_approximate_helper, (void *)py_list);
    if(PyErr_Occurred()) {
        Py_DECREF(py_list);
        return NULL;
    }
    return py_list;
}

static long
trie_nohash(PyObject *self)
{
    PyErr_SetString(PyExc_TypeError, "trie objects are unhashable");
    return -1;
}