Skip to content

Commit f33913b

Browse files
committed
Add support for ndigits in __round__.
1 parent 49c70b2 commit f33913b

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

src/lazy_object_proxy/cext.c

+11-7
Original file line numberDiff line numberDiff line change
@@ -839,24 +839,28 @@ static PyObject *Proxy_reduce(
839839

840840
/* ------------------------------------------------------------------------- */
841841

842-
static PyObject *Proxy_round(
843-
ProxyObject *self, PyObject *args)
842+
static PyObject *Proxy_round(ProxyObject *self, PyObject *args, PyObject *kwds)
844843
{
845844
PyObject *module = NULL;
846-
PyObject *dict = NULL;
847845
PyObject *round = NULL;
846+
PyObject *ndigits = NULL;
848847

849848
PyObject *result = NULL;
850849

850+
char *const kwlist[] = { "ndigits", NULL };
851+
851852
Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
852853

854+
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:ObjectProxy", kwlist, &ndigits)) {
855+
return NULL;
856+
}
857+
853858
module = PyImport_ImportModule("builtins");
854859

855860
if (!module)
856861
return NULL;
857862

858-
dict = PyModule_GetDict(module);
859-
round = PyDict_GetItemString(dict, "round");
863+
round = PyObject_GetAttrString(module, "round");
860864

861865
if (!round) {
862866
Py_DECREF(module);
@@ -866,7 +870,7 @@ static PyObject *Proxy_round(
866870
Py_INCREF(round);
867871
Py_DECREF(module);
868872

869-
result = PyObject_CallFunctionObjArgs(round, self->wrapped, NULL);
873+
result = PyObject_CallFunctionObjArgs(round, self->wrapped, ndigits, NULL);
870874

871875
Py_DECREF(round);
872876

@@ -1324,7 +1328,7 @@ static PyMethodDef Proxy_methods[] = {
13241328
{ "__reduce__", (PyCFunction)Proxy_reduce, METH_NOARGS, 0 },
13251329
{ "__reduce_ex__", (PyCFunction)Proxy_reduce, METH_O, 0 },
13261330
{ "__fspath__", (PyCFunction)Proxy_fspath, METH_NOARGS, 0 },
1327-
{ "__round__", (PyCFunction)Proxy_round, METH_NOARGS, 0 },
1331+
{ "__round__", (PyCFunction)Proxy_round, METH_VARARGS | METH_KEYWORDS, 0 },
13281332
{ "__aenter__", (PyCFunction)Proxy_aenter, METH_NOARGS, 0 },
13291333
{ "__aexit__", (PyCFunction)Proxy_aexit, METH_VARARGS | METH_KEYWORDS, 0 },
13301334
{ "__format__", (PyCFunction)Proxy_format, METH_VARARGS, 0 },

src/lazy_object_proxy/slots.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,8 @@ def __fspath__(self):
167167
def __reversed__(self):
168168
return reversed(self.__wrapped__)
169169

170-
def __round__(self):
171-
return round(self.__wrapped__)
170+
def __round__(self, ndigits=None):
171+
return round(self.__wrapped__, ndigits)
172172

173173
def __lt__(self, other):
174174
return self.__wrapped__ < other

tests/test_lazy_object_proxy.py

+5
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ def test_round(lop):
3535
assert round(proxy) == 1
3636

3737

38+
def test_round_ndigits(lop):
39+
proxy = lop.Proxy(lambda: 1.49494)
40+
assert round(proxy, 3) == 1.495
41+
42+
3843
def test_attributes(lop):
3944
def function1(*args, **kwargs):
4045
return args, kwargs

0 commit comments

Comments
 (0)