Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(116251)

Side by Side Diff: Modules/_decimal/_decimal.c

Issue 7652: Merge C version of decimal into py3k.
Patch Set: Created 7 years, 8 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Misc/valgrind-python.supp ('k') | Modules/_decimal/docstrings.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Copyright (c) 2008-2010 Stefan Krah. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28
29 #include <Python.h>
30 #include "longintrepr.h"
31 #include "pythread.h"
32 #include "structmember.h"
33 #include "complexobject.h"
34 #include "mpdecimal.h"
35
36 #include <stdlib.h>
37
38 #include "docstrings.h"
39 #include "memory.h"
40
41
42 /*
43 * Type sizes with assertions in mpdecimal.h and pyport.h:
44 * sizeof(size_t) == sizeof(Py_ssize_t)
45 * sizeof(size_t) == sizeof(mpd_uint_t) == sizeof(mpd_ssize_t)
46 */
47
48 #ifdef TEST_COVERAGE
49 #undef Py_LOCAL_INLINE
50 #define Py_LOCAL_INLINE Py_LOCAL
51 #endif
52
53 #define MPD_Float_operation MPD_Not_implemented
54
55 #define BOUNDS_CHECK(x, MIN, MAX) x = (x < MIN || MAX < x) ? MAX : x
56
57
58 typedef struct {
59 PyObject_HEAD
60 mpd_t *dec;
61 } PyDecObject;
62
63 typedef struct {
64 PyObject_HEAD
65 uint32_t *flags;
66 } PyDecSignalDictObject;
67
68 typedef struct {
69 PyObject_HEAD
70 mpd_context_t ctx;
71 PyObject *traps;
72 PyObject *flags;
73 int capitals;
74 } PyDecContextObject;
75
76 typedef struct {
77 PyObject_HEAD
78 PyObject *local;
79 PyObject *global;
80 } PyDecContextManagerObject;
81
82
83 #undef MPD
84 #undef CTX
85 static PyTypeObject PyDec_Type;
86 static PyTypeObject *PyDecSignalDict_Type;
87 static PyTypeObject PyDecContext_Type;
88 static PyTypeObject PyDecContextManager_Type;
89 #define PyDec_CheckExact(v) (Py_TYPE(v) == &PyDec_Type)
90 #define PyDec_Check(v) PyObject_TypeCheck(v, &PyDec_Type)
91 #define PyDecSignalDict_Check(v) (Py_TYPE(v) == PyDecSignalDict_Type)
92 #define PyDecContext_Check(v) (Py_TYPE(v) == &PyDecContext_Type)
93 #define MPD(v) (((PyDecObject *)v)->dec)
94 #define SdFlagAddr(v) (((PyDecSignalDictObject *)v)->flags)
95 #define SdFlags(v) (*((PyDecSignalDictObject *)v)->flags)
96 #define CTX(v) (&((PyDecContextObject *)v)->ctx)
97 #define CtxCaps(v) (((PyDecContextObject *)v)->capitals)
98
99
100 Py_LOCAL_INLINE(PyObject *)
101 incr_true(void)
102 {
103 Py_INCREF(Py_True);
104 return Py_True;
105 }
106
107 Py_LOCAL_INLINE(PyObject *)
108 incr_false(void)
109 {
110 Py_INCREF(Py_False);
111 return Py_False;
112 }
113
114
115 #ifdef WITHOUT_THREADS
116 /* Default module context */
117 static PyObject *module_context = NULL;
118 #else
119 /* Key for thread state dictionary */
120 static PyObject *tls_context_key = NULL;
121 #endif
122
123 /* Template for creating new thread contexts, calling Context() without
124 * arguments and initializing the module_context on first access. */
125 static PyObject *default_context_template = NULL;
126 /* Basic and extended context templates */
127 static PyObject *basic_context_template = NULL;
128 static PyObject *extended_context_template = NULL;
129
130
131 /* Error codes for functions that return signals or conditions */
132 #define DEC_INVALID_SIGNALS (MPD_Max_status+1U)
133 #define DEC_ERR_OCCURRED (DEC_INVALID_SIGNALS<<1)
134 #define DEC_ERRORS (DEC_INVALID_SIGNALS|DEC_ERR_OCCURRED)
135
136 typedef struct {
137 const char *name; /* condition or signal name */
138 const char *fqname; /* fully qualified name */
139 uint32_t flag; /* libmpdec flag */
140 PyObject *ex; /* corresponding exception */
141 } DecCondMap;
142
143 /* Top level Exception; inherits from ArithmeticError */
144 static PyObject *DecimalException = NULL;
145
146 /* Exceptions that correspond to IEEE signals; inherit from DecimalException */
147 #define SIGNAL_MAP_LEN 9
148 static DecCondMap signal_map[] = {
149 {"InvalidOperation", "decimal.InvalidOperation", MPD_IEEE_Invalid_operation, N ULL},
150 {"FloatOperation", "decimal.FloatOperation", MPD_Float_operation, NULL},
151 {"DivisionByZero", "decimal.DivisionByZero", MPD_Division_by_zero, NULL},
152 {"Overflow", "decimal.Overflow", MPD_Overflow, NULL},
153 {"Underflow", "decimal.Underflow", MPD_Underflow, NULL},
154 {"Subnormal", "decimal.Subnormal", MPD_Subnormal, NULL},
155 {"Inexact", "decimal.Inexact", MPD_Inexact, NULL},
156 {"Rounded", "decimal.Rounded", MPD_Rounded, NULL},
157 {"Clamped", "decimal.Clamped", MPD_Clamped, NULL},
158 {NULL}
159 };
160
161 /* Exceptions that inherit from InvalidOperation */
162 static DecCondMap cond_map[] = {
163 {"InvalidOperation", "decimal.InvalidOperation", MPD_Invalid_operation, NULL},
164 {"ConversionSyntax", "decimal.ConversionSyntax", MPD_Conversion_syntax, NULL},
165 {"DivisionImpossible", "decimal.DivisionImpossible", MPD_Division_impossible, NULL},
166 {"DivisionUndefined", "decimal.DivisionUndefined", MPD_Division_undefined, NUL L},
167 {"InvalidContext", "decimal.InvalidContext", MPD_Invalid_context, NULL},
168 {"MallocError", "decimal.MallocError", MPD_Malloc_error, NULL},
169 {NULL}
170 };
171
172 static const char *dec_signal_string[MPD_NUM_FLAGS] = {
173 "Clamped",
174 "InvalidOperation",
175 "DivisionByZero",
176 "InvalidOperation",
177 "InvalidOperation",
178 "InvalidOperation",
179 "Inexact",
180 "InvalidOperation",
181 "InvalidOperation",
182 "InvalidOperation",
183 "FloatOperation",
184 "Overflow",
185 "Rounded",
186 "Subnormal",
187 "Underflow",
188 };
189
190 static const char *invalid_rounding_err =
191 "valid values for rounding are:\n\
192 [ROUND_CEILING, ROUND_FLOOR, ROUND_UP, ROUND_DOWN,\n\
193 ROUND_HALF_UP, ROUND_HALF_DOWN, ROUND_HALF_EVEN,\n\
194 ROUND_05UP]";
195
196 static const char *invalid_signals_err =
197 "valid values for signals are:\n\
198 [InvalidOperation, FloatOperation, DivisionByZero,\n\
199 Overflow, Underflow, Subnormal, Inexact, Rounded,\n\
200 Clamped]";
201
202 #ifdef EXTRA_FUNCTIONALITY
203 static const char *invalid_flags_err =
204 "valid values for _flags or _traps are:\n\
205 signals:\n\
206 [DecIEEEInvalidOperation, DecFloatOperation, DecDivisionByZero,\n\
207 DecOverflow, DecUnderflow, DecSubnormal, DecInexact, DecRounded,\n\
208 DecClamped]\n\
209 conditions which trigger DecIEEEInvalidOperation:\n\
210 [DecInvalidOperation, DecConversionSyntax, DecDivisionImpossible,\n\
211 DecDivisionUndefined, DecFpuError, DecInvalidContext, DecMallocError]";
212 #endif
213
214 static int
215 value_error_int(const char *mesg)
216 {
217 PyErr_SetString(PyExc_ValueError, mesg);
218 return -1;
219 }
220
221 static PyObject *
222 value_error_ptr(const char *mesg)
223 {
224 PyErr_SetString(PyExc_ValueError, mesg);
225 return NULL;
226 }
227
228 static int
229 type_error_int(const char *mesg)
230 {
231 PyErr_SetString(PyExc_TypeError, mesg);
232 return -1;
233 }
234
235 static PyObject *
236 type_error_ptr(const char *mesg)
237 {
238 PyErr_SetString(PyExc_TypeError, mesg);
239 return NULL;
240 }
241
242 static int
243 runtime_error_int(const char *mesg)
244 {
245 PyErr_SetString(PyExc_RuntimeError, mesg);
246 return -1;
247 }
248 #define INTERNAL_ERROR_INT(funcname) \
249 return runtime_error_int("internal error in " funcname)
250
251 static PyObject *
252 runtime_error_ptr(const char *mesg)
253 {
254 PyErr_SetString(PyExc_RuntimeError, mesg);
255 return NULL;
256 }
257 #define INTERNAL_ERROR_PTR(funcname) \
258 return runtime_error_ptr("internal error in " funcname)
259
260 static void
261 dec_traphandler(mpd_context_t *ctx UNUSED) /* GCOV_NOT_REACHED */
262 { /* GCOV_NOT_REACHED */
263 return; /* GCOV_NOT_REACHED */
264 }
265
266 static PyObject *
267 flags_as_exception(uint32_t flags)
268 {
269 DecCondMap *cm;
270
271 for (cm = signal_map; cm->name != NULL; cm++) {
272 if (flags&cm->flag) {
273 return cm->ex;
274 }
275 }
276
277 INTERNAL_ERROR_PTR("flags_as_exception"); /* GCOV_NOT_REACHED */
278 }
279
280 Py_LOCAL_INLINE(uint32_t)
281 exception_as_flag(PyObject *ex)
282 {
283 DecCondMap *cm;
284
285 for (cm = signal_map; cm->name != NULL; cm++) {
286 if (cm->ex == ex) {
287 return cm->flag;
288 }
289 }
290
291 PyErr_SetString(PyExc_KeyError, invalid_signals_err);
292 return DEC_INVALID_SIGNALS;
293 }
294
295 static PyObject *
296 flags_as_list(uint32_t flags)
297 {
298 PyObject *list;
299 DecCondMap *cm;
300
301 list = PyList_New(0);
302 if (list == NULL) {
303 return NULL;
304 }
305
306 for (cm = cond_map; cm->name != NULL; cm++) {
307 if (flags&cm->flag) {
308 if (PyList_Append(list, cm->ex) < 0) {
309 goto error;
310 }
311 }
312 }
313 for (cm = signal_map+1; cm->name != NULL; cm++) {
314 if (flags&cm->flag) {
315 if (PyList_Append(list, cm->ex) < 0) {
316 goto error;
317 }
318 }
319 }
320
321 return list;
322
323 error:
324 Py_DECREF(list);
325 return NULL;
326 }
327
328 static PyObject *
329 signals_as_list(uint32_t flags)
330 {
331 PyObject *list;
332 DecCondMap *cm;
333
334 list = PyList_New(0);
335 if (list == NULL) {
336 return NULL;
337 }
338
339 for (cm = signal_map; cm->name != NULL; cm++) {
340 if (flags&cm->flag) {
341 if (PyList_Append(list, cm->ex) < 0) {
342 Py_DECREF(list);
343 return NULL;
344 }
345 }
346 }
347
348 return list;
349 }
350
351 static uint32_t
352 list_as_flags(PyObject *list)
353 {
354 PyObject *item;
355 uint32_t flags, x;
356 Py_ssize_t n, j;
357
358 assert(PyList_Check(list));
359
360 n = PyList_Size(list);
361 flags = 0;
362 for (j = 0; j < n; j++) {
363 item = PyList_GetItem(list, j);
364 x = exception_as_flag(item);
365 if (x & DEC_ERRORS) {
366 return x;
367 }
368 flags |= x;
369 }
370
371 return flags;
372 }
373
374 static PyObject *
375 flags_as_dict(uint32_t flags)
376 {
377 DecCondMap *cm;
378 PyObject *dict;
379
380 dict = PyDict_New();
381 if (dict == NULL) {
382 return NULL;
383 }
384
385 for (cm = signal_map; cm->name != NULL; cm++) {
386 PyObject *b = flags&cm->flag ? Py_True : Py_False;
387 if (PyDict_SetItem(dict, cm->ex, b) < 0) {
388 Py_DECREF(dict);
389 return NULL;
390 }
391 }
392
393 return dict;
394 }
395
396 static uint32_t
397 dict_as_flags(PyObject *val)
398 {
399 PyObject *b;
400 DecCondMap *cm;
401 uint32_t flags = 0;
402 int x;
403
404 if (!PyDict_Check(val)) {
405 PyErr_SetString(PyExc_TypeError,
406 "argument must be a signal dict");
407 return DEC_INVALID_SIGNALS;
408 }
409
410 for (cm = signal_map; cm->name != NULL; cm++) {
411 b = PyDict_GetItemWithError(val, cm->ex);
412 if (b == NULL) {
413 if (PyErr_Occurred()) {
414 return DEC_ERR_OCCURRED;
415 }
416 PyErr_SetString(PyExc_KeyError,
417 "invalid signal dict");
418 return DEC_INVALID_SIGNALS;
419 }
420
421 x = PyObject_IsTrue(b);
422 if (x < 0) {
423 return DEC_ERR_OCCURRED;
424 }
425 if (x == 1) {
426 flags |= cm->flag;
427 }
428 }
429
430 return flags;
431 }
432
433 #ifdef EXTRA_FUNCTIONALITY
434 static uint32_t
435 long_as_flags(PyObject *v)
436 {
437 long x;
438
439 x = PyLong_AsLong(v);
440 if (x == -1 && PyErr_Occurred()) {
441 return DEC_ERR_OCCURRED;
442 }
443 if (x < 0 || x > (long)MPD_Max_status) {
444 PyErr_SetString(PyExc_TypeError, invalid_flags_err);
445 return DEC_INVALID_SIGNALS;
446 }
447
448 return x;
449 }
450 #endif
451
452 static int
453 dec_addstatus(PyObject *context, uint32_t status)
454 {
455 mpd_context_t *ctx = CTX(context);
456
457 ctx->status |= status;
458 if (ctx->traps&status) {
459 PyObject *ex, *siglist;
460
461 ex = flags_as_exception(ctx->traps&status);
462 if (ex == NULL) {
463 return 1; /* GCOV_NOT_REACHED */
464 }
465 siglist = flags_as_list(ctx->traps&status);
466 if (siglist == NULL) {
467 return 1;
468 }
469
470 PyErr_SetObject(ex, siglist);
471 Py_DECREF(siglist);
472 return 1;
473 }
474 return 0;
475 }
476
477
478 /******************************************************************************/
479 /* SignalDict Object */
480 /******************************************************************************/
481
482 /* The SignalDict is a MutableMapping that provides access to the
483 mpd_context_t flags, which reside in the context object. When a
484 new context is created, context.traps and context.flags are
485 initialized to new SignalDicts. Once a SignalDict is tied to
486 a context, it cannot be deleted. */
487
488 static int
489 signaldict_init(PyObject *self, PyObject *args UNUSED, PyObject *kwds UNUSED)
490 {
491 SdFlagAddr(self) = NULL;
492 return 0;
493 }
494
495 static Py_ssize_t
496 signaldict_len(PyObject *self UNUSED)
497 {
498 return SIGNAL_MAP_LEN;
499 }
500
501 static PyObject *SignalTuple;
502 static PyObject *
503 signaldict_iter(PyObject *self UNUSED)
504 {
505 return PyTuple_Type.tp_iter(SignalTuple);
506 }
507
508 static PyObject *
509 signaldict_getitem(PyObject *self, PyObject *key)
510 {
511 uint32_t flag;
512
513 flag = exception_as_flag(key);
514 if (flag & DEC_ERRORS) {
515 return NULL;
516 }
517
518 return SdFlags(self)&flag ? incr_true() : incr_false();
519 }
520
521 static int
522 signaldict_setitem(PyObject *self, PyObject *key, PyObject *value)
523 {
524 uint32_t flag;
525 int x;
526
527 if (value == NULL) {
528 return value_error_int("signal keys cannot be deleted");
529 }
530
531 flag = exception_as_flag(key);
532 if (flag & DEC_ERRORS) {
533 return -1;
534 }
535
536 x = PyObject_IsTrue(value);
537 if (x < 0) {
538 return -1;
539 }
540
541 if (x == 1) {
542 SdFlags(self) |= flag;
543 }
544 else {
545 SdFlags(self) &= ~flag;
546 }
547
548 return 0;
549 }
550
551 static PyObject *
552 signaldict_repr(PyObject *self)
553 {
554 DecCondMap *cm;
555 const char *n[SIGNAL_MAP_LEN]; /* name */
556 const char *b[SIGNAL_MAP_LEN]; /* bool */
557 int i;
558
559 assert(SIGNAL_MAP_LEN == 9);
560
561 for (cm=signal_map, i=0; cm->name != NULL; cm++, i++) {
562 n[i] = cm->fqname;
563 b[i] = SdFlags(self)&cm->flag ? "True" : "False";
564 }
565 return PyUnicode_FromFormat(
566 "{<class '%s'>:%s, <class '%s'>:%s, <class '%s'>:%s, "
567 "<class '%s'>:%s, <class '%s'>:%s, <class '%s'>:%s, "
568 "<class '%s'>:%s, <class '%s'>:%s, <class '%s'>:%s}",
569 n[0], b[0], n[1], b[1], n[2], b[2],
570 n[3], b[3], n[4], b[4], n[5], b[5],
571 n[6], b[6], n[7], b[7], n[8], b[8]);
572 }
573
574 static PyObject *
575 signaldict_richcompare(PyObject *v, PyObject *w, int op)
576 {
577 PyObject *res = Py_NotImplemented;
578
579 assert(PyDecSignalDict_Check(v));
580
581 if (op == Py_EQ || op == Py_NE) {
582 if (PyDecSignalDict_Check(w)) {
583 res = (SdFlags(v)==SdFlags(w)) ^ (op==Py_NE) ? Py_True : Py_False;
584 }
585 else if (PyDict_Check(w)) {
586 uint32_t flags = dict_as_flags(w);
587 if (flags & DEC_ERRORS) {
588 if (flags & DEC_INVALID_SIGNALS) {
589 /* non-comparable: Py_NotImplemented */
590 PyErr_Clear();
591 }
592 else {
593 return NULL;
594 }
595 }
596 else {
597 res = (SdFlags(v)==flags) ^ (op==Py_NE) ? Py_Tru e : Py_False;
598 }
599 }
600 }
601
602 Py_INCREF(res);
603 return res;
604 }
605
606 static PyObject *
607 signaldict_copy(PyObject *self)
608 {
609 return flags_as_dict(SdFlags(self));
610 }
611
612
613 static PyMappingMethods signaldict_as_mapping = {
614 (lenfunc)signaldict_len, /* mp_length */
615 (binaryfunc)signaldict_getitem, /* mp_subscript */
616 (objobjargproc)signaldict_setitem /* mp_ass_subscript */
617 };
618
619 static PyMethodDef signaldict_methods[] = {
620 { "copy", (PyCFunction)signaldict_copy, METH_NOARGS, NULL},
621 {NULL, NULL}
622 };
623
624
625 static PyTypeObject PyDecSignalDictMixin_Type =
626 {
627 PyVarObject_HEAD_INIT(0, 0)
628 "decimal.SignalDictMixin", /* tp_name */
629 sizeof(PyDecSignalDictObject), /* tp_basicsize */
630 0, /* tp_itemsize */
631 0, /* tp_dealloc */
632 0, /* tp_print */
633 (getattrfunc) 0, /* tp_getattr */
634 (setattrfunc) 0, /* tp_setattr */
635 0, /* tp_reserved */
636 (reprfunc) signaldict_repr, /* tp_repr */
637 0, /* tp_as_number */
638 0, /* tp_as_sequence */
639 &signaldict_as_mapping, /* tp_as_mapping */
640 PyObject_HashNotImplemented, /* tp_hash */
641 0, /* tp_call */
642 (reprfunc) 0, /* tp_str */
643 PyObject_GenericGetAttr, /* tp_getattro */
644 (setattrofunc) 0, /* tp_setattro */
645 (PyBufferProcs *) 0, /* tp_as_buffer */
646 Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE|
647 Py_TPFLAGS_HAVE_GC, /* tp_flags */
648 0, /* tp_doc */
649 0, /* tp_traverse */
650 0, /* tp_clear */
651 signaldict_richcompare, /* tp_richcompare */
652 0, /* tp_weaklistoffset */
653 (getiterfunc)signaldict_iter, /* tp_iter */
654 0, /* tp_iternext */
655 signaldict_methods, /* tp_methods */
656 0, /* tp_members */
657 0, /* tp_getset */
658 0, /* tp_base */
659 0, /* tp_dict */
660 0, /* tp_descr_get */
661 0, /* tp_descr_set */
662 0, /* tp_dictoffset */
663 (initproc)signaldict_init, /* tp_init */
664 0, /* tp_alloc */
665 PyType_GenericNew, /* tp_new */
666 };
667
668
669 /******************************************************************************/
670 /* Context Object, Part 1 */
671 /******************************************************************************/
672
673 #define Dec_CONTEXT_GET_SSIZE(mem) \
674 static PyObject * \
675 context_get##mem(PyObject *self, void *closure UNUSED) \
676 { \
677 return PyLong_FromSsize_t(mpd_get##mem(CTX(self))); \
678 }
679
680 #define Dec_CONTEXT_GET_ULONG(mem) \
681 static PyObject * \
682 context_get##mem(PyObject *self, void *closure UNUSED) \
683 { \
684 return PyLong_FromUnsignedLong(mpd_get##mem(CTX(self))); \
685 }
686
687 Dec_CONTEXT_GET_SSIZE(prec)
688 Dec_CONTEXT_GET_SSIZE(emax)
689 Dec_CONTEXT_GET_SSIZE(emin)
690 Dec_CONTEXT_GET_SSIZE(round)
691 Dec_CONTEXT_GET_SSIZE(clamp)
692
693 #ifdef EXTRA_FUNCTIONALITY
694 Dec_CONTEXT_GET_ULONG(traps)
695 Dec_CONTEXT_GET_ULONG(status)
696 #endif
697
698 static PyObject *
699 context_getcapitals(PyObject *self, void *closure UNUSED)
700 {
701 return PyLong_FromLong(CtxCaps(self));
702 }
703
704 #ifdef EXTRA_FUNCTIONALITY
705 static PyObject *
706 context_getallcr(PyObject *self, void *closure UNUSED)
707 {
708 return PyLong_FromLong(mpd_getcr(CTX(self)));
709 }
710 #endif
711
712 static PyObject *
713 context_getetiny(PyObject *self, PyObject *dummy UNUSED)
714 {
715 return PyLong_FromSsize_t(mpd_etiny(CTX(self)));
716 }
717
718 static PyObject *
719 context_getetop(PyObject *self, PyObject *dummy UNUSED)
720 {
721 return PyLong_FromSsize_t(mpd_etop(CTX(self)));
722 }
723
724 static int
725 context_setprec(PyObject *self, PyObject *value, void *closure UNUSED)
726 {
727 mpd_context_t *ctx;
728 mpd_ssize_t x;
729
730 x = PyLong_AsSsize_t(value);
731 if (x == -1 && PyErr_Occurred()) {
732 return -1;
733 }
734
735 ctx = CTX(self);
736 if (!mpd_qsetprec(ctx, x)) {
737 return value_error_int(
738 "valid range for prec is [1, MAX_PREC]");
739 }
740
741 return 0;
742 }
743
744 static int
745 context_setemin(PyObject *self, PyObject *value, void *closure UNUSED)
746 {
747 mpd_context_t *ctx;
748 mpd_ssize_t x;
749
750 x = PyLong_AsSsize_t(value);
751 if (x == -1 && PyErr_Occurred()) {
752 return -1;
753 }
754
755 ctx = CTX(self);
756 if (!mpd_qsetemin(ctx, x)) {
757 return value_error_int(
758 "valid range for Emin is [MIN_EMIN, 0]");
759 }
760
761 return 0;
762 }
763
764 static int
765 context_setemax(PyObject *self, PyObject *value, void *closure UNUSED)
766 {
767 mpd_context_t *ctx;
768 mpd_ssize_t x;
769
770 x = PyLong_AsSsize_t(value);
771 if (x == -1 && PyErr_Occurred()) {
772 return -1;
773 }
774
775 ctx = CTX(self);
776 if (!mpd_qsetemax(ctx, x)) {
777 return value_error_int(
778 "valid range for Emax is [0, MAX_EMAX]");
779 }
780
781 return 0;
782 }
783
784 #ifdef CONFIG_32
785 static PyObject *
786 context_unsafe_setprec(PyObject *self, PyObject *value)
787 {
788 mpd_context_t *ctx = CTX(self);
789 mpd_ssize_t x;
790
791 x = PyLong_AsSsize_t(value);
792 if (x == -1 && PyErr_Occurred()) {
793 return NULL;
794 }
795
796 if (x < 1 || x > 1070000000L) {
797 return value_error_ptr(
798 "valid range for unsafe prec is [1, 1070000000]");
799 }
800
801 ctx->prec = x;
802 Py_RETURN_NONE;
803 }
804
805 static PyObject *
806 context_unsafe_setemin(PyObject *self, PyObject *value)
807 {
808 mpd_context_t *ctx = CTX(self);
809 mpd_ssize_t x;
810
811 x = PyLong_AsSsize_t(value);
812 if (x == -1 && PyErr_Occurred()) {
813 return NULL;
814 }
815
816 if (x < -1070000000L || x > 0) {
817 return value_error_ptr(
818 "valid range for unsafe emin is [-1070000000, 0]");
819 }
820
821 ctx->emin = x;
822 Py_RETURN_NONE;
823 }
824
825 static PyObject *
826 context_unsafe_setemax(PyObject *self, PyObject *value)
827 {
828 mpd_context_t *ctx = CTX(self);
829 mpd_ssize_t x;
830
831 x = PyLong_AsSsize_t(value);
832 if (x == -1 && PyErr_Occurred()) {
833 return NULL;
834 }
835
836 if (x < 0 || x > 1070000000L) {
837 return value_error_ptr(
838 "valid range for unsafe emax is [0, 1070000000]");
839 }
840
841 ctx->emax = x;
842 Py_RETURN_NONE;
843 }
844 #endif
845
846 static int
847 context_setround(PyObject *self, PyObject *value, void *closure UNUSED)
848 {
849 mpd_context_t *ctx;
850 mpd_ssize_t x;
851
852 x = PyLong_AsSsize_t(value);
853 if (x == -1 && PyErr_Occurred()) {
854 return -1;
855 }
856 BOUNDS_CHECK(x, INT_MIN, INT_MAX);
857
858 ctx = CTX(self);
859 if (!mpd_qsetround(ctx, (int)x)) {
860 return type_error_int(invalid_rounding_err);
861 }
862
863 return 0;
864 }
865
866 static int
867 context_setcapitals(PyObject *self, PyObject *value, void *closure UNUSED)
868 {
869 mpd_ssize_t x;
870
871 x = PyLong_AsSsize_t(value);
872 if (x == -1 && PyErr_Occurred()) {
873 return -1;
874 }
875
876 if (x != 0 && x != 1) {
877 return value_error_int(
878 "valid values for capitals are 0 or 1");
879 }
880 CtxCaps(self) = (int)x;
881
882 return 0;
883 }
884
885 #ifdef EXTRA_FUNCTIONALITY
886 static int
887 context_settraps(PyObject *self, PyObject *value, void *closure UNUSED)
888 {
889 mpd_context_t *ctx;
890 uint32_t flags;
891
892 flags = long_as_flags(value);
893 if (flags & DEC_ERRORS) {
894 return -1;
895 }
896
897 ctx = CTX(self);
898 if (!mpd_qsettraps(ctx, flags)) {
899 INTERNAL_ERROR_INT("context_settraps");
900 }
901
902 return 0;
903 }
904 #endif
905
906 static int
907 context_settraps_list(PyObject *self, PyObject *value)
908 {
909 mpd_context_t *ctx;
910 uint32_t flags;
911
912 flags = list_as_flags(value);
913 if (flags & DEC_ERRORS) {
914 return -1;
915 }
916
917 ctx = CTX(self);
918 if (!mpd_qsettraps(ctx, flags)) {
919 INTERNAL_ERROR_INT("context_settraps_list");
920 }
921
922 return 0;
923 }
924
925 static int
926 context_settraps_dict(PyObject *self, PyObject *value)
927 {
928 mpd_context_t *ctx;
929 uint32_t flags;
930
931 if (PyDecSignalDict_Check(value)) {
932 flags = SdFlags(value);
933 }
934 else {
935 flags = dict_as_flags(value);
936 if (flags & DEC_ERRORS) {
937 return -1;
938 }
939 }
940
941 ctx = CTX(self);
942 if (!mpd_qsettraps(ctx, flags)) {
943 INTERNAL_ERROR_INT("context_settraps_dict");
944 }
945
946 return 0;
947 }
948
949 #ifdef EXTRA_FUNCTIONALITY
950 static int
951 context_setstatus(PyObject *self, PyObject *value, void *closure UNUSED)
952 {
953 mpd_context_t *ctx;
954 uint32_t flags;
955
956 flags = long_as_flags(value);
957 if (flags & DEC_ERRORS) {
958 return -1;
959 }
960
961 ctx = CTX(self);
962 if (!mpd_qsetstatus(ctx, flags)) {
963 INTERNAL_ERROR_INT("context_setstatus");
964 }
965
966 return 0;
967 }
968 #endif
969
970 static int
971 context_setstatus_list(PyObject *self, PyObject *value)
972 {
973 mpd_context_t *ctx;
974 uint32_t flags;
975
976 flags = list_as_flags(value);
977 if (flags & DEC_ERRORS) {
978 return -1;
979 }
980
981 ctx = CTX(self);
982 if (!mpd_qsetstatus(ctx, flags)) {
983 INTERNAL_ERROR_INT("context_setstatus_list");
984 }
985
986 return 0;
987 }
988
989 static int
990 context_setstatus_dict(PyObject *self, PyObject *value)
991 {
992 mpd_context_t *ctx;
993 uint32_t flags;
994
995 if (PyDecSignalDict_Check(value)) {
996 flags = SdFlags(value);
997 }
998 else {
999 flags = dict_as_flags(value);
1000 if (flags & DEC_ERRORS) {
1001 return -1;
1002 }
1003 }
1004
1005 ctx = CTX(self);
1006 if (!mpd_qsetstatus(ctx, flags)) {
1007 INTERNAL_ERROR_INT("context_setstatus_dict");
1008 }
1009
1010 return 0;
1011 }
1012
1013 static int
1014 context_setclamp(PyObject *self, PyObject *value, void *closure UNUSED)
1015 {
1016 mpd_context_t *ctx;
1017 mpd_ssize_t x;
1018
1019 x = PyLong_AsSsize_t(value);
1020 if (x == -1 && PyErr_Occurred()) {
1021 return -1;
1022 }
1023 BOUNDS_CHECK(x, INT_MIN, INT_MAX);
1024
1025 ctx = CTX(self);
1026 if (!mpd_qsetclamp(ctx, (int)x)) {
1027 return value_error_int("valid values for clamp are 0 or 1");
1028 }
1029
1030 return 0;
1031 }
1032
1033 #ifdef EXTRA_FUNCTIONALITY
1034 static int
1035 context_setallcr(PyObject *self, PyObject *value, void *closure UNUSED)
1036 {
1037 mpd_context_t *ctx;
1038 mpd_ssize_t x;
1039
1040 x = PyLong_AsSsize_t(value);
1041 if (x == -1 && PyErr_Occurred()) {
1042 return -1;
1043 }
1044 BOUNDS_CHECK(x, INT_MIN, INT_MAX);
1045
1046 ctx = CTX(self);
1047 if (!mpd_qsetcr(ctx, (int)x)) {
1048 return value_error_int("valid values for _allcr are 0 or 1");
1049 }
1050
1051 return 0;
1052 }
1053 #endif
1054
1055 static PyObject *
1056 context_getattr(PyObject *self, PyObject *name)
1057 {
1058 PyObject *retval;
1059
1060 if (PyUnicode_Check(name)) {
1061 if (PyUnicode_CompareWithASCIIString(name, "traps") == 0) {
1062 retval = ((PyDecContextObject *)self)->traps;
1063 Py_INCREF(retval);
1064 return retval;
1065 }
1066 if (PyUnicode_CompareWithASCIIString(name, "flags") == 0) {
1067 retval = ((PyDecContextObject *)self)->flags;
1068 Py_INCREF(retval);
1069 return retval;
1070 }
1071 }
1072
1073 return PyObject_GenericGetAttr(self, name);
1074 }
1075
1076 static int
1077 context_setattr(PyObject *self, PyObject *name, PyObject *value)
1078 {
1079 if (value == NULL) {
1080 PyErr_SetString(PyExc_AttributeError,
1081 "context attributes cannot be deleted");
1082 return -1;
1083 }
1084
1085 if (PyUnicode_Check(name)) {
1086 if (PyUnicode_CompareWithASCIIString(name, "traps") == 0) {
1087 return context_settraps_dict(self, value);
1088 }
1089 if (PyUnicode_CompareWithASCIIString(name, "flags") == 0) {
1090 return context_setstatus_dict(self, value);
1091 }
1092 }
1093
1094 return PyObject_GenericSetAttr(self, name, value);
1095 }
1096
1097 static PyObject *
1098 context_clear_traps(PyObject *self, PyObject *dummy UNUSED)
1099 {
1100 CTX(self)->traps = 0;
1101 Py_RETURN_NONE;
1102 }
1103
1104 static PyObject *
1105 context_clear_flags(PyObject *self, PyObject *dummy UNUSED)
1106 {
1107 CTX(self)->status = 0;
1108 Py_RETURN_NONE;
1109 }
1110
1111 static PyObject *
1112 context_new(PyTypeObject *type UNUSED, PyObject *args UNUSED,
1113 PyObject *kwds UNUSED)
1114 {
1115 PyDecContextObject *self = NULL;
1116 mpd_context_t *ctx;
1117
1118 self = PyObject_New(PyDecContextObject, &PyDecContext_Type);
1119 if (self == NULL) {
1120 return NULL;
1121 }
1122
1123 self->traps = PyObject_CallObject((PyObject *)PyDecSignalDict_Type, NULL );
1124 if (self->traps == NULL) {
1125 self->flags = NULL;
1126 Py_DECREF(self);
1127 return NULL;
1128 }
1129 self->flags = PyObject_CallObject((PyObject *)PyDecSignalDict_Type, NULL );
1130 if (self->flags == NULL) {
1131 Py_DECREF(self);
1132 return NULL;
1133 }
1134
1135 ctx = CTX(self);
1136 SdFlagAddr(self->traps) = &ctx->traps;
1137 SdFlagAddr(self->flags) = &ctx->status;
1138
1139 return (PyObject *)self;
1140 }
1141
1142 static void
1143 context_dealloc(PyDecContextObject *self)
1144 {
1145 Py_XDECREF(self->traps);
1146 Py_XDECREF(self->flags);
1147 PyObject_Del(self);
1148 }
1149
1150 #ifdef CONFIG_64
1151 #define DEC_DFLT_EMAX 999999999
1152 #define DEC_DFLT_EMIN -999999999
1153 #else
1154 #define DEC_DFLT_EMAX MPD_MAX_EMAX
1155 #define DEC_DFLT_EMIN MPD_MIN_EMIN
1156 #endif
1157
1158 static mpd_context_t dflt_ctx = {
1159 28, DEC_DFLT_EMAX, DEC_DFLT_EMIN,
1160 MPD_IEEE_Invalid_operation|MPD_Division_by_zero|MPD_Overflow,
1161 0, 0, MPD_ROUND_HALF_EVEN, 0, 1
1162 };
1163
1164 static int
1165 getround(PyObject *v)
1166 {
1167 const char *s;
1168 long x;
1169 int i;
1170
1171 if (PyLong_Check(v)) {
1172 x = PyLong_AsLong(v);
1173 if (x == -1 && PyErr_Occurred()) {
1174 return -1;
1175 }
1176 BOUNDS_CHECK(x, 0, INT_MAX);
1177 return (int)x;
1178 }
1179 else if (PyUnicode_Check(v)) {
1180 for (i = 0; i < MPD_ROUND_GUARD; i++) {
1181 s = mpd_round_string[i];
1182 if (PyUnicode_CompareWithASCIIString(v, s) == 0) {
1183 return i;
1184 }
1185 }
1186 }
1187
1188 return type_error_int("invalid rounding mode");
1189 }
1190
1191 static int
1192 context_init(PyObject *self, PyObject *args, PyObject *kwds)
1193 {
1194 static char *kwlist[] = {
1195 "prec", "rounding", "Emin", "Emax", "capitals", "clamp",
1196 "flags", "traps", "_allcr", NULL
1197 };
1198 PyObject *rounding = NULL;
1199 PyObject *traps = NULL;
1200 PyObject *status = NULL;
1201 mpd_context_t *ctx, t=dflt_ctx;
1202 int capitals = 1;
1203 int ret;
1204
1205 assert(PyTuple_Check(args));
1206 ctx = CTX(self);
1207
1208 if (default_context_template) {
1209 t = *CTX(default_context_template);
1210 }
1211 if (!PyArg_ParseTupleAndKeywords(
1212 args, kwds,
1213 "|nOnniiOOi", kwlist,
1214 &t.prec, &rounding, &t.emin, &t.emax, &capitals, &t.clamp,
1215 &status, &traps, &t.allcr
1216 )) {
1217 return -1;
1218 }
1219 if (rounding != NULL) {
1220 t.round = getround(rounding);
1221 if (t.round < 0) {
1222 return -1;
1223 }
1224 }
1225
1226 if (!mpd_qsetprec(ctx, t.prec) ||
1227 !mpd_qsetemin(ctx, t.emin) ||
1228 !mpd_qsetemax(ctx, t.emax) ||
1229 !mpd_qsetclamp(ctx, t.clamp) ||
1230 !mpd_qsetcr(ctx, t.allcr)) {
1231 return value_error_int("invalid context");
1232 }
1233 if (!mpd_qsetround(ctx, t.round) ||
1234 !mpd_qsettraps(ctx, t.traps) ||
1235 !mpd_qsetstatus(ctx, t.status)) {
1236 return type_error_int("invalid context");
1237 }
1238
1239 if (capitals != 0 && capitals != 1) {
1240 return value_error_int("invalid context");
1241 }
1242 CtxCaps(self) = capitals;
1243
1244 if (traps != NULL) {
1245 if (PyList_Check(traps)) {
1246 ret = context_settraps_list(self, traps);
1247 }
1248 #ifdef EXTRA_FUNCTIONALITY
1249 else if (PyLong_Check(traps)) {
1250 ret = context_settraps(self, traps, NULL);
1251 }
1252 #endif
1253 else {
1254 ret = context_settraps_dict(self, traps);
1255 }
1256 if (ret < 0) {
1257 return ret;
1258 }
1259 }
1260 if (status != NULL) {
1261 if (PyList_Check(status)) {
1262 ret = context_setstatus_list(self, status);
1263 }
1264 #ifdef EXTRA_FUNCTIONALITY
1265 else if (PyLong_Check(status)) {
1266 ret = context_setstatus(self, status, NULL);
1267 }
1268 #endif
1269 else {
1270 ret = context_setstatus_dict(self, status);
1271 }
1272 if (ret < 0) {
1273 return ret;
1274 }
1275 }
1276
1277 return 0;
1278 }
1279
1280 static PyObject *
1281 context_repr(PyDecContextObject *self)
1282 {
1283 mpd_context_t *ctx;
1284 char flags[MPD_MAX_SIGNAL_LIST];
1285 char traps[MPD_MAX_SIGNAL_LIST];
1286 int n, mem;
1287
1288 assert(PyDecContext_Check(self));
1289 ctx = CTX(self);
1290
1291 mem = MPD_MAX_SIGNAL_LIST;
1292 n = mpd_lsnprint_signals(flags, mem, ctx->status, dec_signal_string);
1293 if (n < 0 || n >= mem) {
1294 INTERNAL_ERROR_PTR("context_repr");
1295 }
1296
1297 n = mpd_lsnprint_signals(traps, mem, ctx->traps, dec_signal_string);
1298 if (n < 0 || n >= mem) {
1299 INTERNAL_ERROR_PTR("context_repr");
1300 }
1301
1302 return PyUnicode_FromFormat(
1303 "Context(prec=%zd, rounding=%s, Emin=%zd, Emax=%zd, "
1304 "capitals=%d, clamp=%d, flags=%s, traps=%s)",
1305 ctx->prec, mpd_round_string[ctx->round], ctx->emin, ctx->emax,
1306 self->capitals, ctx->clamp, flags, traps);
1307 }
1308
1309 static void
1310 init_basic_context(PyObject *v)
1311 {
1312 mpd_context_t ctx = dflt_ctx;
1313
1314 ctx.prec = 9;
1315 ctx.traps |= (MPD_Underflow|MPD_Clamped);
1316 ctx.round = MPD_ROUND_HALF_UP;
1317
1318 *CTX(v) = ctx;
1319 CtxCaps(v) = 1;
1320 }
1321
1322 static void
1323 init_extended_context(PyObject *v)
1324 {
1325 mpd_context_t ctx = dflt_ctx;
1326
1327 ctx.prec = 9;
1328 ctx.traps = 0;
1329
1330 *CTX(v) = ctx;
1331 CtxCaps(v) = 1;
1332 }
1333
1334 #ifdef EXTRA_FUNCTIONALITY
1335 /* Factory function for creating IEEE interchange format contexts */
1336 static PyObject *
1337 ieee_context(PyObject *dummy UNUSED, PyObject *v)
1338 {
1339 PyObject *context;
1340 mpd_ssize_t bits;
1341 mpd_context_t ctx;
1342
1343 bits = PyLong_AsSsize_t(v);
1344 if (bits == -1 && PyErr_Occurred()) {
1345 return NULL;
1346 }
1347 if (bits <= 0 || bits > INT_MAX) {
1348 goto error;
1349 }
1350 if (mpd_ieee_context(&ctx, (int)bits) < 0) {
1351 goto error;
1352 }
1353
1354 context = PyObject_CallObject((PyObject *)&PyDecContext_Type, NULL);
1355 if (context == NULL) {
1356 return NULL;
1357 }
1358 *CTX(context) = ctx;
1359
1360 return context;
1361
1362 error:
1363 PyErr_Format(PyExc_ValueError,
1364 "argument must be a multiple of 32, with a maximum of %d",
1365 MPD_IEEE_CONTEXT_MAX_BITS);
1366
1367 return NULL;
1368 }
1369 #endif
1370
1371 static PyObject *
1372 context_copy(PyObject *self)
1373 {
1374 PyObject *copy;
1375
1376 copy = PyObject_CallObject((PyObject *)&PyDecContext_Type, NULL);
1377 if (copy == NULL) {
1378 return NULL;
1379 }
1380
1381 *CTX(copy) = *CTX(self);
1382 CTX(copy)->newtrap = 0;
1383 CtxCaps(copy) = CtxCaps(self);
1384
1385 return copy;
1386 }
1387
1388 static PyObject *
1389 context_reduce(PyObject *self, PyObject *args UNUSED)
1390 {
1391 PyObject *flags;
1392 PyObject *traps;
1393 PyObject *ret;
1394 mpd_context_t *ctx;
1395
1396 ctx = CTX(self);
1397
1398 flags = signals_as_list(ctx->status);
1399 if (flags == NULL) {
1400 return NULL;
1401 }
1402 traps = signals_as_list(ctx->traps);
1403 if (traps == NULL) {
1404 Py_DECREF(flags);
1405 return NULL;
1406 }
1407
1408 ret = Py_BuildValue(
1409 "O(nsnniiOO)",
1410 Py_TYPE(self),
1411 ctx->prec, mpd_round_string[ctx->round], ctx->emin, ctx->emax,
1412 CtxCaps(self), ctx->clamp, flags, traps
1413 );
1414
1415 Py_DECREF(flags);
1416 Py_DECREF(traps);
1417 return ret;
1418 }
1419
1420
1421 static PyGetSetDef context_getsets [] =
1422 {
1423 { "prec", (getter)context_getprec, (setter)context_setprec, NULL, NULL},
1424 { "Emax", (getter)context_getemax, (setter)context_setemax, NULL, NULL},
1425 { "Emin", (getter)context_getemin, (setter)context_setemin, NULL, NULL},
1426 { "rounding", (getter)context_getround, (setter)context_setround, NULL, NULL},
1427 { "capitals", (getter)context_getcapitals, (setter)context_setcapitals, NULL, NULL},
1428 { "clamp", (getter)context_getclamp, (setter)context_setclamp, NULL, NULL},
1429 #ifdef EXTRA_FUNCTIONALITY
1430 { "_allcr", (getter)context_getallcr, (setter)context_setallcr, NULL, NULL},
1431 { "_traps", (getter)context_gettraps, (setter)context_settraps, NULL, NULL},
1432 { "_flags", (getter)context_getstatus, (setter)context_setstatus, NULL, NULL},
1433 #endif
1434 {NULL}
1435 };
1436
1437
1438 #define CONTEXT_CHECK(obj) \
1439 if (!PyDecContext_Check(obj)) { \
1440 PyErr_SetString(PyExc_TypeError, \
1441 "argument must be a context"); \
1442 return NULL; \
1443 }
1444
1445 #define CONTEXT_CHECK_VA(obj) \
1446 if (!PyDecContext_Check(obj)) { \
1447 PyErr_SetString(PyExc_TypeError, \
1448 "optional argument must be a context"); \
1449 return NULL; \
1450 }
1451
1452
1453 /******************************************************************************/
1454 /* Global, thread local and temporary contexts */
1455 /******************************************************************************/
1456
1457 #ifdef WITHOUT_THREADS
1458 /* Return borrowed reference to the current context. When compiled
1459 * without threads, this is always the module context. */
1460 static int module_context_set = 0;
1461 static PyObject *
1462 current_context(void)
1463 {
1464 /* In decimal.py, the module context is automatically initialized
1465 * from the DefaultContext when it is first accessed. This
1466 * complicates the code and has a speed penalty of 1-2%. */
1467 if (module_context_set) {
1468 return module_context;
1469 }
1470
1471 *CTX(module_context) = *CTX(default_context_template);
1472 module_context_set = 1;
1473 return module_context;
1474 }
1475
1476 /* ctxobj := borrowed reference to the current context */
1477 #define CURRENT_CONTEXT(ctxobj) \
1478 ctxobj = current_context()
1479
1480 /* ctx := pointer to the mpd_context_t struct of the current context */
1481 #define CURRENT_CONTEXT_ADDR(ctx) \
1482 ctx = CTX(current_context())
1483
1484 /* Return current context, increment reference */
1485 static PyObject *
1486 PyDec_GetCurrentContext(void)
1487 {
1488 PyObject *context;
1489
1490 CURRENT_CONTEXT(context);
1491
1492 Py_INCREF(context);
1493 return context;
1494 }
1495
1496 /* Set the module context to a new context, decrement old reference */
1497 static PyObject *
1498 PyDec_SetCurrentContext(PyObject *self UNUSED, PyObject *v)
1499 {
1500 CONTEXT_CHECK(v);
1501
1502 /* If the new context is one of the templates, make a copy.
1503 * This is the current behavior of decimal.py. */
1504 if (v == default_context_template ||
1505 v == basic_context_template ||
1506 v == extended_context_template) {
1507 v = context_copy(v);
1508 if (v == NULL) {
1509 return NULL;
1510 }
1511 }
1512 else {
1513 Py_INCREF(v);
1514 }
1515
1516 Py_XDECREF(module_context);
1517 module_context = v;
1518 module_context_set = 1;
1519 Py_RETURN_NONE;
1520 }
1521 #else
1522 /*
1523 * Thread local storage currently has a speed penalty of about 16%.
1524 * All functions that map Python's arithmetic operators to mpdecimal
1525 * functions have to look up the current context for each and every
1526 * operation.
1527 */
1528
1529 /* Return borrowed reference to thread local context. */
1530 static PyObject *
1531 current_context(void)
1532 {
1533 PyObject *dict = NULL;
1534 PyObject *tl_context = NULL;
1535
1536 dict = PyThreadState_GetDict();
1537 if (dict == NULL) {
1538 PyErr_SetString(PyExc_RuntimeError,
1539 "cannot get thread state");
1540 return NULL;
1541 }
1542
1543 tl_context = PyDict_GetItemWithError(dict, tls_context_key);
1544 if (tl_context != NULL) {
1545 /* We already have a thread local context and
1546 * return a borrowed reference. */
1547 CONTEXT_CHECK(tl_context);
1548 return tl_context;
1549 }
1550 if (PyErr_Occurred()) {
1551 return NULL;
1552 }
1553
1554 /* Otherwise, set up a new thread local context. */
1555 tl_context = context_copy(default_context_template);
1556 if (tl_context == NULL) {
1557 return NULL;
1558 }
1559 if (PyDict_SetItem(dict, tls_context_key, tl_context) < 0) {
1560 Py_DECREF(tl_context);
1561 return NULL;
1562 }
1563 Py_DECREF(tl_context);
1564
1565 /* refcount is 1 */
1566 return tl_context;
1567 }
1568
1569 /* ctxobj := borrowed reference to the current context */
1570 #define CURRENT_CONTEXT(ctxobj) \
1571 ctxobj = current_context(); \
1572 if (ctxobj == NULL) { \
1573 return NULL; \
1574 }
1575
1576 /* ctx := pointer to the mpd_context_t struct of the current context */
1577 #define CURRENT_CONTEXT_ADDR(ctx) { \
1578 PyObject *_c_t_x_o_b_j = current_context(); \
1579 if (_c_t_x_o_b_j == NULL) { \
1580 return NULL; \
1581 } \
1582 ctx = CTX(_c_t_x_o_b_j); \
1583 }
1584
1585 /* Return current context, increment reference */
1586 static PyObject *
1587 PyDec_GetCurrentContext(void)
1588 {
1589 PyObject *context;
1590
1591 context = current_context();
1592 if (context == NULL) {
1593 return NULL;
1594 }
1595
1596 Py_INCREF(context);
1597 return context;
1598 }
1599
1600 /* Set the thread local context to a new context, decrement old reference */
1601 static PyObject *
1602 PyDec_SetCurrentContext(PyObject *self UNUSED, PyObject *v)
1603 {
1604 PyObject *dict;
1605
1606 CONTEXT_CHECK(v);
1607
1608 dict = PyThreadState_GetDict();
1609 if (dict == NULL) {
1610 PyErr_SetString(PyExc_RuntimeError,
1611 "cannot get thread state");
1612 return NULL;
1613 }
1614
1615 /* If the new context is one of the templates, make a copy.
1616 * This is the current behavior of decimal.py. */
1617 if (v == default_context_template ||
1618 v == basic_context_template ||
1619 v == extended_context_template) {
1620 v = context_copy(v);
1621 if (v == NULL) {
1622 return NULL;
1623 }
1624 }
1625 else {
1626 Py_INCREF(v);
1627 }
1628
1629 if (PyDict_SetItem(dict, tls_context_key, v) < 0) {
1630 Py_DECREF(v);
1631 return NULL;
1632 }
1633
1634 Py_DECREF(v);
1635 Py_RETURN_NONE;
1636 }
1637 #endif
1638
1639 /* Context manager object for the 'with' statement. The manager
1640 * owns one reference to the global (outer) context and one
1641 * to the local (inner) context. */
1642 static PyObject *
1643 ctxmanager_new(PyTypeObject *type UNUSED, PyObject *args)
1644 {
1645 PyDecContextManagerObject *self;
1646 PyObject *local;
1647 PyObject *global;
1648
1649 CURRENT_CONTEXT(global);
1650 local = global;
1651 if (!PyArg_ParseTuple(args, "|O", &local)) {
1652 return NULL;
1653 }
1654 CONTEXT_CHECK_VA(local);
1655
1656 self = PyObject_New(PyDecContextManagerObject,
1657 &PyDecContextManager_Type);
1658 if (self == NULL) {
1659 return NULL;
1660 }
1661
1662 self->local = context_copy(local);
1663 if (self->local == NULL) {
1664 self->global = NULL;
1665 Py_DECREF(self);
1666 return NULL;
1667 }
1668 self->global = global;
1669 Py_INCREF(self->global);
1670
1671 return (PyObject *)self;
1672 }
1673
1674 static void
1675 ctxmanager_dealloc(PyDecContextManagerObject *self)
1676 {
1677 Py_XDECREF(self->local);
1678 Py_XDECREF(self->global);
1679 PyObject_Del(self);
1680 }
1681
1682 static PyObject *
1683 ctxmanager_set_local(PyDecContextManagerObject *self, PyObject *args UNUSED)
1684 {
1685 PyObject *ret;
1686
1687 ret = PyDec_SetCurrentContext(NULL, self->local);
1688 if (ret == NULL) {
1689 return NULL;
1690 }
1691 Py_DECREF(ret);
1692
1693 Py_INCREF(self->local);
1694 return self->local;
1695 }
1696
1697 static PyObject *
1698 ctxmanager_restore_global(PyDecContextManagerObject *self,
1699 PyObject *args UNUSED)
1700 {
1701 PyObject *ret;
1702
1703 ret = PyDec_SetCurrentContext(NULL, self->global);
1704 if (ret == NULL) {
1705 return NULL;
1706 }
1707 Py_DECREF(ret);
1708
1709 Py_RETURN_NONE;
1710 }
1711
1712
1713 static PyMethodDef ctxmanager_methods[] = {
1714 {"__enter__", (PyCFunction)ctxmanager_set_local, METH_NOARGS, NULL},
1715 {"__exit__", (PyCFunction)ctxmanager_restore_global, METH_VARARGS, NULL},
1716 {NULL, NULL}
1717 };
1718
1719 static PyTypeObject PyDecContextManager_Type =
1720 {
1721 PyVarObject_HEAD_INIT(NULL, 0)
1722 "decimal.ContextManager", /* tp_name */
1723 sizeof(PyDecContextManagerObject), /* tp_basicsize */
1724 0, /* tp_itemsize */
1725 (destructor) ctxmanager_dealloc, /* tp_dealloc */
1726 0, /* tp_print */
1727 (getattrfunc) 0, /* tp_getattr */
1728 (setattrfunc) 0, /* tp_setattr */
1729 0, /* tp_compare */
1730 (reprfunc) 0, /* tp_repr */
1731 0, /* tp_as_number */
1732 0, /* tp_as_sequence */
1733 0, /* tp_as_mapping */
1734 0, /* tp_hash */
1735 0, /* tp_call */
1736 0, /* tp_str */
1737 (getattrofunc) PyObject_GenericGetAttr, /* tp_getattro */
1738 (setattrofunc) 0, /* tp_setattro */
1739 (PyBufferProcs *) 0, /* tp_as_buffer */
1740 Py_TPFLAGS_DEFAULT, /* tp_flags */
1741 0, /* tp_doc */
1742 0, /* tp_traverse */
1743 0, /* tp_clear */
1744 0, /* tp_richcompare */
1745 0, /* tp_weaklistoffset */
1746 0, /* tp_iter */
1747 0, /* tp_iternext */
1748 ctxmanager_methods, /* tp_methods */
1749 };
1750
1751
1752 /******************************************************************************/
1753 /* New Decimal Object */
1754 /******************************************************************************/
1755
1756 static PyObject *
1757 PyDecType_New(PyTypeObject *type)
1758 {
1759 PyObject *dec;
1760
1761 if (type == &PyDec_Type) {
1762 dec = (PyObject *)PyObject_New(PyDecObject, &PyDec_Type);
1763 }
1764 else {
1765 dec = type->tp_alloc(type, 0);
1766 }
1767 if (dec == NULL) {
1768 return NULL;
1769 }
1770
1771 MPD(dec) = mpd_qnew();
1772 if (MPD(dec) == NULL) {
1773 Py_DECREF(dec);
1774 PyErr_NoMemory();
1775 return NULL;
1776 }
1777
1778 return dec;
1779 }
1780 #define dec_alloc() PyDecType_New(&PyDec_Type)
1781
1782 static void
1783 dec_dealloc(PyObject *dec)
1784 {
1785 if (MPD(dec)) {
1786 mpd_del(MPD(dec));
1787 }
1788 Py_TYPE(dec)->tp_free(dec);
1789 }
1790
1791
1792 /******************************************************************************/
1793 /* Conversions to Decimal */
1794 /******************************************************************************/
1795
1796 Py_LOCAL_INLINE(int)
1797 is_space(enum PyUnicode_Kind kind, void *data, Py_ssize_t pos)
1798 {
1799 Py_UCS4 ch = PyUnicode_READ(kind, data, pos);
1800 return Py_UNICODE_ISSPACE(ch);
1801 }
1802
1803 /* Return the ASCII representation of a numeric Unicode string. The numeric
1804 string may contain ascii characters in the range [1, 127], any Unicode
1805 space and any unicode digit. If strip_ws is true, leading and trailing
1806 whitespace is stripped.
1807
1808 Return NULL if malloc fails and an empty string if invalid characters
1809 are found. */
1810 static char *
1811 numeric_as_ascii(const PyObject *u, int strip_ws)
1812 {
1813 enum PyUnicode_Kind kind;
1814 void *data;
1815 Py_UCS4 ch;
1816 char *res, *cp;
1817 Py_ssize_t j, len;
1818 int d;
1819
1820 assert(PyUnicode_IS_READY(u));
1821
1822 kind = PyUnicode_KIND(u);
1823 data = PyUnicode_DATA(u);
1824 len = PyUnicode_GET_LENGTH(u);
1825
1826 cp = res = PyMem_Malloc(len+1);
1827 if (res == NULL) {
1828 PyErr_NoMemory();
1829 return NULL;
1830 }
1831
1832 j = 0;
1833 if (strip_ws) {
1834 while (len > 0 && is_space(kind, data, len-1)) {
1835 len--;
1836 }
1837 while (j < len && is_space(kind, data, j)) {
1838 j++;
1839 }
1840 }
1841
1842 for (; j < len; j++) {
1843 ch = PyUnicode_READ(kind, data, j);
1844 if (0 < ch && ch <= 127) {
1845 *cp++ = ch;
1846 continue;
1847 }
1848 if (Py_UNICODE_ISSPACE(ch)) {
1849 *cp++ = ' ';
1850 continue;
1851 }
1852 d = Py_UNICODE_TODECIMAL(ch);
1853 if (d < 0) {
1854 /* empty string triggers ConversionSyntax */
1855 *res = '\0';
1856 return res;
1857 }
1858 *cp++ = '0' + d;
1859 }
1860 *cp = '\0';
1861 return res;
1862 }
1863
1864 /* Return a new PyDecObject or a subtype from a C string. Use the context
1865 during conversion. */
1866 static PyObject *
1867 PyDecType_FromCString(PyTypeObject *type, const char *s,
1868 PyObject *context)
1869 {
1870 PyObject *dec;
1871 uint32_t status = 0;
1872
1873 dec = PyDecType_New(type);
1874 if (dec == NULL) {
1875 return NULL;
1876 }
1877
1878 mpd_qset_string(MPD(dec), s, CTX(context), &status);
1879 if (dec_addstatus(context, status)) {
1880 Py_DECREF(dec);
1881 return NULL;
1882 }
1883 return dec;
1884 }
1885
1886 /* Return a new PyDecObject or a subtype from a C string. Attempt exact
1887 conversion. If the operand cannot be converted exactly, set
1888 InvalidOperation. */
1889 static PyObject *
1890 PyDecType_FromCStringExact(PyTypeObject *type, const char *s,
1891 PyObject *context)
1892 {
1893 PyObject *dec;
1894 uint32_t status = 0;
1895 mpd_context_t maxctx;
1896
1897 dec = PyDecType_New(type);
1898 if (dec == NULL) {
1899 return NULL;
1900 }
1901
1902 mpd_maxcontext(&maxctx);
1903
1904 mpd_qset_string(MPD(dec), s, &maxctx, &status);
1905 if (status & (MPD_Inexact|MPD_Rounded)) {
1906 /* we want exact results */
1907 mpd_seterror(MPD(dec), MPD_Invalid_operation, &status);
1908 }
1909 status &= MPD_Errors;
1910 if (dec_addstatus(context, status)) {
1911 Py_DECREF(dec);
1912 return NULL;
1913 }
1914
1915 return dec;
1916 }
1917
1918 /* Return a new PyDecObject or a subtype from a PyUnicodeObject. */
1919 static PyObject *
1920 PyDecType_FromUnicode(PyTypeObject *type, const PyObject *u,
1921 PyObject *context)
1922 {
1923 PyObject *dec;
1924 char *s;
1925
1926 s = numeric_as_ascii(u, 0);
1927 if (s == NULL) {
1928 return NULL;
1929 }
1930
1931 dec = PyDecType_FromCString(type, s, context);
1932 PyMem_Free(s);
1933 return dec;
1934 }
1935
1936 /* Return a new PyDecObject or a subtype from a PyUnicodeObject. Attempt exact
1937 * conversion. If the conversion is not exact, fail with InvalidOperation.
1938 * Allow leading and trailing whitespace in the input operand. */
1939 static PyObject *
1940 PyDecType_FromUnicodeExactWS(PyTypeObject *type, const PyObject *u,
1941 PyObject *context)
1942 {
1943 PyObject *dec;
1944 char *s;
1945
1946 s = numeric_as_ascii(u, 1);
1947 if (s == NULL) {
1948 return NULL;
1949 }
1950
1951 dec = PyDecType_FromCStringExact(type, s, context);
1952 PyMem_Free(s);
1953 return dec;
1954 }
1955
1956 /* Set PyDecObject from triple without any error checking. */
1957 Py_LOCAL_INLINE(void)
1958 _dec_settriple(PyObject *dec, uint8_t sign, uint32_t v, mpd_ssize_t exp)
1959 {
1960
1961 #ifdef CONFIG_64
1962 MPD(dec)->data[0] = v;
1963 MPD(dec)->len = 1;
1964 #else
1965 uint32_t q, r;
1966 q = v / MPD_RADIX;
1967 r = v - q * MPD_RADIX;
1968 MPD(dec)->data[1] = q;
1969 MPD(dec)->data[0] = r;
1970 MPD(dec)->len = q ? 2 : 1;
1971 #endif
1972 mpd_set_flags(MPD(dec), sign);
1973 MPD(dec)->exp = exp;
1974 mpd_setdigits(MPD(dec));
1975 }
1976
1977 /* Return a new PyDecObject from an mpd_ssize_t. */
1978 static PyObject *
1979 PyDecType_FromSsize(PyTypeObject *type, mpd_ssize_t v, PyObject *context)
1980 {
1981 PyObject *dec;
1982 uint32_t status = 0;
1983
1984 dec = PyDecType_New(type);
1985 if (dec == NULL) {
1986 return NULL;
1987 }
1988
1989 mpd_qset_ssize(MPD(dec), v, CTX(context), &status);
1990 if (dec_addstatus(context, status)) {
1991 Py_DECREF(dec);
1992 return NULL;
1993 }
1994 return dec;
1995 }
1996
1997 /* Return a new PyDecObject from an mpd_ssize_t. Conversion is exact. */
1998 static PyObject *
1999 PyDecType_FromSsizeExact(PyTypeObject *type, mpd_ssize_t v, PyObject *context)
2000 {
2001 PyObject *dec;
2002 uint32_t status = 0;
2003 mpd_context_t maxctx;
2004
2005 dec = PyDecType_New(type);
2006 if (dec == NULL) {
2007 return NULL;
2008 }
2009
2010 mpd_maxcontext(&maxctx);
2011
2012 mpd_qset_ssize(MPD(dec), v, &maxctx, &status);
2013 if (dec_addstatus(context, status)) {
2014 Py_DECREF(dec);
2015 return NULL;
2016 }
2017 return dec;
2018 }
2019
2020 /* Convert from a PyLongObject. The context is not modified; flags set
2021 during conversion are accumulated in the status parameter. */
2022 static PyObject *
2023 dec_from_long(PyTypeObject *type, const PyObject *v,
2024 const mpd_context_t *ctx, uint32_t *status)
2025 {
2026 PyObject *dec;
2027 PyLongObject *l = (PyLongObject *)v;
2028 Py_ssize_t ob_size;
2029 size_t len;
2030 uint8_t sign;
2031
2032 dec = PyDecType_New(type);
2033 if (dec == NULL) {
2034 return NULL;
2035 }
2036
2037 ob_size = Py_SIZE(l);
2038 if (ob_size == 0) {
2039 _dec_settriple(dec, MPD_POS, 0, 0);
2040 return dec;
2041 }
2042
2043 if (ob_size < 0) {
2044 len = -ob_size;
2045 sign = MPD_NEG;
2046 }
2047 else {
2048 len = ob_size;
2049 sign = MPD_POS;
2050 }
2051
2052 if (len == 1) {
2053 _dec_settriple(dec, sign, *l->ob_digit, 0);
2054 mpd_qfinalize(MPD(dec), ctx, status);
2055 return dec;
2056 }
2057
2058 #if PYLONG_BITS_IN_DIGIT == 30
2059 mpd_qimport_u32(MPD(dec), l->ob_digit, len, sign, PyLong_BASE,
2060 ctx, status);
2061 #elif PYLONG_BITS_IN_DIGIT == 15
2062 mpd_qimport_u16(MPD(dec), l->ob_digit, len, sign, PyLong_BASE,
2063 ctx, status);
2064 #else
2065 #error "PYLONG_BITS_IN_DIGIT should be 15 or 30"
2066 #endif
2067
2068 return dec;
2069 }
2070
2071 /* Return a new PyDecObject from a PyLongObject. Use the context for
2072 conversion. */
2073 static PyObject *
2074 PyDecType_FromLong(PyTypeObject *type, const PyObject *pylong,
2075 PyObject *context)
2076 {
2077 PyObject *dec;
2078 uint32_t status = 0;
2079
2080 dec = dec_from_long(type, pylong, CTX(context), &status);
2081 if (dec == NULL) {
2082 return NULL;
2083 }
2084
2085 if (dec_addstatus(context, status)) {
2086 Py_DECREF(dec);
2087 return NULL;
2088 }
2089
2090 return dec;
2091 }
2092
2093 /* Return a new PyDecObject from a PyLongObject. Use a maximum context
2094 for conversion. If the conversion is not exact, set InvalidOperation. */
2095 static PyObject *
2096 PyDecType_FromLongExact(PyTypeObject *type, const PyObject *pylong,
2097 PyObject *context)
2098 {
2099 PyObject *dec;
2100 uint32_t status = 0;
2101 mpd_context_t maxctx;
2102
2103 mpd_maxcontext(&maxctx);
2104 dec = dec_from_long(type, pylong, &maxctx, &status);
2105 if (dec == NULL) {
2106 return NULL;
2107 }
2108
2109 if (status & (MPD_Inexact|MPD_Rounded)) {
2110 /* we want exact results */
2111 mpd_seterror(MPD(dec), MPD_Invalid_operation, &status);
2112 }
2113 status &= MPD_Errors;
2114 if (dec_addstatus(context, status)) {
2115 Py_DECREF(dec);
2116 return NULL;
2117 }
2118
2119 return dec;
2120 }
2121
2122 /* Return a PyDecObject or a subtype from a PyFloatObject.
2123 Conversion is exact. */
2124 static PyObject *
2125 PyDecType_FromFloatExact(PyTypeObject *type, PyObject *v,
2126 PyObject *context)
2127 {
2128 PyObject *dec, *tmp;
2129 PyObject *n, *d, *n_d;
2130 mpd_ssize_t k;
2131 double x;
2132 int sign;
2133 mpd_t *d1, *d2;
2134 uint32_t status = 0;
2135 mpd_context_t maxctx;
2136
2137
2138 assert(PyType_IsSubtype(type, &PyDec_Type));
2139
2140 if (PyLong_Check(v)) {
2141 return PyDecType_FromLongExact(type, v, context);
2142 }
2143 if (!PyFloat_Check(v)) {
2144 PyErr_SetString(PyExc_TypeError,
2145 "argument must be int of float");
2146 return NULL;
2147 }
2148
2149 x = PyFloat_AsDouble(v);
2150 if (x == -1.0 && PyErr_Occurred()) {
2151 return NULL;
2152 }
2153 sign = (copysign(1.0, x) == 1.0) ? 0 : 1;
2154
2155 if (Py_IS_NAN(x) || Py_IS_INFINITY(x)) {
2156 dec = PyDecType_New(type);
2157 if (dec == NULL) {
2158 return NULL;
2159 }
2160 if (Py_IS_NAN(x)) {
2161 /* decimal.py calls repr(float(+-nan)),
2162 * which always gives a positive result. */
2163 mpd_setspecial(MPD(dec), MPD_POS, MPD_NAN);
2164 }
2165 else {
2166 mpd_setspecial(MPD(dec), sign, MPD_INF);
2167 }
2168 return dec;
2169 }
2170
2171 /* absolute value of the float */
2172 tmp = PyObject_CallMethod(v, "__abs__", NULL);
2173 if (tmp == NULL) {
2174 return NULL;
2175 }
2176
2177 /* float as integer ratio: numerator/denominator */
2178 n_d = PyObject_CallMethod(tmp, "as_integer_ratio", NULL);
2179 Py_DECREF(tmp);
2180 if (n_d == NULL) {
2181 return NULL;
2182 }
2183 n = PyTuple_GET_ITEM(n_d, 0);
2184 d = PyTuple_GET_ITEM(n_d, 1);
2185
2186 tmp = PyObject_CallMethod(d, "bit_length", NULL);
2187 if (tmp == NULL) {
2188 Py_DECREF(n_d);
2189 return NULL;
2190 }
2191 k = PyLong_AsSsize_t(tmp);
2192 Py_DECREF(tmp);
2193 if (k == -1 && PyErr_Occurred()) {
2194 Py_DECREF(n_d);
2195 return NULL;
2196 }
2197 k--;
2198
2199 dec = PyDecType_FromLongExact(type, n, context);
2200 Py_DECREF(n_d);
2201 if (dec == NULL) {
2202 return NULL;
2203 }
2204
2205 d1 = mpd_qnew();
2206 if (d1 == NULL) {
2207 Py_DECREF(dec);
2208 PyErr_NoMemory();
2209 return NULL;
2210 }
2211 d2 = mpd_qnew();
2212 if (d2 == NULL) {
2213 mpd_del(d1);
2214 Py_DECREF(dec);
2215 PyErr_NoMemory();
2216 return NULL;
2217 }
2218
2219 mpd_maxcontext(&maxctx);
2220 mpd_qset_uint(d1, 5, &maxctx, &status);
2221 mpd_qset_ssize(d2, k, &maxctx, &status);
2222 mpd_qpow(d1, d1, d2, &maxctx, &status);
2223 if (dec_addstatus(context, status)) {
2224 mpd_del(d1);
2225 mpd_del(d2);
2226 Py_DECREF(dec);
2227 return NULL;
2228 }
2229
2230 /* result = n * 5**k */
2231 mpd_qmul(MPD(dec), MPD(dec), d1, &maxctx, &status);
2232 mpd_del(d1);
2233 mpd_del(d2);
2234 if (dec_addstatus(context, status)) {
2235 Py_DECREF(dec);
2236 return NULL;
2237 }
2238 /* result = +- n * 5**k * 10**-k */
2239 mpd_set_sign(MPD(dec), sign);
2240 MPD(dec)->exp = -k;
2241
2242 return dec;
2243 }
2244
2245 static PyObject *
2246 PyDecType_FromFloat(PyTypeObject *type, PyObject *v,
2247 PyObject *context)
2248 {
2249 PyObject *dec;
2250 uint32_t status = 0;
2251
2252 dec = PyDecType_FromFloatExact(type, v, context);
2253 if (dec == NULL) {
2254 return NULL;
2255 }
2256
2257 mpd_qfinalize(MPD(dec), CTX(context), &status);
2258 if (dec_addstatus(context, status)) {
2259 Py_DECREF(dec);
2260 return NULL;
2261 }
2262
2263 return dec;
2264 }
2265
2266 static PyObject *
2267 sequence_as_tuple(PyObject *v, PyObject *ex, const char *mesg)
2268 {
2269 if (PyTuple_Check(v)) {
2270 Py_INCREF(v);
2271 return v;
2272 }
2273 if (PyList_Check(v)) {
2274 return PyList_AsTuple(v);
2275 }
2276
2277 PyErr_SetString(ex, mesg);
2278 return NULL;
2279 }
2280
2281 /* Return a new C string representation of a DecimalTuple. */
2282 static char *
2283 dectuple_as_str(PyObject *dectuple)
2284 {
2285 PyObject *digits = NULL, *tmp;
2286 char *decstring = NULL;
2287 char sign_special[6];
2288 char *cp;
2289 long sign, l;
2290 mpd_ssize_t exp = 0;
2291 Py_ssize_t i, mem, tsize;
2292 int n;
2293
2294 assert(PyTuple_Check(dectuple));
2295
2296 if (PyTuple_Size(dectuple) != 3) {
2297 PyErr_SetString(PyExc_ValueError,
2298 "argument must be a sequence of length 3");
2299 goto error;
2300 }
2301
2302 /* sign */
2303 tmp = PyTuple_GET_ITEM(dectuple, 0);
2304 if (!PyLong_Check(tmp)) {
2305 PyErr_SetString(PyExc_ValueError,
2306 "sign must be an integer with the value 0 or 1");
2307 goto error;
2308 }
2309 sign = PyLong_AsLong(tmp);
2310 if (sign == -1 && PyErr_Occurred()) {
2311 goto error;
2312 }
2313 if (sign != 0 && sign != 1) {
2314 PyErr_SetString(PyExc_ValueError,
2315 "sign must be an integer with the value 0 or 1");
2316 goto error;
2317 }
2318 sign_special[0] = sign ? '-' : '+';
2319 sign_special[1] = '\0';
2320
2321 /* exponent or encoding for a special number */
2322 tmp = PyTuple_GET_ITEM(dectuple, 2);
2323 if (PyUnicode_Check(tmp)) {
2324 /* special */
2325 if (PyUnicode_CompareWithASCIIString(tmp, "F") == 0) {
2326 strcat(sign_special, "Inf");
2327 }
2328 else if (PyUnicode_CompareWithASCIIString(tmp, "n") == 0) {
2329 strcat(sign_special, "NaN");
2330 }
2331 else if (PyUnicode_CompareWithASCIIString(tmp, "N") == 0) {
2332 strcat(sign_special, "sNaN");
2333 }
2334 else {
2335 PyErr_SetString(PyExc_ValueError,
2336 "string argument in the third position "
2337 "must be 'F', 'n' or 'N'");
2338 goto error;
2339 }
2340 }
2341 else {
2342 /* exponent */
2343 if (!PyLong_Check(tmp)) {
2344 PyErr_SetString(PyExc_ValueError,
2345 "exponent must be an integer");
2346 goto error;
2347 }
2348 exp = PyLong_AsSsize_t(tmp);
2349 if (exp == -1 && PyErr_Occurred()) {
2350 goto error;
2351 }
2352 }
2353
2354 /* coefficient */
2355 digits = sequence_as_tuple(PyTuple_GET_ITEM(dectuple, 1),
2356 PyExc_ValueError, "coefficient must be a tuple of digits");
2357 if (digits == NULL) {
2358 goto error;
2359 }
2360
2361 tsize = PyTuple_Size(digits);
2362 /* [sign][coeffdigits+1][E][-][expdigits+1]['\0'] */
2363 mem = 1 + tsize + 3 + MPD_EXPDIGITS + 2;
2364 cp = decstring = PyMem_Malloc(mem);
2365 if (decstring == NULL) {
2366 PyErr_NoMemory();
2367 goto error;
2368 }
2369
2370 n = snprintf(cp, mem, "%s", sign_special);
2371 if (n < 0 || n >= mem) {
2372 PyErr_SetString(PyExc_RuntimeError,
2373 "internal error in dec_sequence_as_str");
2374 goto error;
2375 }
2376 cp += n;
2377
2378 if (tsize == 0 && sign_special[1] == '\0') {
2379 /* empty tuple: zero coefficient, except for special numbers */
2380 *cp++ = '0';
2381 }
2382 for (i = 0; i < tsize; i++) {
2383 tmp = PyTuple_GET_ITEM(digits, i);
2384 if (!PyLong_Check(tmp)) {
2385 PyErr_SetString(PyExc_ValueError,
2386 "coefficient must be a tuple of digits");
2387 goto error;
2388 }
2389 l = PyLong_AsLong(tmp);
2390 if (l == -1 && PyErr_Occurred()) {
2391 goto error;
2392 }
2393 if (l < 0 || l > 9) {
2394 PyErr_SetString(PyExc_ValueError,
2395 "coefficient must be a tuple of digits");
2396 goto error;
2397 }
2398 *cp++ = (char)l + '0';
2399 }
2400 *cp = '\0';
2401
2402 if (sign_special[1] == '\0') {
2403 /* not a special number */
2404 *cp++ = 'E';
2405 n = snprintf(cp, MPD_EXPDIGITS+1, "%" PRI_mpd_ssize_t, exp);
2406 if (n < 0 || n >= MPD_EXPDIGITS+1) {
2407 PyErr_SetString(PyExc_RuntimeError,
2408 "internal error in dec_sequence_as_str");
2409 goto error;
2410 }
2411 }
2412
2413 Py_XDECREF(digits);
2414 return decstring;
2415
2416
2417 error:
2418 Py_XDECREF(digits);
2419 if (decstring) PyMem_Free(decstring);
2420 return NULL;
2421 }
2422
2423 /* Currently accepts tuples and lists. */
2424 static PyObject *
2425 PyDecType_FromSequence(PyTypeObject *type, PyObject *v,
2426 PyObject *context)
2427 {
2428 PyObject *dectuple;
2429 PyObject *dec;
2430 char *s;
2431
2432 dectuple = sequence_as_tuple(v, PyExc_TypeError,
2433 "argument must be a tuple or list");
2434 if (dectuple == NULL) {
2435 return NULL;
2436 }
2437
2438 s = dectuple_as_str(dectuple);
2439 Py_DECREF(dectuple);
2440 if (s == NULL) {
2441 return NULL;
2442 }
2443
2444 dec = PyDecType_FromCString(type, s, context);
2445
2446 PyMem_Free(s);
2447 return dec;
2448 }
2449
2450 /* Currently accepts tuples and lists. */
2451 static PyObject *
2452 PyDecType_FromSequenceExact(PyTypeObject *type, PyObject *v,
2453 PyObject *context)
2454 {
2455 PyObject *dectuple;
2456 PyObject *dec;
2457 char *s;
2458
2459 dectuple = sequence_as_tuple(v, PyExc_TypeError,
2460 "argument must be a tuple or list");
2461 if (dectuple == NULL) {
2462 return NULL;
2463 }
2464
2465 s = dectuple_as_str(dectuple);
2466 Py_DECREF(dectuple);
2467 if (s == NULL) {
2468 return NULL;
2469 }
2470
2471 dec = PyDecType_FromCStringExact(type, s, context);
2472
2473 PyMem_Free(s);
2474 return dec;
2475 }
2476
2477 #define PyDec_FromCString(str, context) \
2478 PyDecType_FromCString(&PyDec_Type, str, context)
2479 #define PyDec_FromCStringExact(str, context) \
2480 PyDecType_FromCStringExact(&PyDec_Type, str, context)
2481
2482 #define PyDec_FromUnicode(unicode, context) \
2483 PyDecType_FromUnicode(&PyDec_Type, unicode, context)
2484 #define PyDec_FromUnicodeExact(unicode, context) \
2485 PyDecType_FromUnicodeExact(&PyDec_Type, unicode, context)
2486 #define PyDec_FromUnicodeExactWS(unicode, context) \
2487 PyDecType_FromUnicodeExactWS(&PyDec_Type, unicode, context)
2488
2489 #define PyDec_FromSsize(v, context) \
2490 PyDecType_FromSsize(&PyDec_Type, v, context)
2491 #define PyDec_FromSsizeExact(v, context) \
2492 PyDecType_FromSsizeExact(&PyDec_Type, v, context)
2493
2494 #define PyDec_FromLong(pylong, context) \
2495 PyDecType_FromLong(&PyDec_Type, pylong, context)
2496 #define PyDec_FromLongExact(pylong, context) \
2497 PyDecType_FromLongExact(&PyDec_Type, pylong, context)
2498
2499 #define PyDec_FromFloat(pyfloat, context) \
2500 PyDecType_FromFloat(&PyDec_Type, pyfloat, context)
2501 #define PyDec_FromFloatExact(pyfloat, context) \
2502 PyDecType_FromFloatExact(&PyDec_Type, pyfloat, context)
2503
2504 #define PyDec_FromSequence(sequence, context) \
2505 PyDecType_FromSequence(&PyDec_Type, sequence, context)
2506 #define PyDec_FromSequenceExact(sequence, context) \
2507 PyDecType_FromSequenceExact(&PyDec_Type, sequence, context)
2508
2509 /* class method */
2510 static PyObject *
2511 dec_from_float(PyObject *dec, PyObject *pyfloat)
2512 {
2513 PyObject *context;
2514
2515 CURRENT_CONTEXT(context);
2516 return PyDecType_FromFloatExact((PyTypeObject *)dec, pyfloat, context);
2517 }
2518
2519 /* create_decimal_from_float */
2520 static PyObject *
2521 ctx_from_float(PyObject *context, PyObject *v)
2522 {
2523 return PyDec_FromFloat(v, context);
2524 }
2525
2526 /* Apply the context to the input operand. Return a new PyDecObject. */
2527 static PyObject *
2528 dec_apply(PyObject *v, PyObject *context)
2529 {
2530 PyObject *result;
2531 uint32_t status = 0;
2532
2533 result = dec_alloc();
2534 if (result == NULL) {
2535 return NULL;
2536 }
2537
2538 mpd_qcopy(MPD(result), MPD(v), &status);
2539 if (dec_addstatus(context, status)) {
2540 Py_DECREF(result);
2541 return NULL;
2542 }
2543
2544 mpd_qfinalize(MPD(result), CTX(context), &status);
2545 if (dec_addstatus(context, status)) {
2546 Py_DECREF(result);
2547 return NULL;
2548 }
2549
2550 return result;
2551 }
2552
2553 /* 'v' can have any type accepted by the Decimal constructor. Attempt
2554 an exact conversion. If the result does not meet the restrictions
2555 for an mpd_t, fail with InvalidOperation. */
2556 static PyObject *
2557 PyDecType_FromObjectExact(PyTypeObject *type, PyObject *v, PyObject *context)
2558 {
2559 if (v == NULL) {
2560 return PyDecType_FromSsizeExact(type, 0, context);
2561 }
2562 else if (PyDec_Check(v)) {
2563 Py_INCREF(v);
2564 return v;
2565 }
2566 else if (PyUnicode_Check(v)) {
2567 return PyDecType_FromUnicodeExactWS(type, v, context);
2568 }
2569 else if (PyLong_Check(v)) {
2570 return PyDecType_FromLongExact(type, v, context);
2571 }
2572 else if (PyTuple_Check(v) || PyList_Check(v)) {
2573 return PyDecType_FromSequenceExact(type, v, context);
2574 }
2575 else if (PyFloat_Check(v)) {
2576 if (dec_addstatus(context, MPD_Float_operation)) {
2577 return NULL;
2578 }
2579 return PyDecType_FromFloatExact(type, v, context);
2580 }
2581 else {
2582 PyErr_Format(PyExc_TypeError,
2583 "conversion from %s to Decimal is not supported",
2584 v->ob_type->tp_name);
2585 return NULL;
2586 }
2587 }
2588
2589 /* The context is used during conversion. This function is the
2590 equivalent of context.create_decimal(). */
2591 static PyObject *
2592 PyDec_FromObject(PyObject *v, PyObject *context)
2593 {
2594 if (v == NULL) {
2595 return PyDec_FromSsize(0, context);
2596 }
2597 else if (PyDec_Check(v)) {
2598 mpd_context_t *ctx = CTX(context);
2599 if (mpd_isnan(MPD(v)) &&
2600 MPD(v)->digits > ctx->prec - ctx->clamp) {
2601 /* Special case: too many NaN payload digits */
2602 PyObject *result;
2603 if (dec_addstatus(context, MPD_Conversion_syntax)) {
2604 return NULL;
2605 }
2606 result = dec_alloc();
2607 if (result == NULL) {
2608 return NULL;
2609 }
2610 mpd_setspecial(MPD(result), MPD_POS, MPD_NAN);
2611 return result;
2612 }
2613 return dec_apply(v, context);
2614 }
2615 else if (PyUnicode_Check(v)) {
2616 return PyDec_FromUnicode(v, context);
2617 }
2618 else if (PyLong_Check(v)) {
2619 return PyDec_FromLong(v, context);
2620 }
2621 else if (PyTuple_Check(v) || PyList_Check(v)) {
2622 return PyDec_FromSequence(v, context);
2623 }
2624 else if (PyFloat_Check(v)) {
2625 if (dec_addstatus(context, MPD_Float_operation)) {
2626 return NULL;
2627 }
2628 return PyDec_FromFloat(v, context);
2629 }
2630 else {
2631 PyErr_Format(PyExc_TypeError,
2632 "conversion from %s to Decimal is not supported",
2633 v->ob_type->tp_name);
2634 return NULL;
2635 }
2636 }
2637
2638 static PyObject *
2639 dec_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2640 {
2641 static char *kwlist[] = {"value", "context", NULL};
2642 PyObject *v = NULL;
2643 PyObject *context;
2644
2645 CURRENT_CONTEXT(context);
2646 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO", kwlist,
2647 &v, &context)) {
2648 return NULL;
2649 }
2650 CONTEXT_CHECK_VA(context);
2651
2652 return PyDecType_FromObjectExact(type, v, context);
2653 }
2654
2655 static PyObject *
2656 ctx_create_decimal(PyObject *context, PyObject *args)
2657 {
2658 PyObject *v = NULL;
2659
2660 if (!PyArg_ParseTuple(args, "|O", &v)) {
2661 return NULL;
2662 }
2663
2664 return PyDec_FromObject(v, context);
2665 }
2666
2667
2668 /******************************************************************************/
2669 /* Implicit conversions to Decimal */
2670 /******************************************************************************/
2671
2672 /* Try to convert PyObject v to a new PyDecObject conv. If the conversion
2673 fails, set conv to NULL (exception is set). If the conversion is not
2674 implemented, set conv to Py_NotImplemented. */
2675 #define NOT_IMPL 0
2676 #define TYPE_ERR 1
2677 Py_LOCAL_INLINE(int)
2678 convert_op(int type_err, PyObject **conv, PyObject *v, PyObject *context)
2679 {
2680
2681 if (PyDec_Check(v)) {
2682 *conv = v;
2683 Py_INCREF(v);
2684 return 1;
2685 }
2686 if (PyLong_Check(v)) {
2687 *conv = PyDec_FromLongExact(v, context);
2688 if (*conv == NULL) {
2689 return 0;
2690 }
2691 return 1;
2692 }
2693
2694 if (type_err) {
2695 PyErr_Format(PyExc_TypeError,
2696 "conversion from %s to Decimal is not supported",
2697 v->ob_type->tp_name);
2698 }
2699 else {
2700 Py_INCREF(Py_NotImplemented);
2701 *conv = Py_NotImplemented;
2702 }
2703 return 0;
2704 }
2705
2706 /* Return NotImplemented for unsupported types. */
2707 #define CONVERT_OP(a, v, context) \
2708 if (!convert_op(NOT_IMPL, a, v, context)) { \
2709 return *(a); \
2710 }
2711
2712 #define CONVERT_BINOP(a, b, v, w, context) \
2713 if (!convert_op(NOT_IMPL, a, v, context)) { \
2714 return *(a); \
2715 } \
2716 if (!convert_op(NOT_IMPL, b, w, context)) { \
2717 Py_DECREF(*(a)); \
2718 return *(b); \
2719 }
2720
2721 #define CONVERT_TERNOP(a, b, c, v, w, x, context) \
2722 if (!convert_op(NOT_IMPL, a, v, context)) { \
2723 return *(a); \
2724 } \
2725 if (!convert_op(NOT_IMPL, b, w, context)) { \
2726 Py_DECREF(*(a)); \
2727 return *(b); \
2728 } \
2729 if (!convert_op(NOT_IMPL, c, x, context)) { \
2730 Py_DECREF(*(a)); \
2731 Py_DECREF(*(b)); \
2732 return *(c); \
2733 }
2734
2735 /* Raise TypeError for unsupported types. */
2736 #define CONVERT_OP_RAISE(a, v, context) \
2737 if (!convert_op(TYPE_ERR, a, v, context)) { \
2738 return NULL; \
2739 }
2740
2741 #define CONVERT_BINOP_RAISE(a, b, v, w, context) \
2742 if (!convert_op(TYPE_ERR, a, v, context)) { \
2743 return NULL; \
2744 } \
2745 if (!convert_op(TYPE_ERR, b, w, context)) { \
2746 Py_DECREF(*(a)); \
2747 return NULL; \
2748 }
2749
2750 #define CONVERT_TERNOP_RAISE(a, b, c, v, w, x, context) \
2751 if (!convert_op(TYPE_ERR, a, v, context)) { \
2752 return NULL; \
2753 } \
2754 if (!convert_op(TYPE_ERR, b, w, context)) { \
2755 Py_DECREF(*(a)); \
2756 return NULL; \
2757 } \
2758 if (!convert_op(TYPE_ERR, c, x, context)) { \
2759 Py_DECREF(*(a)); \
2760 Py_DECREF(*(b)); \
2761 return NULL; \
2762 }
2763
2764
2765 /******************************************************************************/
2766 /* Implicit conversions to Decimal for comparison */
2767 /******************************************************************************/
2768
2769 /* Convert rationals for comparison */
2770 static PyObject *Rational = NULL;
2771 static PyObject *
2772 multiply_by_denominator(PyObject *v, PyObject *r, PyObject *context)
2773 {
2774 PyObject *result;
2775 PyObject *tmp = NULL;
2776 PyObject *denom = NULL;
2777 uint32_t status = 0;
2778 mpd_context_t maxctx;
2779 mpd_ssize_t exp;
2780 mpd_t *vv;
2781
2782 /* v is not special, r is a rational */
2783 tmp = PyObject_GetAttrString(r, "denominator");
2784 if (tmp == NULL) {
2785 return NULL;
2786 }
2787 denom = PyDec_FromLongExact(tmp, context);
2788 Py_DECREF(tmp);
2789 if (denom == NULL) {
2790 return NULL;
2791 }
2792
2793 vv = mpd_qncopy(MPD(v));
2794 if (vv == NULL) {
2795 Py_DECREF(denom);
2796 PyErr_NoMemory();
2797 return NULL;
2798 }
2799 result = dec_alloc();
2800 if (result == NULL) {
2801 Py_DECREF(denom);
2802 mpd_del(vv);
2803 return NULL;
2804 }
2805
2806 mpd_maxcontext(&maxctx);
2807 /* Prevent Overflow in the following multiplication. The result of
2808 the multiplication is only used in mpd_qcmp, which can handle
2809 values that are technically out of bounds, like (for 32-bit)
2810 99999999999999999999...99999999e+425000000. */
2811 exp = vv->exp;
2812 vv->exp = 0;
2813 mpd_qmul(MPD(result), vv, MPD(denom), &maxctx, &status);
2814 MPD(result)->exp = exp;
2815
2816 Py_DECREF(denom);
2817 mpd_del(vv);
2818 /* If any status has been accumulated during the multiplication,
2819 the result is invalid. This is very unlikely, since even the
2820 32-bit version supports 425000000 digits. */
2821 if (status) {
2822 PyErr_SetString(PyExc_ValueError,
2823 "exact conversion for comparison failed");
2824 Py_DECREF(result);
2825 return NULL;
2826 }
2827
2828 return result;
2829 }
2830
2831 static PyObject *
2832 numerator_as_decimal(PyObject *r, PyObject *context)
2833 {
2834 PyObject *tmp, *num;
2835
2836 tmp = PyObject_GetAttrString(r, "numerator");
2837 if (tmp == NULL) {
2838 return NULL;
2839 }
2840
2841 num = PyDec_FromLongExact(tmp, context);
2842 Py_DECREF(tmp);
2843 return num;
2844 }
2845
2846 /* Convert v and w for comparison. v is a Decimal. If w is a Rational, both
2847 v and w have to be transformed. Return 1 for success, with new references
2848 to the converted objects in vcmp and wcmp. Return 0 for failure. In that
2849 case wcmp is either NULL or Py_NotImplemented (new reference) and vcmp
2850 is undefined. */
2851 static int
2852 convert_op_cmp(PyObject **vcmp, PyObject **wcmp, PyObject *v, PyObject *w,
2853 int op, PyObject *context)
2854 {
2855 mpd_context_t *ctx = CTX(context);
2856
2857 *vcmp = v;
2858
2859 if (PyDec_Check(w)) {
2860 Py_INCREF(w);
2861 *wcmp = w;
2862 }
2863 else if (PyLong_Check(w)) {
2864 *wcmp = PyDec_FromLongExact(w, context);
2865 }
2866 else if (PyFloat_Check(w)) {
2867 if (op != Py_EQ && op != Py_NE &&
2868 dec_addstatus(context, MPD_Float_operation)) {
2869 *wcmp = NULL;
2870 }
2871 else {
2872 ctx->status |= MPD_Float_operation;
2873 *wcmp = PyDec_FromFloatExact(w, context);
2874 }
2875 }
2876 else if (PyComplex_Check(w) && (op == Py_EQ || op == Py_NE)) {
2877 Py_complex c = PyComplex_AsCComplex(w);
2878 if (c.real == -1.0 && PyErr_Occurred()) {
2879 *wcmp = NULL;
2880 }
2881 else if (c.imag == 0.0) {
2882 PyObject *tmp = PyFloat_FromDouble(c.real);
2883 if (tmp == NULL) {
2884 *wcmp = NULL;
2885 }
2886 else {
2887 ctx->status |= MPD_Float_operation;
2888 *wcmp = PyDec_FromFloatExact(tmp, context);
2889 Py_DECREF(tmp);
2890 }
2891 }
2892 else {
2893 Py_INCREF(Py_NotImplemented);
2894 *wcmp = Py_NotImplemented;
2895 }
2896 }
2897 else if (PyObject_IsInstance(w, Rational)) {
2898 *wcmp = numerator_as_decimal(w, context);
2899 if (*wcmp && !mpd_isspecial(MPD(v))) {
2900 *vcmp = multiply_by_denominator(v, w, context);
2901 if (*vcmp == NULL) {
2902 Py_CLEAR(*wcmp);
2903 }
2904 }
2905 }
2906 else {
2907 Py_INCREF(Py_NotImplemented);
2908 *wcmp = Py_NotImplemented;
2909 }
2910
2911 if (*wcmp == NULL || *wcmp == Py_NotImplemented) {
2912 return 0;
2913 }
2914 if (*vcmp == v) {
2915 Py_INCREF(v);
2916 }
2917 return 1;
2918 }
2919
2920 #define CONVERT_BINOP_CMP(vcmp, wcmp, v, w, op, ctx) \
2921 if (!convert_op_cmp(vcmp, wcmp, v, w, op, ctx)) { \
2922 return *(wcmp); \
2923 } \
2924
2925
2926 /******************************************************************************/
2927 /* Conversions from decimal */
2928 /******************************************************************************/
2929
2930 static PyObject *
2931 unicode_fromascii(const char *s, Py_ssize_t size)
2932 {
2933 PyObject *res;
2934
2935 res = PyUnicode_New(size, 127);
2936 if (res == NULL) {
2937 return NULL;
2938 }
2939
2940 memcpy(PyUnicode_1BYTE_DATA(res), s, size);
2941 return res;
2942 }
2943
2944 /* PyDecObject as a string. The default module context is only used for
2945 the value of 'capitals'. */
2946 static PyObject *
2947 dec_str(PyObject *dec)
2948 {
2949 PyObject *res, *context;
2950 mpd_ssize_t size;
2951 char *cp;
2952
2953 CURRENT_CONTEXT(context);
2954 size = mpd_to_sci_size(&cp, MPD(dec), CtxCaps(context));
2955 if (size < 0) {
2956 PyErr_NoMemory();
2957 return NULL;
2958 }
2959
2960 res = unicode_fromascii(cp, size);
2961 mpd_free(cp);
2962 return res;
2963 }
2964
2965 /* Representation of a PyDecObject. */
2966 static PyObject *
2967 dec_repr(PyObject *dec)
2968 {
2969 PyObject *res, *context;
2970 char *cp;
2971
2972 CURRENT_CONTEXT(context);
2973 cp = mpd_to_sci(MPD(dec), CtxCaps(context));
2974 if (cp == NULL) {
2975 PyErr_NoMemory();
2976 return NULL;
2977 }
2978
2979 res = PyUnicode_FromFormat("Decimal('%s')", cp);
2980 mpd_free(cp);
2981 return res;
2982 }
2983
2984 /* Convert decimal_point or thousands_sep, which may be multibyte or in
2985 the range [128, 255], to a UTF8 string. */
2986 static PyObject *
2987 dotsep_as_utf8(const char *s)
2988 {
2989 PyObject *utf8;
2990 PyObject *tmp;
2991 wchar_t buf[2];
2992 size_t n;
2993
2994 n = mbstowcs(buf, s, 2);
2995 if (n != 1) { /* Issue #7442 */
2996 PyErr_SetString(PyExc_ValueError,
2997 "invalid decimal point or unsupported "
2998 "combination of LC_CTYPE and LC_NUMERIC");
2999 return NULL;
3000 }
3001 tmp = PyUnicode_FromWideChar(buf, n);
3002 if (tmp == NULL) {
3003 return NULL;
3004 }
3005 utf8 = PyUnicode_AsUTF8String(tmp);
3006 Py_DECREF(tmp);
3007 return utf8;
3008 }
3009
3010 /* Formatted representation of a PyDecObject. */
3011 static PyObject *
3012 dec_format(PyObject *dec, PyObject *args)
3013 {
3014 PyObject *result = NULL;
3015 PyObject *override = NULL;
3016 PyObject *dot = NULL;
3017 PyObject *sep = NULL;
3018 PyObject *grouping = NULL;
3019 PyObject *fmt = NULL;
3020 PyObject *fmtarg;
3021 PyObject *context;
3022 mpd_spec_t spec;
3023 char *decstring= NULL;
3024 uint32_t status = 0;
3025 size_t n;
3026
3027
3028 CURRENT_CONTEXT(context);
3029 if (!PyArg_ParseTuple(args, "O|O", &fmtarg, &override)) {
3030 return NULL;
3031 }
3032
3033 if (PyUnicode_Check(fmtarg)) {
3034 fmt = PyUnicode_AsUTF8String(fmtarg);
3035 if (fmt == NULL) {
3036 return NULL;
3037 }
3038 }
3039 else {
3040 PyErr_SetString(PyExc_TypeError,
3041 "format arg must be str");
3042 return NULL;
3043 }
3044
3045 if (!mpd_parse_fmt_str(&spec, PyBytes_AS_STRING(fmt),
3046 CtxCaps(context))) {
3047 PyErr_SetString(PyExc_ValueError,
3048 "invalid format string");
3049 goto finish;
3050 }
3051 if (override) {
3052 /* Values for decimal_point, thousands_sep and grouping can
3053 be explicitly specified in the override dict. These values
3054 take precedence over the values obtained from localeconv()
3055 in mpd_parse_fmt_str(). The feature is not documented and
3056 is only used in test_decimal. */
3057 if (!PyDict_Check(override)) {
3058 PyErr_SetString(PyExc_TypeError,
3059 "optional argument must be a dict");
3060 goto finish;
3061 }
3062 if ((dot = PyDict_GetItemString(override, "decimal_point"))) {
3063 if ((dot = PyUnicode_AsUTF8String(dot)) == NULL) {
3064 goto finish;
3065 }
3066 spec.dot = PyBytes_AS_STRING(dot);
3067 }
3068 if ((sep = PyDict_GetItemString(override, "thousands_sep"))) {
3069 if ((sep = PyUnicode_AsUTF8String(sep)) == NULL) {
3070 goto finish;
3071 }
3072 spec.sep = PyBytes_AS_STRING(sep);
3073 }
3074 if ((grouping = PyDict_GetItemString(override, "grouping"))) {
3075 if ((grouping = PyUnicode_AsUTF8String(grouping)) == NUL L) {
3076 goto finish;
3077 }
3078 spec.grouping = PyBytes_AS_STRING(grouping);
3079 }
3080 if (mpd_validate_lconv(&spec) < 0) {
3081 PyErr_SetString(PyExc_ValueError,
3082 "invalid override dict");
3083 goto finish;
3084 }
3085 }
3086 else {
3087 n = strlen(spec.dot);
3088 if (n > 1 || (n == 1 && !isascii((uchar)spec.dot[0]))) {
3089 /* fix locale dependent non-ascii characters */
3090 dot = dotsep_as_utf8(spec.dot);
3091 if (dot == NULL) {
3092 goto finish;
3093 }
3094 spec.dot = PyBytes_AS_STRING(dot);
3095 }
3096 n = strlen(spec.sep);
3097 if (n > 1 || (n == 1 && !isascii((uchar)spec.sep[0]))) {
3098 /* fix locale dependent non-ascii characters */
3099 sep = dotsep_as_utf8(spec.sep);
3100 if (sep == NULL) {
3101 goto finish;
3102 }
3103 spec.sep = PyBytes_AS_STRING(sep);
3104 }
3105 }
3106
3107
3108 decstring = mpd_qformat_spec(MPD(dec), &spec, CTX(context), &status);
3109 if (decstring == NULL) {
3110 dec_addstatus(context, status);
3111 goto finish;
3112 }
3113 result = PyUnicode_DecodeUTF8(decstring, strlen(decstring), NULL);
3114
3115
3116 finish:
3117 Py_XDECREF(grouping);
3118 Py_XDECREF(sep);
3119 Py_XDECREF(dot);
3120 Py_XDECREF(fmt);
3121 if (decstring) mpd_free(decstring);
3122 return result;
3123 }
3124
3125 /* Return a PyLongObject from a PyDecObject, using the specified rounding
3126 * mode. The context precision is not observed. */
3127 static PyObject *
3128 dec_as_long(PyObject *dec, PyObject *context, int round)
3129 {
3130 PyLongObject *pylong;
3131 size_t maxsize, n;
3132 Py_ssize_t i;
3133 mpd_t *x;
3134 mpd_context_t workctx;
3135 uint32_t status = 0;
3136
3137 if (mpd_isspecial(MPD(dec))) {
3138 if (mpd_isnan(MPD(dec))) {
3139 PyErr_SetString(PyExc_ValueError,
3140 "cannot convert NaN to integer");
3141 }
3142 else {
3143 PyErr_SetString(PyExc_OverflowError,
3144 "cannot convert Infinity to integer");
3145 }
3146 return NULL;
3147 }
3148
3149 x = mpd_qnew();
3150 if (x == NULL) {
3151 PyErr_NoMemory();
3152 return NULL;
3153 }
3154 workctx = *CTX(context);
3155 workctx.round = round;
3156 mpd_qround_to_int(x, MPD(dec), &workctx, &status);
3157 if (dec_addstatus(context, status)) {
3158 mpd_del(x);
3159 return NULL;
3160 }
3161
3162 maxsize = mpd_sizeinbase(x, PyLong_BASE);
3163 if (maxsize > PY_SSIZE_T_MAX) {
3164 mpd_del(x);
3165 PyErr_NoMemory();
3166 return NULL;
3167 }
3168 pylong = _PyLong_New(maxsize);
3169 if (pylong == NULL) {
3170 mpd_del(x);
3171 return NULL;
3172 }
3173
3174 status = 0;
3175 #if PYLONG_BITS_IN_DIGIT == 30
3176 n = mpd_qexport_u32(pylong->ob_digit, maxsize, PyLong_BASE, x, &status);
3177 #elif PYLONG_BITS_IN_DIGIT == 15
3178 n = mpd_qexport_u16(pylong->ob_digit, maxsize, PyLong_BASE, x, &status);
3179 #else
3180 #error "PYLONG_BITS_IN_DIGIT should be 15 or 30"
3181 #endif
3182 if (dec_addstatus(context, status)) {
3183 Py_DECREF((PyObject *) pylong);
3184 mpd_del(x);
3185 return NULL;
3186 }
3187
3188 i = n;
3189 while ((i > 0) && (pylong->ob_digit[i-1] == 0)) {
3190 i--;
3191 }
3192
3193 Py_SIZE(pylong) = i;
3194 if (mpd_isnegative(x) && !mpd_iszero(x)) {
3195 Py_SIZE(pylong) = -i;
3196 }
3197
3198 mpd_del(x);
3199 return (PyObject *) pylong;
3200 }
3201
3202 static PyObject *
3203 PyDec_ToIntegralValue(PyObject *dec, PyObject *args, PyObject *kwds)
3204 {
3205 static char *kwlist[] = {"rounding", "context", NULL};
3206 PyObject *result;
3207 PyObject *context;
3208 uint32_t status = 0;
3209 mpd_context_t workctx;
3210 int round = -1;
3211
3212 CURRENT_CONTEXT(context);
3213 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|iO", kwlist,
3214 &round, &context)) {
3215 return NULL;
3216 }
3217 CONTEXT_CHECK_VA(context);
3218
3219 workctx = *CTX(context);
3220 if (round >= 0) {
3221 if (!mpd_qsetround(&workctx, round)) {
3222 return type_error_ptr(invalid_rounding_err);
3223 }
3224 }
3225
3226 result = dec_alloc();
3227 if (result == NULL) {
3228 return NULL;
3229 }
3230
3231 mpd_qround_to_int(MPD(result), MPD(dec), &workctx, &status);
3232 if (dec_addstatus(context, status)) {
3233 Py_DECREF(result);
3234 return NULL;
3235 }
3236
3237 return result;
3238 }
3239
3240 static PyObject *
3241 PyDec_ToIntegralExact(PyObject *dec, PyObject *args, PyObject *kwds)
3242 {
3243 static char *kwlist[] = {"rounding", "context", NULL};
3244 PyObject *result;
3245 PyObject *context;
3246 uint32_t status = 0;
3247 mpd_context_t workctx;
3248 int round = -1;
3249
3250 CURRENT_CONTEXT(context);
3251 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|iO", kwlist,
3252 &round, &context)) {
3253 return NULL;
3254 }
3255 CONTEXT_CHECK_VA(context);
3256
3257 workctx = *CTX(context);
3258 if (round >= 0) {
3259 if (!mpd_qsetround(&workctx, round)) {
3260 return type_error_ptr(invalid_rounding_err);
3261 }
3262 }
3263
3264 result = dec_alloc();
3265 if (result == NULL) {
3266 return NULL;
3267 }
3268
3269 mpd_qround_to_intx(MPD(result), MPD(dec), &workctx, &status);
3270 if (dec_addstatus(context, status)) {
3271 Py_DECREF(result);
3272 return NULL;
3273 }
3274
3275 return result;
3276 }
3277
3278 static PyObject *
3279 PyDec_AsFloat(PyObject *dec)
3280 {
3281 PyObject *f, *s;
3282
3283 s = dec_str(dec);
3284 if (s == NULL) {
3285 return NULL;
3286 }
3287
3288 f = PyFloat_FromString(s);
3289 Py_DECREF(s);
3290
3291 return f;
3292 }
3293
3294 static PyObject *
3295 PyDec_Round(PyObject *dec, PyObject *args)
3296 {
3297 PyObject *result;
3298 PyObject *x = NULL;
3299 uint32_t status = 0;
3300 PyObject *context;
3301
3302
3303 CURRENT_CONTEXT(context);
3304 if (!PyArg_ParseTuple(args, "|O", &x)) {
3305 return NULL;
3306 }
3307
3308 if (x) {
3309 mpd_uint_t dq[1] = {1};
3310 mpd_t q = {MPD_STATIC|MPD_CONST_DATA,0,1,1,1,dq};
3311 mpd_ssize_t y;
3312
3313 if (!PyLong_Check(x)) {
3314 PyErr_SetString(PyExc_TypeError,
3315 "optional arg must be an integer");
3316 return NULL;
3317 }
3318
3319 y = PyLong_AsSsize_t(x);
3320 if (y == -1 && PyErr_Occurred()) {
3321 return NULL;
3322 }
3323 result = dec_alloc();
3324 if (result == NULL) {
3325 return NULL;
3326 }
3327
3328 q.exp = (y == MPD_SSIZE_MIN) ? MPD_SSIZE_MAX : -y;
3329 mpd_qquantize(MPD(result), MPD(dec), &q, CTX(context), &status);
3330 if (dec_addstatus(context, status)) {
3331 Py_DECREF(result);
3332 return NULL;
3333 }
3334
3335 return result;
3336 }
3337 else {
3338 return dec_as_long(dec, context, MPD_ROUND_HALF_EVEN);
3339 }
3340 }
3341
3342 PyObject *DecimalTuple = NULL;
3343 /* Return the DecimalTuple representation of a PyDecObject. */
3344 static PyObject *
3345 PyDec_AsTuple(PyObject *dec, PyObject *dummy UNUSED)
3346 {
3347 PyObject *result = NULL;
3348 PyObject *sign = NULL;
3349 PyObject *coeff = NULL;
3350 PyObject *expt = NULL;
3351 PyObject *tmp = NULL;
3352 mpd_t *x = NULL;
3353 char *intstring = NULL;
3354 Py_ssize_t intlen, i;
3355
3356
3357 x = mpd_qncopy(MPD(dec));
3358 if (x == NULL) {
3359 PyErr_NoMemory();
3360 goto out;
3361 }
3362
3363 sign = PyLong_FromUnsignedLong(mpd_sign(MPD(dec)));
3364 if (sign == NULL) {
3365 goto out;
3366 }
3367
3368 if (mpd_isinfinite(x)) {
3369 expt = PyUnicode_FromString("F");
3370 if (expt == NULL) {
3371 goto out;
3372 }
3373 /* decimal.py has non-compliant infinity payloads. */
3374 coeff = Py_BuildValue("(i)", 0);
3375 if (coeff == NULL) {
3376 goto out;
3377 }
3378 }
3379 else {
3380 if (mpd_isnan(x)) {
3381 expt = PyUnicode_FromString(mpd_isqnan(x)?"n":"N");
3382 }
3383 else {
3384 expt = PyLong_FromSsize_t(MPD(dec)->exp);
3385 }
3386 if (expt == NULL) {
3387 goto out;
3388 }
3389
3390 /* coefficient is defined */
3391 if (x->len > 0) {
3392
3393 /* make an integer */
3394 x->exp = 0;
3395 /* clear NaN and sign */
3396 mpd_clear_flags(x);
3397 intstring = mpd_to_sci(x, 1);
3398 if (intstring == NULL) {
3399 PyErr_NoMemory();
3400 goto out;
3401 }
3402
3403 intlen = strlen(intstring);
3404 coeff = PyTuple_New(intlen);
3405 if (coeff == NULL) {
3406 goto out;
3407 }
3408
3409 for (i = 0; i < intlen; i++) {
3410 tmp = PyLong_FromLong(intstring[i]-'0');
3411 if (tmp == NULL) {
3412 goto out;
3413 }
3414 PyTuple_SET_ITEM(coeff, i, tmp);
3415 }
3416 }
3417 else {
3418 coeff = PyTuple_New(0);
3419 if (coeff == NULL) {
3420 goto out;
3421 }
3422 }
3423 }
3424
3425 result = PyObject_CallFunctionObjArgs(DecimalTuple,
3426 sign, coeff, expt, NULL);
3427
3428 out:
3429 if (x) mpd_del(x);
3430 if (intstring) mpd_free(intstring);
3431 Py_XDECREF(sign);
3432 Py_XDECREF(coeff);
3433 Py_XDECREF(expt);
3434 return result;
3435 }
3436
3437
3438 /******************************************************************************/
3439 /* Macros for converting mpdecimal functions to Decimal methods */
3440 /******************************************************************************/
3441
3442 /* Unary number method that uses the default module context. */
3443 #define Dec_UnaryNumberMethod(MPDFUNC) \
3444 static PyObject * \
3445 nm_##MPDFUNC(PyObject *self) \
3446 { \
3447 PyObject *result; \
3448 PyObject *context; \
3449 uint32_t status = 0; \
3450 \
3451 CURRENT_CONTEXT(context); \
3452 if ((result = dec_alloc()) == NULL) { \
3453 return NULL; \
3454 } \
3455 \
3456 MPDFUNC(MPD(result), MPD(self), CTX(context), &status); \
3457 if (dec_addstatus(context, status)) { \
3458 Py_DECREF(result); \
3459 return NULL; \
3460 } \
3461 \
3462 return result; \
3463 }
3464
3465 /* Binary number method that uses default module context. */
3466 #define Dec_BinaryNumberMethod(MPDFUNC) \
3467 static PyObject * \
3468 nm_##MPDFUNC(PyObject *self, PyObject *other) \
3469 { \
3470 PyObject *a, *b; \
3471 PyObject *result; \
3472 PyObject *context; \
3473 uint32_t status = 0; \
3474 \
3475 CURRENT_CONTEXT(context) ; \
3476 CONVERT_BINOP(&a, &b, self, other, context); \
3477 \
3478 if ((result = dec_alloc()) == NULL) { \
3479 Py_DECREF(a); \
3480 Py_DECREF(b); \
3481 return NULL; \
3482 } \
3483 \
3484 MPDFUNC(MPD(result), MPD(a), MPD(b), CTX(context), &status); \
3485 Py_DECREF(a); \
3486 Py_DECREF(b); \
3487 if (dec_addstatus(context, status)) { \
3488 Py_DECREF(result); \
3489 return NULL; \
3490 } \
3491 \
3492 return result; \
3493 }
3494
3495 /* Boolean function without a context arg. */
3496 #define Dec_BoolFunc(MPDFUNC) \
3497 static PyObject * \
3498 dec_##MPDFUNC(PyObject *self, PyObject *dummy UNUSED) \
3499 { \
3500 return MPDFUNC(MPD(self)) ? incr_true() : incr_false(); \
3501 }
3502
3503 /* Boolean function with an optional context arg. */
3504 #define Dec_BoolFuncVA(MPDFUNC) \
3505 static PyObject * \
3506 dec_##MPDFUNC(PyObject *self, PyObject *args, PyObject *kwds) \
3507 { \
3508 static char *kwlist[] = {"context", NULL}; \
3509 PyObject *context; \
3510 \
3511 CURRENT_CONTEXT(context); \
3512 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O", kwlist, \
3513 &context)) { \
3514 return NULL; \
3515 } \
3516 CONTEXT_CHECK_VA(context); \
3517 \
3518 return MPDFUNC(MPD(self), CTX(context)) ? incr_true() : incr_false(); \
3519 }
3520
3521 /* Unary function with an optional context arg. */
3522 #define Dec_UnaryFuncVA(MPDFUNC) \
3523 static PyObject * \
3524 dec_##MPDFUNC(PyObject *self, PyObject *args, PyObject *kwds) \
3525 { \
3526 static char *kwlist[] = {"context", NULL}; \
3527 PyObject *result; \
3528 PyObject *context; \
3529 uint32_t status = 0; \
3530 \
3531 CURRENT_CONTEXT(context); \
3532 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O", kwlist, \
3533 &context)) { \
3534 return NULL; \
3535 } \
3536 CONTEXT_CHECK_VA(context); \
3537 \
3538 if ((result = dec_alloc()) == NULL) { \
3539 return NULL; \
3540 } \
3541 \
3542 MPDFUNC(MPD(result), MPD(self), CTX(context), &status); \
3543 if (dec_addstatus(context, status)) { \
3544 Py_DECREF(result); \
3545 return NULL; \
3546 } \
3547 \
3548 return result; \
3549 }
3550
3551 /* Unary function with an optional context arg. The actual MPDFUNC
3552 only takes a status parameter. */
3553 #define Dec_UnaryFuncVA_NO_CTX(MPDFUNC) \
3554 static PyObject * \
3555 dec_##MPDFUNC(PyObject *self, PyObject *args, PyObject *kwds) \
3556 { \
3557 static char *kwlist[] = {"context", NULL}; \
3558 PyObject *result; \
3559 PyObject *context; \
3560 uint32_t status = 0; \
3561 \
3562 CURRENT_CONTEXT(context); \
3563 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O", kwlist, \
3564 &context)) { \
3565 return NULL; \
3566 } \
3567 CONTEXT_CHECK_VA(context); \
3568 \
3569 if ((result = dec_alloc()) == NULL) { \
3570 return NULL; \
3571 } \
3572 \
3573 MPDFUNC(MPD(result), MPD(self), &status); \
3574 if (dec_addstatus(context, status)) { \
3575 Py_DECREF(result); \
3576 return NULL; \
3577 } \
3578 \
3579 return result; \
3580 }
3581
3582 /* Binary function with an optional context arg. */
3583 #define Dec_BinaryFuncVA(MPDFUNC) \
3584 static PyObject * \
3585 dec_##MPDFUNC(PyObject *self, PyObject *args, PyObject *kwds) \
3586 { \
3587 static char *kwlist[] = {"other", "context", NULL}; \
3588 PyObject *other, *context; \
3589 PyObject *a, *b; \
3590 PyObject *result; \
3591 uint32_t status = 0; \
3592 \
3593 CURRENT_CONTEXT(context); \
3594 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O", kwlist, \
3595 &other, &context)) { \
3596 return NULL; \
3597 } \
3598 CONTEXT_CHECK_VA(context); \
3599 CONVERT_BINOP_RAISE(&a, &b, self, other, context); \
3600 \
3601 if ((result = dec_alloc()) == NULL) { \
3602 Py_DECREF(a); \
3603 Py_DECREF(b); \
3604 return NULL; \
3605 } \
3606 \
3607 MPDFUNC(MPD(result), MPD(a), MPD(b), CTX(context), &status); \
3608 Py_DECREF(a); \
3609 Py_DECREF(b); \
3610 if (dec_addstatus(context, status)) { \
3611 Py_DECREF(result); \
3612 return NULL; \
3613 } \
3614 \
3615 return result; \
3616 }
3617
3618 /* Binary function with an optional context arg. Actual MPDFUNC does
3619 NOT take a context. The context is used to record InvalidOperation
3620 if the second operand cannot be converted exactly. */
3621 #define Dec_BinaryFuncVA_NO_CTX(MPDFUNC) \
3622 static PyObject * \
3623 dec_##MPDFUNC(PyObject *self, PyObject *args, PyObject *kwds) \
3624 { \
3625 static char *kwlist[] = {"other", "context", NULL}; \
3626 PyObject *other, *context; \
3627 PyObject *a, *b; \
3628 PyObject *result; \
3629 \
3630 CURRENT_CONTEXT(context); \
3631 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O", kwlist, \
3632 &other, &context)) { \
3633 return NULL; \
3634 } \
3635 CONTEXT_CHECK_VA(context); \
3636 CONVERT_BINOP_RAISE(&a, &b, self, other, context); \
3637 \
3638 if ((result = dec_alloc()) == NULL) { \
3639 Py_DECREF(a); \
3640 Py_DECREF(b); \
3641 return NULL; \
3642 } \
3643 \
3644 MPDFUNC(MPD(result), MPD(a), MPD(b)); \
3645 Py_DECREF(a); \
3646 Py_DECREF(b); \
3647 \
3648 return result; \
3649 }
3650
3651 /* Ternary function with an optional context arg. */
3652 #define Dec_TernaryFuncVA(MPDFUNC) \
3653 static PyObject * \
3654 dec_##MPDFUNC(PyObject *self, PyObject *args, PyObject *kwds) \
3655 { \
3656 static char *kwlist[] = {"other", "third", "context", NULL}; \
3657 PyObject *other, *third, *context; \
3658 PyObject *a, *b, *c; \
3659 PyObject *result; \
3660 uint32_t status = 0; \
3661 \
3662 CURRENT_CONTEXT(context); \
3663 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO|O", kwlist, \
3664 &other, &third, &context)) { \
3665 return NULL; \
3666 } \
3667 CONTEXT_CHECK_VA(context); \
3668 CONVERT_TERNOP_RAISE(&a, &b, &c, self, other, third, context); \
3669 \
3670 if ((result = dec_alloc()) == NULL) { \
3671 Py_DECREF(a); \
3672 Py_DECREF(b); \
3673 Py_DECREF(c); \
3674 return NULL; \
3675 } \
3676 \
3677 MPDFUNC(MPD(result), MPD(a), MPD(b), MPD(c), CTX(context), &status); \
3678 Py_DECREF(a); \
3679 Py_DECREF(b); \
3680 Py_DECREF(c); \
3681 if (dec_addstatus(context, status)) { \
3682 Py_DECREF(result); \
3683 return NULL; \
3684 } \
3685 \
3686 return result; \
3687 }
3688
3689
3690 /**********************************************/
3691 /* Number methods */
3692 /**********************************************/
3693
3694 Dec_UnaryNumberMethod(mpd_qminus)
3695 Dec_UnaryNumberMethod(mpd_qplus)
3696 Dec_UnaryNumberMethod(mpd_qabs)
3697
3698 Dec_BinaryNumberMethod(mpd_qadd)
3699 Dec_BinaryNumberMethod(mpd_qsub)
3700 Dec_BinaryNumberMethod(mpd_qmul)
3701 Dec_BinaryNumberMethod(mpd_qdiv)
3702 Dec_BinaryNumberMethod(mpd_qrem)
3703 Dec_BinaryNumberMethod(mpd_qdivint)
3704
3705 static PyObject *
3706 nm_dec_as_long(PyObject *dec)
3707 {
3708 PyObject *context;
3709
3710 CURRENT_CONTEXT(context);
3711 return dec_as_long(dec, context, MPD_ROUND_DOWN);
3712 }
3713
3714 static int
3715 nm_nonzero(PyDecObject *v)
3716 {
3717 return !mpd_iszero(v->dec);
3718 }
3719
3720 static PyObject *
3721 nm_mpd_qdivmod(PyObject *v, PyObject *w)
3722 {
3723 PyObject *a, *b;
3724 PyObject *q, *r;
3725 PyObject *context;
3726 uint32_t status = 0;
3727 PyObject *ret;
3728
3729 CURRENT_CONTEXT(context);
3730 CONVERT_BINOP(&a, &b, v, w, context);
3731
3732 q = dec_alloc();
3733 if (q == NULL) {
3734 Py_DECREF(a);
3735 Py_DECREF(b);
3736 return NULL;
3737 }
3738 r = dec_alloc();
3739 if (r == NULL) {
3740 Py_DECREF(a);
3741 Py_DECREF(b);
3742 Py_DECREF(q);
3743 return NULL;
3744 }
3745
3746 mpd_qdivmod(MPD(q), MPD(r), MPD(a), MPD(b), CTX(context), &status);
3747 Py_DECREF(a);
3748 Py_DECREF(b);
3749 if (dec_addstatus(context, status)) {
3750 Py_DECREF(r);
3751 Py_DECREF(q);
3752 return NULL;
3753 }
3754
3755 ret = Py_BuildValue("(OO)", q, r);
3756 Py_DECREF(r);
3757 Py_DECREF(q);
3758 return ret;
3759 }
3760
3761 static mpd_uint_t data_zero[1] = {0};
3762 static const mpd_t zero = {MPD_STATIC|MPD_CONST_DATA, 0, 1, 1, 1, data_zero};
3763
3764 static PyObject *
3765 nm_mpd_qpow(PyObject *base, PyObject *exp, PyObject *mod)
3766 {
3767 PyObject *a, *b, *c = NULL;
3768 PyObject *result;
3769 PyObject *context;
3770 uint32_t status = 0;
3771
3772 CURRENT_CONTEXT(context);
3773 CONVERT_BINOP(&a, &b, base, exp, context);
3774
3775 if (mod != Py_None) {
3776 if (!convert_op(NOT_IMPL, &c, mod, context)) {
3777 Py_DECREF(a);
3778 Py_DECREF(b);
3779 return c;
3780 }
3781 }
3782
3783 result = dec_alloc();
3784 if (result == NULL) {
3785 Py_DECREF(a);
3786 Py_DECREF(b);
3787 Py_XDECREF(c);
3788 return NULL;
3789 }
3790
3791 if (c == NULL) {
3792 mpd_qpow(MPD(result), MPD(a), MPD(b),
3793 CTX(context), &status);
3794 }
3795 else {
3796 mpd_qpowmod(MPD(result), MPD(a), MPD(b), MPD(c),
3797 CTX(context), &status);
3798 status = (status == MPD_Clamped) ? 0 : status;
3799 /* remove ideal exponent for compatibility with decimal.py */
3800 mpd_qquantize(MPD(result), MPD(result), &zero,
3801 CTX(context), &status);
3802 Py_DECREF(c);
3803 }
3804 Py_DECREF(a);
3805 Py_DECREF(b);
3806 if (dec_addstatus(context, status)) {
3807 Py_DECREF(result);
3808 return NULL;
3809 }
3810
3811 return result;
3812 }
3813
3814
3815 /******************************************************************************/
3816 /* Decimal Methods */
3817 /******************************************************************************/
3818
3819 /* Unary arithmetic functions, optional context arg */
3820 Dec_UnaryFuncVA(mpd_qexp)
3821 Dec_UnaryFuncVA(mpd_qln)
3822 Dec_UnaryFuncVA(mpd_qlog10)
3823 Dec_UnaryFuncVA(mpd_qnext_minus)
3824 Dec_UnaryFuncVA(mpd_qnext_plus)
3825 Dec_UnaryFuncVA(mpd_qreduce)
3826 Dec_UnaryFuncVA(mpd_qsqrt)
3827
3828 /* Binary arithmetic functions, optional context arg */
3829 Dec_BinaryFuncVA(mpd_qcompare)
3830 Dec_BinaryFuncVA(mpd_qcompare_signal)
3831 Dec_BinaryFuncVA(mpd_qmax)
3832 Dec_BinaryFuncVA(mpd_qmax_mag)
3833 Dec_BinaryFuncVA(mpd_qmin)
3834 Dec_BinaryFuncVA(mpd_qmin_mag)
3835 Dec_BinaryFuncVA(mpd_qnext_toward)
3836 Dec_BinaryFuncVA(mpd_qrem_near)
3837
3838 /* Ternary arithmetic functions, optional context arg */
3839 Dec_TernaryFuncVA(mpd_qfma)
3840
3841 /* Boolean functions, no context arg */
3842 Dec_BoolFunc(mpd_iscanonical)
3843 Dec_BoolFunc(mpd_isfinite)
3844 Dec_BoolFunc(mpd_isinfinite)
3845 Dec_BoolFunc(mpd_isnan)
3846 Dec_BoolFunc(mpd_isqnan)
3847 Dec_BoolFunc(mpd_issnan)
3848 Dec_BoolFunc(mpd_issigned)
3849 Dec_BoolFunc(mpd_iszero)
3850
3851 /* Boolean functions, optional context arg */
3852 Dec_BoolFuncVA(mpd_isnormal)
3853 Dec_BoolFuncVA(mpd_issubnormal)
3854
3855 /* Unary functions, no context arg */
3856 static PyObject *
3857 dec_mpd_adjexp(PyObject *self, PyObject *dummy UNUSED)
3858 {
3859 mpd_ssize_t retval;
3860
3861 if (mpd_isspecial(MPD(self))) {
3862 retval = 0;
3863 }
3864 else {
3865 retval = mpd_adjexp(MPD(self));
3866 }
3867
3868 return PyLong_FromSsize_t(retval);
3869 }
3870
3871 static PyObject *
3872 dec_canonical(PyObject *self, PyObject *dummy UNUSED)
3873 {
3874 Py_INCREF(self);
3875 return self;
3876 }
3877
3878 static PyObject *
3879 dec_conjugate(PyObject *self, PyObject *dummy UNUSED)
3880 {
3881 Py_INCREF(self);
3882 return self;
3883 }
3884
3885 static PyObject *
3886 dec_mpd_radix(PyObject *self UNUSED, PyObject *dummy UNUSED)
3887 {
3888 PyObject *result;
3889
3890 result = dec_alloc();
3891 if (result == NULL) {
3892 return NULL;
3893 }
3894
3895 _dec_settriple(result, MPD_POS, 10, 0);
3896 return result;
3897 }
3898
3899 /* Unary functions, optional context arg for conversion errors */
3900 Dec_UnaryFuncVA_NO_CTX(mpd_qcopy_abs)
3901 Dec_UnaryFuncVA_NO_CTX(mpd_qcopy_negate)
3902
3903 /* Unary functions, optional context arg */
3904 Dec_UnaryFuncVA(mpd_qinvert)
3905 Dec_UnaryFuncVA(mpd_qlogb)
3906
3907 static PyObject *
3908 dec_mpd_class(PyObject *self, PyObject *args, PyObject *kwds)
3909 {
3910 static char *kwlist[] = {"context", NULL};
3911 PyObject *context;
3912 const char *cp;
3913
3914 CURRENT_CONTEXT(context);
3915 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O", kwlist,
3916 &context)) {
3917 return NULL;
3918 }
3919 CONTEXT_CHECK_VA(context);
3920
3921 cp = mpd_class(MPD(self), CTX(context));
3922 return PyUnicode_FromString(cp);
3923 }
3924
3925 static PyObject *
3926 dec_mpd_to_eng(PyObject *self, PyObject *args, PyObject *kwds)
3927 {
3928 static char *kwlist[] = {"context", NULL};
3929 PyObject *result;
3930 PyObject *context;
3931 mpd_ssize_t size;
3932 char *s;
3933
3934 CURRENT_CONTEXT(context);
3935 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O", kwlist,
3936 &context)) {
3937 return NULL;
3938 }
3939 CONTEXT_CHECK_VA(context);
3940
3941 size = mpd_to_eng_size(&s, MPD(self), CtxCaps(context));
3942 if (size < 0) {
3943 PyErr_NoMemory();
3944 return NULL;
3945 }
3946
3947 result = unicode_fromascii(s, size);
3948 mpd_free(s);
3949
3950 return result;
3951 }
3952
3953 /* Binary functions, optional context arg for conversion errors */
3954 Dec_BinaryFuncVA_NO_CTX(mpd_compare_total)
3955 Dec_BinaryFuncVA_NO_CTX(mpd_compare_total_mag)
3956
3957 static PyObject *
3958 dec_mpd_qcopy_sign(PyObject *self, PyObject *args, PyObject *kwds)
3959 {
3960 static char *kwlist[] = {"other", "context", NULL};
3961 PyObject *other, *context;
3962 PyObject *a, *b;
3963 PyObject *result;
3964 uint32_t status = 0;
3965
3966 CURRENT_CONTEXT(context);
3967 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O", kwlist,
3968 &other, &context)) {
3969 return NULL;
3970 }
3971 CONTEXT_CHECK_VA(context);
3972 CONVERT_BINOP_RAISE(&a, &b, self, other, context);
3973
3974 result = dec_alloc();
3975 if (result == NULL) {
3976 Py_DECREF(a);
3977 Py_DECREF(b);
3978 return NULL;
3979 }
3980
3981 mpd_qcopy_sign(MPD(result), MPD(a), MPD(b), &status);
3982 Py_DECREF(a);
3983 Py_DECREF(b);
3984 if (dec_addstatus(context, status)) {
3985 Py_DECREF(result);
3986 return NULL;
3987 }
3988
3989 return result;
3990 }
3991
3992 static PyObject *
3993 dec_mpd_same_quantum(PyObject *self, PyObject *args, PyObject *kwds)
3994 {
3995 static char *kwlist[] = {"other", "context", NULL};
3996 PyObject *other, *context;
3997 PyObject *a, *b;
3998 PyObject *result;
3999
4000 CURRENT_CONTEXT(context);
4001 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O", kwlist,
4002 &other, &context)) {
4003 return NULL;
4004 }
4005 CONTEXT_CHECK_VA(context);
4006 CONVERT_BINOP_RAISE(&a, &b, self, other, context);
4007
4008 result = mpd_same_quantum(MPD(a), MPD(b)) ? incr_true() : incr_false();
4009 Py_DECREF(a);
4010 Py_DECREF(b);
4011
4012 return result;
4013 }
4014
4015 /* Binary functions, optional context arg */
4016 Dec_BinaryFuncVA(mpd_qand)
4017 Dec_BinaryFuncVA(mpd_qor)
4018 Dec_BinaryFuncVA(mpd_qxor)
4019
4020 Dec_BinaryFuncVA(mpd_qrotate)
4021 Dec_BinaryFuncVA(mpd_qscaleb)
4022 Dec_BinaryFuncVA(mpd_qshift)
4023
4024 static PyObject *
4025 dec_mpd_qquantize(PyObject *v, PyObject *args, PyObject *kwds)
4026 {
4027 static char *kwlist[] = {"exp", "rounding", "context", NULL};
4028 PyObject *w, *context;
4029 PyObject *a, *b;
4030 PyObject *result;
4031 uint32_t status = 0;
4032 mpd_context_t workctx;
4033 int round = -1;
4034
4035 CURRENT_CONTEXT(context);
4036 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|iO", kwlist,
4037 &w, &round, &context)) {
4038 return NULL;
4039 }
4040 CONTEXT_CHECK_VA(context);
4041
4042 workctx = *CTX(context);
4043 if (round >= 0) {
4044 if (!mpd_qsetround(&workctx, round)) {
4045 return type_error_ptr(invalid_rounding_err);
4046 }
4047 }
4048
4049 CONVERT_BINOP_RAISE(&a, &b, v, w, context);
4050
4051 result = dec_alloc();
4052 if (result == NULL) {
4053 Py_DECREF(a);
4054 Py_DECREF(b);
4055 return NULL;
4056 }
4057
4058 mpd_qquantize(MPD(result), MPD(a), MPD(b), &workctx, &status);
4059 Py_DECREF(a);
4060 Py_DECREF(b);
4061 if (dec_addstatus(context, status)) {
4062 Py_DECREF(result);
4063 return NULL;
4064 }
4065
4066 return result;
4067 }
4068
4069 /* Special methods */
4070 static PyObject *
4071 dec_richcompare(PyObject *v, PyObject *w, int op)
4072 {
4073 PyObject *a;
4074 PyObject *b;
4075 PyObject *context;
4076 uint32_t status = 0;
4077 int a_issnan, b_issnan;
4078 int r;
4079
4080 assert(PyDec_Check(v));
4081
4082 CURRENT_CONTEXT(context);
4083 CONVERT_BINOP_CMP(&a, &b, v, w, op, context);
4084
4085 a_issnan = mpd_issnan(MPD(a));
4086 b_issnan = mpd_issnan(MPD(b));
4087
4088 r = mpd_qcmp(MPD(a), MPD(b), &status);
4089 Py_DECREF(a);
4090 Py_DECREF(b);
4091 if (r == INT_MAX) {
4092 /* sNaNs or op={le,ge,lt,gt} always signal. */
4093 if (a_issnan || b_issnan || (op != Py_EQ && op != Py_NE)) {
4094 if (dec_addstatus(context, status)) {
4095 return NULL;
4096 }
4097 }
4098 /* qNaN comparison with op={eq,ne} or comparison
4099 * with InvalidOperation disabled. */
4100 return (op == Py_NE) ? incr_true() : incr_false();
4101 }
4102
4103 switch (op) {
4104 case Py_EQ:
4105 r = (r == 0);
4106 break;
4107 case Py_NE:
4108 r = (r != 0);
4109 break;
4110 case Py_LE:
4111 r = (r <= 0);
4112 break;
4113 case Py_GE:
4114 r = (r >= 0);
4115 break;
4116 case Py_LT:
4117 r = (r == -1);
4118 break;
4119 case Py_GT:
4120 r = (r == 1);
4121 break;
4122 }
4123
4124 return PyBool_FromLong(r);
4125 }
4126
4127 /* __ceil__ */
4128 static PyObject *
4129 dec_ceil(PyObject *self, PyObject *dummy UNUSED)
4130 {
4131 PyObject *context;
4132
4133 CURRENT_CONTEXT(context);
4134 return dec_as_long(self, context, MPD_ROUND_CEILING);
4135 }
4136
4137 /* __complex__ */
4138 static PyObject *
4139 dec_complex(PyObject *self, PyObject *dummy UNUSED)
4140 {
4141 PyObject *f;
4142 double x;
4143
4144 f = PyDec_AsFloat(self);
4145 if (f == NULL) {
4146 return NULL;
4147 }
4148
4149 x = PyFloat_AsDouble(f);
4150 Py_DECREF(f);
4151 if (x == -1.0 && PyErr_Occurred()) {
4152 return NULL;
4153 }
4154
4155 return PyComplex_FromDoubles(x, 0);
4156 }
4157
4158 /* __copy__ and __deepcopy__ */
4159 static PyObject *
4160 dec_copy(PyObject *self, PyObject *dummy UNUSED)
4161 {
4162 Py_INCREF(self);
4163 return self;
4164 }
4165
4166 /* __floor__ */
4167 static PyObject *
4168 dec_floor(PyObject *self, PyObject *dummy UNUSED)
4169 {
4170 PyObject *context;
4171
4172 CURRENT_CONTEXT(context);
4173 return dec_as_long(self, context, MPD_ROUND_FLOOR);
4174 }
4175
4176 /* Always uses the module context */
4177 static Py_hash_t
4178 dec_hash(PyObject *v)
4179 {
4180 #if defined(CONFIG_64) && _PyHASH_BITS == 61
4181 /* 2**61 - 1 */
4182 mpd_uint_t p_data[1] = {2305843009213693951ULL};
4183 mpd_t p = {MPD_POS|MPD_STATIC|MPD_CONST_DATA, 0, 19, 1, 1, p_data};
4184 /* Inverse of 10 modulo p */
4185 mpd_uint_t inv10_p_data[2] = {2075258708292324556ULL};
4186 mpd_t inv10_p = {MPD_POS|MPD_STATIC|MPD_CONST_DATA,
4187 0, 19, 1, 1, inv10_p_data};
4188 #elif defined(CONFIG_32) && _PyHASH_BITS == 31
4189 /* 2**31 - 1 */
4190 mpd_uint_t p_data[2] = {147483647UL, 2};
4191 mpd_t p = {MPD_POS|MPD_STATIC|MPD_CONST_DATA, 0, 10, 2, 2, p_data};
4192 /* Inverse of 10 modulo p */
4193 mpd_uint_t inv10_p_data[2] = {503238553UL, 1};
4194 mpd_t inv10_p = {MPD_POS|MPD_STATIC|MPD_CONST_DATA,
4195 0, 10, 2, 2, inv10_p_data};
4196 #else
4197 #error "No valid combination of CONFIG_64, CONFIG_32 and _PyHASH_BITS"
4198 #endif
4199 const Py_hash_t py_hash_inf = 314159;
4200 const Py_hash_t py_hash_nan = 0;
4201 mpd_uint_t ten_data[1] = {10};
4202 mpd_t ten = {MPD_POS|MPD_STATIC|MPD_CONST_DATA,
4203 0, 2, 1, 1, ten_data};
4204 Py_hash_t result;
4205 mpd_t *exp_hash = NULL;
4206 mpd_t *tmp = NULL;
4207 mpd_ssize_t exp;
4208 uint32_t status = 0;
4209 mpd_context_t maxctx;
4210 PyObject *context;
4211
4212
4213 context = current_context();
4214 if (context == NULL) {
4215 return -1;
4216 }
4217
4218 if (mpd_isspecial(MPD(v))) {
4219 if (mpd_issnan(MPD(v))) {
4220 PyErr_SetString(PyExc_TypeError,
4221 "Cannot hash a signaling NaN value");
4222 return -1;
4223 }
4224 else if (mpd_isnan(MPD(v))) {
4225 return py_hash_nan;
4226 }
4227 else {
4228 return py_hash_inf * mpd_arith_sign(MPD(v));
4229 }
4230 }
4231
4232 mpd_maxcontext(&maxctx);
4233 exp_hash = mpd_qnew();
4234 if (exp_hash == NULL) {
4235 goto malloc_error;
4236 }
4237 tmp = mpd_qnew();
4238 if (tmp == NULL) {
4239 goto malloc_error;
4240 }
4241
4242 /*
4243 * exp(v): exponent of v
4244 * int(v): coefficient of v
4245 */
4246 exp = MPD(v)->exp;
4247 if (exp >= 0) {
4248 /* 10**exp(v) % p */
4249 mpd_qsset_ssize(tmp, exp, &maxctx, &status);
4250 mpd_qpowmod(exp_hash, &ten, tmp, &p, &maxctx, &status);
4251 }
4252 else {
4253 /* inv10_p**(-exp(v)) % p */
4254 mpd_qsset_ssize(tmp, -exp, &maxctx, &status);
4255 mpd_qpowmod(exp_hash, &inv10_p, tmp, &p, &maxctx, &status);
4256 }
4257
4258 /* hash = (int(v) * exp_hash) % p */
4259 if (!mpd_qcopy(tmp, MPD(v), &status)) {
4260 goto malloc_error;
4261 }
4262 tmp->exp = 0;
4263 mpd_set_positive(tmp);
4264 mpd_qmul(tmp, tmp, exp_hash, &maxctx, &status);
4265 mpd_qrem(tmp, tmp, &p, &maxctx, &status);
4266
4267 result = mpd_qget_ssize(tmp, &status);
4268 result = mpd_ispositive(MPD(v)) ? result : -result;
4269 result = (result == -1) ? -2 : result;
4270
4271 if (status != 0) {
4272 status |= MPD_Invalid_operation;
4273 if (dec_addstatus(context, status)) {
4274 result = -1;
4275 goto finish;
4276 }
4277 }
4278
4279
4280 finish:
4281 if (exp_hash) mpd_del(exp_hash);
4282 if (tmp) mpd_del(tmp);
4283 return result;
4284
4285 malloc_error:
4286 PyErr_NoMemory();
4287 result = -1;
4288 goto finish;
4289 }
4290
4291 /* __reduce__ */
4292 static PyObject *
4293 dec_reduce(PyObject *self, PyObject *dummy UNUSED)
4294 {
4295 PyObject *result, *str;
4296
4297 str = dec_str(self);
4298 if (str == NULL) {
4299 return NULL;
4300 }
4301
4302 result = Py_BuildValue("O(O)", Py_TYPE(self), str);
4303 Py_DECREF(str);
4304
4305 return result;
4306 }
4307
4308 /* __trunc__ */
4309 static PyObject *
4310 dec_trunc(PyObject *self, PyObject *dummy UNUSED)
4311 {
4312 PyObject *context;
4313
4314 CURRENT_CONTEXT(context);
4315 return dec_as_long(self, context, MPD_ROUND_DOWN);
4316 }
4317
4318 /* real and imag */
4319 static PyObject *
4320 dec_real(PyObject *self, void *closure UNUSED)
4321 {
4322 Py_INCREF(self);
4323 return self;
4324 }
4325
4326 static PyObject *
4327 dec_imag(PyObject *self UNUSED, void *closure UNUSED)
4328 {
4329 PyObject *result;
4330
4331 result = dec_alloc();
4332 if (result == NULL) {
4333 return NULL;
4334 }
4335
4336 _dec_settriple(result, MPD_POS, 0, 0);
4337 return result;
4338 }
4339
4340
4341 static PyGetSetDef dec_getsets [] =
4342 {
4343 { "real", (getter)dec_real, NULL, NULL, NULL},
4344 { "imag", (getter)dec_imag, NULL, NULL, NULL},
4345 {NULL}
4346 };
4347
4348 static PyNumberMethods dec_number_methods =
4349 {
4350 (binaryfunc) nm_mpd_qadd,
4351 (binaryfunc) nm_mpd_qsub,
4352 (binaryfunc) nm_mpd_qmul,
4353 (binaryfunc) nm_mpd_qrem,
4354 (binaryfunc) nm_mpd_qdivmod,
4355 (ternaryfunc) nm_mpd_qpow,
4356 (unaryfunc) nm_mpd_qminus,
4357 (unaryfunc) nm_mpd_qplus,
4358 (unaryfunc) nm_mpd_qabs,
4359 (inquiry) nm_nonzero,
4360 (unaryfunc) 0, /* no bit-complement */
4361 (binaryfunc) 0, /* no shiftl */
4362 (binaryfunc) 0, /* no shiftr */
4363 (binaryfunc) 0, /* no bit-and */
4364 (binaryfunc) 0, /* no bit-xor */
4365 (binaryfunc) 0, /* no bit-ior */
4366 (unaryfunc) nm_dec_as_long,
4367 0, /* nb_reserved */
4368 (unaryfunc) PyDec_AsFloat,
4369 0, /* binaryfunc nb_inplace_add; */
4370 0, /* binaryfunc nb_inplace_subtract; */
4371 0, /* binaryfunc nb_inplace_multiply; */
4372 0, /* binaryfunc nb_inplace_remainder; */
4373 0, /* ternaryfunc nb_inplace_power; */
4374 0, /* binaryfunc nb_inplace_lshift; */
4375 0, /* binaryfunc nb_inplace_rshift; */
4376 0, /* binaryfunc nb_inplace_and; */
4377 0, /* binaryfunc nb_inplace_xor; */
4378 0, /* binaryfunc nb_inplace_or; */
4379 (binaryfunc) nm_mpd_qdivint, /* binaryfunc nb_floor_divide; */
4380 (binaryfunc) nm_mpd_qdiv, /* binaryfunc nb_true_divide; */
4381 0, /* binaryfunc nb_inplace_floor_divide; */
4382 0, /* binaryfunc nb_inplace_true_divide; */
4383 };
4384
4385 static PyMethodDef dec_methods [] =
4386 {
4387 /* Unary arithmetic functions, optional context arg */
4388 { "exp", (PyCFunction)dec_mpd_qexp, METH_VARARGS|METH_KEYWORDS, doc_exp },
4389 { "ln", (PyCFunction)dec_mpd_qln, METH_VARARGS|METH_KEYWORDS, doc_ln },
4390 { "log10", (PyCFunction)dec_mpd_qlog10, METH_VARARGS|METH_KEYWORDS, doc_log10 },
4391 { "next_minus", (PyCFunction)dec_mpd_qnext_minus, METH_VARARGS|METH_KEYWORDS, doc_next_minus },
4392 { "next_plus", (PyCFunction)dec_mpd_qnext_plus, METH_VARARGS|METH_KEYWORDS, do c_next_plus },
4393 { "normalize", (PyCFunction)dec_mpd_qreduce, METH_VARARGS|METH_KEYWORDS, doc_n ormalize },
4394 { "to_integral", (PyCFunction)PyDec_ToIntegralValue, METH_VARARGS|METH_KEYWORD S, doc_to_integral },
4395 { "to_integral_exact", (PyCFunction)PyDec_ToIntegralExact, METH_VARARGS|METH_K EYWORDS, doc_to_integral_exact },
4396 { "to_integral_value", (PyCFunction)PyDec_ToIntegralValue, METH_VARARGS|METH_K EYWORDS, doc_to_integral_value },
4397 { "sqrt", (PyCFunction)dec_mpd_qsqrt, METH_VARARGS|METH_KEYWORDS, doc_sqrt },
4398
4399 /* Binary arithmetic functions, optional context arg */
4400 { "compare", (PyCFunction)dec_mpd_qcompare, METH_VARARGS|METH_KEYWORDS, doc_co mpare },
4401 { "compare_signal", (PyCFunction)dec_mpd_qcompare_signal, METH_VARARGS|METH_KE YWORDS, doc_compare_signal },
4402 { "max", (PyCFunction)dec_mpd_qmax, METH_VARARGS|METH_KEYWORDS, doc_max },
4403 { "max_mag", (PyCFunction)dec_mpd_qmax_mag, METH_VARARGS|METH_KEYWORDS, doc_ma x_mag },
4404 { "min", (PyCFunction)dec_mpd_qmin, METH_VARARGS|METH_KEYWORDS, doc_min },
4405 { "min_mag", (PyCFunction)dec_mpd_qmin_mag, METH_VARARGS|METH_KEYWORDS, doc_mi n_mag },
4406 { "next_toward", (PyCFunction)dec_mpd_qnext_toward, METH_VARARGS|METH_KEYWORDS , doc_next_toward },
4407 { "quantize", (PyCFunction)dec_mpd_qquantize, METH_VARARGS|METH_KEYWORDS, doc_ quantize },
4408 { "remainder_near", (PyCFunction)dec_mpd_qrem_near, METH_VARARGS|METH_KEYWORDS , doc_remainder_near },
4409
4410 /* Ternary arithmetic functions, optional context arg */
4411 { "fma", (PyCFunction)dec_mpd_qfma, METH_VARARGS|METH_KEYWORDS, doc_fma },
4412
4413 /* Boolean functions, no context arg */
4414 { "is_canonical", dec_mpd_iscanonical, METH_NOARGS, doc_is_canonical },
4415 { "is_finite", dec_mpd_isfinite, METH_NOARGS, doc_is_finite },
4416 { "is_infinite", dec_mpd_isinfinite, METH_NOARGS, doc_is_infinite },
4417 { "is_nan", dec_mpd_isnan, METH_NOARGS, doc_is_nan },
4418 { "is_qnan", dec_mpd_isqnan, METH_NOARGS, doc_is_qnan },
4419 { "is_snan", dec_mpd_issnan, METH_NOARGS, doc_is_snan },
4420 { "is_signed", dec_mpd_issigned, METH_NOARGS, doc_is_signed },
4421 { "is_zero", dec_mpd_iszero, METH_NOARGS, doc_is_zero },
4422
4423 /* Boolean functions, optional context arg */
4424 { "is_normal", (PyCFunction)dec_mpd_isnormal, METH_VARARGS|METH_KEYWORDS, doc_ is_normal },
4425 { "is_subnormal", (PyCFunction)dec_mpd_issubnormal, METH_VARARGS|METH_KEYWORDS , doc_is_subnormal },
4426
4427 /* Unary functions, no context arg */
4428 { "adjusted", dec_mpd_adjexp, METH_NOARGS, doc_adjusted },
4429 { "canonical", dec_canonical, METH_NOARGS, doc_canonical },
4430 { "conjugate", dec_conjugate, METH_NOARGS, doc_conjugate },
4431 { "radix", dec_mpd_radix, METH_NOARGS, doc_radix },
4432
4433 /* Unary functions, optional context arg for conversion errors */
4434 { "copy_abs", (PyCFunction)dec_mpd_qcopy_abs, METH_VARARGS|METH_KEYWORDS, doc_ copy_abs },
4435 { "copy_negate", (PyCFunction)dec_mpd_qcopy_negate, METH_VARARGS|METH_KEYWORDS , doc_copy_negate },
4436
4437 /* Unary functions, optional context arg */
4438 { "logb", (PyCFunction)dec_mpd_qlogb, METH_VARARGS|METH_KEYWORDS, doc_logb },
4439 { "logical_invert", (PyCFunction)dec_mpd_qinvert, METH_VARARGS|METH_KEYWORDS, doc_logical_invert },
4440 { "number_class", (PyCFunction)dec_mpd_class, METH_VARARGS|METH_KEYWORDS, doc_ number_class },
4441 { "to_eng_string", (PyCFunction)dec_mpd_to_eng, METH_VARARGS|METH_KEYWORDS, do c_to_eng_string },
4442
4443 /* Binary functions, optional context arg for conversion errors */
4444 { "compare_total", (PyCFunction)dec_mpd_compare_total, METH_VARARGS|METH_KEYWO RDS, doc_compare_total },
4445 { "compare_total_mag", (PyCFunction)dec_mpd_compare_total_mag, METH_VARARGS|ME TH_KEYWORDS, doc_compare_total_mag },
4446 { "copy_sign", (PyCFunction)dec_mpd_qcopy_sign, METH_VARARGS|METH_KEYWORDS, do c_copy_sign },
4447 { "same_quantum", (PyCFunction)dec_mpd_same_quantum, METH_VARARGS|METH_KEYWORD S, doc_same_quantum },
4448
4449 /* Binary functions, optional context arg */
4450 { "logical_and", (PyCFunction)dec_mpd_qand, METH_VARARGS|METH_KEYWORDS, doc_lo gical_and },
4451 { "logical_or", (PyCFunction)dec_mpd_qor, METH_VARARGS|METH_KEYWORDS, doc_logi cal_or },
4452 { "logical_xor", (PyCFunction)dec_mpd_qxor, METH_VARARGS|METH_KEYWORDS, doc_lo gical_xor },
4453 { "rotate", (PyCFunction)dec_mpd_qrotate, METH_VARARGS|METH_KEYWORDS, doc_rota te },
4454 { "scaleb", (PyCFunction)dec_mpd_qscaleb, METH_VARARGS|METH_KEYWORDS, doc_scal eb },
4455 { "shift", (PyCFunction)dec_mpd_qshift, METH_VARARGS|METH_KEYWORDS, doc_shift },
4456
4457 /* Miscellaneous */
4458 { "from_float", dec_from_float, METH_O|METH_CLASS, doc_from_float },
4459 { "as_tuple", PyDec_AsTuple, METH_NOARGS, doc_as_tuple },
4460
4461 /* Special methods */
4462 { "__copy__", dec_copy, METH_NOARGS, NULL },
4463 { "__deepcopy__", dec_copy, METH_O, NULL },
4464 { "__format__", dec_format, METH_VARARGS, NULL },
4465 { "__reduce__", dec_reduce, METH_NOARGS, NULL },
4466 { "__round__", PyDec_Round, METH_VARARGS, NULL },
4467 { "__ceil__", dec_ceil, METH_NOARGS, NULL },
4468 { "__floor__", dec_floor, METH_NOARGS, NULL },
4469 { "__trunc__", dec_trunc, METH_NOARGS, NULL },
4470 { "__complex__", dec_complex, METH_NOARGS, NULL },
4471
4472 { NULL, NULL, 1 }
4473 };
4474
4475 static PyTypeObject PyDec_Type =
4476 {
4477 PyVarObject_HEAD_INIT(NULL, 0)
4478 "decimal.Decimal", /* tp_name */
4479 sizeof(PyDecObject), /* tp_basicsize */
4480 0, /* tp_itemsize */
4481 (destructor) dec_dealloc, /* tp_dealloc */
4482 0, /* tp_print */
4483 (getattrfunc) 0, /* tp_getattr */
4484 (setattrfunc) 0, /* tp_setattr */
4485 0, /* tp_reserved */
4486 (reprfunc) dec_repr, /* tp_repr */
4487 &dec_number_methods, /* tp_as_number */
4488 0, /* tp_as_sequence */
4489 0, /* tp_as_mapping */
4490 (hashfunc) dec_hash, /* tp_hash */
4491 0, /* tp_call */
4492 (reprfunc) dec_str, /* tp_str */
4493 (getattrofunc) PyObject_GenericGetAttr, /* tp_getattro */
4494 (setattrofunc) 0, /* tp_setattro */
4495 (PyBufferProcs *) 0, /* tp_as_buffer */
4496 (Py_TPFLAGS_DEFAULT|
4497 Py_TPFLAGS_BASETYPE), /* tp_flags */
4498 doc_decimal, /* tp_doc */
4499 0, /* tp_traverse */
4500 0, /* tp_clear */
4501 dec_richcompare, /* tp_richcompare */
4502 0, /* tp_weaklistoffset */
4503 0, /* tp_iter */
4504 0, /* tp_iternext */
4505 dec_methods, /* tp_methods */
4506 0, /* tp_members */
4507 dec_getsets, /* tp_getset */
4508 0, /* tp_base */
4509 0, /* tp_dict */
4510 0, /* tp_descr_get */
4511 0, /* tp_descr_set */
4512 0, /* tp_dictoffset */
4513 0, /* tp_init */
4514 0, /* tp_alloc */
4515 dec_new, /* tp_new */
4516 PyObject_Del, /* tp_free */
4517 };
4518
4519
4520 /******************************************************************************/
4521 /* Context Object, Part 2 */
4522 /******************************************************************************/
4523
4524
4525 /************************************************************************/
4526 /* Macros for converting mpdecimal functions to Context methods */
4527 /************************************************************************/
4528
4529 /* Boolean context method. */
4530 #define DecCtx_BoolFunc(MPDFUNC) \
4531 static PyObject * \
4532 ctx_##MPDFUNC(PyObject *context, PyObject *v) \
4533 { \
4534 PyObject *ret; \
4535 PyObject *a; \
4536 \
4537 CONVERT_OP_RAISE(&a, v, context); \
4538 \
4539 ret = MPDFUNC(MPD(a), CTX(context)) ? incr_true() : incr_false(); \
4540 Py_DECREF(a); \
4541 return ret; \
4542 }
4543
4544 /* Boolean context method. MPDFUNC does NOT use a context. */
4545 #define DecCtx_BoolFunc_NO_CTX(MPDFUNC) \
4546 static PyObject * \
4547 ctx_##MPDFUNC(PyObject *context, PyObject *v) \
4548 { \
4549 PyObject *ret; \
4550 PyObject *a; \
4551 \
4552 CONVERT_OP_RAISE(&a, v, context); \
4553 \
4554 ret = MPDFUNC(MPD(a)) ? incr_true() : incr_false(); \
4555 Py_DECREF(a); \
4556 return ret; \
4557 }
4558
4559 /* Unary context method. */
4560 #define DecCtx_UnaryFunc(MPDFUNC) \
4561 static PyObject * \
4562 ctx_##MPDFUNC(PyObject *context, PyObject *v) \
4563 { \
4564 PyObject *result, *a; \
4565 uint32_t status = 0; \
4566 \
4567 CONVERT_OP_RAISE(&a, v, context); \
4568 \
4569 if ((result = dec_alloc()) == NULL) { \
4570 Py_DECREF(a); \
4571 return NULL; \
4572 } \
4573 \
4574 MPDFUNC(MPD(result), MPD(a), CTX(context), &status); \
4575 Py_DECREF(a); \
4576 if (dec_addstatus(context, status)) { \
4577 Py_DECREF(result); \
4578 return NULL; \
4579 } \
4580 \
4581 return result; \
4582 }
4583
4584 /* Binary context method. */
4585 #define DecCtx_BinaryFunc(MPDFUNC) \
4586 static PyObject * \
4587 ctx_##MPDFUNC(PyObject *context, PyObject *args) \
4588 { \
4589 PyObject *v, *w; \
4590 PyObject *a, *b; \
4591 PyObject *result; \
4592 uint32_t status = 0; \
4593 \
4594 if (!PyArg_ParseTuple(args, "OO", &v, &w)) { \
4595 return NULL; \
4596 } \
4597 \
4598 CONVERT_BINOP_RAISE(&a, &b, v, w, context); \
4599 \
4600 if ((result = dec_alloc()) == NULL) { \
4601 Py_DECREF(a); \
4602 Py_DECREF(b); \
4603 return NULL; \
4604 } \
4605 \
4606 MPDFUNC(MPD(result), MPD(a), MPD(b), CTX(context), &status); \
4607 Py_DECREF(a); \
4608 Py_DECREF(b); \
4609 if (dec_addstatus(context, status)) { \
4610 Py_DECREF(result); \
4611 return NULL; \
4612 } \
4613 \
4614 return result; \
4615 }
4616
4617 /*
4618 * Binary context method. The context is only used for conversion.
4619 * The actual MPDFUNC does NOT take a context arg.
4620 */
4621 #define DecCtx_BinaryFunc_NO_CTX(MPDFUNC) \
4622 static PyObject * \
4623 ctx_##MPDFUNC(PyObject *context, PyObject *args) \
4624 { \
4625 PyObject *v, *w; \
4626 PyObject *a, *b; \
4627 PyObject *result; \
4628 \
4629 if (!PyArg_ParseTuple(args, "OO", &v, &w)) { \
4630 return NULL; \
4631 } \
4632 \
4633 CONVERT_BINOP_RAISE(&a, &b, v, w, context); \
4634 \
4635 if ((result = dec_alloc()) == NULL) { \
4636 Py_DECREF(a); \
4637 Py_DECREF(b); \
4638 return NULL; \
4639 } \
4640 \
4641 MPDFUNC(MPD(result), MPD(a), MPD(b)); \
4642 Py_DECREF(a); \
4643 Py_DECREF(b); \
4644 \
4645 return result; \
4646 }
4647
4648 /* Ternary context method. */
4649 #define DecCtx_TernaryFunc(MPDFUNC) \
4650 static PyObject * \
4651 ctx_##MPDFUNC(PyObject *context, PyObject *args) \
4652 { \
4653 PyObject *v, *w, *x; \
4654 PyObject *a, *b, *c; \
4655 PyObject *result; \
4656 uint32_t status = 0; \
4657 \
4658 if (!PyArg_ParseTuple(args, "OOO", &v, &w, &x)) { \
4659 return NULL; \
4660 } \
4661 \
4662 CONVERT_TERNOP_RAISE(&a, &b, &c, v, w, x, context); \
4663 \
4664 if ((result = dec_alloc()) == NULL) { \
4665 Py_DECREF(a); \
4666 Py_DECREF(b); \
4667 Py_DECREF(c); \
4668 return NULL; \
4669 } \
4670 \
4671 MPDFUNC(MPD(result), MPD(a), MPD(b), MPD(c), CTX(context), &status); \
4672 Py_DECREF(a); \
4673 Py_DECREF(b); \
4674 Py_DECREF(c); \
4675 if (dec_addstatus(context, status)) { \
4676 Py_DECREF(result); \
4677 return NULL; \
4678 } \
4679 \
4680 return result; \
4681 }
4682
4683
4684 /* Unary arithmetic functions */
4685 DecCtx_UnaryFunc(mpd_qabs)
4686 DecCtx_UnaryFunc(mpd_qexp)
4687 DecCtx_UnaryFunc(mpd_qln)
4688 DecCtx_UnaryFunc(mpd_qlog10)
4689 DecCtx_UnaryFunc(mpd_qminus)
4690 DecCtx_UnaryFunc(mpd_qnext_minus)
4691 DecCtx_UnaryFunc(mpd_qnext_plus)
4692 DecCtx_UnaryFunc(mpd_qplus)
4693 DecCtx_UnaryFunc(mpd_qreduce)
4694 DecCtx_UnaryFunc(mpd_qround_to_int)
4695 DecCtx_UnaryFunc(mpd_qround_to_intx)
4696 DecCtx_UnaryFunc(mpd_qsqrt)
4697
4698 /* Binary arithmetic functions */
4699 DecCtx_BinaryFunc(mpd_qadd)
4700 DecCtx_BinaryFunc(mpd_qcompare)
4701 DecCtx_BinaryFunc(mpd_qcompare_signal)
4702 DecCtx_BinaryFunc(mpd_qdiv)
4703 DecCtx_BinaryFunc(mpd_qdivint)
4704 DecCtx_BinaryFunc(mpd_qmax)
4705 DecCtx_BinaryFunc(mpd_qmax_mag)
4706 DecCtx_BinaryFunc(mpd_qmin)
4707 DecCtx_BinaryFunc(mpd_qmin_mag)
4708 DecCtx_BinaryFunc(mpd_qmul)
4709 DecCtx_BinaryFunc(mpd_qnext_toward)
4710 DecCtx_BinaryFunc(mpd_qquantize)
4711 DecCtx_BinaryFunc(mpd_qrem)
4712 DecCtx_BinaryFunc(mpd_qrem_near)
4713 DecCtx_BinaryFunc(mpd_qsub)
4714
4715 static PyObject *
4716 ctx_mpd_qdivmod(PyObject *context, PyObject *args)
4717 {
4718 PyObject *v, *w;
4719 PyObject *a, *b;
4720 PyObject *q, *r;
4721 uint32_t status = 0;
4722 PyObject *ret;
4723
4724 if (!PyArg_ParseTuple(args, "OO", &v, &w)) {
4725 return NULL;
4726 }
4727
4728 CONVERT_BINOP_RAISE(&a, &b, v, w, context);
4729
4730 q = dec_alloc();
4731 if (q == NULL) {
4732 Py_DECREF(a);
4733 Py_DECREF(b);
4734 return NULL;
4735 }
4736 r = dec_alloc();
4737 if (r == NULL) {
4738 Py_DECREF(a);
4739 Py_DECREF(b);
4740 Py_DECREF(q);
4741 return NULL;
4742 }
4743
4744 mpd_qdivmod(MPD(q), MPD(r), MPD(a), MPD(b), CTX(context), &status);
4745 Py_DECREF(a);
4746 Py_DECREF(b);
4747 if (dec_addstatus(context, status)) {
4748 Py_DECREF(r);
4749 Py_DECREF(q);
4750 return NULL;
4751 }
4752
4753 ret = Py_BuildValue("(OO)", q, r);
4754 Py_DECREF(r);
4755 Py_DECREF(q);
4756 return ret;
4757 }
4758
4759 /* Binary or ternary arithmetic functions */
4760 static PyObject *
4761 ctx_mpd_qpow(PyObject *context, PyObject *args, PyObject *kwds)
4762 {
4763 static char *kwlist[] = {"a", "b", "modulo", NULL};
4764 PyObject *base, *exp, *mod = NULL;
4765 PyObject *a, *b, *c = NULL;
4766 PyObject *result;
4767 uint32_t status = 0;
4768
4769 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO|O", kwlist,
4770 &base, &exp, &mod)) {
4771 return NULL;
4772 }
4773
4774 CONVERT_BINOP_RAISE(&a, &b, base, exp, context);
4775
4776 if (mod != NULL) {
4777 if (!convert_op(TYPE_ERR, &c, mod, context)) {
4778 Py_DECREF(a);
4779 Py_DECREF(b);
4780 return c;
4781 }
4782 }
4783
4784 result = dec_alloc();
4785 if (result == NULL) {
4786 Py_DECREF(a);
4787 Py_DECREF(b);
4788 Py_XDECREF(c);
4789 return NULL;
4790 }
4791
4792 if (c == NULL) {
4793 mpd_qpow(MPD(result), MPD(a), MPD(b),
4794 CTX(context), &status);
4795 }
4796 else {
4797 mpd_qpowmod(MPD(result), MPD(a), MPD(b), MPD(c),
4798 CTX(context), &status);
4799 status = (status == MPD_Clamped) ? 0 : status;
4800 /* remove ideal exponent for compatibility with decimal.py */
4801 mpd_qquantize(MPD(result), MPD(result), &zero,
4802 CTX(context), &status);
4803 Py_DECREF(c);
4804 }
4805 Py_DECREF(a);
4806 Py_DECREF(b);
4807 if (dec_addstatus(context, status)) {
4808 Py_DECREF(result);
4809 return NULL;
4810 }
4811
4812 return result;
4813 }
4814
4815 /* Ternary arithmetic functions */
4816 DecCtx_TernaryFunc(mpd_qfma)
4817
4818 /* No argument */
4819 static PyObject *
4820 ctx_mpd_radix(PyObject *context, PyObject *dummy)
4821 {
4822 return dec_mpd_radix(context, dummy);
4823 }
4824
4825 /* Boolean functions: single decimal argument */
4826 DecCtx_BoolFunc(mpd_isnormal)
4827 DecCtx_BoolFunc(mpd_issubnormal)
4828 DecCtx_BoolFunc_NO_CTX(mpd_isfinite)
4829 DecCtx_BoolFunc_NO_CTX(mpd_isinfinite)
4830 DecCtx_BoolFunc_NO_CTX(mpd_isnan)
4831 DecCtx_BoolFunc_NO_CTX(mpd_isqnan)
4832 DecCtx_BoolFunc_NO_CTX(mpd_issigned)
4833 DecCtx_BoolFunc_NO_CTX(mpd_issnan)
4834 DecCtx_BoolFunc_NO_CTX(mpd_iszero)
4835
4836 static PyObject *
4837 ctx_iscanonical(PyObject *context UNUSED, PyObject *v)
4838 {
4839 if (!PyDec_Check(v)) {
4840 PyErr_SetString(PyExc_TypeError,
4841 "argument must be a Decimal");
4842 return NULL;
4843 }
4844
4845 return mpd_iscanonical(MPD(v)) ? incr_true() : incr_false();
4846 }
4847
4848 /* Functions with a single decimal argument */
4849 static PyObject *
4850 PyDecContext_Apply(PyObject *context, PyObject *v)
4851 {
4852 PyObject *result, *a;
4853
4854 CONVERT_OP_RAISE(&a, v, context);
4855
4856 result = dec_apply(a, context);
4857 Py_DECREF(a);
4858 return result;
4859 }
4860
4861 static PyObject *
4862 ctx_canonical(PyObject *context UNUSED, PyObject *v)
4863 {
4864 if (!PyDec_Check(v)) {
4865 PyErr_SetString(PyExc_TypeError,
4866 "argument must be a Decimal");
4867 return NULL;
4868 }
4869
4870 Py_INCREF(v);
4871 return v;
4872 }
4873
4874 static PyObject *
4875 ctx_mpd_qcopy_abs(PyObject *context, PyObject *v)
4876 {
4877 PyObject *result, *a;
4878 uint32_t status = 0;
4879
4880 CONVERT_OP_RAISE(&a, v, context);