Skip to content

Commit 0e588a2

Browse files
authored
Merge pull request amitdev#51 from miili/feature/keywords
lru.c: adding keywords
2 parents c7a35a6 + d9d1f1f commit 0e588a2

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

src/lru/_lru.c

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ lru_delete_last(LRU *self)
199199
return;
200200

201201
if (self->callback) {
202-
202+
203203
arglist = Py_BuildValue("OO", n->key, n->value);
204204
result = PyObject_CallObject(self->callback, arglist);
205205
Py_XDECREF(result);
@@ -265,26 +265,27 @@ lru_subscript(LRU *self, register PyObject *key)
265265
}
266266

267267
static PyObject *
268-
LRU_get(LRU *self, PyObject *args)
268+
LRU_get(LRU *self, PyObject *args, PyObject *keywds)
269269
{
270270
PyObject *key;
271-
PyObject *instead = NULL;
271+
PyObject *default_obj = NULL;
272272
PyObject *result;
273273

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

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

282-
if (!instead) {
283+
if (!default_obj) {
283284
Py_RETURN_NONE;
284285
}
285286

286-
Py_INCREF(instead);
287-
return instead;
287+
Py_INCREF(default_obj);
288+
return default_obj;
288289
}
289290

290291
static int
@@ -380,7 +381,7 @@ LRU_update(LRU *self, PyObject *args, PyObject *kwargs)
380381
lru_ass_sub(self, key, value);
381382
}
382383
}
383-
384+
384385
if (kwargs != NULL && PyDict_Check(kwargs)) {
385386
while (PyDict_Next(kwargs, &pos, &key, &value))
386387
lru_ass_sub(self, key, value);
@@ -415,13 +416,14 @@ LRU_setdefault(LRU *self, PyObject *args)
415416
}
416417

417418
static PyObject *
418-
LRU_pop(LRU *self, PyObject *args)
419+
LRU_pop(LRU *self, PyObject *args, PyObject *keywds)
419420
{
420421
PyObject *key;
421422
PyObject *default_obj = NULL;
422423
PyObject *result;
423424

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

427429
/* Trying to access the item by key. */
@@ -620,11 +622,11 @@ static PyMethodDef LRU_methods[] = {
620622
PyDoc_STR("L.items() -> list of L's items (key,value) in MRU order")},
621623
{"has_key", (PyCFunction)LRU_contains, METH_VARARGS,
622624
PyDoc_STR("L.has_key(key) -> Check if key is there in L")},
623-
{"get", (PyCFunction)LRU_get, METH_VARARGS,
624-
PyDoc_STR("L.get(key, instead) -> If L has key return its value, otherwise instead")},
625+
{"get", (PyCFunction)LRU_get, METH_VARARGS | METH_KEYWORDS,
626+
PyDoc_STR("L.get(key, default=None) -> If L has key return its value, otherwise default")},
625627
{"setdefault", (PyCFunction)LRU_setdefault, METH_VARARGS,
626628
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")},
627-
{"pop", (PyCFunction)LRU_pop, METH_VARARGS,
629+
{"pop", (PyCFunction)LRU_pop, METH_VARARGS | METH_KEYWORDS,
628630
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.")},
629631
{"popitem", (PyCFunction)LRU_popitem, METH_VARARGS | METH_KEYWORDS,
630632
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.")},

src/lru/py.typed

Whitespace-only changes.

0 commit comments

Comments
 (0)