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

Skip to content

Commit fe17b2b

Browse files
author
Stefan Krah
committed
Raise MemoryError instead of InvalidOperation/MallocError for compatibility
with decimal.py. The standard specifies InsufficientStorage (MallocError) as a sub-condition of InvalidOperation. This allows a calculation to continue with NaN results when allocation fails.
1 parent 1d56179 commit fe17b2b

2 files changed

Lines changed: 9 additions & 2 deletions

File tree

Lib/test/test_decimal.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3837,7 +3837,7 @@ def test_module_attributes(self):
38373837

38383838
x = dir(C)
38393839
y = [s for s in dir(P) if '__' in s or not s.startswith('_')]
3840-
self.assertEqual(set(x) - set(y), {'MallocError'})
3840+
self.assertEqual(set(x) - set(y), set())
38413841

38423842
def test_context_attributes(self):
38433843

Modules/_decimal/_decimal.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,9 @@ static DecCondMap cond_map[] = {
168168
{"DivisionImpossible", "decimal.DivisionImpossible", MPD_Division_impossible, NULL},
169169
{"DivisionUndefined", "decimal.DivisionUndefined", MPD_Division_undefined, NULL},
170170
{"InvalidContext", "decimal.InvalidContext", MPD_Invalid_context, NULL},
171+
#ifdef EXTRA_FUNCTIONALITY
171172
{"MallocError", "decimal.MallocError", MPD_Malloc_error, NULL},
173+
#endif
172174
{NULL}
173175
};
174176

@@ -466,9 +468,14 @@ dec_addstatus(PyObject *context, uint32_t status)
466468
mpd_context_t *ctx = CTX(context);
467469

468470
ctx->status |= status;
469-
if (ctx->traps&status) {
471+
if (status & (ctx->traps|MPD_Malloc_error)) {
470472
PyObject *ex, *siglist;
471473

474+
if (status & MPD_Malloc_error) {
475+
PyErr_NoMemory();
476+
return 1;
477+
}
478+
472479
ex = flags_as_exception(ctx->traps&status);
473480
if (ex == NULL) {
474481
return 1; /* GCOV_NOT_REACHED */

0 commit comments

Comments
 (0)