Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Lib/test/test_decimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -3798,6 +3798,13 @@ def test_localcontext_kwargs(self):
self.assertRaises(TypeError, self.decimal.localcontext, Emin="")
self.assertRaises(TypeError, self.decimal.localcontext, Emax="")

# None is not a valid value for any of these attributes.
for name in ('prec', 'rounding', 'Emin', 'Emax', 'capitals', 'clamp',
'flags', 'traps'):
with self.subTest(name=name):
self.assertRaises(TypeError, self.decimal.localcontext,
**{name: None})

def test_local_context_kwargs_does_not_overwrite_existing_argument(self):
ctx = self.decimal.getcontext()
orig_prec = ctx.prec
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
:func:`decimal.localcontext` now raises :exc:`TypeError` if a keyword argument
is ``None``, as the pure Python implementation already did. Previously the C
implementation silently ignored it.
26 changes: 13 additions & 13 deletions Modules/_decimal/_decimal.c
Original file line number Diff line number Diff line change
Expand Up @@ -1333,7 +1333,7 @@ context_setattr(PyObject *self, PyObject *name, PyObject *value)
return PyObject_GenericSetAttr(self, name, value);
}

/* In the constructor and in localcontext() None means "not specified". */
/* In the constructor None means "not specified". */
#define NONE_TO_NULL(x) ((x) == Py_None ? NULL : (x))

/* Set the given attributes. An attribute is left unchanged if the
Expand Down Expand Up @@ -2099,14 +2099,14 @@ _decimal.localcontext

ctx as local: object = None
*
prec: object = None
rounding: object = None
Emin: object = None
Emax: object = None
capitals: object = None
clamp: object = None
flags: object = None
traps: object = None
prec: object = NULL
rounding: object = NULL
Emin: object = NULL
Emax: object = NULL
capitals: object = NULL
clamp: object = NULL
flags: object = NULL
traps: object = NULL
Comment on lines +2102 to +2109

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As in #155047, this is not rendered well by signature().

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already use @text_signature "($module, /, ctx=None, **kwargs)" for this method. And the Python implementation has the same signature.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, there is no regression. Though, I think in future we could use here explicit arguments with a sentinel.


Return a context manager for a copy of the supplied context.

Expand All @@ -2121,7 +2121,7 @@ _decimal_localcontext_impl(PyObject *module, PyObject *local, PyObject *prec,
PyObject *rounding, PyObject *Emin,
PyObject *Emax, PyObject *capitals,
PyObject *clamp, PyObject *flags, PyObject *traps)
/*[clinic end generated code: output=9bf4e47742a809b0 input=490307b9689c3856]*/
/*[clinic end generated code: output=9bf4e47742a809b0 input=616abb6ee1654373]*/
{
PyObject *global;

Expand All @@ -2142,9 +2142,9 @@ _decimal_localcontext_impl(PyObject *module, PyObject *local, PyObject *prec,
}

int ret = context_setattrs(
local_copy, NONE_TO_NULL(prec), NONE_TO_NULL(rounding),
NONE_TO_NULL(Emin), NONE_TO_NULL(Emax), NONE_TO_NULL(capitals),
NONE_TO_NULL(clamp), NONE_TO_NULL(flags), NONE_TO_NULL(traps)
local_copy, prec, rounding,
Emin, Emax, capitals,
clamp, flags, traps
);
if (ret < 0) {
Py_DECREF(local_copy);
Expand Down
18 changes: 9 additions & 9 deletions Modules/_decimal/clinic/_decimal.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading