Skip to content

Commit

Permalink
The __round__ hook didn't accept ndigits argument.
Browse files Browse the repository at this point in the history
  • Loading branch information
GrahamDumpleton committed Oct 6, 2024
1 parent 49a8f08 commit a8066b5
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
2 changes: 2 additions & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ Note that version 1.17.0 drops support for Python 3.6 and 3.7. Python version
when instead it should have been called directly, ignoring that binding was
not possible.

* The `__round__` hook for the object proxy didn't accept `ndigits` argument.

Version 1.16.0
--------------

Expand Down
19 changes: 14 additions & 5 deletions src/wrapt/_wrappers.c
Original file line number Diff line number Diff line change
Expand Up @@ -1318,26 +1318,34 @@ static PyObject *WraptObjectProxy_reversed(

#if PY_MAJOR_VERSION >= 3
static PyObject *WraptObjectProxy_round(
WraptObjectProxyObject *self, PyObject *args)
WraptObjectProxyObject *self, PyObject *args, PyObject *kwds)
{
PyObject *ndigits = NULL;

PyObject *module = NULL;
PyObject *dict = NULL;
PyObject *round = NULL;

PyObject *result = NULL;

char *const kwlist[] = { "ndigits", NULL };

if (!self->wrapped) {
PyErr_SetString(PyExc_ValueError, "wrapper has not been initialized");
return NULL;
}

if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:ObjectProxy",
kwlist, &ndigits)) {
return NULL;
}

module = PyImport_ImportModule("builtins");

if (!module)
return NULL;

dict = PyModule_GetDict(module);
round = PyDict_GetItemString(dict, "round");
round = PyObject_GetAttrString(module, "round");

if (!round) {
Py_DECREF(module);
Expand All @@ -1347,7 +1355,7 @@ static PyObject *WraptObjectProxy_round(
Py_INCREF(round);
Py_DECREF(module);

result = PyObject_CallFunctionObjArgs(round, self->wrapped, NULL);
result = PyObject_CallFunctionObjArgs(round, self->wrapped, ndigits, NULL);

Py_DECREF(round);

Expand Down Expand Up @@ -1818,7 +1826,8 @@ static PyMethodDef WraptObjectProxy_methods[] = {
{ "__format__", (PyCFunction)WraptObjectProxy_format, METH_VARARGS, 0 },
{ "__reversed__", (PyCFunction)WraptObjectProxy_reversed, METH_NOARGS, 0 },
#if PY_MAJOR_VERSION >= 3
{ "__round__", (PyCFunction)WraptObjectProxy_round, METH_NOARGS, 0 },
{ "__round__", (PyCFunction)WraptObjectProxy_round,
METH_VARARGS | METH_KEYWORDS, 0 },
#endif
{ "__complex__", (PyCFunction)WraptObjectProxy_complex, METH_NOARGS, 0 },
#if PY_MAJOR_VERSION > 3 || (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 7)
Expand Down
4 changes: 2 additions & 2 deletions src/wrapt/wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ def __reversed__(self):
return reversed(self.__wrapped__)

if not PY2:
def __round__(self):
return round(self.__wrapped__)
def __round__(self, ndigits=None):
return round(self.__wrapped__, ndigits)

if sys.hexversion >= 0x03070000:
def __mro_entries__(self, bases):
Expand Down
4 changes: 3 additions & 1 deletion tests/test_object_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -1852,7 +1852,9 @@ def test_fractions_round(self):
proxy = wrapt.ObjectProxy(instance)

self.assertEqual(round(instance), round(proxy))

self.assertEqual(round(instance, 3), round(proxy, 3))
self.assertEqual(round(instance, ndigits=3), round(proxy, ndigits=3))

class TestArgumentUnpacking(unittest.TestCase):

def test_self_keyword_argument_on_dict(self):
Expand Down

0 comments on commit a8066b5

Please sign in to comment.