Skip to content

Commit

Permalink
Merge pull request #51 from miili/feature/keywords
Browse files Browse the repository at this point in the history
lru.c: adding keywords
  • Loading branch information
amitdev authored Nov 5, 2023
2 parents c7a35a6 + d9d1f1f commit 0e588a2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions src/lru/_lru.c
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ lru_delete_last(LRU *self)
return;

if (self->callback) {

arglist = Py_BuildValue("OO", n->key, n->value);
result = PyObject_CallObject(self->callback, arglist);
Py_XDECREF(result);
Expand Down Expand Up @@ -265,26 +265,27 @@ lru_subscript(LRU *self, register PyObject *key)
}

static PyObject *
LRU_get(LRU *self, PyObject *args)
LRU_get(LRU *self, PyObject *args, PyObject *keywds)
{
PyObject *key;
PyObject *instead = NULL;
PyObject *default_obj = NULL;
PyObject *result;

if (!PyArg_ParseTuple(args, "O|O", &key, &instead))
static char *kwlist[] = {"key", "default", NULL};
if (!PyArg_ParseTupleAndKeywords(args, keywds, "O|O", kwlist, &key, &default_obj))
return NULL;

result = lru_subscript(self, key);
PyErr_Clear(); /* GET_NODE sets an exception on miss. Shut it up. */
if (result)
return result;

if (!instead) {
if (!default_obj) {
Py_RETURN_NONE;
}

Py_INCREF(instead);
return instead;
Py_INCREF(default_obj);
return default_obj;
}

static int
Expand Down Expand Up @@ -380,7 +381,7 @@ LRU_update(LRU *self, PyObject *args, PyObject *kwargs)
lru_ass_sub(self, key, value);
}
}

if (kwargs != NULL && PyDict_Check(kwargs)) {
while (PyDict_Next(kwargs, &pos, &key, &value))
lru_ass_sub(self, key, value);
Expand Down Expand Up @@ -415,13 +416,14 @@ LRU_setdefault(LRU *self, PyObject *args)
}

static PyObject *
LRU_pop(LRU *self, PyObject *args)
LRU_pop(LRU *self, PyObject *args, PyObject *keywds)
{
PyObject *key;
PyObject *default_obj = NULL;
PyObject *result;

if (!PyArg_ParseTuple(args, "O|O", &key, &default_obj))
static char *kwlist[] = {"key", "default", NULL};
if (!PyArg_ParseTupleAndKeywords(args, keywds, "O|O", kwlist, &key, &default_obj))
return NULL;

/* Trying to access the item by key. */
Expand Down Expand Up @@ -620,11 +622,11 @@ static PyMethodDef LRU_methods[] = {
PyDoc_STR("L.items() -> list of L's items (key,value) in MRU order")},
{"has_key", (PyCFunction)LRU_contains, METH_VARARGS,
PyDoc_STR("L.has_key(key) -> Check if key is there in L")},
{"get", (PyCFunction)LRU_get, METH_VARARGS,
PyDoc_STR("L.get(key, instead) -> If L has key return its value, otherwise instead")},
{"get", (PyCFunction)LRU_get, METH_VARARGS | METH_KEYWORDS,
PyDoc_STR("L.get(key, default=None) -> If L has key return its value, otherwise default")},
{"setdefault", (PyCFunction)LRU_setdefault, METH_VARARGS,
PyDoc_STR("L.setdefault(key, default=None) -> If L has key return its value, otherwise insert key with a value of default and return default")},
{"pop", (PyCFunction)LRU_pop, METH_VARARGS,
{"pop", (PyCFunction)LRU_pop, METH_VARARGS | METH_KEYWORDS,
PyDoc_STR("L.pop(key[, default]) -> If L has key return its value and remove it from L, otherwise return default. If default is not given and key is not in L, a KeyError is raised.")},
{"popitem", (PyCFunction)LRU_popitem, METH_VARARGS | METH_KEYWORDS,
PyDoc_STR("L.popitem([least_recent=True]) -> Returns and removes a (key, value) pair. The pair returned is the least-recently used if least_recent is true, or the most-recently used if false.")},
Expand Down
Empty file added src/lru/py.typed
Empty file.

0 comments on commit 0e588a2

Please sign in to comment.