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
Next Next commit
Add attribute keyword arguments to decimal.localcontext
  • Loading branch information
dignissimus committed Apr 1, 2022
commit bc114174d96f73c935596d98ff79b6e6dd7fbe0e
7 changes: 5 additions & 2 deletions Lib/_pydecimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ def setcontext(context):

del contextvars # Don't contaminate the namespace

def localcontext(ctx=None):
def localcontext(ctx=None, **kwargs):
"""Return a context manager for a copy of the supplied context

Uses a copy of the current context if no context is specified
Expand Down Expand Up @@ -500,7 +500,10 @@ def sin(x):
>>> print(getcontext().prec)
28
"""
if ctx is None: ctx = getcontext()
if ctx is None:
ctx = getcontext()
for key, value in kwargs.items():
setattr(ctx, key, value)
return _ContextManager(ctx)


Expand Down
151 changes: 95 additions & 56 deletions Modules/_decimal/_decimal.c
Original file line number Diff line number Diff line change
Expand Up @@ -1156,6 +1156,67 @@ context_setattr(PyObject *self, PyObject *name, PyObject *value)
return PyObject_GenericSetAttr(self, name, value);
}

static int
context_setattrs(PyObject *self, PyObject *prec, PyObject *rounding,
PyObject *emin, PyObject *emax, PyObject *capitals,
PyObject *clamp, PyObject *status, PyObject *traps) {

int ret;
if (prec != Py_None && context_setprec(self, prec, NULL) < 0) {
return -1;
}
if (rounding != Py_None && context_setround(self, rounding, NULL) < 0) {
return -1;
}
if (emin != Py_None && context_setemin(self, emin, NULL) < 0) {
return -1;
}
if (emax != Py_None && context_setemax(self, emax, NULL) < 0) {
return -1;
}
if (capitals != Py_None && context_setcapitals(self, capitals, NULL) < 0) {
return -1;
}
if (clamp != Py_None && context_setclamp(self, clamp, NULL) < 0) {
return -1;
}

if (traps != Py_None) {
if (PyList_Check(traps)) {
ret = context_settraps_list(self, traps);
}
#ifdef EXTRA_FUNCTIONALITY
else if (PyLong_Check(traps)) {
ret = context_settraps(self, traps, NULL);
}
#endif
else {
ret = context_settraps_dict(self, traps);
}
if (ret < 0) {
return ret;
}
}
if (status != Py_None) {
if (PyList_Check(status)) {
ret = context_setstatus_list(self, status);
}
#ifdef EXTRA_FUNCTIONALITY
else if (PyLong_Check(status)) {
ret = context_setstatus(self, status, NULL);
}
#endif
else {
ret = context_setstatus_dict(self, status);
}
if (ret < 0) {
return ret;
}
}

return 0;
}

static PyObject *
context_clear_traps(PyObject *self, PyObject *dummy UNUSED)
{
Expand Down Expand Up @@ -1255,7 +1316,6 @@ context_init(PyObject *self, PyObject *args, PyObject *kwds)
PyObject *clamp = Py_None;
PyObject *status = Py_None;
PyObject *traps = Py_None;
int ret;

assert(PyTuple_Check(args));

Expand All @@ -1267,59 +1327,11 @@ context_init(PyObject *self, PyObject *args, PyObject *kwds)
return -1;
}

if (prec != Py_None && context_setprec(self, prec, NULL) < 0) {
return -1;
}
if (rounding != Py_None && context_setround(self, rounding, NULL) < 0) {
return -1;
}
if (emin != Py_None && context_setemin(self, emin, NULL) < 0) {
return -1;
}
if (emax != Py_None && context_setemax(self, emax, NULL) < 0) {
return -1;
}
if (capitals != Py_None && context_setcapitals(self, capitals, NULL) < 0) {
return -1;
}
if (clamp != Py_None && context_setclamp(self, clamp, NULL) < 0) {
return -1;
}

if (traps != Py_None) {
if (PyList_Check(traps)) {
ret = context_settraps_list(self, traps);
}
#ifdef EXTRA_FUNCTIONALITY
else if (PyLong_Check(traps)) {
ret = context_settraps(self, traps, NULL);
}
#endif
else {
ret = context_settraps_dict(self, traps);
}
if (ret < 0) {
return ret;
}
}
if (status != Py_None) {
if (PyList_Check(status)) {
ret = context_setstatus_list(self, status);
}
#ifdef EXTRA_FUNCTIONALITY
else if (PyLong_Check(status)) {
ret = context_setstatus(self, status, NULL);
}
#endif
else {
ret = context_setstatus_dict(self, status);
}
if (ret < 0) {
return ret;
}
}

return 0;
return context_setattrs(
self, prec, rounding,
emin, emax, capitals,
clamp, status, traps
);
}

static PyObject *
Expand Down Expand Up @@ -1721,13 +1733,29 @@ PyDec_SetCurrentContext(PyObject *self UNUSED, PyObject *v)
static PyObject *
ctxmanager_new(PyTypeObject *type UNUSED, PyObject *args, PyObject *kwds)
{
static char *kwlist[] = {"ctx", NULL};
static char *kwlist[] = {
"ctx", "prec", "rounding",
"Emin", "Emax", "capitals",
"clamp", "flags", "traps",
NULL
};
PyDecContextManagerObject *self;
PyObject *local = Py_None;
PyObject *global;

PyObject *prec = Py_None;
PyObject *rounding = Py_None;
PyObject *Emin = Py_None;
PyObject *Emax = Py_None;
PyObject *capitals = Py_None;
PyObject *clamp = Py_None;
PyObject *flags = Py_None;
PyObject *traps = Py_None;


CURRENT_CONTEXT(global);
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O", kwlist, &local)) {
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOOOOOOOO", kwlist, &local,
&prec, &rounding, &Emin, &Emax, &capitals, &clamp, &flags, &traps)) {
return NULL;
}
if (local == Py_None) {
Expand All @@ -1754,6 +1782,17 @@ ctxmanager_new(PyTypeObject *type UNUSED, PyObject *args, PyObject *kwds)
self->global = global;
Py_INCREF(self->global);

int ret = context_setattrs(
self->local, prec, rounding,
Emin, Emax, capitals,
clamp, flags, traps
);

if(ret < 0){
return NULL;
};


return (PyObject *)self;
}

Expand Down
0