Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9a4ff43

Browse files
author
Stefan Krah
committed
Issue #15783: Support None default values in the Context() constructor.
1 parent 92f8b00 commit 9a4ff43

2 files changed

Lines changed: 54 additions & 30 deletions

File tree

Lib/test/test_decimal.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2718,6 +2718,27 @@ class PyPythonAPItests(PythonAPItests):
27182718

27192719
class ContextAPItests(unittest.TestCase):
27202720

2721+
def test_none_args(self):
2722+
Context = self.decimal.Context
2723+
InvalidOperation = self.decimal.InvalidOperation
2724+
DivisionByZero = self.decimal.DivisionByZero
2725+
Overflow = self.decimal.Overflow
2726+
ROUND_HALF_EVEN = self.decimal.ROUND_HALF_EVEN
2727+
2728+
c1 = Context()
2729+
c2 = Context(prec=None, rounding=None, Emax=None, Emin=None,
2730+
capitals=None, clamp=None, flags=None, traps=None)
2731+
for c in [c1, c2]:
2732+
self.assertEqual(c.prec, 28)
2733+
self.assertEqual(c.rounding, ROUND_HALF_EVEN)
2734+
self.assertEqual(c.Emax, 999999)
2735+
self.assertEqual(c.Emin, -999999)
2736+
self.assertEqual(c.capitals, 1)
2737+
self.assertEqual(c.clamp, 0)
2738+
assert_signals(self, c, 'flags', [])
2739+
assert_signals(self, c, 'traps', [InvalidOperation, DivisionByZero,
2740+
Overflow])
2741+
27212742
def test_pickle(self):
27222743

27232744
Context = self.decimal.Context

Modules/_decimal/_decimal.c

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1241,50 +1241,53 @@ context_init(PyObject *self, PyObject *args, PyObject *kwds)
12411241
"prec", "rounding", "Emin", "Emax", "capitals", "clamp",
12421242
"flags", "traps", NULL
12431243
};
1244-
PyObject *rounding = NULL;
1245-
PyObject *traps = NULL;
1246-
PyObject *status = NULL;
1247-
mpd_context_t *ctx, t;
1248-
int capitals = 1;
1244+
PyObject *prec = Py_None;
1245+
PyObject *rounding = Py_None;
1246+
PyObject *emin = Py_None;
1247+
PyObject *emax = Py_None;
1248+
PyObject *capitals = Py_None;
1249+
PyObject *clamp = Py_None;
1250+
PyObject *status = Py_None;
1251+
PyObject *traps = Py_None;
12491252
int ret;
12501253

12511254
assert(PyTuple_Check(args));
1252-
ctx = CTX(self);
12531255

1254-
t = *ctx;
12551256
if (!PyArg_ParseTupleAndKeywords(
12561257
args, kwds,
1257-
"|nOnniiOO", kwlist,
1258-
&t.prec, &rounding, &t.emin, &t.emax, &capitals, &t.clamp,
1259-
&status, &traps
1258+
"|OOOOOOOO", kwlist,
1259+
&prec, &rounding, &emin, &emax, &capitals, &clamp, &status, &traps
12601260
)) {
12611261
return -1;
12621262
}
1263-
if (rounding != NULL) {
1264-
t.round = getround(rounding);
1265-
if (t.round < 0) {
1266-
return -1;
1267-
}
1268-
}
12691263

1270-
if (!mpd_qsetprec(ctx, t.prec) ||
1271-
!mpd_qsetemin(ctx, t.emin) ||
1272-
!mpd_qsetemax(ctx, t.emax) ||
1273-
!mpd_qsetclamp(ctx, t.clamp)) {
1274-
return value_error_int("invalid context");
1264+
if (prec != Py_None && context_setprec(self, prec, NULL) < 0) {
1265+
return -1;
1266+
}
1267+
if (emin != Py_None && context_setemin(self, emin, NULL) < 0) {
1268+
return -1;
12751269
}
1276-
if (!mpd_qsetround(ctx, t.round) ||
1277-
!mpd_qsettraps(ctx, t.traps) ||
1278-
!mpd_qsetstatus(ctx, t.status)) {
1279-
return type_error_int("invalid context");
1270+
if (emax != Py_None && context_setemax(self, emax, NULL) < 0) {
1271+
return -1;
1272+
}
1273+
if (capitals != Py_None && context_setcapitals(self, capitals, NULL) < 0) {
1274+
return -1;
1275+
}
1276+
if (clamp != Py_None && context_setclamp(self, clamp, NULL) < 0) {
1277+
return -1;
12801278
}
12811279

1282-
if (capitals != 0 && capitals != 1) {
1283-
return value_error_int("invalid context");
1280+
if (rounding != Py_None) {
1281+
int x = getround(rounding);
1282+
if (x < 0) {
1283+
return -1;
1284+
}
1285+
if (!mpd_qsetround(CTX(self), x)) {
1286+
return type_error_int(invalid_rounding_err);
1287+
}
12841288
}
1285-
CtxCaps(self) = capitals;
12861289

1287-
if (traps != NULL) {
1290+
if (traps != Py_None) {
12881291
if (PyList_Check(traps)) {
12891292
ret = context_settraps_list(self, traps);
12901293
}
@@ -1300,7 +1303,7 @@ context_init(PyObject *self, PyObject *args, PyObject *kwds)
13001303
return ret;
13011304
}
13021305
}
1303-
if (status != NULL) {
1306+
if (status != Py_None) {
13041307
if (PyList_Check(status)) {
13051308
ret = context_setstatus_list(self, status);
13061309
}

0 commit comments

Comments
 (0)