8000 gh-91291: Accept attributes as keyword arguments in decimal.localcontext by dignissimus · Pull Request #32242 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-91291: Accept attributes as keyword arguments in decimal.localcontext #32242

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
Apr 22, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add documentation, write tests
  • Loading branch information
dignissimus committed Apr 1, 2022
commit 16617094346da726f5e40b613b2c5bf64964de7c
12 changes: 10 additions & 2 deletions Doc/library/decimal.rst
Original file line number Diff line number Diff line change
Expand Up @@ -925,12 +925,13 @@ Each thread has its own current context which is accessed or changed using the
You can also use the :keyword:`with` statement and the :func:`localcontext`
function to temporarily change the active context.

.. function:: localcontext(ctx=None)
.. function:: localcontext(ctx=None, \*\*kwargs)

Return a context manager that will set the current context for the active thread
to a copy of *ctx* on entry to the with-statement and restore the previous context
when exiting the with-statement. If no context is specified, a copy of the
current context is used.
current context is used. The *kwargs* argument is used to specify the attributes
of the context that will be returned.

For example, the following code sets the current decimal precision to 42 places,
performs a calculation, and then automatically restores the previous context::
Expand All @@ -942,10 +943,17 @@ function to temporarily change the active context.
s = calculate_something()
s = +s # Round the final result back to the default precision


Raises :exc:`TypeError` if *kwargs* supplies an attribute that :class:`Context` doesn't
support.

Raises :exc:`ValueError` if *kwargs* supplies an invalid value for an attribute.

New contexts can also be created using the :class:`Context` constructor
described below. In addition, the module provides three pre-made contexts:



.. class:: BasicContext

This is a standard context defined by the General Decimal Arithmetic
Expand Down
5 changes: 4 additions & 1 deletion Lib/_pydecimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,10 @@ def sin(x):
if ctx is None:
ctx = getcontext()
for key, value in kwargs.items():
setattr(ctx, key, value)
try:
setattr(ctx, key, value)
except:
raise TypeError(f"'{key}' is an invalid keyword argument for this function")
return _ContextManager(ctx)


Expand Down
15 changes: 15 additions & 0 deletions Lib/test/test_decimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -3602,6 +3602,21 @@ def test_localcontextarg(self):
self.assertIsNot(new_ctx, set_ctx, 'did not copy the context')
self.assertIs(set_ctx, enter_ctx, '__enter__ returned wrong context')

def test_localcontext_kwargs(self):
with self.decimal.localcontext(
prec=10, rounding=ROUND_HALF_DOWN,
Emin=-20, Emax=20, capitals=0,
clamp=1
) as ctx:
self.assertEqual(ctx.prec, 10)
self.assertEqual(ctx.rounding, self.decimal.ROUND_HALF_DOWN)
self.assertEqual(ctx.Emin, -20)
self.assertEqual(ctx.Emax, 20)
self.assertEqual(ctx.capitals, 0)
self.assertEqual(ctx.clamp, 1)

self.assertRaises(TypeError, self.decimal.localcontext, precision=10)

def test_nested_with_statements(self):
# Use a copy of the supplied context in the block
Decimal = self.decimal.Decimal
Expand Down
2 changes: 1 addition & 1 deletion Modules/_decimal/docstrings.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Set a new default context.\n\
\n");

PyDoc_STRVAR(doc_localcontext,
"localcontext($module, /, ctx=None)\n--\n\n\
"localcontext($module, /, ctx=None, **kwargs)\n--\n\n\
Return a context manager that will set the default context to a copy of ctx\n\
on entry to the with-statement and restore the previous default context when\n\
exiting the with-statement. If no context is specified, a copy of the current\n\
Expand Down
0