1 | n/a | /* |
---|
2 | n/a | * Copyright (c) 2008-2012 Stefan Krah. All rights reserved. |
---|
3 | n/a | * |
---|
4 | n/a | * Redistribution and use in source and binary forms, with or without |
---|
5 | n/a | * modification, are permitted provided that the following conditions |
---|
6 | n/a | * are met: |
---|
7 | n/a | * |
---|
8 | n/a | * 1. Redistributions of source code must retain the above copyright |
---|
9 | n/a | * notice, this list of conditions and the following disclaimer. |
---|
10 | n/a | * |
---|
11 | n/a | * 2. Redistributions in binary form must reproduce the above copyright |
---|
12 | n/a | * notice, this list of conditions and the following disclaimer in the |
---|
13 | n/a | * documentation and/or other materials provided with the distribution. |
---|
14 | n/a | * |
---|
15 | n/a | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND |
---|
16 | n/a | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
17 | n/a | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
18 | n/a | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
---|
19 | n/a | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
20 | n/a | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
21 | n/a | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
22 | n/a | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
23 | n/a | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
24 | n/a | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
25 | n/a | * SUCH DAMAGE. |
---|
26 | n/a | */ |
---|
27 | n/a | |
---|
28 | n/a | |
---|
29 | n/a | #include <Python.h> |
---|
30 | n/a | #include "longintrepr.h" |
---|
31 | n/a | #include "pythread.h" |
---|
32 | n/a | #include "structmember.h" |
---|
33 | n/a | #include "complexobject.h" |
---|
34 | n/a | #include "mpdecimal.h" |
---|
35 | n/a | |
---|
36 | n/a | #include <stdlib.h> |
---|
37 | n/a | |
---|
38 | n/a | #include "docstrings.h" |
---|
39 | n/a | |
---|
40 | n/a | |
---|
41 | n/a | #if !defined(MPD_VERSION_HEX) || MPD_VERSION_HEX < 0x02040100 |
---|
42 | n/a | #error "libmpdec version >= 2.4.1 required" |
---|
43 | n/a | #endif |
---|
44 | n/a | |
---|
45 | n/a | |
---|
46 | n/a | /* |
---|
47 | n/a | * Type sizes with assertions in mpdecimal.h and pyport.h: |
---|
48 | n/a | * sizeof(size_t) == sizeof(Py_ssize_t) |
---|
49 | n/a | * sizeof(size_t) == sizeof(mpd_uint_t) == sizeof(mpd_ssize_t) |
---|
50 | n/a | */ |
---|
51 | n/a | |
---|
52 | n/a | #ifdef TEST_COVERAGE |
---|
53 | n/a | #undef Py_LOCAL_INLINE |
---|
54 | n/a | #define Py_LOCAL_INLINE Py_LOCAL |
---|
55 | n/a | #endif |
---|
56 | n/a | |
---|
57 | n/a | #define MPD_Float_operation MPD_Not_implemented |
---|
58 | n/a | |
---|
59 | n/a | #define BOUNDS_CHECK(x, MIN, MAX) x = (x < MIN || MAX < x) ? MAX : x |
---|
60 | n/a | |
---|
61 | n/a | |
---|
62 | n/a | /* _Py_DEC_MINALLOC >= MPD_MINALLOC */ |
---|
63 | n/a | #define _Py_DEC_MINALLOC 4 |
---|
64 | n/a | |
---|
65 | n/a | typedef struct { |
---|
66 | n/a | PyObject_HEAD |
---|
67 | n/a | Py_hash_t hash; |
---|
68 | n/a | mpd_t dec; |
---|
69 | n/a | mpd_uint_t data[_Py_DEC_MINALLOC]; |
---|
70 | n/a | } PyDecObject; |
---|
71 | n/a | |
---|
72 | n/a | typedef struct { |
---|
73 | n/a | PyObject_HEAD |
---|
74 | n/a | uint32_t *flags; |
---|
75 | n/a | } PyDecSignalDictObject; |
---|
76 | n/a | |
---|
77 | n/a | typedef struct { |
---|
78 | n/a | PyObject_HEAD |
---|
79 | n/a | mpd_context_t ctx; |
---|
80 | n/a | PyObject *traps; |
---|
81 | n/a | PyObject *flags; |
---|
82 | n/a | int capitals; |
---|
83 | n/a | #ifndef WITHOUT_THREADS |
---|
84 | n/a | PyThreadState *tstate; |
---|
85 | n/a | #endif |
---|
86 | n/a | } PyDecContextObject; |
---|
87 | n/a | |
---|
88 | n/a | typedef struct { |
---|
89 | n/a | PyObject_HEAD |
---|
90 | n/a | PyObject *local; |
---|
91 | n/a | PyObject *global; |
---|
92 | n/a | } PyDecContextManagerObject; |
---|
93 | n/a | |
---|
94 | n/a | |
---|
95 | n/a | #undef MPD |
---|
96 | n/a | #undef CTX |
---|
97 | n/a | static PyTypeObject PyDec_Type; |
---|
98 | n/a | static PyTypeObject *PyDecSignalDict_Type; |
---|
99 | n/a | static PyTypeObject PyDecContext_Type; |
---|
100 | n/a | static PyTypeObject PyDecContextManager_Type; |
---|
101 | n/a | #define PyDec_CheckExact(v) (Py_TYPE(v) == &PyDec_Type) |
---|
102 | n/a | #define PyDec_Check(v) PyObject_TypeCheck(v, &PyDec_Type) |
---|
103 | n/a | #define PyDecSignalDict_Check(v) (Py_TYPE(v) == PyDecSignalDict_Type) |
---|
104 | n/a | #define PyDecContext_Check(v) PyObject_TypeCheck(v, &PyDecContext_Type) |
---|
105 | n/a | #define MPD(v) (&((PyDecObject *)v)->dec) |
---|
106 | n/a | #define SdFlagAddr(v) (((PyDecSignalDictObject *)v)->flags) |
---|
107 | n/a | #define SdFlags(v) (*((PyDecSignalDictObject *)v)->flags) |
---|
108 | n/a | #define CTX(v) (&((PyDecContextObject *)v)->ctx) |
---|
109 | n/a | #define CtxCaps(v) (((PyDecContextObject *)v)->capitals) |
---|
110 | n/a | |
---|
111 | n/a | |
---|
112 | n/a | Py_LOCAL_INLINE(PyObject *) |
---|
113 | n/a | incr_true(void) |
---|
114 | n/a | { |
---|
115 | n/a | Py_INCREF(Py_True); |
---|
116 | n/a | return Py_True; |
---|
117 | n/a | } |
---|
118 | n/a | |
---|
119 | n/a | Py_LOCAL_INLINE(PyObject *) |
---|
120 | n/a | incr_false(void) |
---|
121 | n/a | { |
---|
122 | n/a | Py_INCREF(Py_False); |
---|
123 | n/a | return Py_False; |
---|
124 | n/a | } |
---|
125 | n/a | |
---|
126 | n/a | |
---|
127 | n/a | #ifdef WITHOUT_THREADS |
---|
128 | n/a | /* Default module context */ |
---|
129 | n/a | static PyObject *module_context = NULL; |
---|
130 | n/a | #else |
---|
131 | n/a | /* Key for thread state dictionary */ |
---|
132 | n/a | static PyObject *tls_context_key = NULL; |
---|
133 | n/a | /* Invariant: NULL or the most recently accessed thread local context */ |
---|
134 | n/a | static PyDecContextObject *cached_context = NULL; |
---|
135 | n/a | #endif |
---|
136 | n/a | |
---|
137 | n/a | /* Template for creating new thread contexts, calling Context() without |
---|
138 | n/a | * arguments and initializing the module_context on first access. */ |
---|
139 | n/a | static PyObject *default_context_template = NULL; |
---|
140 | n/a | /* Basic and extended context templates */ |
---|
141 | n/a | static PyObject *basic_context_template = NULL; |
---|
142 | n/a | static PyObject *extended_context_template = NULL; |
---|
143 | n/a | |
---|
144 | n/a | |
---|
145 | n/a | /* Error codes for functions that return signals or conditions */ |
---|
146 | n/a | #define DEC_INVALID_SIGNALS (MPD_Max_status+1U) |
---|
147 | n/a | #define DEC_ERR_OCCURRED (DEC_INVALID_SIGNALS<<1) |
---|
148 | n/a | #define DEC_ERRORS (DEC_INVALID_SIGNALS|DEC_ERR_OCCURRED) |
---|
149 | n/a | |
---|
150 | n/a | typedef struct { |
---|
151 | n/a | const char *name; /* condition or signal name */ |
---|
152 | n/a | const char *fqname; /* fully qualified name */ |
---|
153 | n/a | uint32_t flag; /* libmpdec flag */ |
---|
154 | n/a | PyObject *ex; /* corresponding exception */ |
---|
155 | n/a | } DecCondMap; |
---|
156 | n/a | |
---|
157 | n/a | /* Top level Exception; inherits from ArithmeticError */ |
---|
158 | n/a | static PyObject *DecimalException = NULL; |
---|
159 | n/a | |
---|
160 | n/a | /* Exceptions that correspond to IEEE signals */ |
---|
161 | n/a | #define SUBNORMAL 5 |
---|
162 | n/a | #define INEXACT 6 |
---|
163 | n/a | #define ROUNDED 7 |
---|
164 | n/a | #define SIGNAL_MAP_LEN 9 |
---|
165 | n/a | static DecCondMap signal_map[] = { |
---|
166 | n/a | {"InvalidOperation", "decimal.InvalidOperation", MPD_IEEE_Invalid_operation, NULL}, |
---|
167 | n/a | {"FloatOperation", "decimal.FloatOperation", MPD_Float_operation, NULL}, |
---|
168 | n/a | {"DivisionByZero", "decimal.DivisionByZero", MPD_Division_by_zero, NULL}, |
---|
169 | n/a | {"Overflow", "decimal.Overflow", MPD_Overflow, NULL}, |
---|
170 | n/a | {"Underflow", "decimal.Underflow", MPD_Underflow, NULL}, |
---|
171 | n/a | {"Subnormal", "decimal.Subnormal", MPD_Subnormal, NULL}, |
---|
172 | n/a | {"Inexact", "decimal.Inexact", MPD_Inexact, NULL}, |
---|
173 | n/a | {"Rounded", "decimal.Rounded", MPD_Rounded, NULL}, |
---|
174 | n/a | {"Clamped", "decimal.Clamped", MPD_Clamped, NULL}, |
---|
175 | n/a | {NULL} |
---|
176 | n/a | }; |
---|
177 | n/a | |
---|
178 | n/a | /* Exceptions that inherit from InvalidOperation */ |
---|
179 | n/a | static DecCondMap cond_map[] = { |
---|
180 | n/a | {"InvalidOperation", "decimal.InvalidOperation", MPD_Invalid_operation, NULL}, |
---|
181 | n/a | {"ConversionSyntax", "decimal.ConversionSyntax", MPD_Conversion_syntax, NULL}, |
---|
182 | n/a | {"DivisionImpossible", "decimal.DivisionImpossible", MPD_Division_impossible, NULL}, |
---|
183 | n/a | {"DivisionUndefined", "decimal.DivisionUndefined", MPD_Division_undefined, NULL}, |
---|
184 | n/a | {"InvalidContext", "decimal.InvalidContext", MPD_Invalid_context, NULL}, |
---|
185 | n/a | #ifdef EXTRA_FUNCTIONALITY |
---|
186 | n/a | {"MallocError", "decimal.MallocError", MPD_Malloc_error, NULL}, |
---|
187 | n/a | #endif |
---|
188 | n/a | {NULL} |
---|
189 | n/a | }; |
---|
190 | n/a | |
---|
191 | n/a | static const char *dec_signal_string[MPD_NUM_FLAGS] = { |
---|
192 | n/a | "Clamped", |
---|
193 | n/a | "InvalidOperation", |
---|
194 | n/a | "DivisionByZero", |
---|
195 | n/a | "InvalidOperation", |
---|
196 | n/a | "InvalidOperation", |
---|
197 | n/a | "InvalidOperation", |
---|
198 | n/a | "Inexact", |
---|
199 | n/a | "InvalidOperation", |
---|
200 | n/a | "InvalidOperation", |
---|
201 | n/a | "InvalidOperation", |
---|
202 | n/a | "FloatOperation", |
---|
203 | n/a | "Overflow", |
---|
204 | n/a | "Rounded", |
---|
205 | n/a | "Subnormal", |
---|
206 | n/a | "Underflow", |
---|
207 | n/a | }; |
---|
208 | n/a | |
---|
209 | n/a | #ifdef EXTRA_FUNCTIONALITY |
---|
210 | n/a | #define _PY_DEC_ROUND_GUARD MPD_ROUND_GUARD |
---|
211 | n/a | #else |
---|
212 | n/a | #define _PY_DEC_ROUND_GUARD (MPD_ROUND_GUARD-1) |
---|
213 | n/a | #endif |
---|
214 | n/a | static PyObject *round_map[_PY_DEC_ROUND_GUARD]; |
---|
215 | n/a | |
---|
216 | n/a | static const char *invalid_rounding_err = |
---|
217 | n/a | "valid values for rounding are:\n\ |
---|
218 | n/a | [ROUND_CEILING, ROUND_FLOOR, ROUND_UP, ROUND_DOWN,\n\ |
---|
219 | n/a | ROUND_HALF_UP, ROUND_HALF_DOWN, ROUND_HALF_EVEN,\n\ |
---|
220 | n/a | ROUND_05UP]"; |
---|
221 | n/a | |
---|
222 | n/a | static const char *invalid_signals_err = |
---|
223 | n/a | "valid values for signals are:\n\ |
---|
224 | n/a | [InvalidOperation, FloatOperation, DivisionByZero,\n\ |
---|
225 | n/a | Overflow, Underflow, Subnormal, Inexact, Rounded,\n\ |
---|
226 | n/a | Clamped]"; |
---|
227 | n/a | |
---|
228 | n/a | #ifdef EXTRA_FUNCTIONALITY |
---|
229 | n/a | static const char *invalid_flags_err = |
---|
230 | n/a | "valid values for _flags or _traps are:\n\ |
---|
231 | n/a | signals:\n\ |
---|
232 | n/a | [DecIEEEInvalidOperation, DecFloatOperation, DecDivisionByZero,\n\ |
---|
233 | n/a | DecOverflow, DecUnderflow, DecSubnormal, DecInexact, DecRounded,\n\ |
---|
234 | n/a | DecClamped]\n\ |
---|
235 | n/a | conditions which trigger DecIEEEInvalidOperation:\n\ |
---|
236 | n/a | [DecInvalidOperation, DecConversionSyntax, DecDivisionImpossible,\n\ |
---|
237 | n/a | DecDivisionUndefined, DecFpuError, DecInvalidContext, DecMallocError]"; |
---|
238 | n/a | #endif |
---|
239 | n/a | |
---|
240 | n/a | static int |
---|
241 | n/a | value_error_int(const char *mesg) |
---|
242 | n/a | { |
---|
243 | n/a | PyErr_SetString(PyExc_ValueError, mesg); |
---|
244 | n/a | return -1; |
---|
245 | n/a | } |
---|
246 | n/a | |
---|
247 | n/a | #ifdef CONFIG_32 |
---|
248 | n/a | static PyObject * |
---|
249 | n/a | value_error_ptr(const char *mesg) |
---|
250 | n/a | { |
---|
251 | n/a | PyErr_SetString(PyExc_ValueError, mesg); |
---|
252 | n/a | return NULL; |
---|
253 | n/a | } |
---|
254 | n/a | #endif |
---|
255 | n/a | |
---|
256 | n/a | static int |
---|
257 | n/a | type_error_int(const char *mesg) |
---|
258 | n/a | { |
---|
259 | n/a | PyErr_SetString(PyExc_TypeError, mesg); |
---|
260 | n/a | return -1; |
---|
261 | n/a | } |
---|
262 | n/a | |
---|
263 | n/a | static int |
---|
264 | n/a | runtime_error_int(const char *mesg) |
---|
265 | n/a | { |
---|
266 | n/a | PyErr_SetString(PyExc_RuntimeError, mesg); |
---|
267 | n/a | return -1; |
---|
268 | n/a | } |
---|
269 | n/a | #define INTERNAL_ERROR_INT(funcname) \ |
---|
270 | n/a | return runtime_error_int("internal error in " funcname) |
---|
271 | n/a | |
---|
272 | n/a | static PyObject * |
---|
273 | n/a | runtime_error_ptr(const char *mesg) |
---|
274 | n/a | { |
---|
275 | n/a | PyErr_SetString(PyExc_RuntimeError, mesg); |
---|
276 | n/a | return NULL; |
---|
277 | n/a | } |
---|
278 | n/a | #define INTERNAL_ERROR_PTR(funcname) \ |
---|
279 | n/a | return runtime_error_ptr("internal error in " funcname) |
---|
280 | n/a | |
---|
281 | n/a | static void |
---|
282 | n/a | dec_traphandler(mpd_context_t *ctx UNUSED) /* GCOV_NOT_REACHED */ |
---|
283 | n/a | { /* GCOV_NOT_REACHED */ |
---|
284 | n/a | return; /* GCOV_NOT_REACHED */ |
---|
285 | n/a | } |
---|
286 | n/a | |
---|
287 | n/a | static PyObject * |
---|
288 | n/a | flags_as_exception(uint32_t flags) |
---|
289 | n/a | { |
---|
290 | n/a | DecCondMap *cm; |
---|
291 | n/a | |
---|
292 | n/a | for (cm = signal_map; cm->name != NULL; cm++) { |
---|
293 | n/a | if (flags&cm->flag) { |
---|
294 | n/a | return cm->ex; |
---|
295 | n/a | } |
---|
296 | n/a | } |
---|
297 | n/a | |
---|
298 | n/a | INTERNAL_ERROR_PTR("flags_as_exception"); /* GCOV_NOT_REACHED */ |
---|
299 | n/a | } |
---|
300 | n/a | |
---|
301 | n/a | Py_LOCAL_INLINE(uint32_t) |
---|
302 | n/a | exception_as_flag(PyObject *ex) |
---|
303 | n/a | { |
---|
304 | n/a | DecCondMap *cm; |
---|
305 | n/a | |
---|
306 | n/a | for (cm = signal_map; cm->name != NULL; cm++) { |
---|
307 | n/a | if (cm->ex == ex) { |
---|
308 | n/a | return cm->flag; |
---|
309 | n/a | } |
---|
310 | n/a | } |
---|
311 | n/a | |
---|
312 | n/a | PyErr_SetString(PyExc_KeyError, invalid_signals_err); |
---|
313 | n/a | return DEC_INVALID_SIGNALS; |
---|
314 | n/a | } |
---|
315 | n/a | |
---|
316 | n/a | static PyObject * |
---|
317 | n/a | flags_as_list(uint32_t flags) |
---|
318 | n/a | { |
---|
319 | n/a | PyObject *list; |
---|
320 | n/a | DecCondMap *cm; |
---|
321 | n/a | |
---|
322 | n/a | list = PyList_New(0); |
---|
323 | n/a | if (list == NULL) { |
---|
324 | n/a | return NULL; |
---|
325 | n/a | } |
---|
326 | n/a | |
---|
327 | n/a | for (cm = cond_map; cm->name != NULL; cm++) { |
---|
328 | n/a | if (flags&cm->flag) { |
---|
329 | n/a | if (PyList_Append(list, cm->ex) < 0) { |
---|
330 | n/a | goto error; |
---|
331 | n/a | } |
---|
332 | n/a | } |
---|
333 | n/a | } |
---|
334 | n/a | for (cm = signal_map+1; cm->name != NULL; cm++) { |
---|
335 | n/a | if (flags&cm->flag) { |
---|
336 | n/a | if (PyList_Append(list, cm->ex) < 0) { |
---|
337 | n/a | goto error; |
---|
338 | n/a | } |
---|
339 | n/a | } |
---|
340 | n/a | } |
---|
341 | n/a | |
---|
342 | n/a | return list; |
---|
343 | n/a | |
---|
344 | n/a | error: |
---|
345 | n/a | Py_DECREF(list); |
---|
346 | n/a | return NULL; |
---|
347 | n/a | } |
---|
348 | n/a | |
---|
349 | n/a | static PyObject * |
---|
350 | n/a | signals_as_list(uint32_t flags) |
---|
351 | n/a | { |
---|
352 | n/a | PyObject *list; |
---|
353 | n/a | DecCondMap *cm; |
---|
354 | n/a | |
---|
355 | n/a | list = PyList_New(0); |
---|
356 | n/a | if (list == NULL) { |
---|
357 | n/a | return NULL; |
---|
358 | n/a | } |
---|
359 | n/a | |
---|
360 | n/a | for (cm = signal_map; cm->name != NULL; cm++) { |
---|
361 | n/a | if (flags&cm->flag) { |
---|
362 | n/a | if (PyList_Append(list, cm->ex) < 0) { |
---|
363 | n/a | Py_DECREF(list); |
---|
364 | n/a | return NULL; |
---|
365 | n/a | } |
---|
366 | n/a | } |
---|
367 | n/a | } |
---|
368 | n/a | |
---|
369 | n/a | return list; |
---|
370 | n/a | } |
---|
371 | n/a | |
---|
372 | n/a | static uint32_t |
---|
373 | n/a | list_as_flags(PyObject *list) |
---|
374 | n/a | { |
---|
375 | n/a | PyObject *item; |
---|
376 | n/a | uint32_t flags, x; |
---|
377 | n/a | Py_ssize_t n, j; |
---|
378 | n/a | |
---|
379 | n/a | assert(PyList_Check(list)); |
---|
380 | n/a | |
---|
381 | n/a | n = PyList_Size(list); |
---|
382 | n/a | flags = 0; |
---|
383 | n/a | for (j = 0; j < n; j++) { |
---|
384 | n/a | item = PyList_GetItem(list, j); |
---|
385 | n/a | x = exception_as_flag(item); |
---|
386 | n/a | if (x & DEC_ERRORS) { |
---|
387 | n/a | return x; |
---|
388 | n/a | } |
---|
389 | n/a | flags |= x; |
---|
390 | n/a | } |
---|
391 | n/a | |
---|
392 | n/a | return flags; |
---|
393 | n/a | } |
---|
394 | n/a | |
---|
395 | n/a | static PyObject * |
---|
396 | n/a | flags_as_dict(uint32_t flags) |
---|
397 | n/a | { |
---|
398 | n/a | DecCondMap *cm; |
---|
399 | n/a | PyObject *dict; |
---|
400 | n/a | |
---|
401 | n/a | dict = PyDict_New(); |
---|
402 | n/a | if (dict == NULL) { |
---|
403 | n/a | return NULL; |
---|
404 | n/a | } |
---|
405 | n/a | |
---|
406 | n/a | for (cm = signal_map; cm->name != NULL; cm++) { |
---|
407 | n/a | PyObject *b = flags&cm->flag ? Py_True : Py_False; |
---|
408 | n/a | if (PyDict_SetItem(dict, cm->ex, b) < 0) { |
---|
409 | n/a | Py_DECREF(dict); |
---|
410 | n/a | return NULL; |
---|
411 | n/a | } |
---|
412 | n/a | } |
---|
413 | n/a | |
---|
414 | n/a | return dict; |
---|
415 | n/a | } |
---|
416 | n/a | |
---|
417 | n/a | static uint32_t |
---|
418 | n/a | dict_as_flags(PyObject *val) |
---|
419 | n/a | { |
---|
420 | n/a | PyObject *b; |
---|
421 | n/a | DecCondMap *cm; |
---|
422 | n/a | uint32_t flags = 0; |
---|
423 | n/a | int x; |
---|
424 | n/a | |
---|
425 | n/a | if (!PyDict_Check(val)) { |
---|
426 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
427 | n/a | "argument must be a signal dict"); |
---|
428 | n/a | return DEC_INVALID_SIGNALS; |
---|
429 | n/a | } |
---|
430 | n/a | |
---|
431 | n/a | if (PyDict_Size(val) != SIGNAL_MAP_LEN) { |
---|
432 | n/a | PyErr_SetString(PyExc_KeyError, |
---|
433 | n/a | "invalid signal dict"); |
---|
434 | n/a | return DEC_INVALID_SIGNALS; |
---|
435 | n/a | } |
---|
436 | n/a | |
---|
437 | n/a | for (cm = signal_map; cm->name != NULL; cm++) { |
---|
438 | n/a | b = PyDict_GetItemWithError(val, cm->ex); |
---|
439 | n/a | if (b == NULL) { |
---|
440 | n/a | if (PyErr_Occurred()) { |
---|
441 | n/a | return DEC_ERR_OCCURRED; |
---|
442 | n/a | } |
---|
443 | n/a | PyErr_SetString(PyExc_KeyError, |
---|
444 | n/a | "invalid signal dict"); |
---|
445 | n/a | return DEC_INVALID_SIGNALS; |
---|
446 | n/a | } |
---|
447 | n/a | |
---|
448 | n/a | x = PyObject_IsTrue(b); |
---|
449 | n/a | if (x < 0) { |
---|
450 | n/a | return DEC_ERR_OCCURRED; |
---|
451 | n/a | } |
---|
452 | n/a | if (x == 1) { |
---|
453 | n/a | flags |= cm->flag; |
---|
454 | n/a | } |
---|
455 | n/a | } |
---|
456 | n/a | |
---|
457 | n/a | return flags; |
---|
458 | n/a | } |
---|
459 | n/a | |
---|
460 | n/a | #ifdef EXTRA_FUNCTIONALITY |
---|
461 | n/a | static uint32_t |
---|
462 | n/a | long_as_flags(PyObject *v) |
---|
463 | n/a | { |
---|
464 | n/a | long x; |
---|
465 | n/a | |
---|
466 | n/a | x = PyLong_AsLong(v); |
---|
467 | n/a | if (x == -1 && PyErr_Occurred()) { |
---|
468 | n/a | return DEC_ERR_OCCURRED; |
---|
469 | n/a | } |
---|
470 | n/a | if (x < 0 || x > (long)MPD_Max_status) { |
---|
471 | n/a | PyErr_SetString(PyExc_TypeError, invalid_flags_err); |
---|
472 | n/a | return DEC_INVALID_SIGNALS; |
---|
473 | n/a | } |
---|
474 | n/a | |
---|
475 | n/a | return x; |
---|
476 | n/a | } |
---|
477 | n/a | #endif |
---|
478 | n/a | |
---|
479 | n/a | static int |
---|
480 | n/a | dec_addstatus(PyObject *context, uint32_t status) |
---|
481 | n/a | { |
---|
482 | n/a | mpd_context_t *ctx = CTX(context); |
---|
483 | n/a | |
---|
484 | n/a | ctx->status |= status; |
---|
485 | n/a | if (status & (ctx->traps|MPD_Malloc_error)) { |
---|
486 | n/a | PyObject *ex, *siglist; |
---|
487 | n/a | |
---|
488 | n/a | if (status & MPD_Malloc_error) { |
---|
489 | n/a | PyErr_NoMemory(); |
---|
490 | n/a | return 1; |
---|
491 | n/a | } |
---|
492 | n/a | |
---|
493 | n/a | ex = flags_as_exception(ctx->traps&status); |
---|
494 | n/a | if (ex == NULL) { |
---|
495 | n/a | return 1; /* GCOV_NOT_REACHED */ |
---|
496 | n/a | } |
---|
497 | n/a | siglist = flags_as_list(ctx->traps&status); |
---|
498 | n/a | if (siglist == NULL) { |
---|
499 | n/a | return 1; |
---|
500 | n/a | } |
---|
501 | n/a | |
---|
502 | n/a | PyErr_SetObject(ex, siglist); |
---|
503 | n/a | Py_DECREF(siglist); |
---|
504 | n/a | return 1; |
---|
505 | n/a | } |
---|
506 | n/a | return 0; |
---|
507 | n/a | } |
---|
508 | n/a | |
---|
509 | n/a | static int |
---|
510 | n/a | getround(PyObject *v) |
---|
511 | n/a | { |
---|
512 | n/a | int i; |
---|
513 | n/a | |
---|
514 | n/a | if (PyUnicode_Check(v)) { |
---|
515 | n/a | for (i = 0; i < _PY_DEC_ROUND_GUARD; i++) { |
---|
516 | n/a | if (v == round_map[i]) { |
---|
517 | n/a | return i; |
---|
518 | n/a | } |
---|
519 | n/a | } |
---|
520 | n/a | for (i = 0; i < _PY_DEC_ROUND_GUARD; i++) { |
---|
521 | n/a | if (PyUnicode_Compare(v, round_map[i]) == 0) { |
---|
522 | n/a | return i; |
---|
523 | n/a | } |
---|
524 | n/a | } |
---|
525 | n/a | } |
---|
526 | n/a | |
---|
527 | n/a | return type_error_int(invalid_rounding_err); |
---|
528 | n/a | } |
---|
529 | n/a | |
---|
530 | n/a | |
---|
531 | n/a | /******************************************************************************/ |
---|
532 | n/a | /* SignalDict Object */ |
---|
533 | n/a | /******************************************************************************/ |
---|
534 | n/a | |
---|
535 | n/a | /* The SignalDict is a MutableMapping that provides access to the |
---|
536 | n/a | mpd_context_t flags, which reside in the context object. When a |
---|
537 | n/a | new context is created, context.traps and context.flags are |
---|
538 | n/a | initialized to new SignalDicts. Once a SignalDict is tied to |
---|
539 | n/a | a context, it cannot be deleted. */ |
---|
540 | n/a | |
---|
541 | n/a | static int |
---|
542 | n/a | signaldict_init(PyObject *self, PyObject *args UNUSED, PyObject *kwds UNUSED) |
---|
543 | n/a | { |
---|
544 | n/a | SdFlagAddr(self) = NULL; |
---|
545 | n/a | return 0; |
---|
546 | n/a | } |
---|
547 | n/a | |
---|
548 | n/a | static Py_ssize_t |
---|
549 | n/a | signaldict_len(PyObject *self UNUSED) |
---|
550 | n/a | { |
---|
551 | n/a | return SIGNAL_MAP_LEN; |
---|
552 | n/a | } |
---|
553 | n/a | |
---|
554 | n/a | static PyObject *SignalTuple; |
---|
555 | n/a | static PyObject * |
---|
556 | n/a | signaldict_iter(PyObject *self UNUSED) |
---|
557 | n/a | { |
---|
558 | n/a | return PyTuple_Type.tp_iter(SignalTuple); |
---|
559 | n/a | } |
---|
560 | n/a | |
---|
561 | n/a | static PyObject * |
---|
562 | n/a | signaldict_getitem(PyObject *self, PyObject *key) |
---|
563 | n/a | { |
---|
564 | n/a | uint32_t flag; |
---|
565 | n/a | |
---|
566 | n/a | flag = exception_as_flag(key); |
---|
567 | n/a | if (flag & DEC_ERRORS) { |
---|
568 | n/a | return NULL; |
---|
569 | n/a | } |
---|
570 | n/a | |
---|
571 | n/a | return SdFlags(self)&flag ? incr_true() : incr_false(); |
---|
572 | n/a | } |
---|
573 | n/a | |
---|
574 | n/a | static int |
---|
575 | n/a | signaldict_setitem(PyObject *self, PyObject *key, PyObject *value) |
---|
576 | n/a | { |
---|
577 | n/a | uint32_t flag; |
---|
578 | n/a | int x; |
---|
579 | n/a | |
---|
580 | n/a | if (value == NULL) { |
---|
581 | n/a | return value_error_int("signal keys cannot be deleted"); |
---|
582 | n/a | } |
---|
583 | n/a | |
---|
584 | n/a | flag = exception_as_flag(key); |
---|
585 | n/a | if (flag & DEC_ERRORS) { |
---|
586 | n/a | return -1; |
---|
587 | n/a | } |
---|
588 | n/a | |
---|
589 | n/a | x = PyObject_IsTrue(value); |
---|
590 | n/a | if (x < 0) { |
---|
591 | n/a | return -1; |
---|
592 | n/a | } |
---|
593 | n/a | |
---|
594 | n/a | if (x == 1) { |
---|
595 | n/a | SdFlags(self) |= flag; |
---|
596 | n/a | } |
---|
597 | n/a | else { |
---|
598 | n/a | SdFlags(self) &= ~flag; |
---|
599 | n/a | } |
---|
600 | n/a | |
---|
601 | n/a | return 0; |
---|
602 | n/a | } |
---|
603 | n/a | |
---|
604 | n/a | static PyObject * |
---|
605 | n/a | signaldict_repr(PyObject *self) |
---|
606 | n/a | { |
---|
607 | n/a | DecCondMap *cm; |
---|
608 | n/a | const char *n[SIGNAL_MAP_LEN]; /* name */ |
---|
609 | n/a | const char *b[SIGNAL_MAP_LEN]; /* bool */ |
---|
610 | n/a | int i; |
---|
611 | n/a | |
---|
612 | n/a | assert(SIGNAL_MAP_LEN == 9); |
---|
613 | n/a | |
---|
614 | n/a | for (cm=signal_map, i=0; cm->name != NULL; cm++, i++) { |
---|
615 | n/a | n[i] = cm->fqname; |
---|
616 | n/a | b[i] = SdFlags(self)&cm->flag ? "True" : "False"; |
---|
617 | n/a | } |
---|
618 | n/a | return PyUnicode_FromFormat( |
---|
619 | n/a | "{<class '%s'>:%s, <class '%s'>:%s, <class '%s'>:%s, " |
---|
620 | n/a | "<class '%s'>:%s, <class '%s'>:%s, <class '%s'>:%s, " |
---|
621 | n/a | "<class '%s'>:%s, <class '%s'>:%s, <class '%s'>:%s}", |
---|
622 | n/a | n[0], b[0], n[1], b[1], n[2], b[2], |
---|
623 | n/a | n[3], b[3], n[4], b[4], n[5], b[5], |
---|
624 | n/a | n[6], b[6], n[7], b[7], n[8], b[8]); |
---|
625 | n/a | } |
---|
626 | n/a | |
---|
627 | n/a | static PyObject * |
---|
628 | n/a | signaldict_richcompare(PyObject *v, PyObject *w, int op) |
---|
629 | n/a | { |
---|
630 | n/a | PyObject *res = Py_NotImplemented; |
---|
631 | n/a | |
---|
632 | n/a | assert(PyDecSignalDict_Check(v)); |
---|
633 | n/a | |
---|
634 | n/a | if (op == Py_EQ || op == Py_NE) { |
---|
635 | n/a | if (PyDecSignalDict_Check(w)) { |
---|
636 | n/a | res = (SdFlags(v)==SdFlags(w)) ^ (op==Py_NE) ? Py_True : Py_False; |
---|
637 | n/a | } |
---|
638 | n/a | else if (PyDict_Check(w)) { |
---|
639 | n/a | uint32_t flags = dict_as_flags(w); |
---|
640 | n/a | if (flags & DEC_ERRORS) { |
---|
641 | n/a | if (flags & DEC_INVALID_SIGNALS) { |
---|
642 | n/a | /* non-comparable: Py_NotImplemented */ |
---|
643 | n/a | PyErr_Clear(); |
---|
644 | n/a | } |
---|
645 | n/a | else { |
---|
646 | n/a | return NULL; |
---|
647 | n/a | } |
---|
648 | n/a | } |
---|
649 | n/a | else { |
---|
650 | n/a | res = (SdFlags(v)==flags) ^ (op==Py_NE) ? Py_True : Py_False; |
---|
651 | n/a | } |
---|
652 | n/a | } |
---|
653 | n/a | } |
---|
654 | n/a | |
---|
655 | n/a | Py_INCREF(res); |
---|
656 | n/a | return res; |
---|
657 | n/a | } |
---|
658 | n/a | |
---|
659 | n/a | static PyObject * |
---|
660 | n/a | signaldict_copy(PyObject *self, PyObject *args UNUSED) |
---|
661 | n/a | { |
---|
662 | n/a | return flags_as_dict(SdFlags(self)); |
---|
663 | n/a | } |
---|
664 | n/a | |
---|
665 | n/a | |
---|
666 | n/a | static PyMappingMethods signaldict_as_mapping = { |
---|
667 | n/a | (lenfunc)signaldict_len, /* mp_length */ |
---|
668 | n/a | (binaryfunc)signaldict_getitem, /* mp_subscript */ |
---|
669 | n/a | (objobjargproc)signaldict_setitem /* mp_ass_subscript */ |
---|
670 | n/a | }; |
---|
671 | n/a | |
---|
672 | n/a | static PyMethodDef signaldict_methods[] = { |
---|
673 | n/a | { "copy", (PyCFunction)signaldict_copy, METH_NOARGS, NULL}, |
---|
674 | n/a | {NULL, NULL} |
---|
675 | n/a | }; |
---|
676 | n/a | |
---|
677 | n/a | |
---|
678 | n/a | static PyTypeObject PyDecSignalDictMixin_Type = |
---|
679 | n/a | { |
---|
680 | n/a | PyVarObject_HEAD_INIT(0, 0) |
---|
681 | n/a | "decimal.SignalDictMixin", /* tp_name */ |
---|
682 | n/a | sizeof(PyDecSignalDictObject), /* tp_basicsize */ |
---|
683 | n/a | 0, /* tp_itemsize */ |
---|
684 | n/a | 0, /* tp_dealloc */ |
---|
685 | n/a | 0, /* tp_print */ |
---|
686 | n/a | (getattrfunc) 0, /* tp_getattr */ |
---|
687 | n/a | (setattrfunc) 0, /* tp_setattr */ |
---|
688 | n/a | 0, /* tp_reserved */ |
---|
689 | n/a | (reprfunc) signaldict_repr, /* tp_repr */ |
---|
690 | n/a | 0, /* tp_as_number */ |
---|
691 | n/a | 0, /* tp_as_sequence */ |
---|
692 | n/a | &signaldict_as_mapping, /* tp_as_mapping */ |
---|
693 | n/a | PyObject_HashNotImplemented, /* tp_hash */ |
---|
694 | n/a | 0, /* tp_call */ |
---|
695 | n/a | (reprfunc) 0, /* tp_str */ |
---|
696 | n/a | PyObject_GenericGetAttr, /* tp_getattro */ |
---|
697 | n/a | (setattrofunc) 0, /* tp_setattro */ |
---|
698 | n/a | (PyBufferProcs *) 0, /* tp_as_buffer */ |
---|
699 | n/a | Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE| |
---|
700 | n/a | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
---|
701 | n/a | 0, /* tp_doc */ |
---|
702 | n/a | 0, /* tp_traverse */ |
---|
703 | n/a | 0, /* tp_clear */ |
---|
704 | n/a | signaldict_richcompare, /* tp_richcompare */ |
---|
705 | n/a | 0, /* tp_weaklistoffset */ |
---|
706 | n/a | (getiterfunc)signaldict_iter, /* tp_iter */ |
---|
707 | n/a | 0, /* tp_iternext */ |
---|
708 | n/a | signaldict_methods, /* tp_methods */ |
---|
709 | n/a | 0, /* tp_members */ |
---|
710 | n/a | 0, /* tp_getset */ |
---|
711 | n/a | 0, /* tp_base */ |
---|
712 | n/a | 0, /* tp_dict */ |
---|
713 | n/a | 0, /* tp_descr_get */ |
---|
714 | n/a | 0, /* tp_descr_set */ |
---|
715 | n/a | 0, /* tp_dictoffset */ |
---|
716 | n/a | (initproc)signaldict_init, /* tp_init */ |
---|
717 | n/a | 0, /* tp_alloc */ |
---|
718 | n/a | PyType_GenericNew, /* tp_new */ |
---|
719 | n/a | }; |
---|
720 | n/a | |
---|
721 | n/a | |
---|
722 | n/a | /******************************************************************************/ |
---|
723 | n/a | /* Context Object, Part 1 */ |
---|
724 | n/a | /******************************************************************************/ |
---|
725 | n/a | |
---|
726 | n/a | #define Dec_CONTEXT_GET_SSIZE(mem) \ |
---|
727 | n/a | static PyObject * \ |
---|
728 | n/a | context_get##mem(PyObject *self, void *closure UNUSED) \ |
---|
729 | n/a | { \ |
---|
730 | n/a | return PyLong_FromSsize_t(mpd_get##mem(CTX(self))); \ |
---|
731 | n/a | } |
---|
732 | n/a | |
---|
733 | n/a | #define Dec_CONTEXT_GET_ULONG(mem) \ |
---|
734 | n/a | static PyObject * \ |
---|
735 | n/a | context_get##mem(PyObject *self, void *closure UNUSED) \ |
---|
736 | n/a | { \ |
---|
737 | n/a | return PyLong_FromUnsignedLong(mpd_get##mem(CTX(self))); \ |
---|
738 | n/a | } |
---|
739 | n/a | |
---|
740 | n/a | Dec_CONTEXT_GET_SSIZE(prec) |
---|
741 | n/a | Dec_CONTEXT_GET_SSIZE(emax) |
---|
742 | n/a | Dec_CONTEXT_GET_SSIZE(emin) |
---|
743 | n/a | Dec_CONTEXT_GET_SSIZE(clamp) |
---|
744 | n/a | |
---|
745 | n/a | #ifdef EXTRA_FUNCTIONALITY |
---|
746 | n/a | Dec_CONTEXT_GET_ULONG(traps) |
---|
747 | n/a | Dec_CONTEXT_GET_ULONG(status) |
---|
748 | n/a | #endif |
---|
749 | n/a | |
---|
750 | n/a | static PyObject * |
---|
751 | n/a | context_getround(PyObject *self, void *closure UNUSED) |
---|
752 | n/a | { |
---|
753 | n/a | int i = mpd_getround(CTX(self)); |
---|
754 | n/a | |
---|
755 | n/a | Py_INCREF(round_map[i]); |
---|
756 | n/a | return round_map[i]; |
---|
757 | n/a | } |
---|
758 | n/a | |
---|
759 | n/a | static PyObject * |
---|
760 | n/a | context_getcapitals(PyObject *self, void *closure UNUSED) |
---|
761 | n/a | { |
---|
762 | n/a | return PyLong_FromLong(CtxCaps(self)); |
---|
763 | n/a | } |
---|
764 | n/a | |
---|
765 | n/a | #ifdef EXTRA_FUNCTIONALITY |
---|
766 | n/a | static PyObject * |
---|
767 | n/a | context_getallcr(PyObject *self, void *closure UNUSED) |
---|
768 | n/a | { |
---|
769 | n/a | return PyLong_FromLong(mpd_getcr(CTX(self))); |
---|
770 | n/a | } |
---|
771 | n/a | #endif |
---|
772 | n/a | |
---|
773 | n/a | static PyObject * |
---|
774 | n/a | context_getetiny(PyObject *self, PyObject *dummy UNUSED) |
---|
775 | n/a | { |
---|
776 | n/a | return PyLong_FromSsize_t(mpd_etiny(CTX(self))); |
---|
777 | n/a | } |
---|
778 | n/a | |
---|
779 | n/a | static PyObject * |
---|
780 | n/a | context_getetop(PyObject *self, PyObject *dummy UNUSED) |
---|
781 | n/a | { |
---|
782 | n/a | return PyLong_FromSsize_t(mpd_etop(CTX(self))); |
---|
783 | n/a | } |
---|
784 | n/a | |
---|
785 | n/a | static int |
---|
786 | n/a | context_setprec(PyObject *self, PyObject *value, void *closure UNUSED) |
---|
787 | n/a | { |
---|
788 | n/a | mpd_context_t *ctx; |
---|
789 | n/a | mpd_ssize_t x; |
---|
790 | n/a | |
---|
791 | n/a | x = PyLong_AsSsize_t(value); |
---|
792 | n/a | if (x == -1 && PyErr_Occurred()) { |
---|
793 | n/a | return -1; |
---|
794 | n/a | } |
---|
795 | n/a | |
---|
796 | n/a | ctx = CTX(self); |
---|
797 | n/a | if (!mpd_qsetprec(ctx, x)) { |
---|
798 | n/a | return value_error_int( |
---|
799 | n/a | "valid range for prec is [1, MAX_PREC]"); |
---|
800 | n/a | } |
---|
801 | n/a | |
---|
802 | n/a | return 0; |
---|
803 | n/a | } |
---|
804 | n/a | |
---|
805 | n/a | static int |
---|
806 | n/a | context_setemin(PyObject *self, PyObject *value, void *closure UNUSED) |
---|
807 | n/a | { |
---|
808 | n/a | mpd_context_t *ctx; |
---|
809 | n/a | mpd_ssize_t x; |
---|
810 | n/a | |
---|
811 | n/a | x = PyLong_AsSsize_t(value); |
---|
812 | n/a | if (x == -1 && PyErr_Occurred()) { |
---|
813 | n/a | return -1; |
---|
814 | n/a | } |
---|
815 | n/a | |
---|
816 | n/a | ctx = CTX(self); |
---|
817 | n/a | if (!mpd_qsetemin(ctx, x)) { |
---|
818 | n/a | return value_error_int( |
---|
819 | n/a | "valid range for Emin is [MIN_EMIN, 0]"); |
---|
820 | n/a | } |
---|
821 | n/a | |
---|
822 | n/a | return 0; |
---|
823 | n/a | } |
---|
824 | n/a | |
---|
825 | n/a | static int |
---|
826 | n/a | context_setemax(PyObject *self, PyObject *value, void *closure UNUSED) |
---|
827 | n/a | { |
---|
828 | n/a | mpd_context_t *ctx; |
---|
829 | n/a | mpd_ssize_t x; |
---|
830 | n/a | |
---|
831 | n/a | x = PyLong_AsSsize_t(value); |
---|
832 | n/a | if (x == -1 && PyErr_Occurred()) { |
---|
833 | n/a | return -1; |
---|
834 | n/a | } |
---|
835 | n/a | |
---|
836 | n/a | ctx = CTX(self); |
---|
837 | n/a | if (!mpd_qsetemax(ctx, x)) { |
---|
838 | n/a | return value_error_int( |
---|
839 | n/a | "valid range for Emax is [0, MAX_EMAX]"); |
---|
840 | n/a | } |
---|
841 | n/a | |
---|
842 | n/a | return 0; |
---|
843 | n/a | } |
---|
844 | n/a | |
---|
845 | n/a | #ifdef CONFIG_32 |
---|
846 | n/a | static PyObject * |
---|
847 | n/a | context_unsafe_setprec(PyObject *self, PyObject *value) |
---|
848 | n/a | { |
---|
849 | n/a | mpd_context_t *ctx = CTX(self); |
---|
850 | n/a | mpd_ssize_t x; |
---|
851 | n/a | |
---|
852 | n/a | x = PyLong_AsSsize_t(value); |
---|
853 | n/a | if (x == -1 && PyErr_Occurred()) { |
---|
854 | n/a | return NULL; |
---|
855 | n/a | } |
---|
856 | n/a | |
---|
857 | n/a | if (x < 1 || x > 1070000000L) { |
---|
858 | n/a | return value_error_ptr( |
---|
859 | n/a | "valid range for unsafe prec is [1, 1070000000]"); |
---|
860 | n/a | } |
---|
861 | n/a | |
---|
862 | n/a | ctx->prec = x; |
---|
863 | n/a | Py_RETURN_NONE; |
---|
864 | n/a | } |
---|
865 | n/a | |
---|
866 | n/a | static PyObject * |
---|
867 | n/a | context_unsafe_setemin(PyObject *self, PyObject *value) |
---|
868 | n/a | { |
---|
869 | n/a | mpd_context_t *ctx = CTX(self); |
---|
870 | n/a | mpd_ssize_t x; |
---|
871 | n/a | |
---|
872 | n/a | x = PyLong_AsSsize_t(value); |
---|
873 | n/a | if (x == -1 && PyErr_Occurred()) { |
---|
874 | n/a | return NULL; |
---|
875 | n/a | } |
---|
876 | n/a | |
---|
877 | n/a | if (x < -1070000000L || x > 0) { |
---|
878 | n/a | return value_error_ptr( |
---|
879 | n/a | "valid range for unsafe emin is [-1070000000, 0]"); |
---|
880 | n/a | } |
---|
881 | n/a | |
---|
882 | n/a | ctx->emin = x; |
---|
883 | n/a | Py_RETURN_NONE; |
---|
884 | n/a | } |
---|
885 | n/a | |
---|
886 | n/a | static PyObject * |
---|
887 | n/a | context_unsafe_setemax(PyObject *self, PyObject *value) |
---|
888 | n/a | { |
---|
889 | n/a | mpd_context_t *ctx = CTX(self); |
---|
890 | n/a | mpd_ssize_t x; |
---|
891 | n/a | |
---|
892 | n/a | x = PyLong_AsSsize_t(value); |
---|
893 | n/a | if (x == -1 && PyErr_Occurred()) { |
---|
894 | n/a | return NULL; |
---|
895 | n/a | } |
---|
896 | n/a | |
---|
897 | n/a | if (x < 0 || x > 1070000000L) { |
---|
898 | n/a | return value_error_ptr( |
---|
899 | n/a | "valid range for unsafe emax is [0, 1070000000]"); |
---|
900 | n/a | } |
---|
901 | n/a | |
---|
902 | n/a | ctx->emax = x; |
---|
903 | n/a | Py_RETURN_NONE; |
---|
904 | n/a | } |
---|
905 | n/a | #endif |
---|
906 | n/a | |
---|
907 | n/a | static int |
---|
908 | n/a | context_setround(PyObject *self, PyObject *value, void *closure UNUSED) |
---|
909 | n/a | { |
---|
910 | n/a | mpd_context_t *ctx; |
---|
911 | n/a | int x; |
---|
912 | n/a | |
---|
913 | n/a | x = getround(value); |
---|
914 | n/a | if (x == -1) { |
---|
915 | n/a | return -1; |
---|
916 | n/a | } |
---|
917 | n/a | |
---|
918 | n/a | ctx = CTX(self); |
---|
919 | n/a | if (!mpd_qsetround(ctx, x)) { |
---|
920 | n/a | INTERNAL_ERROR_INT("context_setround"); /* GCOV_NOT_REACHED */ |
---|
921 | n/a | } |
---|
922 | n/a | |
---|
923 | n/a | return 0; |
---|
924 | n/a | } |
---|
925 | n/a | |
---|
926 | n/a | static int |
---|
927 | n/a | context_setcapitals(PyObject *self, PyObject *value, void *closure UNUSED) |
---|
928 | n/a | { |
---|
929 | n/a | mpd_ssize_t x; |
---|
930 | n/a | |
---|
931 | n/a | x = PyLong_AsSsize_t(value); |
---|
932 | n/a | if (x == -1 && PyErr_Occurred()) { |
---|
933 | n/a | return -1; |
---|
934 | n/a | } |
---|
935 | n/a | |
---|
936 | n/a | if (x != 0 && x != 1) { |
---|
937 | n/a | return value_error_int( |
---|
938 | n/a | "valid values for capitals are 0 or 1"); |
---|
939 | n/a | } |
---|
940 | n/a | CtxCaps(self) = (int)x; |
---|
941 | n/a | |
---|
942 | n/a | return 0; |
---|
943 | n/a | } |
---|
944 | n/a | |
---|
945 | n/a | #ifdef EXTRA_FUNCTIONALITY |
---|
946 | n/a | static int |
---|
947 | n/a | context_settraps(PyObject *self, PyObject *value, void *closure UNUSED) |
---|
948 | n/a | { |
---|
949 | n/a | mpd_context_t *ctx; |
---|
950 | n/a | uint32_t flags; |
---|
951 | n/a | |
---|
952 | n/a | flags = long_as_flags(value); |
---|
953 | n/a | if (flags & DEC_ERRORS) { |
---|
954 | n/a | return -1; |
---|
955 | n/a | } |
---|
956 | n/a | |
---|
957 | n/a | ctx = CTX(self); |
---|
958 | n/a | if (!mpd_qsettraps(ctx, flags)) { |
---|
959 | n/a | INTERNAL_ERROR_INT("context_settraps"); |
---|
960 | n/a | } |
---|
961 | n/a | |
---|
962 | n/a | return 0; |
---|
963 | n/a | } |
---|
964 | n/a | #endif |
---|
965 | n/a | |
---|
966 | n/a | static int |
---|
967 | n/a | context_settraps_list(PyObject *self, PyObject *value) |
---|
968 | n/a | { |
---|
969 | n/a | mpd_context_t *ctx; |
---|
970 | n/a | uint32_t flags; |
---|
971 | n/a | |
---|
972 | n/a | flags = list_as_flags(value); |
---|
973 | n/a | if (flags & DEC_ERRORS) { |
---|
974 | n/a | return -1; |
---|
975 | n/a | } |
---|
976 | n/a | |
---|
977 | n/a | ctx = CTX(self); |
---|
978 | n/a | if (!mpd_qsettraps(ctx, flags)) { |
---|
979 | n/a | INTERNAL_ERROR_INT("context_settraps_list"); |
---|
980 | n/a | } |
---|
981 | n/a | |
---|
982 | n/a | return 0; |
---|
983 | n/a | } |
---|
984 | n/a | |
---|
985 | n/a | static int |
---|
986 | n/a | context_settraps_dict(PyObject *self, PyObject *value) |
---|
987 | n/a | { |
---|
988 | n/a | mpd_context_t *ctx; |
---|
989 | n/a | uint32_t flags; |
---|
990 | n/a | |
---|
991 | n/a | if (PyDecSignalDict_Check(value)) { |
---|
992 | n/a | flags = SdFlags(value); |
---|
993 | n/a | } |
---|
994 | n/a | else { |
---|
995 | n/a | flags = dict_as_flags(value); |
---|
996 | n/a | if (flags & DEC_ERRORS) { |
---|
997 | n/a | return -1; |
---|
998 | n/a | } |
---|
999 | n/a | } |
---|
1000 | n/a | |
---|
1001 | n/a | ctx = CTX(self); |
---|
1002 | n/a | if (!mpd_qsettraps(ctx, flags)) { |
---|
1003 | n/a | INTERNAL_ERROR_INT("context_settraps_dict"); |
---|
1004 | n/a | } |
---|
1005 | n/a | |
---|
1006 | n/a | return 0; |
---|
1007 | n/a | } |
---|
1008 | n/a | |
---|
1009 | n/a | #ifdef EXTRA_FUNCTIONALITY |
---|
1010 | n/a | static int |
---|
1011 | n/a | context_setstatus(PyObject *self, PyObject *value, void *closure UNUSED) |
---|
1012 | n/a | { |
---|
1013 | n/a | mpd_context_t *ctx; |
---|
1014 | n/a | uint32_t flags; |
---|
1015 | n/a | |
---|
1016 | n/a | flags = long_as_flags(value); |
---|
1017 | n/a | if (flags & DEC_ERRORS) { |
---|
1018 | n/a | return -1; |
---|
1019 | n/a | } |
---|
1020 | n/a | |
---|
1021 | n/a | ctx = CTX(self); |
---|
1022 | n/a | if (!mpd_qsetstatus(ctx, flags)) { |
---|
1023 | n/a | INTERNAL_ERROR_INT("context_setstatus"); |
---|
1024 | n/a | } |
---|
1025 | n/a | |
---|
1026 | n/a | return 0; |
---|
1027 | n/a | } |
---|
1028 | n/a | #endif |
---|
1029 | n/a | |
---|
1030 | n/a | static int |
---|
1031 | n/a | context_setstatus_list(PyObject *self, PyObject *value) |
---|
1032 | n/a | { |
---|
1033 | n/a | mpd_context_t *ctx; |
---|
1034 | n/a | uint32_t flags; |
---|
1035 | n/a | |
---|
1036 | n/a | flags = list_as_flags(value); |
---|
1037 | n/a | if (flags & DEC_ERRORS) { |
---|
1038 | n/a | return -1; |
---|
1039 | n/a | } |
---|
1040 | n/a | |
---|
1041 | n/a | ctx = CTX(self); |
---|
1042 | n/a | if (!mpd_qsetstatus(ctx, flags)) { |
---|
1043 | n/a | INTERNAL_ERROR_INT("context_setstatus_list"); |
---|
1044 | n/a | } |
---|
1045 | n/a | |
---|
1046 | n/a | return 0; |
---|
1047 | n/a | } |
---|
1048 | n/a | |
---|
1049 | n/a | static int |
---|
1050 | n/a | context_setstatus_dict(PyObject *self, PyObject *value) |
---|
1051 | n/a | { |
---|
1052 | n/a | mpd_context_t *ctx; |
---|
1053 | n/a | uint32_t flags; |
---|
1054 | n/a | |
---|
1055 | n/a | if (PyDecSignalDict_Check(value)) { |
---|
1056 | n/a | flags = SdFlags(value); |
---|
1057 | n/a | } |
---|
1058 | n/a | else { |
---|
1059 | n/a | flags = dict_as_flags(value); |
---|
1060 | n/a | if (flags & DEC_ERRORS) { |
---|
1061 | n/a | return -1; |
---|
1062 | n/a | } |
---|
1063 | n/a | } |
---|
1064 | n/a | |
---|
1065 | n/a | ctx = CTX(self); |
---|
1066 | n/a | if (!mpd_qsetstatus(ctx, flags)) { |
---|
1067 | n/a | INTERNAL_ERROR_INT("context_setstatus_dict"); |
---|
1068 | n/a | } |
---|
1069 | n/a | |
---|
1070 | n/a | return 0; |
---|
1071 | n/a | } |
---|
1072 | n/a | |
---|
1073 | n/a | static int |
---|
1074 | n/a | context_setclamp(PyObject *self, PyObject *value, void *closure UNUSED) |
---|
1075 | n/a | { |
---|
1076 | n/a | mpd_context_t *ctx; |
---|
1077 | n/a | mpd_ssize_t x; |
---|
1078 | n/a | |
---|
1079 | n/a | x = PyLong_AsSsize_t(value); |
---|
1080 | n/a | if (x == -1 && PyErr_Occurred()) { |
---|
1081 | n/a | return -1; |
---|
1082 | n/a | } |
---|
1083 | n/a | BOUNDS_CHECK(x, INT_MIN, INT_MAX); |
---|
1084 | n/a | |
---|
1085 | n/a | ctx = CTX(self); |
---|
1086 | n/a | if (!mpd_qsetclamp(ctx, (int)x)) { |
---|
1087 | n/a | return value_error_int("valid values for clamp are 0 or 1"); |
---|
1088 | n/a | } |
---|
1089 | n/a | |
---|
1090 | n/a | return 0; |
---|
1091 | n/a | } |
---|
1092 | n/a | |
---|
1093 | n/a | #ifdef EXTRA_FUNCTIONALITY |
---|
1094 | n/a | static int |
---|
1095 | n/a | context_setallcr(PyObject *self, PyObject *value, void *closure UNUSED) |
---|
1096 | n/a | { |
---|
1097 | n/a | mpd_context_t *ctx; |
---|
1098 | n/a | mpd_ssize_t x; |
---|
1099 | n/a | |
---|
1100 | n/a | x = PyLong_AsSsize_t(value); |
---|
1101 | n/a | if (x == -1 && PyErr_Occurred()) { |
---|
1102 | n/a | return -1; |
---|
1103 | n/a | } |
---|
1104 | n/a | BOUNDS_CHECK(x, INT_MIN, INT_MAX); |
---|
1105 | n/a | |
---|
1106 | n/a | ctx = CTX(self); |
---|
1107 | n/a | if (!mpd_qsetcr(ctx, (int)x)) { |
---|
1108 | n/a | return value_error_int("valid values for _allcr are 0 or 1"); |
---|
1109 | n/a | } |
---|
1110 | n/a | |
---|
1111 | n/a | return 0; |
---|
1112 | n/a | } |
---|
1113 | n/a | #endif |
---|
1114 | n/a | |
---|
1115 | n/a | static PyObject * |
---|
1116 | n/a | context_getattr(PyObject *self, PyObject *name) |
---|
1117 | n/a | { |
---|
1118 | n/a | PyObject *retval; |
---|
1119 | n/a | |
---|
1120 | n/a | if (PyUnicode_Check(name)) { |
---|
1121 | n/a | if (PyUnicode_CompareWithASCIIString(name, "traps") == 0) { |
---|
1122 | n/a | retval = ((PyDecContextObject *)self)->traps; |
---|
1123 | n/a | Py_INCREF(retval); |
---|
1124 | n/a | return retval; |
---|
1125 | n/a | } |
---|
1126 | n/a | if (PyUnicode_CompareWithASCIIString(name, "flags") == 0) { |
---|
1127 | n/a | retval = ((PyDecContextObject *)self)->flags; |
---|
1128 | n/a | Py_INCREF(retval); |
---|
1129 | n/a | return retval; |
---|
1130 | n/a | } |
---|
1131 | n/a | } |
---|
1132 | n/a | |
---|
1133 | n/a | return PyObject_GenericGetAttr(self, name); |
---|
1134 | n/a | } |
---|
1135 | n/a | |
---|
1136 | n/a | static int |
---|
1137 | n/a | context_setattr(PyObject *self, PyObject *name, PyObject *value) |
---|
1138 | n/a | { |
---|
1139 | n/a | if (value == NULL) { |
---|
1140 | n/a | PyErr_SetString(PyExc_AttributeError, |
---|
1141 | n/a | "context attributes cannot be deleted"); |
---|
1142 | n/a | return -1; |
---|
1143 | n/a | } |
---|
1144 | n/a | |
---|
1145 | n/a | if (PyUnicode_Check(name)) { |
---|
1146 | n/a | if (PyUnicode_CompareWithASCIIString(name, "traps") == 0) { |
---|
1147 | n/a | return context_settraps_dict(self, value); |
---|
1148 | n/a | } |
---|
1149 | n/a | if (PyUnicode_CompareWithASCIIString(name, "flags") == 0) { |
---|
1150 | n/a | return context_setstatus_dict(self, value); |
---|
1151 | n/a | } |
---|
1152 | n/a | } |
---|
1153 | n/a | |
---|
1154 | n/a | return PyObject_GenericSetAttr(self, name, value); |
---|
1155 | n/a | } |
---|
1156 | n/a | |
---|
1157 | n/a | static PyObject * |
---|
1158 | n/a | context_clear_traps(PyObject *self, PyObject *dummy UNUSED) |
---|
1159 | n/a | { |
---|
1160 | n/a | CTX(self)->traps = 0; |
---|
1161 | n/a | Py_RETURN_NONE; |
---|
1162 | n/a | } |
---|
1163 | n/a | |
---|
1164 | n/a | static PyObject * |
---|
1165 | n/a | context_clear_flags(PyObject *self, PyObject *dummy UNUSED) |
---|
1166 | n/a | { |
---|
1167 | n/a | CTX(self)->status = 0; |
---|
1168 | n/a | Py_RETURN_NONE; |
---|
1169 | n/a | } |
---|
1170 | n/a | |
---|
1171 | n/a | #define DEC_DFLT_EMAX 999999 |
---|
1172 | n/a | #define DEC_DFLT_EMIN -999999 |
---|
1173 | n/a | |
---|
1174 | n/a | static mpd_context_t dflt_ctx = { |
---|
1175 | n/a | 28, DEC_DFLT_EMAX, DEC_DFLT_EMIN, |
---|
1176 | n/a | MPD_IEEE_Invalid_operation|MPD_Division_by_zero|MPD_Overflow, |
---|
1177 | n/a | 0, 0, MPD_ROUND_HALF_EVEN, 0, 1 |
---|
1178 | n/a | }; |
---|
1179 | n/a | |
---|
1180 | n/a | static PyObject * |
---|
1181 | n/a | context_new(PyTypeObject *type, PyObject *args UNUSED, PyObject *kwds UNUSED) |
---|
1182 | n/a | { |
---|
1183 | n/a | PyDecContextObject *self = NULL; |
---|
1184 | n/a | mpd_context_t *ctx; |
---|
1185 | n/a | |
---|
1186 | n/a | if (type == &PyDecContext_Type) { |
---|
1187 | n/a | self = PyObject_New(PyDecContextObject, &PyDecContext_Type); |
---|
1188 | n/a | } |
---|
1189 | n/a | else { |
---|
1190 | n/a | self = (PyDecContextObject *)type->tp_alloc(type, 0); |
---|
1191 | n/a | } |
---|
1192 | n/a | |
---|
1193 | n/a | if (self == NULL) { |
---|
1194 | n/a | return NULL; |
---|
1195 | n/a | } |
---|
1196 | n/a | |
---|
1197 | n/a | self->traps = PyObject_CallObject((PyObject *)PyDecSignalDict_Type, NULL); |
---|
1198 | n/a | if (self->traps == NULL) { |
---|
1199 | n/a | self->flags = NULL; |
---|
1200 | n/a | Py_DECREF(self); |
---|
1201 | n/a | return NULL; |
---|
1202 | n/a | } |
---|
1203 | n/a | self->flags = PyObject_CallObject((PyObject *)PyDecSignalDict_Type, NULL); |
---|
1204 | n/a | if (self->flags == NULL) { |
---|
1205 | n/a | Py_DECREF(self); |
---|
1206 | n/a | return NULL; |
---|
1207 | n/a | } |
---|
1208 | n/a | |
---|
1209 | n/a | ctx = CTX(self); |
---|
1210 | n/a | |
---|
1211 | n/a | if (default_context_template) { |
---|
1212 | n/a | *ctx = *CTX(default_context_template); |
---|
1213 | n/a | } |
---|
1214 | n/a | else { |
---|
1215 | n/a | *ctx = dflt_ctx; |
---|
1216 | n/a | } |
---|
1217 | n/a | |
---|
1218 | n/a | SdFlagAddr(self->traps) = &ctx->traps; |
---|
1219 | n/a | SdFlagAddr(self->flags) = &ctx->status; |
---|
1220 | n/a | |
---|
1221 | n/a | CtxCaps(self) = 1; |
---|
1222 | n/a | #ifndef WITHOUT_THREADS |
---|
1223 | n/a | self->tstate = NULL; |
---|
1224 | n/a | #endif |
---|
1225 | n/a | |
---|
1226 | n/a | return (PyObject *)self; |
---|
1227 | n/a | } |
---|
1228 | n/a | |
---|
1229 | n/a | static void |
---|
1230 | n/a | context_dealloc(PyDecContextObject *self) |
---|
1231 | n/a | { |
---|
1232 | n/a | #ifndef WITHOUT_THREADS |
---|
1233 | n/a | if (self == cached_context) { |
---|
1234 | n/a | cached_context = NULL; |
---|
1235 | n/a | } |
---|
1236 | n/a | #endif |
---|
1237 | n/a | Py_XDECREF(self->traps); |
---|
1238 | n/a | Py_XDECREF(self->flags); |
---|
1239 | n/a | Py_TYPE(self)->tp_free(self); |
---|
1240 | n/a | } |
---|
1241 | n/a | |
---|
1242 | n/a | static int |
---|
1243 | n/a | context_init(PyObject *self, PyObject *args, PyObject *kwds) |
---|
1244 | n/a | { |
---|
1245 | n/a | static char *kwlist[] = { |
---|
1246 | n/a | "prec", "rounding", "Emin", "Emax", "capitals", "clamp", |
---|
1247 | n/a | "flags", "traps", NULL |
---|
1248 | n/a | }; |
---|
1249 | n/a | PyObject *prec = Py_None; |
---|
1250 | n/a | PyObject *rounding = Py_None; |
---|
1251 | n/a | PyObject *emin = Py_None; |
---|
1252 | n/a | PyObject *emax = Py_None; |
---|
1253 | n/a | PyObject *capitals = Py_None; |
---|
1254 | n/a | PyObject *clamp = Py_None; |
---|
1255 | n/a | PyObject *status = Py_None; |
---|
1256 | n/a | PyObject *traps = Py_None; |
---|
1257 | n/a | int ret; |
---|
1258 | n/a | |
---|
1259 | n/a | assert(PyTuple_Check(args)); |
---|
1260 | n/a | |
---|
1261 | n/a | if (!PyArg_ParseTupleAndKeywords( |
---|
1262 | n/a | args, kwds, |
---|
1263 | n/a | "|OOOOOOOO", kwlist, |
---|
1264 | n/a | &prec, &rounding, &emin, &emax, &capitals, &clamp, &status, &traps |
---|
1265 | n/a | )) { |
---|
1266 | n/a | return -1; |
---|
1267 | n/a | } |
---|
1268 | n/a | |
---|
1269 | n/a | if (prec != Py_None && context_setprec(self, prec, NULL) < 0) { |
---|
1270 | n/a | return -1; |
---|
1271 | n/a | } |
---|
1272 | n/a | if (rounding != Py_None && context_setround(self, rounding, NULL) < 0) { |
---|
1273 | n/a | return -1; |
---|
1274 | n/a | } |
---|
1275 | n/a | if (emin != Py_None && context_setemin(self, emin, NULL) < 0) { |
---|
1276 | n/a | return -1; |
---|
1277 | n/a | } |
---|
1278 | n/a | if (emax != Py_None && context_setemax(self, emax, NULL) < 0) { |
---|
1279 | n/a | return -1; |
---|
1280 | n/a | } |
---|
1281 | n/a | if (capitals != Py_None && context_setcapitals(self, capitals, NULL) < 0) { |
---|
1282 | n/a | return -1; |
---|
1283 | n/a | } |
---|
1284 | n/a | if (clamp != Py_None && context_setclamp(self, clamp, NULL) < 0) { |
---|
1285 | n/a | return -1; |
---|
1286 | n/a | } |
---|
1287 | n/a | |
---|
1288 | n/a | if (traps != Py_None) { |
---|
1289 | n/a | if (PyList_Check(traps)) { |
---|
1290 | n/a | ret = context_settraps_list(self, traps); |
---|
1291 | n/a | } |
---|
1292 | n/a | #ifdef EXTRA_FUNCTIONALITY |
---|
1293 | n/a | else if (PyLong_Check(traps)) { |
---|
1294 | n/a | ret = context_settraps(self, traps, NULL); |
---|
1295 | n/a | } |
---|
1296 | n/a | #endif |
---|
1297 | n/a | else { |
---|
1298 | n/a | ret = context_settraps_dict(self, traps); |
---|
1299 | n/a | } |
---|
1300 | n/a | if (ret < 0) { |
---|
1301 | n/a | return ret; |
---|
1302 | n/a | } |
---|
1303 | n/a | } |
---|
1304 | n/a | if (status != Py_None) { |
---|
1305 | n/a | if (PyList_Check(status)) { |
---|
1306 | n/a | ret = context_setstatus_list(self, status); |
---|
1307 | n/a | } |
---|
1308 | n/a | #ifdef EXTRA_FUNCTIONALITY |
---|
1309 | n/a | else if (PyLong_Check(status)) { |
---|
1310 | n/a | ret = context_setstatus(self, status, NULL); |
---|
1311 | n/a | } |
---|
1312 | n/a | #endif |
---|
1313 | n/a | else { |
---|
1314 | n/a | ret = context_setstatus_dict(self, status); |
---|
1315 | n/a | } |
---|
1316 | n/a | if (ret < 0) { |
---|
1317 | n/a | return ret; |
---|
1318 | n/a | } |
---|
1319 | n/a | } |
---|
1320 | n/a | |
---|
1321 | n/a | return 0; |
---|
1322 | n/a | } |
---|
1323 | n/a | |
---|
1324 | n/a | static PyObject * |
---|
1325 | n/a | context_repr(PyDecContextObject *self) |
---|
1326 | n/a | { |
---|
1327 | n/a | mpd_context_t *ctx; |
---|
1328 | n/a | char flags[MPD_MAX_SIGNAL_LIST]; |
---|
1329 | n/a | char traps[MPD_MAX_SIGNAL_LIST]; |
---|
1330 | n/a | int n, mem; |
---|
1331 | n/a | |
---|
1332 | n/a | assert(PyDecContext_Check(self)); |
---|
1333 | n/a | ctx = CTX(self); |
---|
1334 | n/a | |
---|
1335 | n/a | mem = MPD_MAX_SIGNAL_LIST; |
---|
1336 | n/a | n = mpd_lsnprint_signals(flags, mem, ctx->status, dec_signal_string); |
---|
1337 | n/a | if (n < 0 || n >= mem) { |
---|
1338 | n/a | INTERNAL_ERROR_PTR("context_repr"); |
---|
1339 | n/a | } |
---|
1340 | n/a | |
---|
1341 | n/a | n = mpd_lsnprint_signals(traps, mem, ctx->traps, dec_signal_string); |
---|
1342 | n/a | if (n < 0 || n >= mem) { |
---|
1343 | n/a | INTERNAL_ERROR_PTR("context_repr"); |
---|
1344 | n/a | } |
---|
1345 | n/a | |
---|
1346 | n/a | return PyUnicode_FromFormat( |
---|
1347 | n/a | "Context(prec=%zd, rounding=%s, Emin=%zd, Emax=%zd, " |
---|
1348 | n/a | "capitals=%d, clamp=%d, flags=%s, traps=%s)", |
---|
1349 | n/a | ctx->prec, mpd_round_string[ctx->round], ctx->emin, ctx->emax, |
---|
1350 | n/a | self->capitals, ctx->clamp, flags, traps); |
---|
1351 | n/a | } |
---|
1352 | n/a | |
---|
1353 | n/a | static void |
---|
1354 | n/a | init_basic_context(PyObject *v) |
---|
1355 | n/a | { |
---|
1356 | n/a | mpd_context_t ctx = dflt_ctx; |
---|
1357 | n/a | |
---|
1358 | n/a | ctx.prec = 9; |
---|
1359 | n/a | ctx.traps |= (MPD_Underflow|MPD_Clamped); |
---|
1360 | n/a | ctx.round = MPD_ROUND_HALF_UP; |
---|
1361 | n/a | |
---|
1362 | n/a | *CTX(v) = ctx; |
---|
1363 | n/a | CtxCaps(v) = 1; |
---|
1364 | n/a | } |
---|
1365 | n/a | |
---|
1366 | n/a | static void |
---|
1367 | n/a | init_extended_context(PyObject *v) |
---|
1368 | n/a | { |
---|
1369 | n/a | mpd_context_t ctx = dflt_ctx; |
---|
1370 | n/a | |
---|
1371 | n/a | ctx.prec = 9; |
---|
1372 | n/a | ctx.traps = 0; |
---|
1373 | n/a | |
---|
1374 | n/a | *CTX(v) = ctx; |
---|
1375 | n/a | CtxCaps(v) = 1; |
---|
1376 | n/a | } |
---|
1377 | n/a | |
---|
1378 | n/a | #ifdef EXTRA_FUNCTIONALITY |
---|
1379 | n/a | /* Factory function for creating IEEE interchange format contexts */ |
---|
1380 | n/a | static PyObject * |
---|
1381 | n/a | ieee_context(PyObject *dummy UNUSED, PyObject *v) |
---|
1382 | n/a | { |
---|
1383 | n/a | PyObject *context; |
---|
1384 | n/a | mpd_ssize_t bits; |
---|
1385 | n/a | mpd_context_t ctx; |
---|
1386 | n/a | |
---|
1387 | n/a | bits = PyLong_AsSsize_t(v); |
---|
1388 | n/a | if (bits == -1 && PyErr_Occurred()) { |
---|
1389 | n/a | return NULL; |
---|
1390 | n/a | } |
---|
1391 | n/a | if (bits <= 0 || bits > INT_MAX) { |
---|
1392 | n/a | goto error; |
---|
1393 | n/a | } |
---|
1394 | n/a | if (mpd_ieee_context(&ctx, (int)bits) < 0) { |
---|
1395 | n/a | goto error; |
---|
1396 | n/a | } |
---|
1397 | n/a | |
---|
1398 | n/a | context = PyObject_CallObject((PyObject *)&PyDecContext_Type, NULL); |
---|
1399 | n/a | if (context == NULL) { |
---|
1400 | n/a | return NULL; |
---|
1401 | n/a | } |
---|
1402 | n/a | *CTX(context) = ctx; |
---|
1403 | n/a | |
---|
1404 | n/a | return context; |
---|
1405 | n/a | |
---|
1406 | n/a | error: |
---|
1407 | n/a | PyErr_Format(PyExc_ValueError, |
---|
1408 | n/a | "argument must be a multiple of 32, with a maximum of %d", |
---|
1409 | n/a | MPD_IEEE_CONTEXT_MAX_BITS); |
---|
1410 | n/a | |
---|
1411 | n/a | return NULL; |
---|
1412 | n/a | } |
---|
1413 | n/a | #endif |
---|
1414 | n/a | |
---|
1415 | n/a | static PyObject * |
---|
1416 | n/a | context_copy(PyObject *self, PyObject *args UNUSED) |
---|
1417 | n/a | { |
---|
1418 | n/a | PyObject *copy; |
---|
1419 | n/a | |
---|
1420 | n/a | copy = PyObject_CallObject((PyObject *)&PyDecContext_Type, NULL); |
---|
1421 | n/a | if (copy == NULL) { |
---|
1422 | n/a | return NULL; |
---|
1423 | n/a | } |
---|
1424 | n/a | |
---|
1425 | n/a | *CTX(copy) = *CTX(self); |
---|
1426 | n/a | CTX(copy)->newtrap = 0; |
---|
1427 | n/a | CtxCaps(copy) = CtxCaps(self); |
---|
1428 | n/a | |
---|
1429 | n/a | return copy; |
---|
1430 | n/a | } |
---|
1431 | n/a | |
---|
1432 | n/a | static PyObject * |
---|
1433 | n/a | context_reduce(PyObject *self, PyObject *args UNUSED) |
---|
1434 | n/a | { |
---|
1435 | n/a | PyObject *flags; |
---|
1436 | n/a | PyObject *traps; |
---|
1437 | n/a | PyObject *ret; |
---|
1438 | n/a | mpd_context_t *ctx; |
---|
1439 | n/a | |
---|
1440 | n/a | ctx = CTX(self); |
---|
1441 | n/a | |
---|
1442 | n/a | flags = signals_as_list(ctx->status); |
---|
1443 | n/a | if (flags == NULL) { |
---|
1444 | n/a | return NULL; |
---|
1445 | n/a | } |
---|
1446 | n/a | traps = signals_as_list(ctx->traps); |
---|
1447 | n/a | if (traps == NULL) { |
---|
1448 | n/a | Py_DECREF(flags); |
---|
1449 | n/a | return NULL; |
---|
1450 | n/a | } |
---|
1451 | n/a | |
---|
1452 | n/a | ret = Py_BuildValue( |
---|
1453 | n/a | "O(nsnniiOO)", |
---|
1454 | n/a | Py_TYPE(self), |
---|
1455 | n/a | ctx->prec, mpd_round_string[ctx->round], ctx->emin, ctx->emax, |
---|
1456 | n/a | CtxCaps(self), ctx->clamp, flags, traps |
---|
1457 | n/a | ); |
---|
1458 | n/a | |
---|
1459 | n/a | Py_DECREF(flags); |
---|
1460 | n/a | Py_DECREF(traps); |
---|
1461 | n/a | return ret; |
---|
1462 | n/a | } |
---|
1463 | n/a | |
---|
1464 | n/a | |
---|
1465 | n/a | static PyGetSetDef context_getsets [] = |
---|
1466 | n/a | { |
---|
1467 | n/a | { "prec", (getter)context_getprec, (setter)context_setprec, NULL, NULL}, |
---|
1468 | n/a | { "Emax", (getter)context_getemax, (setter)context_setemax, NULL, NULL}, |
---|
1469 | n/a | { "Emin", (getter)context_getemin, (setter)context_setemin, NULL, NULL}, |
---|
1470 | n/a | { "rounding", (getter)context_getround, (setter)context_setround, NULL, NULL}, |
---|
1471 | n/a | { "capitals", (getter)context_getcapitals, (setter)context_setcapitals, NULL, NULL}, |
---|
1472 | n/a | { "clamp", (getter)context_getclamp, (setter)context_setclamp, NULL, NULL}, |
---|
1473 | n/a | #ifdef EXTRA_FUNCTIONALITY |
---|
1474 | n/a | { "_allcr", (getter)context_getallcr, (setter)context_setallcr, NULL, NULL}, |
---|
1475 | n/a | { "_traps", (getter)context_gettraps, (setter)context_settraps, NULL, NULL}, |
---|
1476 | n/a | { "_flags", (getter)context_getstatus, (setter)context_setstatus, NULL, NULL}, |
---|
1477 | n/a | #endif |
---|
1478 | n/a | {NULL} |
---|
1479 | n/a | }; |
---|
1480 | n/a | |
---|
1481 | n/a | |
---|
1482 | n/a | #define CONTEXT_CHECK(obj) \ |
---|
1483 | n/a | if (!PyDecContext_Check(obj)) { \ |
---|
1484 | n/a | PyErr_SetString(PyExc_TypeError, \ |
---|
1485 | n/a | "argument must be a context"); \ |
---|
1486 | n/a | return NULL; \ |
---|
1487 | n/a | } |
---|
1488 | n/a | |
---|
1489 | n/a | #define CONTEXT_CHECK_VA(obj) \ |
---|
1490 | n/a | if (obj == Py_None) { \ |
---|
1491 | n/a | CURRENT_CONTEXT(obj); \ |
---|
1492 | n/a | } \ |
---|
1493 | n/a | else if (!PyDecContext_Check(obj)) { \ |
---|
1494 | n/a | PyErr_SetString(PyExc_TypeError, \ |
---|
1495 | n/a | "optional argument must be a context"); \ |
---|
1496 | n/a | return NULL; \ |
---|
1497 | n/a | } |
---|
1498 | n/a | |
---|
1499 | n/a | |
---|
1500 | n/a | /******************************************************************************/ |
---|
1501 | n/a | /* Global, thread local and temporary contexts */ |
---|
1502 | n/a | /******************************************************************************/ |
---|
1503 | n/a | |
---|
1504 | n/a | #ifdef WITHOUT_THREADS |
---|
1505 | n/a | /* Return borrowed reference to the current context. When compiled |
---|
1506 | n/a | * without threads, this is always the module context. */ |
---|
1507 | n/a | static int module_context_set = 0; |
---|
1508 | n/a | static PyObject * |
---|
1509 | n/a | current_context(void) |
---|
1510 | n/a | { |
---|
1511 | n/a | /* In decimal.py, the module context is automatically initialized |
---|
1512 | n/a | * from the DefaultContext when it is first accessed. This |
---|
1513 | n/a | * complicates the code and has a speed penalty of 1-2%. */ |
---|
1514 | n/a | if (module_context_set) { |
---|
1515 | n/a | return module_context; |
---|
1516 | n/a | } |
---|
1517 | n/a | |
---|
1518 | n/a | *CTX(module_context) = *CTX(default_context_template); |
---|
1519 | n/a | CTX(module_context)->status = 0; |
---|
1520 | n/a | CTX(module_context)->newtrap = 0; |
---|
1521 | n/a | CtxCaps(module_context) = CtxCaps(default_context_template); |
---|
1522 | n/a | |
---|
1523 | n/a | module_context_set = 1; |
---|
1524 | n/a | return module_context; |
---|
1525 | n/a | } |
---|
1526 | n/a | |
---|
1527 | n/a | /* ctxobj := borrowed reference to the current context */ |
---|
1528 | n/a | #define CURRENT_CONTEXT(ctxobj) \ |
---|
1529 | n/a | ctxobj = current_context() |
---|
1530 | n/a | |
---|
1531 | n/a | /* ctx := pointer to the mpd_context_t struct of the current context */ |
---|
1532 | n/a | #define CURRENT_CONTEXT_ADDR(ctx) \ |
---|
1533 | n/a | ctx = CTX(current_context()) |
---|
1534 | n/a | |
---|
1535 | n/a | /* Return a new reference to the current context */ |
---|
1536 | n/a | static PyObject * |
---|
1537 | n/a | PyDec_GetCurrentContext(PyObject *self UNUSED, PyObject *args UNUSED) |
---|
1538 | n/a | { |
---|
1539 | n/a | PyObject *context; |
---|
1540 | n/a | |
---|
1541 | n/a | CURRENT_CONTEXT(context); |
---|
1542 | n/a | |
---|
1543 | n/a | Py_INCREF(context); |
---|
1544 | n/a | return context; |
---|
1545 | n/a | } |
---|
1546 | n/a | |
---|
1547 | n/a | /* Set the module context to a new context, decrement old reference */ |
---|
1548 | n/a | static PyObject * |
---|
1549 | n/a | PyDec_SetCurrentContext(PyObject *self UNUSED, PyObject *v) |
---|
1550 | n/a | { |
---|
1551 | n/a | CONTEXT_CHECK(v); |
---|
1552 | n/a | |
---|
1553 | n/a | /* If the new context is one of the templates, make a copy. |
---|
1554 | n/a | * This is the current behavior of decimal.py. */ |
---|
1555 | n/a | if (v == default_context_template || |
---|
1556 | n/a | v == basic_context_template || |
---|
1557 | n/a | v == extended_context_template) { |
---|
1558 | n/a | v = context_copy(v, NULL); |
---|
1559 | n/a | if (v == NULL) { |
---|
1560 | n/a | return NULL; |
---|
1561 | n/a | } |
---|
1562 | n/a | CTX(v)->status = 0; |
---|
1563 | n/a | } |
---|
1564 | n/a | else { |
---|
1565 | n/a | Py_INCREF(v); |
---|
1566 | n/a | } |
---|
1567 | n/a | |
---|
1568 | n/a | Py_XDECREF(module_context); |
---|
1569 | n/a | module_context = v; |
---|
1570 | n/a | module_context_set = 1; |
---|
1571 | n/a | Py_RETURN_NONE; |
---|
1572 | n/a | } |
---|
1573 | n/a | #else |
---|
1574 | n/a | /* |
---|
1575 | n/a | * Thread local storage currently has a speed penalty of about 4%. |
---|
1576 | n/a | * All functions that map Python's arithmetic operators to mpdecimal |
---|
1577 | n/a | * functions have to look up the current context for each and every |
---|
1578 | n/a | * operation. |
---|
1579 | n/a | */ |
---|
1580 | n/a | |
---|
1581 | n/a | /* Get the context from the thread state dictionary. */ |
---|
1582 | n/a | static PyObject * |
---|
1583 | n/a | current_context_from_dict(void) |
---|
1584 | n/a | { |
---|
1585 | n/a | PyObject *dict; |
---|
1586 | n/a | PyObject *tl_context; |
---|
1587 | n/a | PyThreadState *tstate; |
---|
1588 | n/a | |
---|
1589 | n/a | dict = PyThreadState_GetDict(); |
---|
1590 | n/a | if (dict == NULL) { |
---|
1591 | n/a | PyErr_SetString(PyExc_RuntimeError, |
---|
1592 | n/a | "cannot get thread state"); |
---|
1593 | n/a | return NULL; |
---|
1594 | n/a | } |
---|
1595 | n/a | |
---|
1596 | n/a | tl_context = PyDict_GetItemWithError(dict, tls_context_key); |
---|
1597 | n/a | if (tl_context != NULL) { |
---|
1598 | n/a | /* We already have a thread local context. */ |
---|
1599 | n/a | CONTEXT_CHECK(tl_context); |
---|
1600 | n/a | } |
---|
1601 | n/a | else { |
---|
1602 | n/a | if (PyErr_Occurred()) { |
---|
1603 | n/a | return NULL; |
---|
1604 | n/a | } |
---|
1605 | n/a | |
---|
1606 | n/a | /* Set up a new thread local context. */ |
---|
1607 | n/a | tl_context = context_copy(default_context_template, NULL); |
---|
1608 | n/a | if (tl_context == NULL) { |
---|
1609 | n/a | return NULL; |
---|
1610 | n/a | } |
---|
1611 | n/a | CTX(tl_context)->status = 0; |
---|
1612 | n/a | |
---|
1613 | n/a | if (PyDict_SetItem(dict, tls_context_key, tl_context) < 0) { |
---|
1614 | n/a | Py_DECREF(tl_context); |
---|
1615 | n/a | return NULL; |
---|
1616 | n/a | } |
---|
1617 | n/a | Py_DECREF(tl_context); |
---|
1618 | n/a | } |
---|
1619 | n/a | |
---|
1620 | n/a | /* Cache the context of the current thread, assuming that it |
---|
1621 | n/a | * will be accessed several times before a thread switch. */ |
---|
1622 | n/a | tstate = PyThreadState_GET(); |
---|
1623 | n/a | if (tstate) { |
---|
1624 | n/a | cached_context = (PyDecContextObject *)tl_context; |
---|
1625 | n/a | cached_context->tstate = tstate; |
---|
1626 | n/a | } |
---|
1627 | n/a | |
---|
1628 | n/a | /* Borrowed reference with refcount==1 */ |
---|
1629 | n/a | return tl_context; |
---|
1630 | n/a | } |
---|
1631 | n/a | |
---|
1632 | n/a | /* Return borrowed reference to thread local context. */ |
---|
1633 | n/a | static PyObject * |
---|
1634 | n/a | current_context(void) |
---|
1635 | n/a | { |
---|
1636 | n/a | PyThreadState *tstate; |
---|
1637 | n/a | |
---|
1638 | n/a | tstate = PyThreadState_GET(); |
---|
1639 | n/a | if (cached_context && cached_context->tstate == tstate) { |
---|
1640 | n/a | return (PyObject *)cached_context; |
---|
1641 | n/a | } |
---|
1642 | n/a | |
---|
1643 | n/a | return current_context_from_dict(); |
---|
1644 | n/a | } |
---|
1645 | n/a | |
---|
1646 | n/a | /* ctxobj := borrowed reference to the current context */ |
---|
1647 | n/a | #define CURRENT_CONTEXT(ctxobj) \ |
---|
1648 | n/a | ctxobj = current_context(); \ |
---|
1649 | n/a | if (ctxobj == NULL) { \ |
---|
1650 | n/a | return NULL; \ |
---|
1651 | n/a | } |
---|
1652 | n/a | |
---|
1653 | n/a | /* ctx := pointer to the mpd_context_t struct of the current context */ |
---|
1654 | n/a | #define CURRENT_CONTEXT_ADDR(ctx) { \ |
---|
1655 | n/a | PyObject *_c_t_x_o_b_j = current_context(); \ |
---|
1656 | n/a | if (_c_t_x_o_b_j == NULL) { \ |
---|
1657 | n/a | return NULL; \ |
---|
1658 | n/a | } \ |
---|
1659 | n/a | ctx = CTX(_c_t_x_o_b_j); \ |
---|
1660 | n/a | } |
---|
1661 | n/a | |
---|
1662 | n/a | /* Return a new reference to the current context */ |
---|
1663 | n/a | static PyObject * |
---|
1664 | n/a | PyDec_GetCurrentContext(PyObject *self UNUSED, PyObject *args UNUSED) |
---|
1665 | n/a | { |
---|
1666 | n/a | PyObject *context; |
---|
1667 | n/a | |
---|
1668 | n/a | context = current_context(); |
---|
1669 | n/a | if (context == NULL) { |
---|
1670 | n/a | return NULL; |
---|
1671 | n/a | } |
---|
1672 | n/a | |
---|
1673 | n/a | Py_INCREF(context); |
---|
1674 | n/a | return context; |
---|
1675 | n/a | } |
---|
1676 | n/a | |
---|
1677 | n/a | /* Set the thread local context to a new context, decrement old reference */ |
---|
1678 | n/a | static PyObject * |
---|
1679 | n/a | PyDec_SetCurrentContext(PyObject *self UNUSED, PyObject *v) |
---|
1680 | n/a | { |
---|
1681 | n/a | PyObject *dict; |
---|
1682 | n/a | |
---|
1683 | n/a | CONTEXT_CHECK(v); |
---|
1684 | n/a | |
---|
1685 | n/a | dict = PyThreadState_GetDict(); |
---|
1686 | n/a | if (dict == NULL) { |
---|
1687 | n/a | PyErr_SetString(PyExc_RuntimeError, |
---|
1688 | n/a | "cannot get thread state"); |
---|
1689 | n/a | return NULL; |
---|
1690 | n/a | } |
---|
1691 | n/a | |
---|
1692 | n/a | /* If the new context is one of the templates, make a copy. |
---|
1693 | n/a | * This is the current behavior of decimal.py. */ |
---|
1694 | n/a | if (v == default_context_template || |
---|
1695 | n/a | v == basic_context_template || |
---|
1696 | n/a | v == extended_context_template) { |
---|
1697 | n/a | v = context_copy(v, NULL); |
---|
1698 | n/a | if (v == NULL) { |
---|
1699 | n/a | return NULL; |
---|
1700 | n/a | } |
---|
1701 | n/a | CTX(v)->status = 0; |
---|
1702 | n/a | } |
---|
1703 | n/a | else { |
---|
1704 | n/a | Py_INCREF(v); |
---|
1705 | n/a | } |
---|
1706 | n/a | |
---|
1707 | n/a | cached_context = NULL; |
---|
1708 | n/a | if (PyDict_SetItem(dict, tls_context_key, v) < 0) { |
---|
1709 | n/a | Py_DECREF(v); |
---|
1710 | n/a | return NULL; |
---|
1711 | n/a | } |
---|
1712 | n/a | |
---|
1713 | n/a | Py_DECREF(v); |
---|
1714 | n/a | Py_RETURN_NONE; |
---|
1715 | n/a | } |
---|
1716 | n/a | #endif |
---|
1717 | n/a | |
---|
1718 | n/a | /* Context manager object for the 'with' statement. The manager |
---|
1719 | n/a | * owns one reference to the global (outer) context and one |
---|
1720 | n/a | * to the local (inner) context. */ |
---|
1721 | n/a | static PyObject * |
---|
1722 | n/a | ctxmanager_new(PyTypeObject *type UNUSED, PyObject *args, PyObject *kwds) |
---|
1723 | n/a | { |
---|
1724 | n/a | static char *kwlist[] = {"ctx", NULL}; |
---|
1725 | n/a | PyDecContextManagerObject *self; |
---|
1726 | n/a | PyObject *local = Py_None; |
---|
1727 | n/a | PyObject *global; |
---|
1728 | n/a | |
---|
1729 | n/a | CURRENT_CONTEXT(global); |
---|
1730 | n/a | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O", kwlist, &local)) { |
---|
1731 | n/a | return NULL; |
---|
1732 | n/a | } |
---|
1733 | n/a | if (local == Py_None) { |
---|
1734 | n/a | local = global; |
---|
1735 | n/a | } |
---|
1736 | n/a | else if (!PyDecContext_Check(local)) { |
---|
1737 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
1738 | n/a | "optional argument must be a context"); |
---|
1739 | n/a | return NULL; |
---|
1740 | n/a | } |
---|
1741 | n/a | |
---|
1742 | n/a | self = PyObject_New(PyDecContextManagerObject, |
---|
1743 | n/a | &PyDecContextManager_Type); |
---|
1744 | n/a | if (self == NULL) { |
---|
1745 | n/a | return NULL; |
---|
1746 | n/a | } |
---|
1747 | n/a | |
---|
1748 | n/a | self->local = context_copy(local, NULL); |
---|
1749 | n/a | if (self->local == NULL) { |
---|
1750 | n/a | self->global = NULL; |
---|
1751 | n/a | Py_DECREF(self); |
---|
1752 | n/a | return NULL; |
---|
1753 | n/a | } |
---|
1754 | n/a | self->global = global; |
---|
1755 | n/a | Py_INCREF(self->global); |
---|
1756 | n/a | |
---|
1757 | n/a | return (PyObject *)self; |
---|
1758 | n/a | } |
---|
1759 | n/a | |
---|
1760 | n/a | static void |
---|
1761 | n/a | ctxmanager_dealloc(PyDecContextManagerObject *self) |
---|
1762 | n/a | { |
---|
1763 | n/a | Py_XDECREF(self->local); |
---|
1764 | n/a | Py_XDECREF(self->global); |
---|
1765 | n/a | PyObject_Del(self); |
---|
1766 | n/a | } |
---|
1767 | n/a | |
---|
1768 | n/a | static PyObject * |
---|
1769 | n/a | ctxmanager_set_local(PyDecContextManagerObject *self, PyObject *args UNUSED) |
---|
1770 | n/a | { |
---|
1771 | n/a | PyObject *ret; |
---|
1772 | n/a | |
---|
1773 | n/a | ret = PyDec_SetCurrentContext(NULL, self->local); |
---|
1774 | n/a | if (ret == NULL) { |
---|
1775 | n/a | return NULL; |
---|
1776 | n/a | } |
---|
1777 | n/a | Py_DECREF(ret); |
---|
1778 | n/a | |
---|
1779 | n/a | Py_INCREF(self->local); |
---|
1780 | n/a | return self->local; |
---|
1781 | n/a | } |
---|
1782 | n/a | |
---|
1783 | n/a | static PyObject * |
---|
1784 | n/a | ctxmanager_restore_global(PyDecContextManagerObject *self, |
---|
1785 | n/a | PyObject *args UNUSED) |
---|
1786 | n/a | { |
---|
1787 | n/a | PyObject *ret; |
---|
1788 | n/a | |
---|
1789 | n/a | ret = PyDec_SetCurrentContext(NULL, self->global); |
---|
1790 | n/a | if (ret == NULL) { |
---|
1791 | n/a | return NULL; |
---|
1792 | n/a | } |
---|
1793 | n/a | Py_DECREF(ret); |
---|
1794 | n/a | |
---|
1795 | n/a | Py_RETURN_NONE; |
---|
1796 | n/a | } |
---|
1797 | n/a | |
---|
1798 | n/a | |
---|
1799 | n/a | static PyMethodDef ctxmanager_methods[] = { |
---|
1800 | n/a | {"__enter__", (PyCFunction)ctxmanager_set_local, METH_NOARGS, NULL}, |
---|
1801 | n/a | {"__exit__", (PyCFunction)ctxmanager_restore_global, METH_VARARGS, NULL}, |
---|
1802 | n/a | {NULL, NULL} |
---|
1803 | n/a | }; |
---|
1804 | n/a | |
---|
1805 | n/a | static PyTypeObject PyDecContextManager_Type = |
---|
1806 | n/a | { |
---|
1807 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
---|
1808 | n/a | "decimal.ContextManager", /* tp_name */ |
---|
1809 | n/a | sizeof(PyDecContextManagerObject), /* tp_basicsize */ |
---|
1810 | n/a | 0, /* tp_itemsize */ |
---|
1811 | n/a | (destructor) ctxmanager_dealloc, /* tp_dealloc */ |
---|
1812 | n/a | 0, /* tp_print */ |
---|
1813 | n/a | (getattrfunc) 0, /* tp_getattr */ |
---|
1814 | n/a | (setattrfunc) 0, /* tp_setattr */ |
---|
1815 | n/a | 0, /* tp_reserved */ |
---|
1816 | n/a | (reprfunc) 0, /* tp_repr */ |
---|
1817 | n/a | 0, /* tp_as_number */ |
---|
1818 | n/a | 0, /* tp_as_sequence */ |
---|
1819 | n/a | 0, /* tp_as_mapping */ |
---|
1820 | n/a | 0, /* tp_hash */ |
---|
1821 | n/a | 0, /* tp_call */ |
---|
1822 | n/a | 0, /* tp_str */ |
---|
1823 | n/a | (getattrofunc) PyObject_GenericGetAttr, /* tp_getattro */ |
---|
1824 | n/a | (setattrofunc) 0, /* tp_setattro */ |
---|
1825 | n/a | (PyBufferProcs *) 0, /* tp_as_buffer */ |
---|
1826 | n/a | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
---|
1827 | n/a | 0, /* tp_doc */ |
---|
1828 | n/a | 0, /* tp_traverse */ |
---|
1829 | n/a | 0, /* tp_clear */ |
---|
1830 | n/a | 0, /* tp_richcompare */ |
---|
1831 | n/a | 0, /* tp_weaklistoffset */ |
---|
1832 | n/a | 0, /* tp_iter */ |
---|
1833 | n/a | 0, /* tp_iternext */ |
---|
1834 | n/a | ctxmanager_methods, /* tp_methods */ |
---|
1835 | n/a | }; |
---|
1836 | n/a | |
---|
1837 | n/a | |
---|
1838 | n/a | /******************************************************************************/ |
---|
1839 | n/a | /* New Decimal Object */ |
---|
1840 | n/a | /******************************************************************************/ |
---|
1841 | n/a | |
---|
1842 | n/a | static PyObject * |
---|
1843 | n/a | PyDecType_New(PyTypeObject *type) |
---|
1844 | n/a | { |
---|
1845 | n/a | PyDecObject *dec; |
---|
1846 | n/a | |
---|
1847 | n/a | if (type == &PyDec_Type) { |
---|
1848 | n/a | dec = PyObject_New(PyDecObject, &PyDec_Type); |
---|
1849 | n/a | } |
---|
1850 | n/a | else { |
---|
1851 | n/a | dec = (PyDecObject *)type->tp_alloc(type, 0); |
---|
1852 | n/a | } |
---|
1853 | n/a | if (dec == NULL) { |
---|
1854 | n/a | return NULL; |
---|
1855 | n/a | } |
---|
1856 | n/a | |
---|
1857 | n/a | dec->hash = -1; |
---|
1858 | n/a | |
---|
1859 | n/a | MPD(dec)->flags = MPD_STATIC|MPD_STATIC_DATA; |
---|
1860 | n/a | MPD(dec)->exp = 0; |
---|
1861 | n/a | MPD(dec)->digits = 0; |
---|
1862 | n/a | MPD(dec)->len = 0; |
---|
1863 | n/a | MPD(dec)->alloc = _Py_DEC_MINALLOC; |
---|
1864 | n/a | MPD(dec)->data = dec->data; |
---|
1865 | n/a | |
---|
1866 | n/a | return (PyObject *)dec; |
---|
1867 | n/a | } |
---|
1868 | n/a | #define dec_alloc() PyDecType_New(&PyDec_Type) |
---|
1869 | n/a | |
---|
1870 | n/a | static void |
---|
1871 | n/a | dec_dealloc(PyObject *dec) |
---|
1872 | n/a | { |
---|
1873 | n/a | mpd_del(MPD(dec)); |
---|
1874 | n/a | Py_TYPE(dec)->tp_free(dec); |
---|
1875 | n/a | } |
---|
1876 | n/a | |
---|
1877 | n/a | |
---|
1878 | n/a | /******************************************************************************/ |
---|
1879 | n/a | /* Conversions to Decimal */ |
---|
1880 | n/a | /******************************************************************************/ |
---|
1881 | n/a | |
---|
1882 | n/a | Py_LOCAL_INLINE(int) |
---|
1883 | n/a | is_space(enum PyUnicode_Kind kind, void *data, Py_ssize_t pos) |
---|
1884 | n/a | { |
---|
1885 | n/a | Py_UCS4 ch = PyUnicode_READ(kind, data, pos); |
---|
1886 | n/a | return Py_UNICODE_ISSPACE(ch); |
---|
1887 | n/a | } |
---|
1888 | n/a | |
---|
1889 | n/a | /* Return the ASCII representation of a numeric Unicode string. The numeric |
---|
1890 | n/a | string may contain ascii characters in the range [1, 127], any Unicode |
---|
1891 | n/a | space and any unicode digit. If strip_ws is true, leading and trailing |
---|
1892 | n/a | whitespace is stripped. If ignore_underscores is true, underscores are |
---|
1893 | n/a | ignored. |
---|
1894 | n/a | |
---|
1895 | n/a | Return NULL if malloc fails and an empty string if invalid characters |
---|
1896 | n/a | are found. */ |
---|
1897 | n/a | static char * |
---|
1898 | n/a | numeric_as_ascii(const PyObject *u, int strip_ws, int ignore_underscores) |
---|
1899 | n/a | { |
---|
1900 | n/a | enum PyUnicode_Kind kind; |
---|
1901 | n/a | void *data; |
---|
1902 | n/a | Py_UCS4 ch; |
---|
1903 | n/a | char *res, *cp; |
---|
1904 | n/a | Py_ssize_t j, len; |
---|
1905 | n/a | int d; |
---|
1906 | n/a | |
---|
1907 | n/a | if (PyUnicode_READY(u) == -1) { |
---|
1908 | n/a | return NULL; |
---|
1909 | n/a | } |
---|
1910 | n/a | |
---|
1911 | n/a | kind = PyUnicode_KIND(u); |
---|
1912 | n/a | data = PyUnicode_DATA(u); |
---|
1913 | n/a | len = PyUnicode_GET_LENGTH(u); |
---|
1914 | n/a | |
---|
1915 | n/a | cp = res = PyMem_Malloc(len+1); |
---|
1916 | n/a | if (res == NULL) { |
---|
1917 | n/a | PyErr_NoMemory(); |
---|
1918 | n/a | return NULL; |
---|
1919 | n/a | } |
---|
1920 | n/a | |
---|
1921 | n/a | j = 0; |
---|
1922 | n/a | if (strip_ws) { |
---|
1923 | n/a | while (len > 0 && is_space(kind, data, len-1)) { |
---|
1924 | n/a | len--; |
---|
1925 | n/a | } |
---|
1926 | n/a | while (j < len && is_space(kind, data, j)) { |
---|
1927 | n/a | j++; |
---|
1928 | n/a | } |
---|
1929 | n/a | } |
---|
1930 | n/a | |
---|
1931 | n/a | for (; j < len; j++) { |
---|
1932 | n/a | ch = PyUnicode_READ(kind, data, j); |
---|
1933 | n/a | if (ignore_underscores && ch == '_') { |
---|
1934 | n/a | continue; |
---|
1935 | n/a | } |
---|
1936 | n/a | if (0 < ch && ch <= 127) { |
---|
1937 | n/a | *cp++ = ch; |
---|
1938 | n/a | continue; |
---|
1939 | n/a | } |
---|
1940 | n/a | if (Py_UNICODE_ISSPACE(ch)) { |
---|
1941 | n/a | *cp++ = ' '; |
---|
1942 | n/a | continue; |
---|
1943 | n/a | } |
---|
1944 | n/a | d = Py_UNICODE_TODECIMAL(ch); |
---|
1945 | n/a | if (d < 0) { |
---|
1946 | n/a | /* empty string triggers ConversionSyntax */ |
---|
1947 | n/a | *res = '\0'; |
---|
1948 | n/a | return res; |
---|
1949 | n/a | } |
---|
1950 | n/a | *cp++ = '0' + d; |
---|
1951 | n/a | } |
---|
1952 | n/a | *cp = '\0'; |
---|
1953 | n/a | return res; |
---|
1954 | n/a | } |
---|
1955 | n/a | |
---|
1956 | n/a | /* Return a new PyDecObject or a subtype from a C string. Use the context |
---|
1957 | n/a | during conversion. */ |
---|
1958 | n/a | static PyObject * |
---|
1959 | n/a | PyDecType_FromCString(PyTypeObject *type, const char *s, |
---|
1960 | n/a | PyObject *context) |
---|
1961 | n/a | { |
---|
1962 | n/a | PyObject *dec; |
---|
1963 | n/a | uint32_t status = 0; |
---|
1964 | n/a | |
---|
1965 | n/a | dec = PyDecType_New(type); |
---|
1966 | n/a | if (dec == NULL) { |
---|
1967 | n/a | return NULL; |
---|
1968 | n/a | } |
---|
1969 | n/a | |
---|
1970 | n/a | mpd_qset_string(MPD(dec), s, CTX(context), &status); |
---|
1971 | n/a | if (dec_addstatus(context, status)) { |
---|
1972 | n/a | Py_DECREF(dec); |
---|
1973 | n/a | return NULL; |
---|
1974 | n/a | } |
---|
1975 | n/a | return dec; |
---|
1976 | n/a | } |
---|
1977 | n/a | |
---|
1978 | n/a | /* Return a new PyDecObject or a subtype from a C string. Attempt exact |
---|
1979 | n/a | conversion. If the operand cannot be converted exactly, set |
---|
1980 | n/a | InvalidOperation. */ |
---|
1981 | n/a | static PyObject * |
---|
1982 | n/a | PyDecType_FromCStringExact(PyTypeObject *type, const char *s, |
---|
1983 | n/a | PyObject *context) |
---|
1984 | n/a | { |
---|
1985 | n/a | PyObject *dec; |
---|
1986 | n/a | uint32_t status = 0; |
---|
1987 | n/a | mpd_context_t maxctx; |
---|
1988 | n/a | |
---|
1989 | n/a | dec = PyDecType_New(type); |
---|
1990 | n/a | if (dec == NULL) { |
---|
1991 | n/a | return NULL; |
---|
1992 | n/a | } |
---|
1993 | n/a | |
---|
1994 | n/a | mpd_maxcontext(&maxctx); |
---|
1995 | n/a | |
---|
1996 | n/a | mpd_qset_string(MPD(dec), s, &maxctx, &status); |
---|
1997 | n/a | if (status & (MPD_Inexact|MPD_Rounded|MPD_Clamped)) { |
---|
1998 | n/a | /* we want exact results */ |
---|
1999 | n/a | mpd_seterror(MPD(dec), MPD_Invalid_operation, &status); |
---|
2000 | n/a | } |
---|
2001 | n/a | status &= MPD_Errors; |
---|
2002 | n/a | if (dec_addstatus(context, status)) { |
---|
2003 | n/a | Py_DECREF(dec); |
---|
2004 | n/a | return NULL; |
---|
2005 | n/a | } |
---|
2006 | n/a | |
---|
2007 | n/a | return dec; |
---|
2008 | n/a | } |
---|
2009 | n/a | |
---|
2010 | n/a | /* Return a new PyDecObject or a subtype from a PyUnicodeObject. */ |
---|
2011 | n/a | static PyObject * |
---|
2012 | n/a | PyDecType_FromUnicode(PyTypeObject *type, const PyObject *u, |
---|
2013 | n/a | PyObject *context) |
---|
2014 | n/a | { |
---|
2015 | n/a | PyObject *dec; |
---|
2016 | n/a | char *s; |
---|
2017 | n/a | |
---|
2018 | n/a | s = numeric_as_ascii(u, 0, 0); |
---|
2019 | n/a | if (s == NULL) { |
---|
2020 | n/a | return NULL; |
---|
2021 | n/a | } |
---|
2022 | n/a | |
---|
2023 | n/a | dec = PyDecType_FromCString(type, s, context); |
---|
2024 | n/a | PyMem_Free(s); |
---|
2025 | n/a | return dec; |
---|
2026 | n/a | } |
---|
2027 | n/a | |
---|
2028 | n/a | /* Return a new PyDecObject or a subtype from a PyUnicodeObject. Attempt exact |
---|
2029 | n/a | * conversion. If the conversion is not exact, fail with InvalidOperation. |
---|
2030 | n/a | * Allow leading and trailing whitespace in the input operand. */ |
---|
2031 | n/a | static PyObject * |
---|
2032 | n/a | PyDecType_FromUnicodeExactWS(PyTypeObject *type, const PyObject *u, |
---|
2033 | n/a | PyObject *context) |
---|
2034 | n/a | { |
---|
2035 | n/a | PyObject *dec; |
---|
2036 | n/a | char *s; |
---|
2037 | n/a | |
---|
2038 | n/a | s = numeric_as_ascii(u, 1, 1); |
---|
2039 | n/a | if (s == NULL) { |
---|
2040 | n/a | return NULL; |
---|
2041 | n/a | } |
---|
2042 | n/a | |
---|
2043 | n/a | dec = PyDecType_FromCStringExact(type, s, context); |
---|
2044 | n/a | PyMem_Free(s); |
---|
2045 | n/a | return dec; |
---|
2046 | n/a | } |
---|
2047 | n/a | |
---|
2048 | n/a | /* Set PyDecObject from triple without any error checking. */ |
---|
2049 | n/a | Py_LOCAL_INLINE(void) |
---|
2050 | n/a | _dec_settriple(PyObject *dec, uint8_t sign, uint32_t v, mpd_ssize_t exp) |
---|
2051 | n/a | { |
---|
2052 | n/a | |
---|
2053 | n/a | #ifdef CONFIG_64 |
---|
2054 | n/a | MPD(dec)->data[0] = v; |
---|
2055 | n/a | MPD(dec)->len = 1; |
---|
2056 | n/a | #else |
---|
2057 | n/a | uint32_t q, r; |
---|
2058 | n/a | q = v / MPD_RADIX; |
---|
2059 | n/a | r = v - q * MPD_RADIX; |
---|
2060 | n/a | MPD(dec)->data[1] = q; |
---|
2061 | n/a | MPD(dec)->data[0] = r; |
---|
2062 | n/a | MPD(dec)->len = q ? 2 : 1; |
---|
2063 | n/a | #endif |
---|
2064 | n/a | mpd_set_flags(MPD(dec), sign); |
---|
2065 | n/a | MPD(dec)->exp = exp; |
---|
2066 | n/a | mpd_setdigits(MPD(dec)); |
---|
2067 | n/a | } |
---|
2068 | n/a | |
---|
2069 | n/a | /* Return a new PyDecObject from an mpd_ssize_t. */ |
---|
2070 | n/a | static PyObject * |
---|
2071 | n/a | PyDecType_FromSsize(PyTypeObject *type, mpd_ssize_t v, PyObject *context) |
---|
2072 | n/a | { |
---|
2073 | n/a | PyObject *dec; |
---|
2074 | n/a | uint32_t status = 0; |
---|
2075 | n/a | |
---|
2076 | n/a | dec = PyDecType_New(type); |
---|
2077 | n/a | if (dec == NULL) { |
---|
2078 | n/a | return NULL; |
---|
2079 | n/a | } |
---|
2080 | n/a | |
---|
2081 | n/a | mpd_qset_ssize(MPD(dec), v, CTX(context), &status); |
---|
2082 | n/a | if (dec_addstatus(context, status)) { |
---|
2083 | n/a | Py_DECREF(dec); |
---|
2084 | n/a | return NULL; |
---|
2085 | n/a | } |
---|
2086 | n/a | return dec; |
---|
2087 | n/a | } |
---|
2088 | n/a | |
---|
2089 | n/a | /* Return a new PyDecObject from an mpd_ssize_t. Conversion is exact. */ |
---|
2090 | n/a | static PyObject * |
---|
2091 | n/a | PyDecType_FromSsizeExact(PyTypeObject *type, mpd_ssize_t v, PyObject *context) |
---|
2092 | n/a | { |
---|
2093 | n/a | PyObject *dec; |
---|
2094 | n/a | uint32_t status = 0; |
---|
2095 | n/a | mpd_context_t maxctx; |
---|
2096 | n/a | |
---|
2097 | n/a | dec = PyDecType_New(type); |
---|
2098 | n/a | if (dec == NULL) { |
---|
2099 | n/a | return NULL; |
---|
2100 | n/a | } |
---|
2101 | n/a | |
---|
2102 | n/a | mpd_maxcontext(&maxctx); |
---|
2103 | n/a | |
---|
2104 | n/a | mpd_qset_ssize(MPD(dec), v, &maxctx, &status); |
---|
2105 | n/a | if (dec_addstatus(context, status)) { |
---|
2106 | n/a | Py_DECREF(dec); |
---|
2107 | n/a | return NULL; |
---|
2108 | n/a | } |
---|
2109 | n/a | return dec; |
---|
2110 | n/a | } |
---|
2111 | n/a | |
---|
2112 | n/a | /* Convert from a PyLongObject. The context is not modified; flags set |
---|
2113 | n/a | during conversion are accumulated in the status parameter. */ |
---|
2114 | n/a | static PyObject * |
---|
2115 | n/a | dec_from_long(PyTypeObject *type, const PyObject *v, |
---|
2116 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
2117 | n/a | { |
---|
2118 | n/a | PyObject *dec; |
---|
2119 | n/a | PyLongObject *l = (PyLongObject *)v; |
---|
2120 | n/a | Py_ssize_t ob_size; |
---|
2121 | n/a | size_t len; |
---|
2122 | n/a | uint8_t sign; |
---|
2123 | n/a | |
---|
2124 | n/a | dec = PyDecType_New(type); |
---|
2125 | n/a | if (dec == NULL) { |
---|
2126 | n/a | return NULL; |
---|
2127 | n/a | } |
---|
2128 | n/a | |
---|
2129 | n/a | ob_size = Py_SIZE(l); |
---|
2130 | n/a | if (ob_size == 0) { |
---|
2131 | n/a | _dec_settriple(dec, MPD_POS, 0, 0); |
---|
2132 | n/a | return dec; |
---|
2133 | n/a | } |
---|
2134 | n/a | |
---|
2135 | n/a | if (ob_size < 0) { |
---|
2136 | n/a | len = -ob_size; |
---|
2137 | n/a | sign = MPD_NEG; |
---|
2138 | n/a | } |
---|
2139 | n/a | else { |
---|
2140 | n/a | len = ob_size; |
---|
2141 | n/a | sign = MPD_POS; |
---|
2142 | n/a | } |
---|
2143 | n/a | |
---|
2144 | n/a | if (len == 1) { |
---|
2145 | n/a | _dec_settriple(dec, sign, *l->ob_digit, 0); |
---|
2146 | n/a | mpd_qfinalize(MPD(dec), ctx, status); |
---|
2147 | n/a | return dec; |
---|
2148 | n/a | } |
---|
2149 | n/a | |
---|
2150 | n/a | #if PYLONG_BITS_IN_DIGIT == 30 |
---|
2151 | n/a | mpd_qimport_u32(MPD(dec), l->ob_digit, len, sign, PyLong_BASE, |
---|
2152 | n/a | ctx, status); |
---|
2153 | n/a | #elif PYLONG_BITS_IN_DIGIT == 15 |
---|
2154 | n/a | mpd_qimport_u16(MPD(dec), l->ob_digit, len, sign, PyLong_BASE, |
---|
2155 | n/a | ctx, status); |
---|
2156 | n/a | #else |
---|
2157 | n/a | #error "PYLONG_BITS_IN_DIGIT should be 15 or 30" |
---|
2158 | n/a | #endif |
---|
2159 | n/a | |
---|
2160 | n/a | return dec; |
---|
2161 | n/a | } |
---|
2162 | n/a | |
---|
2163 | n/a | /* Return a new PyDecObject from a PyLongObject. Use the context for |
---|
2164 | n/a | conversion. */ |
---|
2165 | n/a | static PyObject * |
---|
2166 | n/a | PyDecType_FromLong(PyTypeObject *type, const PyObject *pylong, |
---|
2167 | n/a | PyObject *context) |
---|
2168 | n/a | { |
---|
2169 | n/a | PyObject *dec; |
---|
2170 | n/a | uint32_t status = 0; |
---|
2171 | n/a | |
---|
2172 | n/a | dec = dec_from_long(type, pylong, CTX(context), &status); |
---|
2173 | n/a | if (dec == NULL) { |
---|
2174 | n/a | return NULL; |
---|
2175 | n/a | } |
---|
2176 | n/a | |
---|
2177 | n/a | if (dec_addstatus(context, status)) { |
---|
2178 | n/a | Py_DECREF(dec); |
---|
2179 | n/a | return NULL; |
---|
2180 | n/a | } |
---|
2181 | n/a | |
---|
2182 | n/a | return dec; |
---|
2183 | n/a | } |
---|
2184 | n/a | |
---|
2185 | n/a | /* Return a new PyDecObject from a PyLongObject. Use a maximum context |
---|
2186 | n/a | for conversion. If the conversion is not exact, set InvalidOperation. */ |
---|
2187 | n/a | static PyObject * |
---|
2188 | n/a | PyDecType_FromLongExact(PyTypeObject *type, const PyObject *pylong, |
---|
2189 | n/a | PyObject *context) |
---|
2190 | n/a | { |
---|
2191 | n/a | PyObject *dec; |
---|
2192 | n/a | uint32_t status = 0; |
---|
2193 | n/a | mpd_context_t maxctx; |
---|
2194 | n/a | |
---|
2195 | n/a | mpd_maxcontext(&maxctx); |
---|
2196 | n/a | dec = dec_from_long(type, pylong, &maxctx, &status); |
---|
2197 | n/a | if (dec == NULL) { |
---|
2198 | n/a | return NULL; |
---|
2199 | n/a | } |
---|
2200 | n/a | |
---|
2201 | n/a | if (status & (MPD_Inexact|MPD_Rounded|MPD_Clamped)) { |
---|
2202 | n/a | /* we want exact results */ |
---|
2203 | n/a | mpd_seterror(MPD(dec), MPD_Invalid_operation, &status); |
---|
2204 | n/a | } |
---|
2205 | n/a | status &= MPD_Errors; |
---|
2206 | n/a | if (dec_addstatus(context, status)) { |
---|
2207 | n/a | Py_DECREF(dec); |
---|
2208 | n/a | return NULL; |
---|
2209 | n/a | } |
---|
2210 | n/a | |
---|
2211 | n/a | return dec; |
---|
2212 | n/a | } |
---|
2213 | n/a | |
---|
2214 | n/a | /* External C-API functions */ |
---|
2215 | n/a | static binaryfunc _py_long_multiply; |
---|
2216 | n/a | static binaryfunc _py_long_floor_divide; |
---|
2217 | n/a | static ternaryfunc _py_long_power; |
---|
2218 | n/a | static unaryfunc _py_float_abs; |
---|
2219 | n/a | static PyCFunction _py_long_bit_length; |
---|
2220 | n/a | static PyCFunction _py_float_as_integer_ratio; |
---|
2221 | n/a | |
---|
2222 | n/a | /* Return a PyDecObject or a subtype from a PyFloatObject. |
---|
2223 | n/a | Conversion is exact. */ |
---|
2224 | n/a | static PyObject * |
---|
2225 | n/a | PyDecType_FromFloatExact(PyTypeObject *type, PyObject *v, |
---|
2226 | n/a | PyObject *context) |
---|
2227 | n/a | { |
---|
2228 | n/a | PyObject *dec, *tmp; |
---|
2229 | n/a | PyObject *n, *d, *n_d; |
---|
2230 | n/a | mpd_ssize_t k; |
---|
2231 | n/a | double x; |
---|
2232 | n/a | int sign; |
---|
2233 | n/a | mpd_t *d1, *d2; |
---|
2234 | n/a | uint32_t status = 0; |
---|
2235 | n/a | mpd_context_t maxctx; |
---|
2236 | n/a | |
---|
2237 | n/a | |
---|
2238 | n/a | assert(PyType_IsSubtype(type, &PyDec_Type)); |
---|
2239 | n/a | |
---|
2240 | n/a | if (PyLong_Check(v)) { |
---|
2241 | n/a | return PyDecType_FromLongExact(type, v, context); |
---|
2242 | n/a | } |
---|
2243 | n/a | if (!PyFloat_Check(v)) { |
---|
2244 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
2245 | n/a | "argument must be int of float"); |
---|
2246 | n/a | return NULL; |
---|
2247 | n/a | } |
---|
2248 | n/a | |
---|
2249 | n/a | x = PyFloat_AsDouble(v); |
---|
2250 | n/a | if (x == -1.0 && PyErr_Occurred()) { |
---|
2251 | n/a | return NULL; |
---|
2252 | n/a | } |
---|
2253 | n/a | sign = (copysign(1.0, x) == 1.0) ? 0 : 1; |
---|
2254 | n/a | |
---|
2255 | n/a | if (Py_IS_NAN(x) || Py_IS_INFINITY(x)) { |
---|
2256 | n/a | dec = PyDecType_New(type); |
---|
2257 | n/a | if (dec == NULL) { |
---|
2258 | n/a | return NULL; |
---|
2259 | n/a | } |
---|
2260 | n/a | if (Py_IS_NAN(x)) { |
---|
2261 | n/a | /* decimal.py calls repr(float(+-nan)), |
---|
2262 | n/a | * which always gives a positive result. */ |
---|
2263 | n/a | mpd_setspecial(MPD(dec), MPD_POS, MPD_NAN); |
---|
2264 | n/a | } |
---|
2265 | n/a | else { |
---|
2266 | n/a | mpd_setspecial(MPD(dec), sign, MPD_INF); |
---|
2267 | n/a | } |
---|
2268 | n/a | return dec; |
---|
2269 | n/a | } |
---|
2270 | n/a | |
---|
2271 | n/a | /* absolute value of the float */ |
---|
2272 | n/a | tmp = _py_float_abs(v); |
---|
2273 | n/a | if (tmp == NULL) { |
---|
2274 | n/a | return NULL; |
---|
2275 | n/a | } |
---|
2276 | n/a | |
---|
2277 | n/a | /* float as integer ratio: numerator/denominator */ |
---|
2278 | n/a | n_d = _py_float_as_integer_ratio(tmp, NULL); |
---|
2279 | n/a | Py_DECREF(tmp); |
---|
2280 | n/a | if (n_d == NULL) { |
---|
2281 | n/a | return NULL; |
---|
2282 | n/a | } |
---|
2283 | n/a | n = PyTuple_GET_ITEM(n_d, 0); |
---|
2284 | n/a | d = PyTuple_GET_ITEM(n_d, 1); |
---|
2285 | n/a | |
---|
2286 | n/a | tmp = _py_long_bit_length(d, NULL); |
---|
2287 | n/a | if (tmp == NULL) { |
---|
2288 | n/a | Py_DECREF(n_d); |
---|
2289 | n/a | return NULL; |
---|
2290 | n/a | } |
---|
2291 | n/a | k = PyLong_AsSsize_t(tmp); |
---|
2292 | n/a | Py_DECREF(tmp); |
---|
2293 | n/a | if (k == -1 && PyErr_Occurred()) { |
---|
2294 | n/a | Py_DECREF(n_d); |
---|
2295 | n/a | return NULL; |
---|
2296 | n/a | } |
---|
2297 | n/a | k--; |
---|
2298 | n/a | |
---|
2299 | n/a | dec = PyDecType_FromLongExact(type, n, context); |
---|
2300 | n/a | Py_DECREF(n_d); |
---|
2301 | n/a | if (dec == NULL) { |
---|
2302 | n/a | return NULL; |
---|
2303 | n/a | } |
---|
2304 | n/a | |
---|
2305 | n/a | d1 = mpd_qnew(); |
---|
2306 | n/a | if (d1 == NULL) { |
---|
2307 | n/a | Py_DECREF(dec); |
---|
2308 | n/a | PyErr_NoMemory(); |
---|
2309 | n/a | return NULL; |
---|
2310 | n/a | } |
---|
2311 | n/a | d2 = mpd_qnew(); |
---|
2312 | n/a | if (d2 == NULL) { |
---|
2313 | n/a | mpd_del(d1); |
---|
2314 | n/a | Py_DECREF(dec); |
---|
2315 | n/a | PyErr_NoMemory(); |
---|
2316 | n/a | return NULL; |
---|
2317 | n/a | } |
---|
2318 | n/a | |
---|
2319 | n/a | mpd_maxcontext(&maxctx); |
---|
2320 | n/a | mpd_qset_uint(d1, 5, &maxctx, &status); |
---|
2321 | n/a | mpd_qset_ssize(d2, k, &maxctx, &status); |
---|
2322 | n/a | mpd_qpow(d1, d1, d2, &maxctx, &status); |
---|
2323 | n/a | if (dec_addstatus(context, status)) { |
---|
2324 | n/a | mpd_del(d1); |
---|
2325 | n/a | mpd_del(d2); |
---|
2326 | n/a | Py_DECREF(dec); |
---|
2327 | n/a | return NULL; |
---|
2328 | n/a | } |
---|
2329 | n/a | |
---|
2330 | n/a | /* result = n * 5**k */ |
---|
2331 | n/a | mpd_qmul(MPD(dec), MPD(dec), d1, &maxctx, &status); |
---|
2332 | n/a | mpd_del(d1); |
---|
2333 | n/a | mpd_del(d2); |
---|
2334 | n/a | if (dec_addstatus(context, status)) { |
---|
2335 | n/a | Py_DECREF(dec); |
---|
2336 | n/a | return NULL; |
---|
2337 | n/a | } |
---|
2338 | n/a | /* result = +- n * 5**k * 10**-k */ |
---|
2339 | n/a | mpd_set_sign(MPD(dec), sign); |
---|
2340 | n/a | MPD(dec)->exp = -k; |
---|
2341 | n/a | |
---|
2342 | n/a | return dec; |
---|
2343 | n/a | } |
---|
2344 | n/a | |
---|
2345 | n/a | static PyObject * |
---|
2346 | n/a | PyDecType_FromFloat(PyTypeObject *type, PyObject *v, |
---|
2347 | n/a | PyObject *context) |
---|
2348 | n/a | { |
---|
2349 | n/a | PyObject *dec; |
---|
2350 | n/a | uint32_t status = 0; |
---|
2351 | n/a | |
---|
2352 | n/a | dec = PyDecType_FromFloatExact(type, v, context); |
---|
2353 | n/a | if (dec == NULL) { |
---|
2354 | n/a | return NULL; |
---|
2355 | n/a | } |
---|
2356 | n/a | |
---|
2357 | n/a | mpd_qfinalize(MPD(dec), CTX(context), &status); |
---|
2358 | n/a | if (dec_addstatus(context, status)) { |
---|
2359 | n/a | Py_DECREF(dec); |
---|
2360 | n/a | return NULL; |
---|
2361 | n/a | } |
---|
2362 | n/a | |
---|
2363 | n/a | return dec; |
---|
2364 | n/a | } |
---|
2365 | n/a | |
---|
2366 | n/a | /* Return a new PyDecObject or a subtype from a Decimal. */ |
---|
2367 | n/a | static PyObject * |
---|
2368 | n/a | PyDecType_FromDecimalExact(PyTypeObject *type, PyObject *v, PyObject *context) |
---|
2369 | n/a | { |
---|
2370 | n/a | PyObject *dec; |
---|
2371 | n/a | uint32_t status = 0; |
---|
2372 | n/a | |
---|
2373 | n/a | if (type == &PyDec_Type && PyDec_CheckExact(v)) { |
---|
2374 | n/a | Py_INCREF(v); |
---|
2375 | n/a | return v; |
---|
2376 | n/a | } |
---|
2377 | n/a | |
---|
2378 | n/a | dec = PyDecType_New(type); |
---|
2379 | n/a | if (dec == NULL) { |
---|
2380 | n/a | return NULL; |
---|
2381 | n/a | } |
---|
2382 | n/a | |
---|
2383 | n/a | mpd_qcopy(MPD(dec), MPD(v), &status); |
---|
2384 | n/a | if (dec_addstatus(context, status)) { |
---|
2385 | n/a | Py_DECREF(dec); |
---|
2386 | n/a | return NULL; |
---|
2387 | n/a | } |
---|
2388 | n/a | |
---|
2389 | n/a | return dec; |
---|
2390 | n/a | } |
---|
2391 | n/a | |
---|
2392 | n/a | static PyObject * |
---|
2393 | n/a | sequence_as_tuple(PyObject *v, PyObject *ex, const char *mesg) |
---|
2394 | n/a | { |
---|
2395 | n/a | if (PyTuple_Check(v)) { |
---|
2396 | n/a | Py_INCREF(v); |
---|
2397 | n/a | return v; |
---|
2398 | n/a | } |
---|
2399 | n/a | if (PyList_Check(v)) { |
---|
2400 | n/a | return PyList_AsTuple(v); |
---|
2401 | n/a | } |
---|
2402 | n/a | |
---|
2403 | n/a | PyErr_SetString(ex, mesg); |
---|
2404 | n/a | return NULL; |
---|
2405 | n/a | } |
---|
2406 | n/a | |
---|
2407 | n/a | /* Return a new C string representation of a DecimalTuple. */ |
---|
2408 | n/a | static char * |
---|
2409 | n/a | dectuple_as_str(PyObject *dectuple) |
---|
2410 | n/a | { |
---|
2411 | n/a | PyObject *digits = NULL, *tmp; |
---|
2412 | n/a | char *decstring = NULL; |
---|
2413 | n/a | char sign_special[6]; |
---|
2414 | n/a | char *cp; |
---|
2415 | n/a | long sign, l; |
---|
2416 | n/a | mpd_ssize_t exp = 0; |
---|
2417 | n/a | Py_ssize_t i, mem, tsize; |
---|
2418 | n/a | int is_infinite = 0; |
---|
2419 | n/a | int n; |
---|
2420 | n/a | |
---|
2421 | n/a | assert(PyTuple_Check(dectuple)); |
---|
2422 | n/a | |
---|
2423 | n/a | if (PyTuple_Size(dectuple) != 3) { |
---|
2424 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
2425 | n/a | "argument must be a sequence of length 3"); |
---|
2426 | n/a | goto error; |
---|
2427 | n/a | } |
---|
2428 | n/a | |
---|
2429 | n/a | /* sign */ |
---|
2430 | n/a | tmp = PyTuple_GET_ITEM(dectuple, 0); |
---|
2431 | n/a | if (!PyLong_Check(tmp)) { |
---|
2432 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
2433 | n/a | "sign must be an integer with the value 0 or 1"); |
---|
2434 | n/a | goto error; |
---|
2435 | n/a | } |
---|
2436 | n/a | sign = PyLong_AsLong(tmp); |
---|
2437 | n/a | if (sign == -1 && PyErr_Occurred()) { |
---|
2438 | n/a | goto error; |
---|
2439 | n/a | } |
---|
2440 | n/a | if (sign != 0 && sign != 1) { |
---|
2441 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
2442 | n/a | "sign must be an integer with the value 0 or 1"); |
---|
2443 | n/a | goto error; |
---|
2444 | n/a | } |
---|
2445 | n/a | sign_special[0] = sign ? '-' : '+'; |
---|
2446 | n/a | sign_special[1] = '\0'; |
---|
2447 | n/a | |
---|
2448 | n/a | /* exponent or encoding for a special number */ |
---|
2449 | n/a | tmp = PyTuple_GET_ITEM(dectuple, 2); |
---|
2450 | n/a | if (PyUnicode_Check(tmp)) { |
---|
2451 | n/a | /* special */ |
---|
2452 | n/a | if (PyUnicode_CompareWithASCIIString(tmp, "F") == 0) { |
---|
2453 | n/a | strcat(sign_special, "Inf"); |
---|
2454 | n/a | is_infinite = 1; |
---|
2455 | n/a | } |
---|
2456 | n/a | else if (PyUnicode_CompareWithASCIIString(tmp, "n") == 0) { |
---|
2457 | n/a | strcat(sign_special, "NaN"); |
---|
2458 | n/a | } |
---|
2459 | n/a | else if (PyUnicode_CompareWithASCIIString(tmp, "N") == 0) { |
---|
2460 | n/a | strcat(sign_special, "sNaN"); |
---|
2461 | n/a | } |
---|
2462 | n/a | else { |
---|
2463 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
2464 | n/a | "string argument in the third position " |
---|
2465 | n/a | "must be 'F', 'n' or 'N'"); |
---|
2466 | n/a | goto error; |
---|
2467 | n/a | } |
---|
2468 | n/a | } |
---|
2469 | n/a | else { |
---|
2470 | n/a | /* exponent */ |
---|
2471 | n/a | if (!PyLong_Check(tmp)) { |
---|
2472 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
2473 | n/a | "exponent must be an integer"); |
---|
2474 | n/a | goto error; |
---|
2475 | n/a | } |
---|
2476 | n/a | exp = PyLong_AsSsize_t(tmp); |
---|
2477 | n/a | if (exp == -1 && PyErr_Occurred()) { |
---|
2478 | n/a | goto error; |
---|
2479 | n/a | } |
---|
2480 | n/a | } |
---|
2481 | n/a | |
---|
2482 | n/a | /* coefficient */ |
---|
2483 | n/a | digits = sequence_as_tuple(PyTuple_GET_ITEM(dectuple, 1), PyExc_ValueError, |
---|
2484 | n/a | "coefficient must be a tuple of digits"); |
---|
2485 | n/a | if (digits == NULL) { |
---|
2486 | n/a | goto error; |
---|
2487 | n/a | } |
---|
2488 | n/a | |
---|
2489 | n/a | tsize = PyTuple_Size(digits); |
---|
2490 | n/a | /* [sign][coeffdigits+1][E][-][expdigits+1]['\0'] */ |
---|
2491 | n/a | mem = 1 + tsize + 3 + MPD_EXPDIGITS + 2; |
---|
2492 | n/a | cp = decstring = PyMem_Malloc(mem); |
---|
2493 | n/a | if (decstring == NULL) { |
---|
2494 | n/a | PyErr_NoMemory(); |
---|
2495 | n/a | goto error; |
---|
2496 | n/a | } |
---|
2497 | n/a | |
---|
2498 | n/a | n = snprintf(cp, mem, "%s", sign_special); |
---|
2499 | n/a | if (n < 0 || n >= mem) { |
---|
2500 | n/a | PyErr_SetString(PyExc_RuntimeError, |
---|
2501 | n/a | "internal error in dec_sequence_as_str"); |
---|
2502 | n/a | goto error; |
---|
2503 | n/a | } |
---|
2504 | n/a | cp += n; |
---|
2505 | n/a | |
---|
2506 | n/a | if (tsize == 0 && sign_special[1] == '\0') { |
---|
2507 | n/a | /* empty tuple: zero coefficient, except for special numbers */ |
---|
2508 | n/a | *cp++ = '0'; |
---|
2509 | n/a | } |
---|
2510 | n/a | for (i = 0; i < tsize; i++) { |
---|
2511 | n/a | tmp = PyTuple_GET_ITEM(digits, i); |
---|
2512 | n/a | if (!PyLong_Check(tmp)) { |
---|
2513 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
2514 | n/a | "coefficient must be a tuple of digits"); |
---|
2515 | n/a | goto error; |
---|
2516 | n/a | } |
---|
2517 | n/a | l = PyLong_AsLong(tmp); |
---|
2518 | n/a | if (l == -1 && PyErr_Occurred()) { |
---|
2519 | n/a | goto error; |
---|
2520 | n/a | } |
---|
2521 | n/a | if (l < 0 || l > 9) { |
---|
2522 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
2523 | n/a | "coefficient must be a tuple of digits"); |
---|
2524 | n/a | goto error; |
---|
2525 | n/a | } |
---|
2526 | n/a | if (is_infinite) { |
---|
2527 | n/a | /* accept but ignore any well-formed coefficient for compatibility |
---|
2528 | n/a | with decimal.py */ |
---|
2529 | n/a | continue; |
---|
2530 | n/a | } |
---|
2531 | n/a | *cp++ = (char)l + '0'; |
---|
2532 | n/a | } |
---|
2533 | n/a | *cp = '\0'; |
---|
2534 | n/a | |
---|
2535 | n/a | if (sign_special[1] == '\0') { |
---|
2536 | n/a | /* not a special number */ |
---|
2537 | n/a | *cp++ = 'E'; |
---|
2538 | n/a | n = snprintf(cp, MPD_EXPDIGITS+2, "%" PRI_mpd_ssize_t, exp); |
---|
2539 | n/a | if (n < 0 || n >= MPD_EXPDIGITS+2) { |
---|
2540 | n/a | PyErr_SetString(PyExc_RuntimeError, |
---|
2541 | n/a | "internal error in dec_sequence_as_str"); |
---|
2542 | n/a | goto error; |
---|
2543 | n/a | } |
---|
2544 | n/a | } |
---|
2545 | n/a | |
---|
2546 | n/a | Py_XDECREF(digits); |
---|
2547 | n/a | return decstring; |
---|
2548 | n/a | |
---|
2549 | n/a | |
---|
2550 | n/a | error: |
---|
2551 | n/a | Py_XDECREF(digits); |
---|
2552 | n/a | if (decstring) PyMem_Free(decstring); |
---|
2553 | n/a | return NULL; |
---|
2554 | n/a | } |
---|
2555 | n/a | |
---|
2556 | n/a | /* Currently accepts tuples and lists. */ |
---|
2557 | n/a | static PyObject * |
---|
2558 | n/a | PyDecType_FromSequence(PyTypeObject *type, PyObject *v, |
---|
2559 | n/a | PyObject *context) |
---|
2560 | n/a | { |
---|
2561 | n/a | PyObject *dectuple; |
---|
2562 | n/a | PyObject *dec; |
---|
2563 | n/a | char *s; |
---|
2564 | n/a | |
---|
2565 | n/a | dectuple = sequence_as_tuple(v, PyExc_TypeError, |
---|
2566 | n/a | "argument must be a tuple or list"); |
---|
2567 | n/a | if (dectuple == NULL) { |
---|
2568 | n/a | return NULL; |
---|
2569 | n/a | } |
---|
2570 | n/a | |
---|
2571 | n/a | s = dectuple_as_str(dectuple); |
---|
2572 | n/a | Py_DECREF(dectuple); |
---|
2573 | n/a | if (s == NULL) { |
---|
2574 | n/a | return NULL; |
---|
2575 | n/a | } |
---|
2576 | n/a | |
---|
2577 | n/a | dec = PyDecType_FromCString(type, s, context); |
---|
2578 | n/a | |
---|
2579 | n/a | PyMem_Free(s); |
---|
2580 | n/a | return dec; |
---|
2581 | n/a | } |
---|
2582 | n/a | |
---|
2583 | n/a | /* Currently accepts tuples and lists. */ |
---|
2584 | n/a | static PyObject * |
---|
2585 | n/a | PyDecType_FromSequenceExact(PyTypeObject *type, PyObject *v, |
---|
2586 | n/a | PyObject *context) |
---|
2587 | n/a | { |
---|
2588 | n/a | PyObject *dectuple; |
---|
2589 | n/a | PyObject *dec; |
---|
2590 | n/a | char *s; |
---|
2591 | n/a | |
---|
2592 | n/a | dectuple = sequence_as_tuple(v, PyExc_TypeError, |
---|
2593 | n/a | "argument must be a tuple or list"); |
---|
2594 | n/a | if (dectuple == NULL) { |
---|
2595 | n/a | return NULL; |
---|
2596 | n/a | } |
---|
2597 | n/a | |
---|
2598 | n/a | s = dectuple_as_str(dectuple); |
---|
2599 | n/a | Py_DECREF(dectuple); |
---|
2600 | n/a | if (s == NULL) { |
---|
2601 | n/a | return NULL; |
---|
2602 | n/a | } |
---|
2603 | n/a | |
---|
2604 | n/a | dec = PyDecType_FromCStringExact(type, s, context); |
---|
2605 | n/a | |
---|
2606 | n/a | PyMem_Free(s); |
---|
2607 | n/a | return dec; |
---|
2608 | n/a | } |
---|
2609 | n/a | |
---|
2610 | n/a | #define PyDec_FromCString(str, context) \ |
---|
2611 | n/a | PyDecType_FromCString(&PyDec_Type, str, context) |
---|
2612 | n/a | #define PyDec_FromCStringExact(str, context) \ |
---|
2613 | n/a | PyDecType_FromCStringExact(&PyDec_Type, str, context) |
---|
2614 | n/a | |
---|
2615 | n/a | #define PyDec_FromUnicode(unicode, context) \ |
---|
2616 | n/a | PyDecType_FromUnicode(&PyDec_Type, unicode, context) |
---|
2617 | n/a | #define PyDec_FromUnicodeExact(unicode, context) \ |
---|
2618 | n/a | PyDecType_FromUnicodeExact(&PyDec_Type, unicode, context) |
---|
2619 | n/a | #define PyDec_FromUnicodeExactWS(unicode, context) \ |
---|
2620 | n/a | PyDecType_FromUnicodeExactWS(&PyDec_Type, unicode, context) |
---|
2621 | n/a | |
---|
2622 | n/a | #define PyDec_FromSsize(v, context) \ |
---|
2623 | n/a | PyDecType_FromSsize(&PyDec_Type, v, context) |
---|
2624 | n/a | #define PyDec_FromSsizeExact(v, context) \ |
---|
2625 | n/a | PyDecType_FromSsizeExact(&PyDec_Type, v, context) |
---|
2626 | n/a | |
---|
2627 | n/a | #define PyDec_FromLong(pylong, context) \ |
---|
2628 | n/a | PyDecType_FromLong(&PyDec_Type, pylong, context) |
---|
2629 | n/a | #define PyDec_FromLongExact(pylong, context) \ |
---|
2630 | n/a | PyDecType_FromLongExact(&PyDec_Type, pylong, context) |
---|
2631 | n/a | |
---|
2632 | n/a | #define PyDec_FromFloat(pyfloat, context) \ |
---|
2633 | n/a | PyDecType_FromFloat(&PyDec_Type, pyfloat, context) |
---|
2634 | n/a | #define PyDec_FromFloatExact(pyfloat, context) \ |
---|
2635 | n/a | PyDecType_FromFloatExact(&PyDec_Type, pyfloat, context) |
---|
2636 | n/a | |
---|
2637 | n/a | #define PyDec_FromSequence(sequence, context) \ |
---|
2638 | n/a | PyDecType_FromSequence(&PyDec_Type, sequence, context) |
---|
2639 | n/a | #define PyDec_FromSequenceExact(sequence, context) \ |
---|
2640 | n/a | PyDecType_FromSequenceExact(&PyDec_Type, sequence, context) |
---|
2641 | n/a | |
---|
2642 | n/a | /* class method */ |
---|
2643 | n/a | static PyObject * |
---|
2644 | n/a | dec_from_float(PyObject *type, PyObject *pyfloat) |
---|
2645 | n/a | { |
---|
2646 | n/a | PyObject *context; |
---|
2647 | n/a | PyObject *result; |
---|
2648 | n/a | |
---|
2649 | n/a | CURRENT_CONTEXT(context); |
---|
2650 | n/a | result = PyDecType_FromFloatExact(&PyDec_Type, pyfloat, context); |
---|
2651 | n/a | if (type != (PyObject *)&PyDec_Type && result != NULL) { |
---|
2652 | n/a | Py_SETREF(result, PyObject_CallFunctionObjArgs(type, result, NULL)); |
---|
2653 | n/a | } |
---|
2654 | n/a | |
---|
2655 | n/a | return result; |
---|
2656 | n/a | } |
---|
2657 | n/a | |
---|
2658 | n/a | /* create_decimal_from_float */ |
---|
2659 | n/a | static PyObject * |
---|
2660 | n/a | ctx_from_float(PyObject *context, PyObject *v) |
---|
2661 | n/a | { |
---|
2662 | n/a | return PyDec_FromFloat(v, context); |
---|
2663 | n/a | } |
---|
2664 | n/a | |
---|
2665 | n/a | /* Apply the context to the input operand. Return a new PyDecObject. */ |
---|
2666 | n/a | static PyObject * |
---|
2667 | n/a | dec_apply(PyObject *v, PyObject *context) |
---|
2668 | n/a | { |
---|
2669 | n/a | PyObject *result; |
---|
2670 | n/a | uint32_t status = 0; |
---|
2671 | n/a | |
---|
2672 | n/a | result = dec_alloc(); |
---|
2673 | n/a | if (result == NULL) { |
---|
2674 | n/a | return NULL; |
---|
2675 | n/a | } |
---|
2676 | n/a | |
---|
2677 | n/a | mpd_qcopy(MPD(result), MPD(v), &status); |
---|
2678 | n/a | if (dec_addstatus(context, status)) { |
---|
2679 | n/a | Py_DECREF(result); |
---|
2680 | n/a | return NULL; |
---|
2681 | n/a | } |
---|
2682 | n/a | |
---|
2683 | n/a | mpd_qfinalize(MPD(result), CTX(context), &status); |
---|
2684 | n/a | if (dec_addstatus(context, status)) { |
---|
2685 | n/a | Py_DECREF(result); |
---|
2686 | n/a | return NULL; |
---|
2687 | n/a | } |
---|
2688 | n/a | |
---|
2689 | n/a | return result; |
---|
2690 | n/a | } |
---|
2691 | n/a | |
---|
2692 | n/a | /* 'v' can have any type accepted by the Decimal constructor. Attempt |
---|
2693 | n/a | an exact conversion. If the result does not meet the restrictions |
---|
2694 | n/a | for an mpd_t, fail with InvalidOperation. */ |
---|
2695 | n/a | static PyObject * |
---|
2696 | n/a | PyDecType_FromObjectExact(PyTypeObject *type, PyObject *v, PyObject *context) |
---|
2697 | n/a | { |
---|
2698 | n/a | if (v == NULL) { |
---|
2699 | n/a | return PyDecType_FromSsizeExact(type, 0, context); |
---|
2700 | n/a | } |
---|
2701 | n/a | else if (PyDec_Check(v)) { |
---|
2702 | n/a | return PyDecType_FromDecimalExact(type, v, context); |
---|
2703 | n/a | } |
---|
2704 | n/a | else if (PyUnicode_Check(v)) { |
---|
2705 | n/a | return PyDecType_FromUnicodeExactWS(type, v, context); |
---|
2706 | n/a | } |
---|
2707 | n/a | else if (PyLong_Check(v)) { |
---|
2708 | n/a | return PyDecType_FromLongExact(type, v, context); |
---|
2709 | n/a | } |
---|
2710 | n/a | else if (PyTuple_Check(v) || PyList_Check(v)) { |
---|
2711 | n/a | return PyDecType_FromSequenceExact(type, v, context); |
---|
2712 | n/a | } |
---|
2713 | n/a | else if (PyFloat_Check(v)) { |
---|
2714 | n/a | if (dec_addstatus(context, MPD_Float_operation)) { |
---|
2715 | n/a | return NULL; |
---|
2716 | n/a | } |
---|
2717 | n/a | return PyDecType_FromFloatExact(type, v, context); |
---|
2718 | n/a | } |
---|
2719 | n/a | else { |
---|
2720 | n/a | PyErr_Format(PyExc_TypeError, |
---|
2721 | n/a | "conversion from %s to Decimal is not supported", |
---|
2722 | n/a | v->ob_type->tp_name); |
---|
2723 | n/a | return NULL; |
---|
2724 | n/a | } |
---|
2725 | n/a | } |
---|
2726 | n/a | |
---|
2727 | n/a | /* The context is used during conversion. This function is the |
---|
2728 | n/a | equivalent of context.create_decimal(). */ |
---|
2729 | n/a | static PyObject * |
---|
2730 | n/a | PyDec_FromObject(PyObject *v, PyObject *context) |
---|
2731 | n/a | { |
---|
2732 | n/a | if (v == NULL) { |
---|
2733 | n/a | return PyDec_FromSsize(0, context); |
---|
2734 | n/a | } |
---|
2735 | n/a | else if (PyDec_Check(v)) { |
---|
2736 | n/a | mpd_context_t *ctx = CTX(context); |
---|
2737 | n/a | if (mpd_isnan(MPD(v)) && |
---|
2738 | n/a | MPD(v)->digits > ctx->prec - ctx->clamp) { |
---|
2739 | n/a | /* Special case: too many NaN payload digits */ |
---|
2740 | n/a | PyObject *result; |
---|
2741 | n/a | if (dec_addstatus(context, MPD_Conversion_syntax)) { |
---|
2742 | n/a | return NULL; |
---|
2743 | n/a | } |
---|
2744 | n/a | result = dec_alloc(); |
---|
2745 | n/a | if (result == NULL) { |
---|
2746 | n/a | return NULL; |
---|
2747 | n/a | } |
---|
2748 | n/a | mpd_setspecial(MPD(result), MPD_POS, MPD_NAN); |
---|
2749 | n/a | return result; |
---|
2750 | n/a | } |
---|
2751 | n/a | return dec_apply(v, context); |
---|
2752 | n/a | } |
---|
2753 | n/a | else if (PyUnicode_Check(v)) { |
---|
2754 | n/a | return PyDec_FromUnicode(v, context); |
---|
2755 | n/a | } |
---|
2756 | n/a | else if (PyLong_Check(v)) { |
---|
2757 | n/a | return PyDec_FromLong(v, context); |
---|
2758 | n/a | } |
---|
2759 | n/a | else if (PyTuple_Check(v) || PyList_Check(v)) { |
---|
2760 | n/a | return PyDec_FromSequence(v, context); |
---|
2761 | n/a | } |
---|
2762 | n/a | else if (PyFloat_Check(v)) { |
---|
2763 | n/a | if (dec_addstatus(context, MPD_Float_operation)) { |
---|
2764 | n/a | return NULL; |
---|
2765 | n/a | } |
---|
2766 | n/a | return PyDec_FromFloat(v, context); |
---|
2767 | n/a | } |
---|
2768 | n/a | else { |
---|
2769 | n/a | PyErr_Format(PyExc_TypeError, |
---|
2770 | n/a | "conversion from %s to Decimal is not supported", |
---|
2771 | n/a | v->ob_type->tp_name); |
---|
2772 | n/a | return NULL; |
---|
2773 | n/a | } |
---|
2774 | n/a | } |
---|
2775 | n/a | |
---|
2776 | n/a | static PyObject * |
---|
2777 | n/a | dec_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
---|
2778 | n/a | { |
---|
2779 | n/a | static char *kwlist[] = {"value", "context", NULL}; |
---|
2780 | n/a | PyObject *v = NULL; |
---|
2781 | n/a | PyObject *context = Py_None; |
---|
2782 | n/a | |
---|
2783 | n/a | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO", kwlist, |
---|
2784 | n/a | &v, &context)) { |
---|
2785 | n/a | return NULL; |
---|
2786 | n/a | } |
---|
2787 | n/a | CONTEXT_CHECK_VA(context); |
---|
2788 | n/a | |
---|
2789 | n/a | return PyDecType_FromObjectExact(type, v, context); |
---|
2790 | n/a | } |
---|
2791 | n/a | |
---|
2792 | n/a | static PyObject * |
---|
2793 | n/a | ctx_create_decimal(PyObject *context, PyObject *args) |
---|
2794 | n/a | { |
---|
2795 | n/a | PyObject *v = NULL; |
---|
2796 | n/a | |
---|
2797 | n/a | if (!PyArg_ParseTuple(args, "|O", &v)) { |
---|
2798 | n/a | return NULL; |
---|
2799 | n/a | } |
---|
2800 | n/a | |
---|
2801 | n/a | return PyDec_FromObject(v, context); |
---|
2802 | n/a | } |
---|
2803 | n/a | |
---|
2804 | n/a | |
---|
2805 | n/a | /******************************************************************************/ |
---|
2806 | n/a | /* Implicit conversions to Decimal */ |
---|
2807 | n/a | /******************************************************************************/ |
---|
2808 | n/a | |
---|
2809 | n/a | /* Try to convert PyObject v to a new PyDecObject conv. If the conversion |
---|
2810 | n/a | fails, set conv to NULL (exception is set). If the conversion is not |
---|
2811 | n/a | implemented, set conv to Py_NotImplemented. */ |
---|
2812 | n/a | #define NOT_IMPL 0 |
---|
2813 | n/a | #define TYPE_ERR 1 |
---|
2814 | n/a | Py_LOCAL_INLINE(int) |
---|
2815 | n/a | convert_op(int type_err, PyObject **conv, PyObject *v, PyObject *context) |
---|
2816 | n/a | { |
---|
2817 | n/a | |
---|
2818 | n/a | if (PyDec_Check(v)) { |
---|
2819 | n/a | *conv = v; |
---|
2820 | n/a | Py_INCREF(v); |
---|
2821 | n/a | return 1; |
---|
2822 | n/a | } |
---|
2823 | n/a | if (PyLong_Check(v)) { |
---|
2824 | n/a | *conv = PyDec_FromLongExact(v, context); |
---|
2825 | n/a | if (*conv == NULL) { |
---|
2826 | n/a | return 0; |
---|
2827 | n/a | } |
---|
2828 | n/a | return 1; |
---|
2829 | n/a | } |
---|
2830 | n/a | |
---|
2831 | n/a | if (type_err) { |
---|
2832 | n/a | PyErr_Format(PyExc_TypeError, |
---|
2833 | n/a | "conversion from %s to Decimal is not supported", |
---|
2834 | n/a | v->ob_type->tp_name); |
---|
2835 | n/a | } |
---|
2836 | n/a | else { |
---|
2837 | n/a | Py_INCREF(Py_NotImplemented); |
---|
2838 | n/a | *conv = Py_NotImplemented; |
---|
2839 | n/a | } |
---|
2840 | n/a | return 0; |
---|
2841 | n/a | } |
---|
2842 | n/a | |
---|
2843 | n/a | /* Return NotImplemented for unsupported types. */ |
---|
2844 | n/a | #define CONVERT_OP(a, v, context) \ |
---|
2845 | n/a | if (!convert_op(NOT_IMPL, a, v, context)) { \ |
---|
2846 | n/a | return *(a); \ |
---|
2847 | n/a | } |
---|
2848 | n/a | |
---|
2849 | n/a | #define CONVERT_BINOP(a, b, v, w, context) \ |
---|
2850 | n/a | if (!convert_op(NOT_IMPL, a, v, context)) { \ |
---|
2851 | n/a | return *(a); \ |
---|
2852 | n/a | } \ |
---|
2853 | n/a | if (!convert_op(NOT_IMPL, b, w, context)) { \ |
---|
2854 | n/a | Py_DECREF(*(a)); \ |
---|
2855 | n/a | return *(b); \ |
---|
2856 | n/a | } |
---|
2857 | n/a | |
---|
2858 | n/a | #define CONVERT_TERNOP(a, b, c, v, w, x, context) \ |
---|
2859 | n/a | if (!convert_op(NOT_IMPL, a, v, context)) { \ |
---|
2860 | n/a | return *(a); \ |
---|
2861 | n/a | } \ |
---|
2862 | n/a | if (!convert_op(NOT_IMPL, b, w, context)) { \ |
---|
2863 | n/a | Py_DECREF(*(a)); \ |
---|
2864 | n/a | return *(b); \ |
---|
2865 | n/a | } \ |
---|
2866 | n/a | if (!convert_op(NOT_IMPL, c, x, context)) { \ |
---|
2867 | n/a | Py_DECREF(*(a)); \ |
---|
2868 | n/a | Py_DECREF(*(b)); \ |
---|
2869 | n/a | return *(c); \ |
---|
2870 | n/a | } |
---|
2871 | n/a | |
---|
2872 | n/a | /* Raise TypeError for unsupported types. */ |
---|
2873 | n/a | #define CONVERT_OP_RAISE(a, v, context) \ |
---|
2874 | n/a | if (!convert_op(TYPE_ERR, a, v, context)) { \ |
---|
2875 | n/a | return NULL; \ |
---|
2876 | n/a | } |
---|
2877 | n/a | |
---|
2878 | n/a | #define CONVERT_BINOP_RAISE(a, b, v, w, context) \ |
---|
2879 | n/a | if (!convert_op(TYPE_ERR, a, v, context)) { \ |
---|
2880 | n/a | return NULL; \ |
---|
2881 | n/a | } \ |
---|
2882 | n/a | if (!convert_op(TYPE_ERR, b, w, context)) { \ |
---|
2883 | n/a | Py_DECREF(*(a)); \ |
---|
2884 | n/a | return NULL; \ |
---|
2885 | n/a | } |
---|
2886 | n/a | |
---|
2887 | n/a | #define CONVERT_TERNOP_RAISE(a, b, c, v, w, x, context) \ |
---|
2888 | n/a | if (!convert_op(TYPE_ERR, a, v, context)) { \ |
---|
2889 | n/a | return NULL; \ |
---|
2890 | n/a | } \ |
---|
2891 | n/a | if (!convert_op(TYPE_ERR, b, w, context)) { \ |
---|
2892 | n/a | Py_DECREF(*(a)); \ |
---|
2893 | n/a | return NULL; \ |
---|
2894 | n/a | } \ |
---|
2895 | n/a | if (!convert_op(TYPE_ERR, c, x, context)) { \ |
---|
2896 | n/a | Py_DECREF(*(a)); \ |
---|
2897 | n/a | Py_DECREF(*(b)); \ |
---|
2898 | n/a | return NULL; \ |
---|
2899 | n/a | } |
---|
2900 | n/a | |
---|
2901 | n/a | |
---|
2902 | n/a | /******************************************************************************/ |
---|
2903 | n/a | /* Implicit conversions to Decimal for comparison */ |
---|
2904 | n/a | /******************************************************************************/ |
---|
2905 | n/a | |
---|
2906 | n/a | /* Convert rationals for comparison */ |
---|
2907 | n/a | static PyObject *Rational = NULL; |
---|
2908 | n/a | static PyObject * |
---|
2909 | n/a | multiply_by_denominator(PyObject *v, PyObject *r, PyObject *context) |
---|
2910 | n/a | { |
---|
2911 | n/a | PyObject *result; |
---|
2912 | n/a | PyObject *tmp = NULL; |
---|
2913 | n/a | PyObject *denom = NULL; |
---|
2914 | n/a | uint32_t status = 0; |
---|
2915 | n/a | mpd_context_t maxctx; |
---|
2916 | n/a | mpd_ssize_t exp; |
---|
2917 | n/a | mpd_t *vv; |
---|
2918 | n/a | |
---|
2919 | n/a | /* v is not special, r is a rational */ |
---|
2920 | n/a | tmp = PyObject_GetAttrString(r, "denominator"); |
---|
2921 | n/a | if (tmp == NULL) { |
---|
2922 | n/a | return NULL; |
---|
2923 | n/a | } |
---|
2924 | n/a | denom = PyDec_FromLongExact(tmp, context); |
---|
2925 | n/a | Py_DECREF(tmp); |
---|
2926 | n/a | if (denom == NULL) { |
---|
2927 | n/a | return NULL; |
---|
2928 | n/a | } |
---|
2929 | n/a | |
---|
2930 | n/a | vv = mpd_qncopy(MPD(v)); |
---|
2931 | n/a | if (vv == NULL) { |
---|
2932 | n/a | Py_DECREF(denom); |
---|
2933 | n/a | PyErr_NoMemory(); |
---|
2934 | n/a | return NULL; |
---|
2935 | n/a | } |
---|
2936 | n/a | result = dec_alloc(); |
---|
2937 | n/a | if (result == NULL) { |
---|
2938 | n/a | Py_DECREF(denom); |
---|
2939 | n/a | mpd_del(vv); |
---|
2940 | n/a | return NULL; |
---|
2941 | n/a | } |
---|
2942 | n/a | |
---|
2943 | n/a | mpd_maxcontext(&maxctx); |
---|
2944 | n/a | /* Prevent Overflow in the following multiplication. The result of |
---|
2945 | n/a | the multiplication is only used in mpd_qcmp, which can handle |
---|
2946 | n/a | values that are technically out of bounds, like (for 32-bit) |
---|
2947 | n/a | 99999999999999999999...99999999e+425000000. */ |
---|
2948 | n/a | exp = vv->exp; |
---|
2949 | n/a | vv->exp = 0; |
---|
2950 | n/a | mpd_qmul(MPD(result), vv, MPD(denom), &maxctx, &status); |
---|
2951 | n/a | MPD(result)->exp = exp; |
---|
2952 | n/a | |
---|
2953 | n/a | Py_DECREF(denom); |
---|
2954 | n/a | mpd_del(vv); |
---|
2955 | n/a | /* If any status has been accumulated during the multiplication, |
---|
2956 | n/a | the result is invalid. This is very unlikely, since even the |
---|
2957 | n/a | 32-bit version supports 425000000 digits. */ |
---|
2958 | n/a | if (status) { |
---|
2959 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
2960 | n/a | "exact conversion for comparison failed"); |
---|
2961 | n/a | Py_DECREF(result); |
---|
2962 | n/a | return NULL; |
---|
2963 | n/a | } |
---|
2964 | n/a | |
---|
2965 | n/a | return result; |
---|
2966 | n/a | } |
---|
2967 | n/a | |
---|
2968 | n/a | static PyObject * |
---|
2969 | n/a | numerator_as_decimal(PyObject *r, PyObject *context) |
---|
2970 | n/a | { |
---|
2971 | n/a | PyObject *tmp, *num; |
---|
2972 | n/a | |
---|
2973 | n/a | tmp = PyObject_GetAttrString(r, "numerator"); |
---|
2974 | n/a | if (tmp == NULL) { |
---|
2975 | n/a | return NULL; |
---|
2976 | n/a | } |
---|
2977 | n/a | |
---|
2978 | n/a | num = PyDec_FromLongExact(tmp, context); |
---|
2979 | n/a | Py_DECREF(tmp); |
---|
2980 | n/a | return num; |
---|
2981 | n/a | } |
---|
2982 | n/a | |
---|
2983 | n/a | /* Convert v and w for comparison. v is a Decimal. If w is a Rational, both |
---|
2984 | n/a | v and w have to be transformed. Return 1 for success, with new references |
---|
2985 | n/a | to the converted objects in vcmp and wcmp. Return 0 for failure. In that |
---|
2986 | n/a | case wcmp is either NULL or Py_NotImplemented (new reference) and vcmp |
---|
2987 | n/a | is undefined. */ |
---|
2988 | n/a | static int |
---|
2989 | n/a | convert_op_cmp(PyObject **vcmp, PyObject **wcmp, PyObject *v, PyObject *w, |
---|
2990 | n/a | int op, PyObject *context) |
---|
2991 | n/a | { |
---|
2992 | n/a | mpd_context_t *ctx = CTX(context); |
---|
2993 | n/a | |
---|
2994 | n/a | *vcmp = v; |
---|
2995 | n/a | |
---|
2996 | n/a | if (PyDec_Check(w)) { |
---|
2997 | n/a | Py_INCREF(w); |
---|
2998 | n/a | *wcmp = w; |
---|
2999 | n/a | } |
---|
3000 | n/a | else if (PyLong_Check(w)) { |
---|
3001 | n/a | *wcmp = PyDec_FromLongExact(w, context); |
---|
3002 | n/a | } |
---|
3003 | n/a | else if (PyFloat_Check(w)) { |
---|
3004 | n/a | if (op != Py_EQ && op != Py_NE && |
---|
3005 | n/a | dec_addstatus(context, MPD_Float_operation)) { |
---|
3006 | n/a | *wcmp = NULL; |
---|
3007 | n/a | } |
---|
3008 | n/a | else { |
---|
3009 | n/a | ctx->status |= MPD_Float_operation; |
---|
3010 | n/a | *wcmp = PyDec_FromFloatExact(w, context); |
---|
3011 | n/a | } |
---|
3012 | n/a | } |
---|
3013 | n/a | else if (PyComplex_Check(w) && (op == Py_EQ || op == Py_NE)) { |
---|
3014 | n/a | Py_complex c = PyComplex_AsCComplex(w); |
---|
3015 | n/a | if (c.real == -1.0 && PyErr_Occurred()) { |
---|
3016 | n/a | *wcmp = NULL; |
---|
3017 | n/a | } |
---|
3018 | n/a | else if (c.imag == 0.0) { |
---|
3019 | n/a | PyObject *tmp = PyFloat_FromDouble(c.real); |
---|
3020 | n/a | if (tmp == NULL) { |
---|
3021 | n/a | *wcmp = NULL; |
---|
3022 | n/a | } |
---|
3023 | n/a | else { |
---|
3024 | n/a | ctx->status |= MPD_Float_operation; |
---|
3025 | n/a | *wcmp = PyDec_FromFloatExact(tmp, context); |
---|
3026 | n/a | Py_DECREF(tmp); |
---|
3027 | n/a | } |
---|
3028 | n/a | } |
---|
3029 | n/a | else { |
---|
3030 | n/a | Py_INCREF(Py_NotImplemented); |
---|
3031 | n/a | *wcmp = Py_NotImplemented; |
---|
3032 | n/a | } |
---|
3033 | n/a | } |
---|
3034 | n/a | else { |
---|
3035 | n/a | int is_rational = PyObject_IsInstance(w, Rational); |
---|
3036 | n/a | if (is_rational < 0) { |
---|
3037 | n/a | *wcmp = NULL; |
---|
3038 | n/a | } |
---|
3039 | n/a | else if (is_rational > 0) { |
---|
3040 | n/a | *wcmp = numerator_as_decimal(w, context); |
---|
3041 | n/a | if (*wcmp && !mpd_isspecial(MPD(v))) { |
---|
3042 | n/a | *vcmp = multiply_by_denominator(v, w, context); |
---|
3043 | n/a | if (*vcmp == NULL) { |
---|
3044 | n/a | Py_CLEAR(*wcmp); |
---|
3045 | n/a | } |
---|
3046 | n/a | } |
---|
3047 | n/a | } |
---|
3048 | n/a | else { |
---|
3049 | n/a | Py_INCREF(Py_NotImplemented); |
---|
3050 | n/a | *wcmp = Py_NotImplemented; |
---|
3051 | n/a | } |
---|
3052 | n/a | } |
---|
3053 | n/a | |
---|
3054 | n/a | if (*wcmp == NULL || *wcmp == Py_NotImplemented) { |
---|
3055 | n/a | return 0; |
---|
3056 | n/a | } |
---|
3057 | n/a | if (*vcmp == v) { |
---|
3058 | n/a | Py_INCREF(v); |
---|
3059 | n/a | } |
---|
3060 | n/a | return 1; |
---|
3061 | n/a | } |
---|
3062 | n/a | |
---|
3063 | n/a | #define CONVERT_BINOP_CMP(vcmp, wcmp, v, w, op, ctx) \ |
---|
3064 | n/a | if (!convert_op_cmp(vcmp, wcmp, v, w, op, ctx)) { \ |
---|
3065 | n/a | return *(wcmp); \ |
---|
3066 | n/a | } \ |
---|
3067 | n/a | |
---|
3068 | n/a | |
---|
3069 | n/a | /******************************************************************************/ |
---|
3070 | n/a | /* Conversions from decimal */ |
---|
3071 | n/a | /******************************************************************************/ |
---|
3072 | n/a | |
---|
3073 | n/a | static PyObject * |
---|
3074 | n/a | unicode_fromascii(const char *s, Py_ssize_t size) |
---|
3075 | n/a | { |
---|
3076 | n/a | PyObject *res; |
---|
3077 | n/a | |
---|
3078 | n/a | res = PyUnicode_New(size, 127); |
---|
3079 | n/a | if (res == NULL) { |
---|
3080 | n/a | return NULL; |
---|
3081 | n/a | } |
---|
3082 | n/a | |
---|
3083 | n/a | memcpy(PyUnicode_1BYTE_DATA(res), s, size); |
---|
3084 | n/a | return res; |
---|
3085 | n/a | } |
---|
3086 | n/a | |
---|
3087 | n/a | /* PyDecObject as a string. The default module context is only used for |
---|
3088 | n/a | the value of 'capitals'. */ |
---|
3089 | n/a | static PyObject * |
---|
3090 | n/a | dec_str(PyObject *dec) |
---|
3091 | n/a | { |
---|
3092 | n/a | PyObject *res, *context; |
---|
3093 | n/a | mpd_ssize_t size; |
---|
3094 | n/a | char *cp; |
---|
3095 | n/a | |
---|
3096 | n/a | CURRENT_CONTEXT(context); |
---|
3097 | n/a | size = mpd_to_sci_size(&cp, MPD(dec), CtxCaps(context)); |
---|
3098 | n/a | if (size < 0) { |
---|
3099 | n/a | PyErr_NoMemory(); |
---|
3100 | n/a | return NULL; |
---|
3101 | n/a | } |
---|
3102 | n/a | |
---|
3103 | n/a | res = unicode_fromascii(cp, size); |
---|
3104 | n/a | mpd_free(cp); |
---|
3105 | n/a | return res; |
---|
3106 | n/a | } |
---|
3107 | n/a | |
---|
3108 | n/a | /* Representation of a PyDecObject. */ |
---|
3109 | n/a | static PyObject * |
---|
3110 | n/a | dec_repr(PyObject *dec) |
---|
3111 | n/a | { |
---|
3112 | n/a | PyObject *res, *context; |
---|
3113 | n/a | char *cp; |
---|
3114 | n/a | |
---|
3115 | n/a | CURRENT_CONTEXT(context); |
---|
3116 | n/a | cp = mpd_to_sci(MPD(dec), CtxCaps(context)); |
---|
3117 | n/a | if (cp == NULL) { |
---|
3118 | n/a | PyErr_NoMemory(); |
---|
3119 | n/a | return NULL; |
---|
3120 | n/a | } |
---|
3121 | n/a | |
---|
3122 | n/a | res = PyUnicode_FromFormat("Decimal('%s')", cp); |
---|
3123 | n/a | mpd_free(cp); |
---|
3124 | n/a | return res; |
---|
3125 | n/a | } |
---|
3126 | n/a | |
---|
3127 | n/a | /* Return a duplicate of src, copy embedded null characters. */ |
---|
3128 | n/a | static char * |
---|
3129 | n/a | dec_strdup(const char *src, Py_ssize_t size) |
---|
3130 | n/a | { |
---|
3131 | n/a | char *dest = PyMem_Malloc(size+1); |
---|
3132 | n/a | if (dest == NULL) { |
---|
3133 | n/a | PyErr_NoMemory(); |
---|
3134 | n/a | return NULL; |
---|
3135 | n/a | } |
---|
3136 | n/a | |
---|
3137 | n/a | memcpy(dest, src, size); |
---|
3138 | n/a | dest[size] = '\0'; |
---|
3139 | n/a | return dest; |
---|
3140 | n/a | } |
---|
3141 | n/a | |
---|
3142 | n/a | static void |
---|
3143 | n/a | dec_replace_fillchar(char *dest) |
---|
3144 | n/a | { |
---|
3145 | n/a | while (*dest != '\0') { |
---|
3146 | n/a | if (*dest == '\xff') *dest = '\0'; |
---|
3147 | n/a | dest++; |
---|
3148 | n/a | } |
---|
3149 | n/a | } |
---|
3150 | n/a | |
---|
3151 | n/a | /* Convert decimal_point or thousands_sep, which may be multibyte or in |
---|
3152 | n/a | the range [128, 255], to a UTF8 string. */ |
---|
3153 | n/a | static PyObject * |
---|
3154 | n/a | dotsep_as_utf8(const char *s) |
---|
3155 | n/a | { |
---|
3156 | n/a | PyObject *utf8; |
---|
3157 | n/a | PyObject *tmp; |
---|
3158 | n/a | wchar_t buf[2]; |
---|
3159 | n/a | size_t n; |
---|
3160 | n/a | |
---|
3161 | n/a | n = mbstowcs(buf, s, 2); |
---|
3162 | n/a | if (n != 1) { /* Issue #7442 */ |
---|
3163 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
3164 | n/a | "invalid decimal point or unsupported " |
---|
3165 | n/a | "combination of LC_CTYPE and LC_NUMERIC"); |
---|
3166 | n/a | return NULL; |
---|
3167 | n/a | } |
---|
3168 | n/a | tmp = PyUnicode_FromWideChar(buf, n); |
---|
3169 | n/a | if (tmp == NULL) { |
---|
3170 | n/a | return NULL; |
---|
3171 | n/a | } |
---|
3172 | n/a | utf8 = PyUnicode_AsUTF8String(tmp); |
---|
3173 | n/a | Py_DECREF(tmp); |
---|
3174 | n/a | return utf8; |
---|
3175 | n/a | } |
---|
3176 | n/a | |
---|
3177 | n/a | /* Formatted representation of a PyDecObject. */ |
---|
3178 | n/a | static PyObject * |
---|
3179 | n/a | dec_format(PyObject *dec, PyObject *args) |
---|
3180 | n/a | { |
---|
3181 | n/a | PyObject *result = NULL; |
---|
3182 | n/a | PyObject *override = NULL; |
---|
3183 | n/a | PyObject *dot = NULL; |
---|
3184 | n/a | PyObject *sep = NULL; |
---|
3185 | n/a | PyObject *grouping = NULL; |
---|
3186 | n/a | PyObject *fmtarg; |
---|
3187 | n/a | PyObject *context; |
---|
3188 | n/a | mpd_spec_t spec; |
---|
3189 | n/a | char *fmt; |
---|
3190 | n/a | char *decstring = NULL; |
---|
3191 | n/a | uint32_t status = 0; |
---|
3192 | n/a | int replace_fillchar = 0; |
---|
3193 | n/a | Py_ssize_t size; |
---|
3194 | n/a | |
---|
3195 | n/a | |
---|
3196 | n/a | CURRENT_CONTEXT(context); |
---|
3197 | n/a | if (!PyArg_ParseTuple(args, "O|O", &fmtarg, &override)) { |
---|
3198 | n/a | return NULL; |
---|
3199 | n/a | } |
---|
3200 | n/a | |
---|
3201 | n/a | if (PyUnicode_Check(fmtarg)) { |
---|
3202 | n/a | fmt = (char *)PyUnicode_AsUTF8AndSize(fmtarg, &size); |
---|
3203 | n/a | if (fmt == NULL) { |
---|
3204 | n/a | return NULL; |
---|
3205 | n/a | } |
---|
3206 | n/a | if (size > 0 && fmt[0] == '\0') { |
---|
3207 | n/a | /* NUL fill character: must be replaced with a valid UTF-8 char |
---|
3208 | n/a | before calling mpd_parse_fmt_str(). */ |
---|
3209 | n/a | replace_fillchar = 1; |
---|
3210 | n/a | fmt = dec_strdup(fmt, size); |
---|
3211 | n/a | if (fmt == NULL) { |
---|
3212 | n/a | return NULL; |
---|
3213 | n/a | } |
---|
3214 | n/a | fmt[0] = '_'; |
---|
3215 | n/a | } |
---|
3216 | n/a | } |
---|
3217 | n/a | else { |
---|
3218 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
3219 | n/a | "format arg must be str"); |
---|
3220 | n/a | return NULL; |
---|
3221 | n/a | } |
---|
3222 | n/a | |
---|
3223 | n/a | if (!mpd_parse_fmt_str(&spec, fmt, CtxCaps(context))) { |
---|
3224 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
3225 | n/a | "invalid format string"); |
---|
3226 | n/a | goto finish; |
---|
3227 | n/a | } |
---|
3228 | n/a | if (replace_fillchar) { |
---|
3229 | n/a | /* In order to avoid clobbering parts of UTF-8 thousands separators or |
---|
3230 | n/a | decimal points when the substitution is reversed later, the actual |
---|
3231 | n/a | placeholder must be an invalid UTF-8 byte. */ |
---|
3232 | n/a | spec.fill[0] = '\xff'; |
---|
3233 | n/a | spec.fill[1] = '\0'; |
---|
3234 | n/a | } |
---|
3235 | n/a | |
---|
3236 | n/a | if (override) { |
---|
3237 | n/a | /* Values for decimal_point, thousands_sep and grouping can |
---|
3238 | n/a | be explicitly specified in the override dict. These values |
---|
3239 | n/a | take precedence over the values obtained from localeconv() |
---|
3240 | n/a | in mpd_parse_fmt_str(). The feature is not documented and |
---|
3241 | n/a | is only used in test_decimal. */ |
---|
3242 | n/a | if (!PyDict_Check(override)) { |
---|
3243 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
3244 | n/a | "optional argument must be a dict"); |
---|
3245 | n/a | goto finish; |
---|
3246 | n/a | } |
---|
3247 | n/a | if ((dot = PyDict_GetItemString(override, "decimal_point"))) { |
---|
3248 | n/a | if ((dot = PyUnicode_AsUTF8String(dot)) == NULL) { |
---|
3249 | n/a | goto finish; |
---|
3250 | n/a | } |
---|
3251 | n/a | spec.dot = PyBytes_AS_STRING(dot); |
---|
3252 | n/a | } |
---|
3253 | n/a | if ((sep = PyDict_GetItemString(override, "thousands_sep"))) { |
---|
3254 | n/a | if ((sep = PyUnicode_AsUTF8String(sep)) == NULL) { |
---|
3255 | n/a | goto finish; |
---|
3256 | n/a | } |
---|
3257 | n/a | spec.sep = PyBytes_AS_STRING(sep); |
---|
3258 | n/a | } |
---|
3259 | n/a | if ((grouping = PyDict_GetItemString(override, "grouping"))) { |
---|
3260 | n/a | if ((grouping = PyUnicode_AsUTF8String(grouping)) == NULL) { |
---|
3261 | n/a | goto finish; |
---|
3262 | n/a | } |
---|
3263 | n/a | spec.grouping = PyBytes_AS_STRING(grouping); |
---|
3264 | n/a | } |
---|
3265 | n/a | if (mpd_validate_lconv(&spec) < 0) { |
---|
3266 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
3267 | n/a | "invalid override dict"); |
---|
3268 | n/a | goto finish; |
---|
3269 | n/a | } |
---|
3270 | n/a | } |
---|
3271 | n/a | else { |
---|
3272 | n/a | size_t n = strlen(spec.dot); |
---|
3273 | n/a | if (n > 1 || (n == 1 && !isascii((uchar)spec.dot[0]))) { |
---|
3274 | n/a | /* fix locale dependent non-ascii characters */ |
---|
3275 | n/a | dot = dotsep_as_utf8(spec.dot); |
---|
3276 | n/a | if (dot == NULL) { |
---|
3277 | n/a | goto finish; |
---|
3278 | n/a | } |
---|
3279 | n/a | spec.dot = PyBytes_AS_STRING(dot); |
---|
3280 | n/a | } |
---|
3281 | n/a | n = strlen(spec.sep); |
---|
3282 | n/a | if (n > 1 || (n == 1 && !isascii((uchar)spec.sep[0]))) { |
---|
3283 | n/a | /* fix locale dependent non-ascii characters */ |
---|
3284 | n/a | sep = dotsep_as_utf8(spec.sep); |
---|
3285 | n/a | if (sep == NULL) { |
---|
3286 | n/a | goto finish; |
---|
3287 | n/a | } |
---|
3288 | n/a | spec.sep = PyBytes_AS_STRING(sep); |
---|
3289 | n/a | } |
---|
3290 | n/a | } |
---|
3291 | n/a | |
---|
3292 | n/a | |
---|
3293 | n/a | decstring = mpd_qformat_spec(MPD(dec), &spec, CTX(context), &status); |
---|
3294 | n/a | if (decstring == NULL) { |
---|
3295 | n/a | if (status & MPD_Malloc_error) { |
---|
3296 | n/a | PyErr_NoMemory(); |
---|
3297 | n/a | } |
---|
3298 | n/a | else { |
---|
3299 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
3300 | n/a | "format specification exceeds internal limits of _decimal"); |
---|
3301 | n/a | } |
---|
3302 | n/a | goto finish; |
---|
3303 | n/a | } |
---|
3304 | n/a | size = strlen(decstring); |
---|
3305 | n/a | if (replace_fillchar) { |
---|
3306 | n/a | dec_replace_fillchar(decstring); |
---|
3307 | n/a | } |
---|
3308 | n/a | |
---|
3309 | n/a | result = PyUnicode_DecodeUTF8(decstring, size, NULL); |
---|
3310 | n/a | |
---|
3311 | n/a | |
---|
3312 | n/a | finish: |
---|
3313 | n/a | Py_XDECREF(grouping); |
---|
3314 | n/a | Py_XDECREF(sep); |
---|
3315 | n/a | Py_XDECREF(dot); |
---|
3316 | n/a | if (replace_fillchar) PyMem_Free(fmt); |
---|
3317 | n/a | if (decstring) mpd_free(decstring); |
---|
3318 | n/a | return result; |
---|
3319 | n/a | } |
---|
3320 | n/a | |
---|
3321 | n/a | /* Return a PyLongObject from a PyDecObject, using the specified rounding |
---|
3322 | n/a | * mode. The context precision is not observed. */ |
---|
3323 | n/a | static PyObject * |
---|
3324 | n/a | dec_as_long(PyObject *dec, PyObject *context, int round) |
---|
3325 | n/a | { |
---|
3326 | n/a | PyLongObject *pylong; |
---|
3327 | n/a | digit *ob_digit; |
---|
3328 | n/a | size_t n; |
---|
3329 | n/a | Py_ssize_t i; |
---|
3330 | n/a | mpd_t *x; |
---|
3331 | n/a | mpd_context_t workctx; |
---|
3332 | n/a | uint32_t status = 0; |
---|
3333 | n/a | |
---|
3334 | n/a | if (mpd_isspecial(MPD(dec))) { |
---|
3335 | n/a | if (mpd_isnan(MPD(dec))) { |
---|
3336 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
3337 | n/a | "cannot convert NaN to integer"); |
---|
3338 | n/a | } |
---|
3339 | n/a | else { |
---|
3340 | n/a | PyErr_SetString(PyExc_OverflowError, |
---|
3341 | n/a | "cannot convert Infinity to integer"); |
---|
3342 | n/a | } |
---|
3343 | n/a | return NULL; |
---|
3344 | n/a | } |
---|
3345 | n/a | |
---|
3346 | n/a | x = mpd_qnew(); |
---|
3347 | n/a | if (x == NULL) { |
---|
3348 | n/a | PyErr_NoMemory(); |
---|
3349 | n/a | return NULL; |
---|
3350 | n/a | } |
---|
3351 | n/a | workctx = *CTX(context); |
---|
3352 | n/a | workctx.round = round; |
---|
3353 | n/a | mpd_qround_to_int(x, MPD(dec), &workctx, &status); |
---|
3354 | n/a | if (dec_addstatus(context, status)) { |
---|
3355 | n/a | mpd_del(x); |
---|
3356 | n/a | return NULL; |
---|
3357 | n/a | } |
---|
3358 | n/a | |
---|
3359 | n/a | status = 0; |
---|
3360 | n/a | ob_digit = NULL; |
---|
3361 | n/a | #if PYLONG_BITS_IN_DIGIT == 30 |
---|
3362 | n/a | n = mpd_qexport_u32(&ob_digit, 0, PyLong_BASE, x, &status); |
---|
3363 | n/a | #elif PYLONG_BITS_IN_DIGIT == 15 |
---|
3364 | n/a | n = mpd_qexport_u16(&ob_digit, 0, PyLong_BASE, x, &status); |
---|
3365 | n/a | #else |
---|
3366 | n/a | #error "PYLONG_BITS_IN_DIGIT should be 15 or 30" |
---|
3367 | n/a | #endif |
---|
3368 | n/a | |
---|
3369 | n/a | if (n == SIZE_MAX) { |
---|
3370 | n/a | PyErr_NoMemory(); |
---|
3371 | n/a | mpd_del(x); |
---|
3372 | n/a | return NULL; |
---|
3373 | n/a | } |
---|
3374 | n/a | |
---|
3375 | n/a | assert(n > 0); |
---|
3376 | n/a | pylong = _PyLong_New(n); |
---|
3377 | n/a | if (pylong == NULL) { |
---|
3378 | n/a | mpd_free(ob_digit); |
---|
3379 | n/a | mpd_del(x); |
---|
3380 | n/a | return NULL; |
---|
3381 | n/a | } |
---|
3382 | n/a | |
---|
3383 | n/a | memcpy(pylong->ob_digit, ob_digit, n * sizeof(digit)); |
---|
3384 | n/a | mpd_free(ob_digit); |
---|
3385 | n/a | |
---|
3386 | n/a | i = n; |
---|
3387 | n/a | while ((i > 0) && (pylong->ob_digit[i-1] == 0)) { |
---|
3388 | n/a | i--; |
---|
3389 | n/a | } |
---|
3390 | n/a | |
---|
3391 | n/a | Py_SIZE(pylong) = i; |
---|
3392 | n/a | if (mpd_isnegative(x) && !mpd_iszero(x)) { |
---|
3393 | n/a | Py_SIZE(pylong) = -i; |
---|
3394 | n/a | } |
---|
3395 | n/a | |
---|
3396 | n/a | mpd_del(x); |
---|
3397 | n/a | return (PyObject *) pylong; |
---|
3398 | n/a | } |
---|
3399 | n/a | |
---|
3400 | n/a | /* Convert a Decimal to its exact integer ratio representation. */ |
---|
3401 | n/a | static PyObject * |
---|
3402 | n/a | dec_as_integer_ratio(PyObject *self, PyObject *args UNUSED) |
---|
3403 | n/a | { |
---|
3404 | n/a | PyObject *numerator = NULL; |
---|
3405 | n/a | PyObject *denominator = NULL; |
---|
3406 | n/a | PyObject *exponent = NULL; |
---|
3407 | n/a | PyObject *result = NULL; |
---|
3408 | n/a | PyObject *tmp; |
---|
3409 | n/a | mpd_ssize_t exp; |
---|
3410 | n/a | PyObject *context; |
---|
3411 | n/a | uint32_t status = 0; |
---|
3412 | n/a | |
---|
3413 | n/a | if (mpd_isspecial(MPD(self))) { |
---|
3414 | n/a | if (mpd_isnan(MPD(self))) { |
---|
3415 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
3416 | n/a | "cannot convert NaN to integer ratio"); |
---|
3417 | n/a | } |
---|
3418 | n/a | else { |
---|
3419 | n/a | PyErr_SetString(PyExc_OverflowError, |
---|
3420 | n/a | "cannot convert Infinity to integer ratio"); |
---|
3421 | n/a | } |
---|
3422 | n/a | return NULL; |
---|
3423 | n/a | } |
---|
3424 | n/a | |
---|
3425 | n/a | CURRENT_CONTEXT(context); |
---|
3426 | n/a | |
---|
3427 | n/a | tmp = dec_alloc(); |
---|
3428 | n/a | if (tmp == NULL) { |
---|
3429 | n/a | return NULL; |
---|
3430 | n/a | } |
---|
3431 | n/a | |
---|
3432 | n/a | if (!mpd_qcopy(MPD(tmp), MPD(self), &status)) { |
---|
3433 | n/a | Py_DECREF(tmp); |
---|
3434 | n/a | PyErr_NoMemory(); |
---|
3435 | n/a | return NULL; |
---|
3436 | n/a | } |
---|
3437 | n/a | |
---|
3438 | n/a | exp = mpd_iszero(MPD(tmp)) ? 0 : MPD(tmp)->exp; |
---|
3439 | n/a | MPD(tmp)->exp = 0; |
---|
3440 | n/a | |
---|
3441 | n/a | /* context and rounding are unused here: the conversion is exact */ |
---|
3442 | n/a | numerator = dec_as_long(tmp, context, MPD_ROUND_FLOOR); |
---|
3443 | n/a | Py_DECREF(tmp); |
---|
3444 | n/a | if (numerator == NULL) { |
---|
3445 | n/a | goto error; |
---|
3446 | n/a | } |
---|
3447 | n/a | |
---|
3448 | n/a | exponent = PyLong_FromSsize_t(exp < 0 ? -exp : exp); |
---|
3449 | n/a | if (exponent == NULL) { |
---|
3450 | n/a | goto error; |
---|
3451 | n/a | } |
---|
3452 | n/a | |
---|
3453 | n/a | tmp = PyLong_FromLong(10); |
---|
3454 | n/a | if (tmp == NULL) { |
---|
3455 | n/a | goto error; |
---|
3456 | n/a | } |
---|
3457 | n/a | |
---|
3458 | n/a | Py_SETREF(exponent, _py_long_power(tmp, exponent, Py_None)); |
---|
3459 | n/a | Py_DECREF(tmp); |
---|
3460 | n/a | if (exponent == NULL) { |
---|
3461 | n/a | goto error; |
---|
3462 | n/a | } |
---|
3463 | n/a | |
---|
3464 | n/a | if (exp >= 0) { |
---|
3465 | n/a | Py_SETREF(numerator, _py_long_multiply(numerator, exponent)); |
---|
3466 | n/a | if (numerator == NULL) { |
---|
3467 | n/a | goto error; |
---|
3468 | n/a | } |
---|
3469 | n/a | denominator = PyLong_FromLong(1); |
---|
3470 | n/a | if (denominator == NULL) { |
---|
3471 | n/a | goto error; |
---|
3472 | n/a | } |
---|
3473 | n/a | } |
---|
3474 | n/a | else { |
---|
3475 | n/a | denominator = exponent; |
---|
3476 | n/a | exponent = NULL; |
---|
3477 | n/a | tmp = _PyLong_GCD(numerator, denominator); |
---|
3478 | n/a | if (tmp == NULL) { |
---|
3479 | n/a | goto error; |
---|
3480 | n/a | } |
---|
3481 | n/a | Py_SETREF(numerator, _py_long_floor_divide(numerator, tmp)); |
---|
3482 | n/a | Py_SETREF(denominator, _py_long_floor_divide(denominator, tmp)); |
---|
3483 | n/a | Py_DECREF(tmp); |
---|
3484 | n/a | if (numerator == NULL || denominator == NULL) { |
---|
3485 | n/a | goto error; |
---|
3486 | n/a | } |
---|
3487 | n/a | } |
---|
3488 | n/a | |
---|
3489 | n/a | result = PyTuple_Pack(2, numerator, denominator); |
---|
3490 | n/a | |
---|
3491 | n/a | |
---|
3492 | n/a | error: |
---|
3493 | n/a | Py_XDECREF(exponent); |
---|
3494 | n/a | Py_XDECREF(denominator); |
---|
3495 | n/a | Py_XDECREF(numerator); |
---|
3496 | n/a | return result; |
---|
3497 | n/a | } |
---|
3498 | n/a | |
---|
3499 | n/a | static PyObject * |
---|
3500 | n/a | PyDec_ToIntegralValue(PyObject *dec, PyObject *args, PyObject *kwds) |
---|
3501 | n/a | { |
---|
3502 | n/a | static char *kwlist[] = {"rounding", "context", NULL}; |
---|
3503 | n/a | PyObject *result; |
---|
3504 | n/a | PyObject *rounding = Py_None; |
---|
3505 | n/a | PyObject *context = Py_None; |
---|
3506 | n/a | uint32_t status = 0; |
---|
3507 | n/a | mpd_context_t workctx; |
---|
3508 | n/a | |
---|
3509 | n/a | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO", kwlist, |
---|
3510 | n/a | &rounding, &context)) { |
---|
3511 | n/a | return NULL; |
---|
3512 | n/a | } |
---|
3513 | n/a | CONTEXT_CHECK_VA(context); |
---|
3514 | n/a | |
---|
3515 | n/a | workctx = *CTX(context); |
---|
3516 | n/a | if (rounding != Py_None) { |
---|
3517 | n/a | int round = getround(rounding); |
---|
3518 | n/a | if (round < 0) { |
---|
3519 | n/a | return NULL; |
---|
3520 | n/a | } |
---|
3521 | n/a | if (!mpd_qsetround(&workctx, round)) { |
---|
3522 | n/a | INTERNAL_ERROR_PTR("PyDec_ToIntegralValue"); /* GCOV_NOT_REACHED */ |
---|
3523 | n/a | } |
---|
3524 | n/a | } |
---|
3525 | n/a | |
---|
3526 | n/a | result = dec_alloc(); |
---|
3527 | n/a | if (result == NULL) { |
---|
3528 | n/a | return NULL; |
---|
3529 | n/a | } |
---|
3530 | n/a | |
---|
3531 | n/a | mpd_qround_to_int(MPD(result), MPD(dec), &workctx, &status); |
---|
3532 | n/a | if (dec_addstatus(context, status)) { |
---|
3533 | n/a | Py_DECREF(result); |
---|
3534 | n/a | return NULL; |
---|
3535 | n/a | } |
---|
3536 | n/a | |
---|
3537 | n/a | return result; |
---|
3538 | n/a | } |
---|
3539 | n/a | |
---|
3540 | n/a | static PyObject * |
---|
3541 | n/a | PyDec_ToIntegralExact(PyObject *dec, PyObject *args, PyObject *kwds) |
---|
3542 | n/a | { |
---|
3543 | n/a | static char *kwlist[] = {"rounding", "context", NULL}; |
---|
3544 | n/a | PyObject *result; |
---|
3545 | n/a | PyObject *rounding = Py_None; |
---|
3546 | n/a | PyObject *context = Py_None; |
---|
3547 | n/a | uint32_t status = 0; |
---|
3548 | n/a | mpd_context_t workctx; |
---|
3549 | n/a | |
---|
3550 | n/a | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO", kwlist, |
---|
3551 | n/a | &rounding, &context)) { |
---|
3552 | n/a | return NULL; |
---|
3553 | n/a | } |
---|
3554 | n/a | CONTEXT_CHECK_VA(context); |
---|
3555 | n/a | |
---|
3556 | n/a | workctx = *CTX(context); |
---|
3557 | n/a | if (rounding != Py_None) { |
---|
3558 | n/a | int round = getround(rounding); |
---|
3559 | n/a | if (round < 0) { |
---|
3560 | n/a | return NULL; |
---|
3561 | n/a | } |
---|
3562 | n/a | if (!mpd_qsetround(&workctx, round)) { |
---|
3563 | n/a | INTERNAL_ERROR_PTR("PyDec_ToIntegralExact"); /* GCOV_NOT_REACHED */ |
---|
3564 | n/a | } |
---|
3565 | n/a | } |
---|
3566 | n/a | |
---|
3567 | n/a | result = dec_alloc(); |
---|
3568 | n/a | if (result == NULL) { |
---|
3569 | n/a | return NULL; |
---|
3570 | n/a | } |
---|
3571 | n/a | |
---|
3572 | n/a | mpd_qround_to_intx(MPD(result), MPD(dec), &workctx, &status); |
---|
3573 | n/a | if (dec_addstatus(context, status)) { |
---|
3574 | n/a | Py_DECREF(result); |
---|
3575 | n/a | return NULL; |
---|
3576 | n/a | } |
---|
3577 | n/a | |
---|
3578 | n/a | return result; |
---|
3579 | n/a | } |
---|
3580 | n/a | |
---|
3581 | n/a | static PyObject * |
---|
3582 | n/a | PyDec_AsFloat(PyObject *dec) |
---|
3583 | n/a | { |
---|
3584 | n/a | PyObject *f, *s; |
---|
3585 | n/a | |
---|
3586 | n/a | if (mpd_isnan(MPD(dec))) { |
---|
3587 | n/a | if (mpd_issnan(MPD(dec))) { |
---|
3588 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
3589 | n/a | "cannot convert signaling NaN to float"); |
---|
3590 | n/a | return NULL; |
---|
3591 | n/a | } |
---|
3592 | n/a | if (mpd_isnegative(MPD(dec))) { |
---|
3593 | n/a | s = PyUnicode_FromString("-nan"); |
---|
3594 | n/a | } |
---|
3595 | n/a | else { |
---|
3596 | n/a | s = PyUnicode_FromString("nan"); |
---|
3597 | n/a | } |
---|
3598 | n/a | } |
---|
3599 | n/a | else { |
---|
3600 | n/a | s = dec_str(dec); |
---|
3601 | n/a | } |
---|
3602 | n/a | |
---|
3603 | n/a | if (s == NULL) { |
---|
3604 | n/a | return NULL; |
---|
3605 | n/a | } |
---|
3606 | n/a | |
---|
3607 | n/a | f = PyFloat_FromString(s); |
---|
3608 | n/a | Py_DECREF(s); |
---|
3609 | n/a | |
---|
3610 | n/a | return f; |
---|
3611 | n/a | } |
---|
3612 | n/a | |
---|
3613 | n/a | static PyObject * |
---|
3614 | n/a | PyDec_Round(PyObject *dec, PyObject *args) |
---|
3615 | n/a | { |
---|
3616 | n/a | PyObject *result; |
---|
3617 | n/a | PyObject *x = NULL; |
---|
3618 | n/a | uint32_t status = 0; |
---|
3619 | n/a | PyObject *context; |
---|
3620 | n/a | |
---|
3621 | n/a | |
---|
3622 | n/a | CURRENT_CONTEXT(context); |
---|
3623 | n/a | if (!PyArg_ParseTuple(args, "|O", &x)) { |
---|
3624 | n/a | return NULL; |
---|
3625 | n/a | } |
---|
3626 | n/a | |
---|
3627 | n/a | if (x) { |
---|
3628 | n/a | mpd_uint_t dq[1] = {1}; |
---|
3629 | n/a | mpd_t q = {MPD_STATIC|MPD_CONST_DATA,0,1,1,1,dq}; |
---|
3630 | n/a | mpd_ssize_t y; |
---|
3631 | n/a | |
---|
3632 | n/a | if (!PyLong_Check(x)) { |
---|
3633 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
3634 | n/a | "optional arg must be an integer"); |
---|
3635 | n/a | return NULL; |
---|
3636 | n/a | } |
---|
3637 | n/a | |
---|
3638 | n/a | y = PyLong_AsSsize_t(x); |
---|
3639 | n/a | if (y == -1 && PyErr_Occurred()) { |
---|
3640 | n/a | return NULL; |
---|
3641 | n/a | } |
---|
3642 | n/a | result = dec_alloc(); |
---|
3643 | n/a | if (result == NULL) { |
---|
3644 | n/a | return NULL; |
---|
3645 | n/a | } |
---|
3646 | n/a | |
---|
3647 | n/a | q.exp = (y == MPD_SSIZE_MIN) ? MPD_SSIZE_MAX : -y; |
---|
3648 | n/a | mpd_qquantize(MPD(result), MPD(dec), &q, CTX(context), &status); |
---|
3649 | n/a | if (dec_addstatus(context, status)) { |
---|
3650 | n/a | Py_DECREF(result); |
---|
3651 | n/a | return NULL; |
---|
3652 | n/a | } |
---|
3653 | n/a | |
---|
3654 | n/a | return result; |
---|
3655 | n/a | } |
---|
3656 | n/a | else { |
---|
3657 | n/a | return dec_as_long(dec, context, MPD_ROUND_HALF_EVEN); |
---|
3658 | n/a | } |
---|
3659 | n/a | } |
---|
3660 | n/a | |
---|
3661 | n/a | static PyTypeObject *DecimalTuple = NULL; |
---|
3662 | n/a | /* Return the DecimalTuple representation of a PyDecObject. */ |
---|
3663 | n/a | static PyObject * |
---|
3664 | n/a | PyDec_AsTuple(PyObject *dec, PyObject *dummy UNUSED) |
---|
3665 | n/a | { |
---|
3666 | n/a | PyObject *result = NULL; |
---|
3667 | n/a | PyObject *sign = NULL; |
---|
3668 | n/a | PyObject *coeff = NULL; |
---|
3669 | n/a | PyObject *expt = NULL; |
---|
3670 | n/a | PyObject *tmp = NULL; |
---|
3671 | n/a | mpd_t *x = NULL; |
---|
3672 | n/a | char *intstring = NULL; |
---|
3673 | n/a | Py_ssize_t intlen, i; |
---|
3674 | n/a | |
---|
3675 | n/a | |
---|
3676 | n/a | x = mpd_qncopy(MPD(dec)); |
---|
3677 | n/a | if (x == NULL) { |
---|
3678 | n/a | PyErr_NoMemory(); |
---|
3679 | n/a | goto out; |
---|
3680 | n/a | } |
---|
3681 | n/a | |
---|
3682 | n/a | sign = PyLong_FromUnsignedLong(mpd_sign(MPD(dec))); |
---|
3683 | n/a | if (sign == NULL) { |
---|
3684 | n/a | goto out; |
---|
3685 | n/a | } |
---|
3686 | n/a | |
---|
3687 | n/a | if (mpd_isinfinite(x)) { |
---|
3688 | n/a | expt = PyUnicode_FromString("F"); |
---|
3689 | n/a | if (expt == NULL) { |
---|
3690 | n/a | goto out; |
---|
3691 | n/a | } |
---|
3692 | n/a | /* decimal.py has non-compliant infinity payloads. */ |
---|
3693 | n/a | coeff = Py_BuildValue("(i)", 0); |
---|
3694 | n/a | if (coeff == NULL) { |
---|
3695 | n/a | goto out; |
---|
3696 | n/a | } |
---|
3697 | n/a | } |
---|
3698 | n/a | else { |
---|
3699 | n/a | if (mpd_isnan(x)) { |
---|
3700 | n/a | expt = PyUnicode_FromString(mpd_isqnan(x)?"n":"N"); |
---|
3701 | n/a | } |
---|
3702 | n/a | else { |
---|
3703 | n/a | expt = PyLong_FromSsize_t(MPD(dec)->exp); |
---|
3704 | n/a | } |
---|
3705 | n/a | if (expt == NULL) { |
---|
3706 | n/a | goto out; |
---|
3707 | n/a | } |
---|
3708 | n/a | |
---|
3709 | n/a | /* coefficient is defined */ |
---|
3710 | n/a | if (x->len > 0) { |
---|
3711 | n/a | |
---|
3712 | n/a | /* make an integer */ |
---|
3713 | n/a | x->exp = 0; |
---|
3714 | n/a | /* clear NaN and sign */ |
---|
3715 | n/a | mpd_clear_flags(x); |
---|
3716 | n/a | intstring = mpd_to_sci(x, 1); |
---|
3717 | n/a | if (intstring == NULL) { |
---|
3718 | n/a | PyErr_NoMemory(); |
---|
3719 | n/a | goto out; |
---|
3720 | n/a | } |
---|
3721 | n/a | |
---|
3722 | n/a | intlen = strlen(intstring); |
---|
3723 | n/a | coeff = PyTuple_New(intlen); |
---|
3724 | n/a | if (coeff == NULL) { |
---|
3725 | n/a | goto out; |
---|
3726 | n/a | } |
---|
3727 | n/a | |
---|
3728 | n/a | for (i = 0; i < intlen; i++) { |
---|
3729 | n/a | tmp = PyLong_FromLong(intstring[i]-'0'); |
---|
3730 | n/a | if (tmp == NULL) { |
---|
3731 | n/a | goto out; |
---|
3732 | n/a | } |
---|
3733 | n/a | PyTuple_SET_ITEM(coeff, i, tmp); |
---|
3734 | n/a | } |
---|
3735 | n/a | } |
---|
3736 | n/a | else { |
---|
3737 | n/a | coeff = PyTuple_New(0); |
---|
3738 | n/a | if (coeff == NULL) { |
---|
3739 | n/a | goto out; |
---|
3740 | n/a | } |
---|
3741 | n/a | } |
---|
3742 | n/a | } |
---|
3743 | n/a | |
---|
3744 | n/a | result = PyObject_CallFunctionObjArgs((PyObject *)DecimalTuple, |
---|
3745 | n/a | sign, coeff, expt, NULL); |
---|
3746 | n/a | |
---|
3747 | n/a | out: |
---|
3748 | n/a | if (x) mpd_del(x); |
---|
3749 | n/a | if (intstring) mpd_free(intstring); |
---|
3750 | n/a | Py_XDECREF(sign); |
---|
3751 | n/a | Py_XDECREF(coeff); |
---|
3752 | n/a | Py_XDECREF(expt); |
---|
3753 | n/a | return result; |
---|
3754 | n/a | } |
---|
3755 | n/a | |
---|
3756 | n/a | |
---|
3757 | n/a | /******************************************************************************/ |
---|
3758 | n/a | /* Macros for converting mpdecimal functions to Decimal methods */ |
---|
3759 | n/a | /******************************************************************************/ |
---|
3760 | n/a | |
---|
3761 | n/a | /* Unary number method that uses the default module context. */ |
---|
3762 | n/a | #define Dec_UnaryNumberMethod(MPDFUNC) \ |
---|
3763 | n/a | static PyObject * \ |
---|
3764 | n/a | nm_##MPDFUNC(PyObject *self) \ |
---|
3765 | n/a | { \ |
---|
3766 | n/a | PyObject *result; \ |
---|
3767 | n/a | PyObject *context; \ |
---|
3768 | n/a | uint32_t status = 0; \ |
---|
3769 | n/a | \ |
---|
3770 | n/a | CURRENT_CONTEXT(context); \ |
---|
3771 | n/a | if ((result = dec_alloc()) == NULL) { \ |
---|
3772 | n/a | return NULL; \ |
---|
3773 | n/a | } \ |
---|
3774 | n/a | \ |
---|
3775 | n/a | MPDFUNC(MPD(result), MPD(self), CTX(context), &status); \ |
---|
3776 | n/a | if (dec_addstatus(context, status)) { \ |
---|
3777 | n/a | Py_DECREF(result); \ |
---|
3778 | n/a | return NULL; \ |
---|
3779 | n/a | } \ |
---|
3780 | n/a | \ |
---|
3781 | n/a | return result; \ |
---|
3782 | n/a | } |
---|
3783 | n/a | |
---|
3784 | n/a | /* Binary number method that uses default module context. */ |
---|
3785 | n/a | #define Dec_BinaryNumberMethod(MPDFUNC) \ |
---|
3786 | n/a | static PyObject * \ |
---|
3787 | n/a | nm_##MPDFUNC(PyObject *self, PyObject *other) \ |
---|
3788 | n/a | { \ |
---|
3789 | n/a | PyObject *a, *b; \ |
---|
3790 | n/a | PyObject *result; \ |
---|
3791 | n/a | PyObject *context; \ |
---|
3792 | n/a | uint32_t status = 0; \ |
---|
3793 | n/a | \ |
---|
3794 | n/a | CURRENT_CONTEXT(context) ; \ |
---|
3795 | n/a | CONVERT_BINOP(&a, &b, self, other, context); \ |
---|
3796 | n/a | \ |
---|
3797 | n/a | if ((result = dec_alloc()) == NULL) { \ |
---|
3798 | n/a | Py_DECREF(a); \ |
---|
3799 | n/a | Py_DECREF(b); \ |
---|
3800 | n/a | return NULL; \ |
---|
3801 | n/a | } \ |
---|
3802 | n/a | \ |
---|
3803 | n/a | MPDFUNC(MPD(result), MPD(a), MPD(b), CTX(context), &status); \ |
---|
3804 | n/a | Py_DECREF(a); \ |
---|
3805 | n/a | Py_DECREF(b); \ |
---|
3806 | n/a | if (dec_addstatus(context, status)) { \ |
---|
3807 | n/a | Py_DECREF(result); \ |
---|
3808 | n/a | return NULL; \ |
---|
3809 | n/a | } \ |
---|
3810 | n/a | \ |
---|
3811 | n/a | return result; \ |
---|
3812 | n/a | } |
---|
3813 | n/a | |
---|
3814 | n/a | /* Boolean function without a context arg. */ |
---|
3815 | n/a | #define Dec_BoolFunc(MPDFUNC) \ |
---|
3816 | n/a | static PyObject * \ |
---|
3817 | n/a | dec_##MPDFUNC(PyObject *self, PyObject *dummy UNUSED) \ |
---|
3818 | n/a | { \ |
---|
3819 | n/a | return MPDFUNC(MPD(self)) ? incr_true() : incr_false(); \ |
---|
3820 | n/a | } |
---|
3821 | n/a | |
---|
3822 | n/a | /* Boolean function with an optional context arg. */ |
---|
3823 | n/a | #define Dec_BoolFuncVA(MPDFUNC) \ |
---|
3824 | n/a | static PyObject * \ |
---|
3825 | n/a | dec_##MPDFUNC(PyObject *self, PyObject *args, PyObject *kwds) \ |
---|
3826 | n/a | { \ |
---|
3827 | n/a | static char *kwlist[] = {"context", NULL}; \ |
---|
3828 | n/a | PyObject *context = Py_None; \ |
---|
3829 | n/a | \ |
---|
3830 | n/a | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O", kwlist, \ |
---|
3831 | n/a | &context)) { \ |
---|
3832 | n/a | return NULL; \ |
---|
3833 | n/a | } \ |
---|
3834 | n/a | CONTEXT_CHECK_VA(context); \ |
---|
3835 | n/a | \ |
---|
3836 | n/a | return MPDFUNC(MPD(self), CTX(context)) ? incr_true() : incr_false(); \ |
---|
3837 | n/a | } |
---|
3838 | n/a | |
---|
3839 | n/a | /* Unary function with an optional context arg. */ |
---|
3840 | n/a | #define Dec_UnaryFuncVA(MPDFUNC) \ |
---|
3841 | n/a | static PyObject * \ |
---|
3842 | n/a | dec_##MPDFUNC(PyObject *self, PyObject *args, PyObject *kwds) \ |
---|
3843 | n/a | { \ |
---|
3844 | n/a | static char *kwlist[] = {"context", NULL}; \ |
---|
3845 | n/a | PyObject *result; \ |
---|
3846 | n/a | PyObject *context = Py_None; \ |
---|
3847 | n/a | uint32_t status = 0; \ |
---|
3848 | n/a | \ |
---|
3849 | n/a | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O", kwlist, \ |
---|
3850 | n/a | &context)) { \ |
---|
3851 | n/a | return NULL; \ |
---|
3852 | n/a | } \ |
---|
3853 | n/a | CONTEXT_CHECK_VA(context); \ |
---|
3854 | n/a | \ |
---|
3855 | n/a | if ((result = dec_alloc()) == NULL) { \ |
---|
3856 | n/a | return NULL; \ |
---|
3857 | n/a | } \ |
---|
3858 | n/a | \ |
---|
3859 | n/a | MPDFUNC(MPD(result), MPD(self), CTX(context), &status); \ |
---|
3860 | n/a | if (dec_addstatus(context, status)) { \ |
---|
3861 | n/a | Py_DECREF(result); \ |
---|
3862 | n/a | return NULL; \ |
---|
3863 | n/a | } \ |
---|
3864 | n/a | \ |
---|
3865 | n/a | return result; \ |
---|
3866 | n/a | } |
---|
3867 | n/a | |
---|
3868 | n/a | /* Binary function with an optional context arg. */ |
---|
3869 | n/a | #define Dec_BinaryFuncVA(MPDFUNC) \ |
---|
3870 | n/a | static PyObject * \ |
---|
3871 | n/a | dec_##MPDFUNC(PyObject *self, PyObject *args, PyObject *kwds) \ |
---|
3872 | n/a | { \ |
---|
3873 | n/a | static char *kwlist[] = {"other", "context", NULL}; \ |
---|
3874 | n/a | PyObject *other; \ |
---|
3875 | n/a | PyObject *a, *b; \ |
---|
3876 | n/a | PyObject *result; \ |
---|
3877 | n/a | PyObject *context = Py_None; \ |
---|
3878 | n/a | uint32_t status = 0; \ |
---|
3879 | n/a | \ |
---|
3880 | n/a | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O", kwlist, \ |
---|
3881 | n/a | &other, &context)) { \ |
---|
3882 | n/a | return NULL; \ |
---|
3883 | n/a | } \ |
---|
3884 | n/a | CONTEXT_CHECK_VA(context); \ |
---|
3885 | n/a | CONVERT_BINOP_RAISE(&a, &b, self, other, context); \ |
---|
3886 | n/a | \ |
---|
3887 | n/a | if ((result = dec_alloc()) == NULL) { \ |
---|
3888 | n/a | Py_DECREF(a); \ |
---|
3889 | n/a | Py_DECREF(b); \ |
---|
3890 | n/a | return NULL; \ |
---|
3891 | n/a | } \ |
---|
3892 | n/a | \ |
---|
3893 | n/a | MPDFUNC(MPD(result), MPD(a), MPD(b), CTX(context), &status); \ |
---|
3894 | n/a | Py_DECREF(a); \ |
---|
3895 | n/a | Py_DECREF(b); \ |
---|
3896 | n/a | if (dec_addstatus(context, status)) { \ |
---|
3897 | n/a | Py_DECREF(result); \ |
---|
3898 | n/a | return NULL; \ |
---|
3899 | n/a | } \ |
---|
3900 | n/a | \ |
---|
3901 | n/a | return result; \ |
---|
3902 | n/a | } |
---|
3903 | n/a | |
---|
3904 | n/a | /* Binary function with an optional context arg. Actual MPDFUNC does |
---|
3905 | n/a | NOT take a context. The context is used to record InvalidOperation |
---|
3906 | n/a | if the second operand cannot be converted exactly. */ |
---|
3907 | n/a | #define Dec_BinaryFuncVA_NO_CTX(MPDFUNC) \ |
---|
3908 | n/a | static PyObject * \ |
---|
3909 | n/a | dec_##MPDFUNC(PyObject *self, PyObject *args, PyObject *kwds) \ |
---|
3910 | n/a | { \ |
---|
3911 | n/a | static char *kwlist[] = {"other", "context", NULL}; \ |
---|
3912 | n/a | PyObject *context = Py_None; \ |
---|
3913 | n/a | PyObject *other; \ |
---|
3914 | n/a | PyObject *a, *b; \ |
---|
3915 | n/a | PyObject *result; \ |
---|
3916 | n/a | \ |
---|
3917 | n/a | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O", kwlist, \ |
---|
3918 | n/a | &other, &context)) { \ |
---|
3919 | n/a | return NULL; \ |
---|
3920 | n/a | } \ |
---|
3921 | n/a | CONTEXT_CHECK_VA(context); \ |
---|
3922 | n/a | CONVERT_BINOP_RAISE(&a, &b, self, other, context); \ |
---|
3923 | n/a | \ |
---|
3924 | n/a | if ((result = dec_alloc()) == NULL) { \ |
---|
3925 | n/a | Py_DECREF(a); \ |
---|
3926 | n/a | Py_DECREF(b); \ |
---|
3927 | n/a | return NULL; \ |
---|
3928 | n/a | } \ |
---|
3929 | n/a | \ |
---|
3930 | n/a | MPDFUNC(MPD(result), MPD(a), MPD(b)); \ |
---|
3931 | n/a | Py_DECREF(a); \ |
---|
3932 | n/a | Py_DECREF(b); \ |
---|
3933 | n/a | \ |
---|
3934 | n/a | return result; \ |
---|
3935 | n/a | } |
---|
3936 | n/a | |
---|
3937 | n/a | /* Ternary function with an optional context arg. */ |
---|
3938 | n/a | #define Dec_TernaryFuncVA(MPDFUNC) \ |
---|
3939 | n/a | static PyObject * \ |
---|
3940 | n/a | dec_##MPDFUNC(PyObject *self, PyObject *args, PyObject *kwds) \ |
---|
3941 | n/a | { \ |
---|
3942 | n/a | static char *kwlist[] = {"other", "third", "context", NULL}; \ |
---|
3943 | n/a | PyObject *other, *third; \ |
---|
3944 | n/a | PyObject *a, *b, *c; \ |
---|
3945 | n/a | PyObject *result; \ |
---|
3946 | n/a | PyObject *context = Py_None; \ |
---|
3947 | n/a | uint32_t status = 0; \ |
---|
3948 | n/a | \ |
---|
3949 | n/a | if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO|O", kwlist, \ |
---|
3950 | n/a | &other, &third, &context)) { \ |
---|
3951 | n/a | return NULL; \ |
---|
3952 | n/a | } \ |
---|
3953 | n/a | CONTEXT_CHECK_VA(context); \ |
---|
3954 | n/a | CONVERT_TERNOP_RAISE(&a, &b, &c, self, other, third, context); \ |
---|
3955 | n/a | \ |
---|
3956 | n/a | if ((result = dec_alloc()) == NULL) { \ |
---|
3957 | n/a | Py_DECREF(a); \ |
---|
3958 | n/a | Py_DECREF(b); \ |
---|
3959 | n/a | Py_DECREF(c); \ |
---|
3960 | n/a | return NULL; \ |
---|
3961 | n/a | } \ |
---|
3962 | n/a | \ |
---|
3963 | n/a | MPDFUNC(MPD(result), MPD(a), MPD(b), MPD(c), CTX(context), &status); \ |
---|
3964 | n/a | Py_DECREF(a); \ |
---|
3965 | n/a | Py_DECREF(b); \ |
---|
3966 | n/a | Py_DECREF(c); \ |
---|
3967 | n/a | if (dec_addstatus(context, status)) { \ |
---|
3968 | n/a | Py_DECREF(result); \ |
---|
3969 | n/a | return NULL; \ |
---|
3970 | n/a | } \ |
---|
3971 | n/a | \ |
---|
3972 | n/a | return result; \ |
---|
3973 | n/a | } |
---|
3974 | n/a | |
---|
3975 | n/a | |
---|
3976 | n/a | /**********************************************/ |
---|
3977 | n/a | /* Number methods */ |
---|
3978 | n/a | /**********************************************/ |
---|
3979 | n/a | |
---|
3980 | n/a | Dec_UnaryNumberMethod(mpd_qminus) |
---|
3981 | n/a | Dec_UnaryNumberMethod(mpd_qplus) |
---|
3982 | n/a | Dec_UnaryNumberMethod(mpd_qabs) |
---|
3983 | n/a | |
---|
3984 | n/a | Dec_BinaryNumberMethod(mpd_qadd) |
---|
3985 | n/a | Dec_BinaryNumberMethod(mpd_qsub) |
---|
3986 | n/a | Dec_BinaryNumberMethod(mpd_qmul) |
---|
3987 | n/a | Dec_BinaryNumberMethod(mpd_qdiv) |
---|
3988 | n/a | Dec_BinaryNumberMethod(mpd_qrem) |
---|
3989 | n/a | Dec_BinaryNumberMethod(mpd_qdivint) |
---|
3990 | n/a | |
---|
3991 | n/a | static PyObject * |
---|
3992 | n/a | nm_dec_as_long(PyObject *dec) |
---|
3993 | n/a | { |
---|
3994 | n/a | PyObject *context; |
---|
3995 | n/a | |
---|
3996 | n/a | CURRENT_CONTEXT(context); |
---|
3997 | n/a | return dec_as_long(dec, context, MPD_ROUND_DOWN); |
---|
3998 | n/a | } |
---|
3999 | n/a | |
---|
4000 | n/a | static int |
---|
4001 | n/a | nm_nonzero(PyObject *v) |
---|
4002 | n/a | { |
---|
4003 | n/a | return !mpd_iszero(MPD(v)); |
---|
4004 | n/a | } |
---|
4005 | n/a | |
---|
4006 | n/a | static PyObject * |
---|
4007 | n/a | nm_mpd_qdivmod(PyObject *v, PyObject *w) |
---|
4008 | n/a | { |
---|
4009 | n/a | PyObject *a, *b; |
---|
4010 | n/a | PyObject *q, *r; |
---|
4011 | n/a | PyObject *context; |
---|
4012 | n/a | uint32_t status = 0; |
---|
4013 | n/a | PyObject *ret; |
---|
4014 | n/a | |
---|
4015 | n/a | CURRENT_CONTEXT(context); |
---|
4016 | n/a | CONVERT_BINOP(&a, &b, v, w, context); |
---|
4017 | n/a | |
---|
4018 | n/a | q = dec_alloc(); |
---|
4019 | n/a | if (q == NULL) { |
---|
4020 | n/a | Py_DECREF(a); |
---|
4021 | n/a | Py_DECREF(b); |
---|
4022 | n/a | return NULL; |
---|
4023 | n/a | } |
---|
4024 | n/a | r = dec_alloc(); |
---|
4025 | n/a | if (r == NULL) { |
---|
4026 | n/a | Py_DECREF(a); |
---|
4027 | n/a | Py_DECREF(b); |
---|
4028 | n/a | Py_DECREF(q); |
---|
4029 | n/a | return NULL; |
---|
4030 | n/a | } |
---|
4031 | n/a | |
---|
4032 | n/a | mpd_qdivmod(MPD(q), MPD(r), MPD(a), MPD(b), CTX(context), &status); |
---|
4033 | n/a | Py_DECREF(a); |
---|
4034 | n/a | Py_DECREF(b); |
---|
4035 | n/a | if (dec_addstatus(context, status)) { |
---|
4036 | n/a | Py_DECREF(r); |
---|
4037 | n/a | Py_DECREF(q); |
---|
4038 | n/a | return NULL; |
---|
4039 | n/a | } |
---|
4040 | n/a | |
---|
4041 | n/a | ret = Py_BuildValue("(OO)", q, r); |
---|
4042 | n/a | Py_DECREF(r); |
---|
4043 | n/a | Py_DECREF(q); |
---|
4044 | n/a | return ret; |
---|
4045 | n/a | } |
---|
4046 | n/a | |
---|
4047 | n/a | static PyObject * |
---|
4048 | n/a | nm_mpd_qpow(PyObject *base, PyObject *exp, PyObject *mod) |
---|
4049 | n/a | { |
---|
4050 | n/a | PyObject *a, *b, *c = NULL; |
---|
4051 | n/a | PyObject *result; |
---|
4052 | n/a | PyObject *context; |
---|
4053 | n/a | uint32_t status = 0; |
---|
4054 | n/a | |
---|
4055 | n/a | CURRENT_CONTEXT(context); |
---|
4056 | n/a | CONVERT_BINOP(&a, &b, base, exp, context); |
---|
4057 | n/a | |
---|
4058 | n/a | if (mod != Py_None) { |
---|
4059 | n/a | if (!convert_op(NOT_IMPL, &c, mod, context)) { |
---|
4060 | n/a | Py_DECREF(a); |
---|
4061 | n/a | Py_DECREF(b); |
---|
4062 | n/a | return c; |
---|
4063 | n/a | } |
---|
4064 | n/a | } |
---|
4065 | n/a | |
---|
4066 | n/a | result = dec_alloc(); |
---|
4067 | n/a | if (result == NULL) { |
---|
4068 | n/a | Py_DECREF(a); |
---|
4069 | n/a | Py_DECREF(b); |
---|
4070 | n/a | Py_XDECREF(c); |
---|
4071 | n/a | return NULL; |
---|
4072 | n/a | } |
---|
4073 | n/a | |
---|
4074 | n/a | if (c == NULL) { |
---|
4075 | n/a | mpd_qpow(MPD(result), MPD(a), MPD(b), |
---|
4076 | n/a | CTX(context), &status); |
---|
4077 | n/a | } |
---|
4078 | n/a | else { |
---|
4079 | n/a | mpd_qpowmod(MPD(result), MPD(a), MPD(b), MPD(c), |
---|
4080 | n/a | CTX(context), &status); |
---|
4081 | n/a | Py_DECREF(c); |
---|
4082 | n/a | } |
---|
4083 | n/a | Py_DECREF(a); |
---|
4084 | n/a | Py_DECREF(b); |
---|
4085 | n/a | if (dec_addstatus(context, status)) { |
---|
4086 | n/a | Py_DECREF(result); |
---|
4087 | n/a | return NULL; |
---|
4088 | n/a | } |
---|
4089 | n/a | |
---|
4090 | n/a | return result; |
---|
4091 | n/a | } |
---|
4092 | n/a | |
---|
4093 | n/a | |
---|
4094 | n/a | /******************************************************************************/ |
---|
4095 | n/a | /* Decimal Methods */ |
---|
4096 | n/a | /******************************************************************************/ |
---|
4097 | n/a | |
---|
4098 | n/a | /* Unary arithmetic functions, optional context arg */ |
---|
4099 | n/a | Dec_UnaryFuncVA(mpd_qexp) |
---|
4100 | n/a | Dec_UnaryFuncVA(mpd_qln) |
---|
4101 | n/a | Dec_UnaryFuncVA(mpd_qlog10) |
---|
4102 | n/a | Dec_UnaryFuncVA(mpd_qnext_minus) |
---|
4103 | n/a | Dec_UnaryFuncVA(mpd_qnext_plus) |
---|
4104 | n/a | Dec_UnaryFuncVA(mpd_qreduce) |
---|
4105 | n/a | Dec_UnaryFuncVA(mpd_qsqrt) |
---|
4106 | n/a | |
---|
4107 | n/a | /* Binary arithmetic functions, optional context arg */ |
---|
4108 | n/a | Dec_BinaryFuncVA(mpd_qcompare) |
---|
4109 | n/a | Dec_BinaryFuncVA(mpd_qcompare_signal) |
---|
4110 | n/a | Dec_BinaryFuncVA(mpd_qmax) |
---|
4111 | n/a | Dec_BinaryFuncVA(mpd_qmax_mag) |
---|
4112 | n/a | Dec_BinaryFuncVA(mpd_qmin) |
---|
4113 | n/a | Dec_BinaryFuncVA(mpd_qmin_mag) |
---|
4114 | n/a | Dec_BinaryFuncVA(mpd_qnext_toward) |
---|
4115 | n/a | Dec_BinaryFuncVA(mpd_qrem_near) |
---|
4116 | n/a | |
---|
4117 | n/a | /* Ternary arithmetic functions, optional context arg */ |
---|
4118 | n/a | Dec_TernaryFuncVA(mpd_qfma) |
---|
4119 | n/a | |
---|
4120 | n/a | /* Boolean functions, no context arg */ |
---|
4121 | n/a | Dec_BoolFunc(mpd_iscanonical) |
---|
4122 | n/a | Dec_BoolFunc(mpd_isfinite) |
---|
4123 | n/a | Dec_BoolFunc(mpd_isinfinite) |
---|
4124 | n/a | Dec_BoolFunc(mpd_isnan) |
---|
4125 | n/a | Dec_BoolFunc(mpd_isqnan) |
---|
4126 | n/a | Dec_BoolFunc(mpd_issnan) |
---|
4127 | n/a | Dec_BoolFunc(mpd_issigned) |
---|
4128 | n/a | Dec_BoolFunc(mpd_iszero) |
---|
4129 | n/a | |
---|
4130 | n/a | /* Boolean functions, optional context arg */ |
---|
4131 | n/a | Dec_BoolFuncVA(mpd_isnormal) |
---|
4132 | n/a | Dec_BoolFuncVA(mpd_issubnormal) |
---|
4133 | n/a | |
---|
4134 | n/a | /* Unary functions, no context arg */ |
---|
4135 | n/a | static PyObject * |
---|
4136 | n/a | dec_mpd_adjexp(PyObject *self, PyObject *dummy UNUSED) |
---|
4137 | n/a | { |
---|
4138 | n/a | mpd_ssize_t retval; |
---|
4139 | n/a | |
---|
4140 | n/a | if (mpd_isspecial(MPD(self))) { |
---|
4141 | n/a | retval = 0; |
---|
4142 | n/a | } |
---|
4143 | n/a | else { |
---|
4144 | n/a | retval = mpd_adjexp(MPD(self)); |
---|
4145 | n/a | } |
---|
4146 | n/a | |
---|
4147 | n/a | return PyLong_FromSsize_t(retval); |
---|
4148 | n/a | } |
---|
4149 | n/a | |
---|
4150 | n/a | static PyObject * |
---|
4151 | n/a | dec_canonical(PyObject *self, PyObject *dummy UNUSED) |
---|
4152 | n/a | { |
---|
4153 | n/a | Py_INCREF(self); |
---|
4154 | n/a | return self; |
---|
4155 | n/a | } |
---|
4156 | n/a | |
---|
4157 | n/a | static PyObject * |
---|
4158 | n/a | dec_conjugate(PyObject *self, PyObject *dummy UNUSED) |
---|
4159 | n/a | { |
---|
4160 | n/a | Py_INCREF(self); |
---|
4161 | n/a | return self; |
---|
4162 | n/a | } |
---|
4163 | n/a | |
---|
4164 | n/a | static PyObject * |
---|
4165 | n/a | dec_mpd_radix(PyObject *self UNUSED, PyObject *dummy UNUSED) |
---|
4166 | n/a | { |
---|
4167 | n/a | PyObject *result; |
---|
4168 | n/a | |
---|
4169 | n/a | result = dec_alloc(); |
---|
4170 | n/a | if (result == NULL) { |
---|
4171 | n/a | return NULL; |
---|
4172 | n/a | } |
---|
4173 | n/a | |
---|
4174 | n/a | _dec_settriple(result, MPD_POS, 10, 0); |
---|
4175 | n/a | return result; |
---|
4176 | n/a | } |
---|
4177 | n/a | |
---|
4178 | n/a | static PyObject * |
---|
4179 | n/a | dec_mpd_qcopy_abs(PyObject *self, PyObject *dummy UNUSED) |
---|
4180 | n/a | { |
---|
4181 | n/a | PyObject *result; |
---|
4182 | n/a | uint32_t status = 0; |
---|
4183 | n/a | |
---|
4184 | n/a | if ((result = dec_alloc()) == NULL) { |
---|
4185 | n/a | return NULL; |
---|
4186 | n/a | } |
---|
4187 | n/a | |
---|
4188 | n/a | mpd_qcopy_abs(MPD(result), MPD(self), &status); |
---|
4189 | n/a | if (status & MPD_Malloc_error) { |
---|
4190 | n/a | Py_DECREF(result); |
---|
4191 | n/a | PyErr_NoMemory(); |
---|
4192 | n/a | return NULL; |
---|
4193 | n/a | } |
---|
4194 | n/a | |
---|
4195 | n/a | return result; |
---|
4196 | n/a | } |
---|
4197 | n/a | |
---|
4198 | n/a | static PyObject * |
---|
4199 | n/a | dec_mpd_qcopy_negate(PyObject *self, PyObject *dummy UNUSED) |
---|
4200 | n/a | { |
---|
4201 | n/a | PyObject *result; |
---|
4202 | n/a | uint32_t status = 0; |
---|
4203 | n/a | |
---|
4204 | n/a | if ((result = dec_alloc()) == NULL) { |
---|
4205 | n/a | return NULL; |
---|
4206 | n/a | } |
---|
4207 | n/a | |
---|
4208 | n/a | mpd_qcopy_negate(MPD(result), MPD(self), &status); |
---|
4209 | n/a | if (status & MPD_Malloc_error) { |
---|
4210 | n/a | Py_DECREF(result); |
---|
4211 | n/a | PyErr_NoMemory(); |
---|
4212 | n/a | return NULL; |
---|
4213 | n/a | } |
---|
4214 | n/a | |
---|
4215 | n/a | return result; |
---|
4216 | n/a | } |
---|
4217 | n/a | |
---|
4218 | n/a | /* Unary functions, optional context arg */ |
---|
4219 | n/a | Dec_UnaryFuncVA(mpd_qinvert) |
---|
4220 | n/a | Dec_UnaryFuncVA(mpd_qlogb) |
---|
4221 | n/a | |
---|
4222 | n/a | static PyObject * |
---|
4223 | n/a | dec_mpd_class(PyObject *self, PyObject *args, PyObject *kwds) |
---|
4224 | n/a | { |
---|
4225 | n/a | static char *kwlist[] = {"context", NULL}; |
---|
4226 | n/a | PyObject *context = Py_None; |
---|
4227 | n/a | const char *cp; |
---|
4228 | n/a | |
---|
4229 | n/a | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O", kwlist, |
---|
4230 | n/a | &context)) { |
---|
4231 | n/a | return NULL; |
---|
4232 | n/a | } |
---|
4233 | n/a | CONTEXT_CHECK_VA(context); |
---|
4234 | n/a | |
---|
4235 | n/a | cp = mpd_class(MPD(self), CTX(context)); |
---|
4236 | n/a | return PyUnicode_FromString(cp); |
---|
4237 | n/a | } |
---|
4238 | n/a | |
---|
4239 | n/a | static PyObject * |
---|
4240 | n/a | dec_mpd_to_eng(PyObject *self, PyObject *args, PyObject *kwds) |
---|
4241 | n/a | { |
---|
4242 | n/a | static char *kwlist[] = {"context", NULL}; |
---|
4243 | n/a | PyObject *result; |
---|
4244 | n/a | PyObject *context = Py_None; |
---|
4245 | n/a | mpd_ssize_t size; |
---|
4246 | n/a | char *s; |
---|
4247 | n/a | |
---|
4248 | n/a | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O", kwlist, |
---|
4249 | n/a | &context)) { |
---|
4250 | n/a | return NULL; |
---|
4251 | n/a | } |
---|
4252 | n/a | CONTEXT_CHECK_VA(context); |
---|
4253 | n/a | |
---|
4254 | n/a | size = mpd_to_eng_size(&s, MPD(self), CtxCaps(context)); |
---|
4255 | n/a | if (size < 0) { |
---|
4256 | n/a | PyErr_NoMemory(); |
---|
4257 | n/a | return NULL; |
---|
4258 | n/a | } |
---|
4259 | n/a | |
---|
4260 | n/a | result = unicode_fromascii(s, size); |
---|
4261 | n/a | mpd_free(s); |
---|
4262 | n/a | |
---|
4263 | n/a | return result; |
---|
4264 | n/a | } |
---|
4265 | n/a | |
---|
4266 | n/a | /* Binary functions, optional context arg for conversion errors */ |
---|
4267 | n/a | Dec_BinaryFuncVA_NO_CTX(mpd_compare_total) |
---|
4268 | n/a | Dec_BinaryFuncVA_NO_CTX(mpd_compare_total_mag) |
---|
4269 | n/a | |
---|
4270 | n/a | static PyObject * |
---|
4271 | n/a | dec_mpd_qcopy_sign(PyObject *self, PyObject *args, PyObject *kwds) |
---|
4272 | n/a | { |
---|
4273 | n/a | static char *kwlist[] = {"other", "context", NULL}; |
---|
4274 | n/a | PyObject *other; |
---|
4275 | n/a | PyObject *a, *b; |
---|
4276 | n/a | PyObject *result; |
---|
4277 | n/a | PyObject *context = Py_None; |
---|
4278 | n/a | uint32_t status = 0; |
---|
4279 | n/a | |
---|
4280 | n/a | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O", kwlist, |
---|
4281 | n/a | &other, &context)) { |
---|
4282 | n/a | return NULL; |
---|
4283 | n/a | } |
---|
4284 | n/a | CONTEXT_CHECK_VA(context); |
---|
4285 | n/a | CONVERT_BINOP_RAISE(&a, &b, self, other, context); |
---|
4286 | n/a | |
---|
4287 | n/a | result = dec_alloc(); |
---|
4288 | n/a | if (result == NULL) { |
---|
4289 | n/a | Py_DECREF(a); |
---|
4290 | n/a | Py_DECREF(b); |
---|
4291 | n/a | return NULL; |
---|
4292 | n/a | } |
---|
4293 | n/a | |
---|
4294 | n/a | mpd_qcopy_sign(MPD(result), MPD(a), MPD(b), &status); |
---|
4295 | n/a | Py_DECREF(a); |
---|
4296 | n/a | Py_DECREF(b); |
---|
4297 | n/a | if (dec_addstatus(context, status)) { |
---|
4298 | n/a | Py_DECREF(result); |
---|
4299 | n/a | return NULL; |
---|
4300 | n/a | } |
---|
4301 | n/a | |
---|
4302 | n/a | return result; |
---|
4303 | n/a | } |
---|
4304 | n/a | |
---|
4305 | n/a | static PyObject * |
---|
4306 | n/a | dec_mpd_same_quantum(PyObject *self, PyObject *args, PyObject *kwds) |
---|
4307 | n/a | { |
---|
4308 | n/a | static char *kwlist[] = {"other", "context", NULL}; |
---|
4309 | n/a | PyObject *other; |
---|
4310 | n/a | PyObject *a, *b; |
---|
4311 | n/a | PyObject *result; |
---|
4312 | n/a | PyObject *context = Py_None; |
---|
4313 | n/a | |
---|
4314 | n/a | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O", kwlist, |
---|
4315 | n/a | &other, &context)) { |
---|
4316 | n/a | return NULL; |
---|
4317 | n/a | } |
---|
4318 | n/a | CONTEXT_CHECK_VA(context); |
---|
4319 | n/a | CONVERT_BINOP_RAISE(&a, &b, self, other, context); |
---|
4320 | n/a | |
---|
4321 | n/a | result = mpd_same_quantum(MPD(a), MPD(b)) ? incr_true() : incr_false(); |
---|
4322 | n/a | Py_DECREF(a); |
---|
4323 | n/a | Py_DECREF(b); |
---|
4324 | n/a | |
---|
4325 | n/a | return result; |
---|
4326 | n/a | } |
---|
4327 | n/a | |
---|
4328 | n/a | /* Binary functions, optional context arg */ |
---|
4329 | n/a | Dec_BinaryFuncVA(mpd_qand) |
---|
4330 | n/a | Dec_BinaryFuncVA(mpd_qor) |
---|
4331 | n/a | Dec_BinaryFuncVA(mpd_qxor) |
---|
4332 | n/a | |
---|
4333 | n/a | Dec_BinaryFuncVA(mpd_qrotate) |
---|
4334 | n/a | Dec_BinaryFuncVA(mpd_qscaleb) |
---|
4335 | n/a | Dec_BinaryFuncVA(mpd_qshift) |
---|
4336 | n/a | |
---|
4337 | n/a | static PyObject * |
---|
4338 | n/a | dec_mpd_qquantize(PyObject *v, PyObject *args, PyObject *kwds) |
---|
4339 | n/a | { |
---|
4340 | n/a | static char *kwlist[] = {"exp", "rounding", "context", NULL}; |
---|
4341 | n/a | PyObject *rounding = Py_None; |
---|
4342 | n/a | PyObject *context = Py_None; |
---|
4343 | n/a | PyObject *w, *a, *b; |
---|
4344 | n/a | PyObject *result; |
---|
4345 | n/a | uint32_t status = 0; |
---|
4346 | n/a | mpd_context_t workctx; |
---|
4347 | n/a | |
---|
4348 | n/a | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OO", kwlist, |
---|
4349 | n/a | &w, &rounding, &context)) { |
---|
4350 | n/a | return NULL; |
---|
4351 | n/a | } |
---|
4352 | n/a | CONTEXT_CHECK_VA(context); |
---|
4353 | n/a | |
---|
4354 | n/a | workctx = *CTX(context); |
---|
4355 | n/a | if (rounding != Py_None) { |
---|
4356 | n/a | int round = getround(rounding); |
---|
4357 | n/a | if (round < 0) { |
---|
4358 | n/a | return NULL; |
---|
4359 | n/a | } |
---|
4360 | n/a | if (!mpd_qsetround(&workctx, round)) { |
---|
4361 | n/a | INTERNAL_ERROR_PTR("dec_mpd_qquantize"); /* GCOV_NOT_REACHED */ |
---|
4362 | n/a | } |
---|
4363 | n/a | } |
---|
4364 | n/a | |
---|
4365 | n/a | CONVERT_BINOP_RAISE(&a, &b, v, w, context); |
---|
4366 | n/a | |
---|
4367 | n/a | result = dec_alloc(); |
---|
4368 | n/a | if (result == NULL) { |
---|
4369 | n/a | Py_DECREF(a); |
---|
4370 | n/a | Py_DECREF(b); |
---|
4371 | n/a | return NULL; |
---|
4372 | n/a | } |
---|
4373 | n/a | |
---|
4374 | n/a | mpd_qquantize(MPD(result), MPD(a), MPD(b), &workctx, &status); |
---|
4375 | n/a | Py_DECREF(a); |
---|
4376 | n/a | Py_DECREF(b); |
---|
4377 | n/a | if (dec_addstatus(context, status)) { |
---|
4378 | n/a | Py_DECREF(result); |
---|
4379 | n/a | return NULL; |
---|
4380 | n/a | } |
---|
4381 | n/a | |
---|
4382 | n/a | return result; |
---|
4383 | n/a | } |
---|
4384 | n/a | |
---|
4385 | n/a | /* Special methods */ |
---|
4386 | n/a | static PyObject * |
---|
4387 | n/a | dec_richcompare(PyObject *v, PyObject *w, int op) |
---|
4388 | n/a | { |
---|
4389 | n/a | PyObject *a; |
---|
4390 | n/a | PyObject *b; |
---|
4391 | n/a | PyObject *context; |
---|
4392 | n/a | uint32_t status = 0; |
---|
4393 | n/a | int a_issnan, b_issnan; |
---|
4394 | n/a | int r; |
---|
4395 | n/a | |
---|
4396 | n/a | assert(PyDec_Check(v)); |
---|
4397 | n/a | |
---|
4398 | n/a | CURRENT_CONTEXT(context); |
---|
4399 | n/a | CONVERT_BINOP_CMP(&a, &b, v, w, op, context); |
---|
4400 | n/a | |
---|
4401 | n/a | a_issnan = mpd_issnan(MPD(a)); |
---|
4402 | n/a | b_issnan = mpd_issnan(MPD(b)); |
---|
4403 | n/a | |
---|
4404 | n/a | r = mpd_qcmp(MPD(a), MPD(b), &status); |
---|
4405 | n/a | Py_DECREF(a); |
---|
4406 | n/a | Py_DECREF(b); |
---|
4407 | n/a | if (r == INT_MAX) { |
---|
4408 | n/a | /* sNaNs or op={le,ge,lt,gt} always signal. */ |
---|
4409 | n/a | if (a_issnan || b_issnan || (op != Py_EQ && op != Py_NE)) { |
---|
4410 | n/a | if (dec_addstatus(context, status)) { |
---|
4411 | n/a | return NULL; |
---|
4412 | n/a | } |
---|
4413 | n/a | } |
---|
4414 | n/a | /* qNaN comparison with op={eq,ne} or comparison |
---|
4415 | n/a | * with InvalidOperation disabled. */ |
---|
4416 | n/a | return (op == Py_NE) ? incr_true() : incr_false(); |
---|
4417 | n/a | } |
---|
4418 | n/a | |
---|
4419 | n/a | switch (op) { |
---|
4420 | n/a | case Py_EQ: |
---|
4421 | n/a | r = (r == 0); |
---|
4422 | n/a | break; |
---|
4423 | n/a | case Py_NE: |
---|
4424 | n/a | r = (r != 0); |
---|
4425 | n/a | break; |
---|
4426 | n/a | case Py_LE: |
---|
4427 | n/a | r = (r <= 0); |
---|
4428 | n/a | break; |
---|
4429 | n/a | case Py_GE: |
---|
4430 | n/a | r = (r >= 0); |
---|
4431 | n/a | break; |
---|
4432 | n/a | case Py_LT: |
---|
4433 | n/a | r = (r == -1); |
---|
4434 | n/a | break; |
---|
4435 | n/a | case Py_GT: |
---|
4436 | n/a | r = (r == 1); |
---|
4437 | n/a | break; |
---|
4438 | n/a | } |
---|
4439 | n/a | |
---|
4440 | n/a | return PyBool_FromLong(r); |
---|
4441 | n/a | } |
---|
4442 | n/a | |
---|
4443 | n/a | /* __ceil__ */ |
---|
4444 | n/a | static PyObject * |
---|
4445 | n/a | dec_ceil(PyObject *self, PyObject *dummy UNUSED) |
---|
4446 | n/a | { |
---|
4447 | n/a | PyObject *context; |
---|
4448 | n/a | |
---|
4449 | n/a | CURRENT_CONTEXT(context); |
---|
4450 | n/a | return dec_as_long(self, context, MPD_ROUND_CEILING); |
---|
4451 | n/a | } |
---|
4452 | n/a | |
---|
4453 | n/a | /* __complex__ */ |
---|
4454 | n/a | static PyObject * |
---|
4455 | n/a | dec_complex(PyObject *self, PyObject *dummy UNUSED) |
---|
4456 | n/a | { |
---|
4457 | n/a | PyObject *f; |
---|
4458 | n/a | double x; |
---|
4459 | n/a | |
---|
4460 | n/a | f = PyDec_AsFloat(self); |
---|
4461 | n/a | if (f == NULL) { |
---|
4462 | n/a | return NULL; |
---|
4463 | n/a | } |
---|
4464 | n/a | |
---|
4465 | n/a | x = PyFloat_AsDouble(f); |
---|
4466 | n/a | Py_DECREF(f); |
---|
4467 | n/a | if (x == -1.0 && PyErr_Occurred()) { |
---|
4468 | n/a | return NULL; |
---|
4469 | n/a | } |
---|
4470 | n/a | |
---|
4471 | n/a | return PyComplex_FromDoubles(x, 0); |
---|
4472 | n/a | } |
---|
4473 | n/a | |
---|
4474 | n/a | /* __copy__ and __deepcopy__ */ |
---|
4475 | n/a | static PyObject * |
---|
4476 | n/a | dec_copy(PyObject *self, PyObject *dummy UNUSED) |
---|
4477 | n/a | { |
---|
4478 | n/a | Py_INCREF(self); |
---|
4479 | n/a | return self; |
---|
4480 | n/a | } |
---|
4481 | n/a | |
---|
4482 | n/a | /* __floor__ */ |
---|
4483 | n/a | static PyObject * |
---|
4484 | n/a | dec_floor(PyObject *self, PyObject *dummy UNUSED) |
---|
4485 | n/a | { |
---|
4486 | n/a | PyObject *context; |
---|
4487 | n/a | |
---|
4488 | n/a | CURRENT_CONTEXT(context); |
---|
4489 | n/a | return dec_as_long(self, context, MPD_ROUND_FLOOR); |
---|
4490 | n/a | } |
---|
4491 | n/a | |
---|
4492 | n/a | /* Always uses the module context */ |
---|
4493 | n/a | static Py_hash_t |
---|
4494 | n/a | _dec_hash(PyDecObject *v) |
---|
4495 | n/a | { |
---|
4496 | n/a | #if defined(CONFIG_64) && _PyHASH_BITS == 61 |
---|
4497 | n/a | /* 2**61 - 1 */ |
---|
4498 | n/a | mpd_uint_t p_data[1] = {2305843009213693951ULL}; |
---|
4499 | n/a | mpd_t p = {MPD_POS|MPD_STATIC|MPD_CONST_DATA, 0, 19, 1, 1, p_data}; |
---|
4500 | n/a | /* Inverse of 10 modulo p */ |
---|
4501 | n/a | mpd_uint_t inv10_p_data[1] = {2075258708292324556ULL}; |
---|
4502 | n/a | mpd_t inv10_p = {MPD_POS|MPD_STATIC|MPD_CONST_DATA, |
---|
4503 | n/a | 0, 19, 1, 1, inv10_p_data}; |
---|
4504 | n/a | #elif defined(CONFIG_32) && _PyHASH_BITS == 31 |
---|
4505 | n/a | /* 2**31 - 1 */ |
---|
4506 | n/a | mpd_uint_t p_data[2] = {147483647UL, 2}; |
---|
4507 | n/a | mpd_t p = {MPD_POS|MPD_STATIC|MPD_CONST_DATA, 0, 10, 2, 2, p_data}; |
---|
4508 | n/a | /* Inverse of 10 modulo p */ |
---|
4509 | n/a | mpd_uint_t inv10_p_data[2] = {503238553UL, 1}; |
---|
4510 | n/a | mpd_t inv10_p = {MPD_POS|MPD_STATIC|MPD_CONST_DATA, |
---|
4511 | n/a | 0, 10, 2, 2, inv10_p_data}; |
---|
4512 | n/a | #else |
---|
4513 | n/a | #error "No valid combination of CONFIG_64, CONFIG_32 and _PyHASH_BITS" |
---|
4514 | n/a | #endif |
---|
4515 | n/a | const Py_hash_t py_hash_inf = 314159; |
---|
4516 | n/a | const Py_hash_t py_hash_nan = 0; |
---|
4517 | n/a | mpd_uint_t ten_data[1] = {10}; |
---|
4518 | n/a | mpd_t ten = {MPD_POS|MPD_STATIC|MPD_CONST_DATA, |
---|
4519 | n/a | 0, 2, 1, 1, ten_data}; |
---|
4520 | n/a | Py_hash_t result; |
---|
4521 | n/a | mpd_t *exp_hash = NULL; |
---|
4522 | n/a | mpd_t *tmp = NULL; |
---|
4523 | n/a | mpd_ssize_t exp; |
---|
4524 | n/a | uint32_t status = 0; |
---|
4525 | n/a | mpd_context_t maxctx; |
---|
4526 | n/a | PyObject *context; |
---|
4527 | n/a | |
---|
4528 | n/a | |
---|
4529 | n/a | context = current_context(); |
---|
4530 | n/a | if (context == NULL) { |
---|
4531 | n/a | return -1; |
---|
4532 | n/a | } |
---|
4533 | n/a | |
---|
4534 | n/a | if (mpd_isspecial(MPD(v))) { |
---|
4535 | n/a | if (mpd_issnan(MPD(v))) { |
---|
4536 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
4537 | n/a | "Cannot hash a signaling NaN value"); |
---|
4538 | n/a | return -1; |
---|
4539 | n/a | } |
---|
4540 | n/a | else if (mpd_isnan(MPD(v))) { |
---|
4541 | n/a | return py_hash_nan; |
---|
4542 | n/a | } |
---|
4543 | n/a | else { |
---|
4544 | n/a | return py_hash_inf * mpd_arith_sign(MPD(v)); |
---|
4545 | n/a | } |
---|
4546 | n/a | } |
---|
4547 | n/a | |
---|
4548 | n/a | mpd_maxcontext(&maxctx); |
---|
4549 | n/a | exp_hash = mpd_qnew(); |
---|
4550 | n/a | if (exp_hash == NULL) { |
---|
4551 | n/a | goto malloc_error; |
---|
4552 | n/a | } |
---|
4553 | n/a | tmp = mpd_qnew(); |
---|
4554 | n/a | if (tmp == NULL) { |
---|
4555 | n/a | goto malloc_error; |
---|
4556 | n/a | } |
---|
4557 | n/a | |
---|
4558 | n/a | /* |
---|
4559 | n/a | * exp(v): exponent of v |
---|
4560 | n/a | * int(v): coefficient of v |
---|
4561 | n/a | */ |
---|
4562 | n/a | exp = MPD(v)->exp; |
---|
4563 | n/a | if (exp >= 0) { |
---|
4564 | n/a | /* 10**exp(v) % p */ |
---|
4565 | n/a | mpd_qsset_ssize(tmp, exp, &maxctx, &status); |
---|
4566 | n/a | mpd_qpowmod(exp_hash, &ten, tmp, &p, &maxctx, &status); |
---|
4567 | n/a | } |
---|
4568 | n/a | else { |
---|
4569 | n/a | /* inv10_p**(-exp(v)) % p */ |
---|
4570 | n/a | mpd_qsset_ssize(tmp, -exp, &maxctx, &status); |
---|
4571 | n/a | mpd_qpowmod(exp_hash, &inv10_p, tmp, &p, &maxctx, &status); |
---|
4572 | n/a | } |
---|
4573 | n/a | |
---|
4574 | n/a | /* hash = (int(v) * exp_hash) % p */ |
---|
4575 | n/a | if (!mpd_qcopy(tmp, MPD(v), &status)) { |
---|
4576 | n/a | goto malloc_error; |
---|
4577 | n/a | } |
---|
4578 | n/a | tmp->exp = 0; |
---|
4579 | n/a | mpd_set_positive(tmp); |
---|
4580 | n/a | |
---|
4581 | n/a | maxctx.prec = MPD_MAX_PREC + 21; |
---|
4582 | n/a | maxctx.emax = MPD_MAX_EMAX + 21; |
---|
4583 | n/a | maxctx.emin = MPD_MIN_EMIN - 21; |
---|
4584 | n/a | |
---|
4585 | n/a | mpd_qmul(tmp, tmp, exp_hash, &maxctx, &status); |
---|
4586 | n/a | mpd_qrem(tmp, tmp, &p, &maxctx, &status); |
---|
4587 | n/a | |
---|
4588 | n/a | result = mpd_qget_ssize(tmp, &status); |
---|
4589 | n/a | result = mpd_ispositive(MPD(v)) ? result : -result; |
---|
4590 | n/a | result = (result == -1) ? -2 : result; |
---|
4591 | n/a | |
---|
4592 | n/a | if (status != 0) { |
---|
4593 | n/a | if (status & MPD_Malloc_error) { |
---|
4594 | n/a | goto malloc_error; |
---|
4595 | n/a | } |
---|
4596 | n/a | else { |
---|
4597 | n/a | PyErr_SetString(PyExc_RuntimeError, /* GCOV_NOT_REACHED */ |
---|
4598 | n/a | "dec_hash: internal error: please report"); /* GCOV_NOT_REACHED */ |
---|
4599 | n/a | } |
---|
4600 | n/a | result = -1; /* GCOV_NOT_REACHED */ |
---|
4601 | n/a | } |
---|
4602 | n/a | |
---|
4603 | n/a | |
---|
4604 | n/a | finish: |
---|
4605 | n/a | if (exp_hash) mpd_del(exp_hash); |
---|
4606 | n/a | if (tmp) mpd_del(tmp); |
---|
4607 | n/a | return result; |
---|
4608 | n/a | |
---|
4609 | n/a | malloc_error: |
---|
4610 | n/a | PyErr_NoMemory(); |
---|
4611 | n/a | result = -1; |
---|
4612 | n/a | goto finish; |
---|
4613 | n/a | } |
---|
4614 | n/a | |
---|
4615 | n/a | static Py_hash_t |
---|
4616 | n/a | dec_hash(PyDecObject *self) |
---|
4617 | n/a | { |
---|
4618 | n/a | if (self->hash == -1) { |
---|
4619 | n/a | self->hash = _dec_hash(self); |
---|
4620 | n/a | } |
---|
4621 | n/a | |
---|
4622 | n/a | return self->hash; |
---|
4623 | n/a | } |
---|
4624 | n/a | |
---|
4625 | n/a | /* __reduce__ */ |
---|
4626 | n/a | static PyObject * |
---|
4627 | n/a | dec_reduce(PyObject *self, PyObject *dummy UNUSED) |
---|
4628 | n/a | { |
---|
4629 | n/a | PyObject *result, *str; |
---|
4630 | n/a | |
---|
4631 | n/a | str = dec_str(self); |
---|
4632 | n/a | if (str == NULL) { |
---|
4633 | n/a | return NULL; |
---|
4634 | n/a | } |
---|
4635 | n/a | |
---|
4636 | n/a | result = Py_BuildValue("O(O)", Py_TYPE(self), str); |
---|
4637 | n/a | Py_DECREF(str); |
---|
4638 | n/a | |
---|
4639 | n/a | return result; |
---|
4640 | n/a | } |
---|
4641 | n/a | |
---|
4642 | n/a | /* __sizeof__ */ |
---|
4643 | n/a | static PyObject * |
---|
4644 | n/a | dec_sizeof(PyObject *v, PyObject *dummy UNUSED) |
---|
4645 | n/a | { |
---|
4646 | n/a | Py_ssize_t res; |
---|
4647 | n/a | |
---|
4648 | n/a | res = _PyObject_SIZE(Py_TYPE(v)); |
---|
4649 | n/a | if (mpd_isdynamic_data(MPD(v))) { |
---|
4650 | n/a | res += MPD(v)->alloc * sizeof(mpd_uint_t); |
---|
4651 | n/a | } |
---|
4652 | n/a | return PyLong_FromSsize_t(res); |
---|
4653 | n/a | } |
---|
4654 | n/a | |
---|
4655 | n/a | /* __trunc__ */ |
---|
4656 | n/a | static PyObject * |
---|
4657 | n/a | dec_trunc(PyObject *self, PyObject *dummy UNUSED) |
---|
4658 | n/a | { |
---|
4659 | n/a | PyObject *context; |
---|
4660 | n/a | |
---|
4661 | n/a | CURRENT_CONTEXT(context); |
---|
4662 | n/a | return dec_as_long(self, context, MPD_ROUND_DOWN); |
---|
4663 | n/a | } |
---|
4664 | n/a | |
---|
4665 | n/a | /* real and imag */ |
---|
4666 | n/a | static PyObject * |
---|
4667 | n/a | dec_real(PyObject *self, void *closure UNUSED) |
---|
4668 | n/a | { |
---|
4669 | n/a | Py_INCREF(self); |
---|
4670 | n/a | return self; |
---|
4671 | n/a | } |
---|
4672 | n/a | |
---|
4673 | n/a | static PyObject * |
---|
4674 | n/a | dec_imag(PyObject *self UNUSED, void *closure UNUSED) |
---|
4675 | n/a | { |
---|
4676 | n/a | PyObject *result; |
---|
4677 | n/a | |
---|
4678 | n/a | result = dec_alloc(); |
---|
4679 | n/a | if (result == NULL) { |
---|
4680 | n/a | return NULL; |
---|
4681 | n/a | } |
---|
4682 | n/a | |
---|
4683 | n/a | _dec_settriple(result, MPD_POS, 0, 0); |
---|
4684 | n/a | return result; |
---|
4685 | n/a | } |
---|
4686 | n/a | |
---|
4687 | n/a | |
---|
4688 | n/a | static PyGetSetDef dec_getsets [] = |
---|
4689 | n/a | { |
---|
4690 | n/a | { "real", (getter)dec_real, NULL, NULL, NULL}, |
---|
4691 | n/a | { "imag", (getter)dec_imag, NULL, NULL, NULL}, |
---|
4692 | n/a | {NULL} |
---|
4693 | n/a | }; |
---|
4694 | n/a | |
---|
4695 | n/a | static PyNumberMethods dec_number_methods = |
---|
4696 | n/a | { |
---|
4697 | n/a | (binaryfunc) nm_mpd_qadd, |
---|
4698 | n/a | (binaryfunc) nm_mpd_qsub, |
---|
4699 | n/a | (binaryfunc) nm_mpd_qmul, |
---|
4700 | n/a | (binaryfunc) nm_mpd_qrem, |
---|
4701 | n/a | (binaryfunc) nm_mpd_qdivmod, |
---|
4702 | n/a | (ternaryfunc) nm_mpd_qpow, |
---|
4703 | n/a | (unaryfunc) nm_mpd_qminus, |
---|
4704 | n/a | (unaryfunc) nm_mpd_qplus, |
---|
4705 | n/a | (unaryfunc) nm_mpd_qabs, |
---|
4706 | n/a | (inquiry) nm_nonzero, |
---|
4707 | n/a | (unaryfunc) 0, /* no bit-complement */ |
---|
4708 | n/a | (binaryfunc) 0, /* no shiftl */ |
---|
4709 | n/a | (binaryfunc) 0, /* no shiftr */ |
---|
4710 | n/a | (binaryfunc) 0, /* no bit-and */ |
---|
4711 | n/a | (binaryfunc) 0, /* no bit-xor */ |
---|
4712 | n/a | (binaryfunc) 0, /* no bit-ior */ |
---|
4713 | n/a | (unaryfunc) nm_dec_as_long, |
---|
4714 | n/a | 0, /* nb_reserved */ |
---|
4715 | n/a | (unaryfunc) PyDec_AsFloat, |
---|
4716 | n/a | 0, /* binaryfunc nb_inplace_add; */ |
---|
4717 | n/a | 0, /* binaryfunc nb_inplace_subtract; */ |
---|
4718 | n/a | 0, /* binaryfunc nb_inplace_multiply; */ |
---|
4719 | n/a | 0, /* binaryfunc nb_inplace_remainder; */ |
---|
4720 | n/a | 0, /* ternaryfunc nb_inplace_power; */ |
---|
4721 | n/a | 0, /* binaryfunc nb_inplace_lshift; */ |
---|
4722 | n/a | 0, /* binaryfunc nb_inplace_rshift; */ |
---|
4723 | n/a | 0, /* binaryfunc nb_inplace_and; */ |
---|
4724 | n/a | 0, /* binaryfunc nb_inplace_xor; */ |
---|
4725 | n/a | 0, /* binaryfunc nb_inplace_or; */ |
---|
4726 | n/a | (binaryfunc) nm_mpd_qdivint, /* binaryfunc nb_floor_divide; */ |
---|
4727 | n/a | (binaryfunc) nm_mpd_qdiv, /* binaryfunc nb_true_divide; */ |
---|
4728 | n/a | 0, /* binaryfunc nb_inplace_floor_divide; */ |
---|
4729 | n/a | 0, /* binaryfunc nb_inplace_true_divide; */ |
---|
4730 | n/a | }; |
---|
4731 | n/a | |
---|
4732 | n/a | static PyMethodDef dec_methods [] = |
---|
4733 | n/a | { |
---|
4734 | n/a | /* Unary arithmetic functions, optional context arg */ |
---|
4735 | n/a | { "exp", (PyCFunction)dec_mpd_qexp, METH_VARARGS|METH_KEYWORDS, doc_exp }, |
---|
4736 | n/a | { "ln", (PyCFunction)dec_mpd_qln, METH_VARARGS|METH_KEYWORDS, doc_ln }, |
---|
4737 | n/a | { "log10", (PyCFunction)dec_mpd_qlog10, METH_VARARGS|METH_KEYWORDS, doc_log10 }, |
---|
4738 | n/a | { "next_minus", (PyCFunction)dec_mpd_qnext_minus, METH_VARARGS|METH_KEYWORDS, doc_next_minus }, |
---|
4739 | n/a | { "next_plus", (PyCFunction)dec_mpd_qnext_plus, METH_VARARGS|METH_KEYWORDS, doc_next_plus }, |
---|
4740 | n/a | { "normalize", (PyCFunction)dec_mpd_qreduce, METH_VARARGS|METH_KEYWORDS, doc_normalize }, |
---|
4741 | n/a | { "to_integral", (PyCFunction)PyDec_ToIntegralValue, METH_VARARGS|METH_KEYWORDS, doc_to_integral }, |
---|
4742 | n/a | { "to_integral_exact", (PyCFunction)PyDec_ToIntegralExact, METH_VARARGS|METH_KEYWORDS, doc_to_integral_exact }, |
---|
4743 | n/a | { "to_integral_value", (PyCFunction)PyDec_ToIntegralValue, METH_VARARGS|METH_KEYWORDS, doc_to_integral_value }, |
---|
4744 | n/a | { "sqrt", (PyCFunction)dec_mpd_qsqrt, METH_VARARGS|METH_KEYWORDS, doc_sqrt }, |
---|
4745 | n/a | |
---|
4746 | n/a | /* Binary arithmetic functions, optional context arg */ |
---|
4747 | n/a | { "compare", (PyCFunction)dec_mpd_qcompare, METH_VARARGS|METH_KEYWORDS, doc_compare }, |
---|
4748 | n/a | { "compare_signal", (PyCFunction)dec_mpd_qcompare_signal, METH_VARARGS|METH_KEYWORDS, doc_compare_signal }, |
---|
4749 | n/a | { "max", (PyCFunction)dec_mpd_qmax, METH_VARARGS|METH_KEYWORDS, doc_max }, |
---|
4750 | n/a | { "max_mag", (PyCFunction)dec_mpd_qmax_mag, METH_VARARGS|METH_KEYWORDS, doc_max_mag }, |
---|
4751 | n/a | { "min", (PyCFunction)dec_mpd_qmin, METH_VARARGS|METH_KEYWORDS, doc_min }, |
---|
4752 | n/a | { "min_mag", (PyCFunction)dec_mpd_qmin_mag, METH_VARARGS|METH_KEYWORDS, doc_min_mag }, |
---|
4753 | n/a | { "next_toward", (PyCFunction)dec_mpd_qnext_toward, METH_VARARGS|METH_KEYWORDS, doc_next_toward }, |
---|
4754 | n/a | { "quantize", (PyCFunction)dec_mpd_qquantize, METH_VARARGS|METH_KEYWORDS, doc_quantize }, |
---|
4755 | n/a | { "remainder_near", (PyCFunction)dec_mpd_qrem_near, METH_VARARGS|METH_KEYWORDS, doc_remainder_near }, |
---|
4756 | n/a | |
---|
4757 | n/a | /* Ternary arithmetic functions, optional context arg */ |
---|
4758 | n/a | { "fma", (PyCFunction)dec_mpd_qfma, METH_VARARGS|METH_KEYWORDS, doc_fma }, |
---|
4759 | n/a | |
---|
4760 | n/a | /* Boolean functions, no context arg */ |
---|
4761 | n/a | { "is_canonical", dec_mpd_iscanonical, METH_NOARGS, doc_is_canonical }, |
---|
4762 | n/a | { "is_finite", dec_mpd_isfinite, METH_NOARGS, doc_is_finite }, |
---|
4763 | n/a | { "is_infinite", dec_mpd_isinfinite, METH_NOARGS, doc_is_infinite }, |
---|
4764 | n/a | { "is_nan", dec_mpd_isnan, METH_NOARGS, doc_is_nan }, |
---|
4765 | n/a | { "is_qnan", dec_mpd_isqnan, METH_NOARGS, doc_is_qnan }, |
---|
4766 | n/a | { "is_snan", dec_mpd_issnan, METH_NOARGS, doc_is_snan }, |
---|
4767 | n/a | { "is_signed", dec_mpd_issigned, METH_NOARGS, doc_is_signed }, |
---|
4768 | n/a | { "is_zero", dec_mpd_iszero, METH_NOARGS, doc_is_zero }, |
---|
4769 | n/a | |
---|
4770 | n/a | /* Boolean functions, optional context arg */ |
---|
4771 | n/a | { "is_normal", (PyCFunction)dec_mpd_isnormal, METH_VARARGS|METH_KEYWORDS, doc_is_normal }, |
---|
4772 | n/a | { "is_subnormal", (PyCFunction)dec_mpd_issubnormal, METH_VARARGS|METH_KEYWORDS, doc_is_subnormal }, |
---|
4773 | n/a | |
---|
4774 | n/a | /* Unary functions, no context arg */ |
---|
4775 | n/a | { "adjusted", dec_mpd_adjexp, METH_NOARGS, doc_adjusted }, |
---|
4776 | n/a | { "canonical", dec_canonical, METH_NOARGS, doc_canonical }, |
---|
4777 | n/a | { "conjugate", dec_conjugate, METH_NOARGS, doc_conjugate }, |
---|
4778 | n/a | { "radix", dec_mpd_radix, METH_NOARGS, doc_radix }, |
---|
4779 | n/a | |
---|
4780 | n/a | /* Unary functions, optional context arg for conversion errors */ |
---|
4781 | n/a | { "copy_abs", dec_mpd_qcopy_abs, METH_NOARGS, doc_copy_abs }, |
---|
4782 | n/a | { "copy_negate", dec_mpd_qcopy_negate, METH_NOARGS, doc_copy_negate }, |
---|
4783 | n/a | |
---|
4784 | n/a | /* Unary functions, optional context arg */ |
---|
4785 | n/a | { "logb", (PyCFunction)dec_mpd_qlogb, METH_VARARGS|METH_KEYWORDS, doc_logb }, |
---|
4786 | n/a | { "logical_invert", (PyCFunction)dec_mpd_qinvert, METH_VARARGS|METH_KEYWORDS, doc_logical_invert }, |
---|
4787 | n/a | { "number_class", (PyCFunction)dec_mpd_class, METH_VARARGS|METH_KEYWORDS, doc_number_class }, |
---|
4788 | n/a | { "to_eng_string", (PyCFunction)dec_mpd_to_eng, METH_VARARGS|METH_KEYWORDS, doc_to_eng_string }, |
---|
4789 | n/a | |
---|
4790 | n/a | /* Binary functions, optional context arg for conversion errors */ |
---|
4791 | n/a | { "compare_total", (PyCFunction)dec_mpd_compare_total, METH_VARARGS|METH_KEYWORDS, doc_compare_total }, |
---|
4792 | n/a | { "compare_total_mag", (PyCFunction)dec_mpd_compare_total_mag, METH_VARARGS|METH_KEYWORDS, doc_compare_total_mag }, |
---|
4793 | n/a | { "copy_sign", (PyCFunction)dec_mpd_qcopy_sign, METH_VARARGS|METH_KEYWORDS, doc_copy_sign }, |
---|
4794 | n/a | { "same_quantum", (PyCFunction)dec_mpd_same_quantum, METH_VARARGS|METH_KEYWORDS, doc_same_quantum }, |
---|
4795 | n/a | |
---|
4796 | n/a | /* Binary functions, optional context arg */ |
---|
4797 | n/a | { "logical_and", (PyCFunction)dec_mpd_qand, METH_VARARGS|METH_KEYWORDS, doc_logical_and }, |
---|
4798 | n/a | { "logical_or", (PyCFunction)dec_mpd_qor, METH_VARARGS|METH_KEYWORDS, doc_logical_or }, |
---|
4799 | n/a | { "logical_xor", (PyCFunction)dec_mpd_qxor, METH_VARARGS|METH_KEYWORDS, doc_logical_xor }, |
---|
4800 | n/a | { "rotate", (PyCFunction)dec_mpd_qrotate, METH_VARARGS|METH_KEYWORDS, doc_rotate }, |
---|
4801 | n/a | { "scaleb", (PyCFunction)dec_mpd_qscaleb, METH_VARARGS|METH_KEYWORDS, doc_scaleb }, |
---|
4802 | n/a | { "shift", (PyCFunction)dec_mpd_qshift, METH_VARARGS|METH_KEYWORDS, doc_shift }, |
---|
4803 | n/a | |
---|
4804 | n/a | /* Miscellaneous */ |
---|
4805 | n/a | { "from_float", dec_from_float, METH_O|METH_CLASS, doc_from_float }, |
---|
4806 | n/a | { "as_tuple", PyDec_AsTuple, METH_NOARGS, doc_as_tuple }, |
---|
4807 | n/a | { "as_integer_ratio", dec_as_integer_ratio, METH_NOARGS, doc_as_integer_ratio }, |
---|
4808 | n/a | |
---|
4809 | n/a | /* Special methods */ |
---|
4810 | n/a | { "__copy__", dec_copy, METH_NOARGS, NULL }, |
---|
4811 | n/a | { "__deepcopy__", dec_copy, METH_O, NULL }, |
---|
4812 | n/a | { "__format__", dec_format, METH_VARARGS, NULL }, |
---|
4813 | n/a | { "__reduce__", dec_reduce, METH_NOARGS, NULL }, |
---|
4814 | n/a | { "__round__", PyDec_Round, METH_VARARGS, NULL }, |
---|
4815 | n/a | { "__ceil__", dec_ceil, METH_NOARGS, NULL }, |
---|
4816 | n/a | { "__floor__", dec_floor, METH_NOARGS, NULL }, |
---|
4817 | n/a | { "__trunc__", dec_trunc, METH_NOARGS, NULL }, |
---|
4818 | n/a | { "__complex__", dec_complex, METH_NOARGS, NULL }, |
---|
4819 | n/a | { "__sizeof__", dec_sizeof, METH_NOARGS, NULL }, |
---|
4820 | n/a | |
---|
4821 | n/a | { NULL, NULL, 1 } |
---|
4822 | n/a | }; |
---|
4823 | n/a | |
---|
4824 | n/a | static PyTypeObject PyDec_Type = |
---|
4825 | n/a | { |
---|
4826 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
---|
4827 | n/a | "decimal.Decimal", /* tp_name */ |
---|
4828 | n/a | sizeof(PyDecObject), /* tp_basicsize */ |
---|
4829 | n/a | 0, /* tp_itemsize */ |
---|
4830 | n/a | (destructor) dec_dealloc, /* tp_dealloc */ |
---|
4831 | n/a | 0, /* tp_print */ |
---|
4832 | n/a | (getattrfunc) 0, /* tp_getattr */ |
---|
4833 | n/a | (setattrfunc) 0, /* tp_setattr */ |
---|
4834 | n/a | 0, /* tp_reserved */ |
---|
4835 | n/a | (reprfunc) dec_repr, /* tp_repr */ |
---|
4836 | n/a | &dec_number_methods, /* tp_as_number */ |
---|
4837 | n/a | 0, /* tp_as_sequence */ |
---|
4838 | n/a | 0, /* tp_as_mapping */ |
---|
4839 | n/a | (hashfunc) dec_hash, /* tp_hash */ |
---|
4840 | n/a | 0, /* tp_call */ |
---|
4841 | n/a | (reprfunc) dec_str, /* tp_str */ |
---|
4842 | n/a | (getattrofunc) PyObject_GenericGetAttr, /* tp_getattro */ |
---|
4843 | n/a | (setattrofunc) 0, /* tp_setattro */ |
---|
4844 | n/a | (PyBufferProcs *) 0, /* tp_as_buffer */ |
---|
4845 | n/a | (Py_TPFLAGS_DEFAULT| |
---|
4846 | n/a | Py_TPFLAGS_BASETYPE), /* tp_flags */ |
---|
4847 | n/a | doc_decimal, /* tp_doc */ |
---|
4848 | n/a | 0, /* tp_traverse */ |
---|
4849 | n/a | 0, /* tp_clear */ |
---|
4850 | n/a | dec_richcompare, /* tp_richcompare */ |
---|
4851 | n/a | 0, /* tp_weaklistoffset */ |
---|
4852 | n/a | 0, /* tp_iter */ |
---|
4853 | n/a | 0, /* tp_iternext */ |
---|
4854 | n/a | dec_methods, /* tp_methods */ |
---|
4855 | n/a | 0, /* tp_members */ |
---|
4856 | n/a | dec_getsets, /* tp_getset */ |
---|
4857 | n/a | 0, /* tp_base */ |
---|
4858 | n/a | 0, /* tp_dict */ |
---|
4859 | n/a | 0, /* tp_descr_get */ |
---|
4860 | n/a | 0, /* tp_descr_set */ |
---|
4861 | n/a | 0, /* tp_dictoffset */ |
---|
4862 | n/a | 0, /* tp_init */ |
---|
4863 | n/a | 0, /* tp_alloc */ |
---|
4864 | n/a | dec_new, /* tp_new */ |
---|
4865 | n/a | PyObject_Del, /* tp_free */ |
---|
4866 | n/a | }; |
---|
4867 | n/a | |
---|
4868 | n/a | |
---|
4869 | n/a | /******************************************************************************/ |
---|
4870 | n/a | /* Context Object, Part 2 */ |
---|
4871 | n/a | /******************************************************************************/ |
---|
4872 | n/a | |
---|
4873 | n/a | |
---|
4874 | n/a | /************************************************************************/ |
---|
4875 | n/a | /* Macros for converting mpdecimal functions to Context methods */ |
---|
4876 | n/a | /************************************************************************/ |
---|
4877 | n/a | |
---|
4878 | n/a | /* Boolean context method. */ |
---|
4879 | n/a | #define DecCtx_BoolFunc(MPDFUNC) \ |
---|
4880 | n/a | static PyObject * \ |
---|
4881 | n/a | ctx_##MPDFUNC(PyObject *context, PyObject *v) \ |
---|
4882 | n/a | { \ |
---|
4883 | n/a | PyObject *ret; \ |
---|
4884 | n/a | PyObject *a; \ |
---|
4885 | n/a | \ |
---|
4886 | n/a | CONVERT_OP_RAISE(&a, v, context); \ |
---|
4887 | n/a | \ |
---|
4888 | n/a | ret = MPDFUNC(MPD(a), CTX(context)) ? incr_true() : incr_false(); \ |
---|
4889 | n/a | Py_DECREF(a); \ |
---|
4890 | n/a | return ret; \ |
---|
4891 | n/a | } |
---|
4892 | n/a | |
---|
4893 | n/a | /* Boolean context method. MPDFUNC does NOT use a context. */ |
---|
4894 | n/a | #define DecCtx_BoolFunc_NO_CTX(MPDFUNC) \ |
---|
4895 | n/a | static PyObject * \ |
---|
4896 | n/a | ctx_##MPDFUNC(PyObject *context, PyObject *v) \ |
---|
4897 | n/a | { \ |
---|
4898 | n/a | PyObject *ret; \ |
---|
4899 | n/a | PyObject *a; \ |
---|
4900 | n/a | \ |
---|
4901 | n/a | CONVERT_OP_RAISE(&a, v, context); \ |
---|
4902 | n/a | \ |
---|
4903 | n/a | ret = MPDFUNC(MPD(a)) ? incr_true() : incr_false(); \ |
---|
4904 | n/a | Py_DECREF(a); \ |
---|
4905 | n/a | return ret; \ |
---|
4906 | n/a | } |
---|
4907 | n/a | |
---|
4908 | n/a | /* Unary context method. */ |
---|
4909 | n/a | #define DecCtx_UnaryFunc(MPDFUNC) \ |
---|
4910 | n/a | static PyObject * \ |
---|
4911 | n/a | ctx_##MPDFUNC(PyObject *context, PyObject *v) \ |
---|
4912 | n/a | { \ |
---|
4913 | n/a | PyObject *result, *a; \ |
---|
4914 | n/a | uint32_t status = 0; \ |
---|
4915 | n/a | \ |
---|
4916 | n/a | CONVERT_OP_RAISE(&a, v, context); \ |
---|
4917 | n/a | \ |
---|
4918 | n/a | if ((result = dec_alloc()) == NULL) { \ |
---|
4919 | n/a | Py_DECREF(a); \ |
---|
4920 | n/a | return NULL; \ |
---|
4921 | n/a | } \ |
---|
4922 | n/a | \ |
---|
4923 | n/a | MPDFUNC(MPD(result), MPD(a), CTX(context), &status); \ |
---|
4924 | n/a | Py_DECREF(a); \ |
---|
4925 | n/a | if (dec_addstatus(context, status)) { \ |
---|
4926 | n/a | Py_DECREF(result); \ |
---|
4927 | n/a | return NULL; \ |
---|
4928 | n/a | } \ |
---|
4929 | n/a | \ |
---|
4930 | n/a | return result; \ |
---|
4931 | n/a | } |
---|
4932 | n/a | |
---|
4933 | n/a | /* Binary context method. */ |
---|
4934 | n/a | #define DecCtx_BinaryFunc(MPDFUNC) \ |
---|
4935 | n/a | static PyObject * \ |
---|
4936 | n/a | ctx_##MPDFUNC(PyObject *context, PyObject *args) \ |
---|
4937 | n/a | { \ |
---|
4938 | n/a | PyObject *v, *w; \ |
---|
4939 | n/a | PyObject *a, *b; \ |
---|
4940 | n/a | PyObject *result; \ |
---|
4941 | n/a | uint32_t status = 0; \ |
---|
4942 | n/a | \ |
---|
4943 | n/a | if (!PyArg_ParseTuple(args, "OO", &v, &w)) { \ |
---|
4944 | n/a | return NULL; \ |
---|
4945 | n/a | } \ |
---|
4946 | n/a | \ |
---|
4947 | n/a | CONVERT_BINOP_RAISE(&a, &b, v, w, context); \ |
---|
4948 | n/a | \ |
---|
4949 | n/a | if ((result = dec_alloc()) == NULL) { \ |
---|
4950 | n/a | Py_DECREF(a); \ |
---|
4951 | n/a | Py_DECREF(b); \ |
---|
4952 | n/a | return NULL; \ |
---|
4953 | n/a | } \ |
---|
4954 | n/a | \ |
---|
4955 | n/a | MPDFUNC(MPD(result), MPD(a), MPD(b), CTX(context), &status); \ |
---|
4956 | n/a | Py_DECREF(a); \ |
---|
4957 | n/a | Py_DECREF(b); \ |
---|
4958 | n/a | if (dec_addstatus(context, status)) { \ |
---|
4959 | n/a | Py_DECREF(result); \ |
---|
4960 | n/a | return NULL; \ |
---|
4961 | n/a | } \ |
---|
4962 | n/a | \ |
---|
4963 | n/a | return result; \ |
---|
4964 | n/a | } |
---|
4965 | n/a | |
---|
4966 | n/a | /* |
---|
4967 | n/a | * Binary context method. The context is only used for conversion. |
---|
4968 | n/a | * The actual MPDFUNC does NOT take a context arg. |
---|
4969 | n/a | */ |
---|
4970 | n/a | #define DecCtx_BinaryFunc_NO_CTX(MPDFUNC) \ |
---|
4971 | n/a | static PyObject * \ |
---|
4972 | n/a | ctx_##MPDFUNC(PyObject *context, PyObject *args) \ |
---|
4973 | n/a | { \ |
---|
4974 | n/a | PyObject *v, *w; \ |
---|
4975 | n/a | PyObject *a, *b; \ |
---|
4976 | n/a | PyObject *result; \ |
---|
4977 | n/a | \ |
---|
4978 | n/a | if (!PyArg_ParseTuple(args, "OO", &v, &w)) { \ |
---|
4979 | n/a | return NULL; \ |
---|
4980 | n/a | } \ |
---|
4981 | n/a | \ |
---|
4982 | n/a | CONVERT_BINOP_RAISE(&a, &b, v, w, context); \ |
---|
4983 | n/a | \ |
---|
4984 | n/a | if ((result = dec_alloc()) == NULL) { \ |
---|
4985 | n/a | Py_DECREF(a); \ |
---|
4986 | n/a | Py_DECREF(b); \ |
---|
4987 | n/a | return NULL; \ |
---|
4988 | n/a | } \ |
---|
4989 | n/a | \ |
---|
4990 | n/a | MPDFUNC(MPD(result), MPD(a), MPD(b)); \ |
---|
4991 | n/a | Py_DECREF(a); \ |
---|
4992 | n/a | Py_DECREF(b); \ |
---|
4993 | n/a | \ |
---|
4994 | n/a | return result; \ |
---|
4995 | n/a | } |
---|
4996 | n/a | |
---|
4997 | n/a | /* Ternary context method. */ |
---|
4998 | n/a | #define DecCtx_TernaryFunc(MPDFUNC) \ |
---|
4999 | n/a | static PyObject * \ |
---|
5000 | n/a | ctx_##MPDFUNC(PyObject *context, PyObject *args) \ |
---|
5001 | n/a | { \ |
---|
5002 | n/a | PyObject *v, *w, *x; \ |
---|
5003 | n/a | PyObject *a, *b, *c; \ |
---|
5004 | n/a | PyObject *result; \ |
---|
5005 | n/a | uint32_t status = 0; \ |
---|
5006 | n/a | \ |
---|
5007 | n/a | if (!PyArg_ParseTuple(args, "OOO", &v, &w, &x)) { \ |
---|
5008 | n/a | return NULL; \ |
---|
5009 | n/a | } \ |
---|
5010 | n/a | \ |
---|
5011 | n/a | CONVERT_TERNOP_RAISE(&a, &b, &c, v, w, x, context); \ |
---|
5012 | n/a | \ |
---|
5013 | n/a | if ((result = dec_alloc()) == NULL) { \ |
---|
5014 | n/a | Py_DECREF(a); \ |
---|
5015 | n/a | Py_DECREF(b); \ |
---|
5016 | n/a | Py_DECREF(c); \ |
---|
5017 | n/a | return NULL; \ |
---|
5018 | n/a | } \ |
---|
5019 | n/a | \ |
---|
5020 | n/a | MPDFUNC(MPD(result), MPD(a), MPD(b), MPD(c), CTX(context), &status); \ |
---|
5021 | n/a | Py_DECREF(a); \ |
---|
5022 | n/a | Py_DECREF(b); \ |
---|
5023 | n/a | Py_DECREF(c); \ |
---|
5024 | n/a | if (dec_addstatus(context, status)) { \ |
---|
5025 | n/a | Py_DECREF(result); \ |
---|
5026 | n/a | return NULL; \ |
---|
5027 | n/a | } \ |
---|
5028 | n/a | \ |
---|
5029 | n/a | return result; \ |
---|
5030 | n/a | } |
---|
5031 | n/a | |
---|
5032 | n/a | |
---|
5033 | n/a | /* Unary arithmetic functions */ |
---|
5034 | n/a | DecCtx_UnaryFunc(mpd_qabs) |
---|
5035 | n/a | DecCtx_UnaryFunc(mpd_qexp) |
---|
5036 | n/a | DecCtx_UnaryFunc(mpd_qln) |
---|
5037 | n/a | DecCtx_UnaryFunc(mpd_qlog10) |
---|
5038 | n/a | DecCtx_UnaryFunc(mpd_qminus) |
---|
5039 | n/a | DecCtx_UnaryFunc(mpd_qnext_minus) |
---|
5040 | n/a | DecCtx_UnaryFunc(mpd_qnext_plus) |
---|
5041 | n/a | DecCtx_UnaryFunc(mpd_qplus) |
---|
5042 | n/a | DecCtx_UnaryFunc(mpd_qreduce) |
---|
5043 | n/a | DecCtx_UnaryFunc(mpd_qround_to_int) |
---|
5044 | n/a | DecCtx_UnaryFunc(mpd_qround_to_intx) |
---|
5045 | n/a | DecCtx_UnaryFunc(mpd_qsqrt) |
---|
5046 | n/a | |
---|
5047 | n/a | /* Binary arithmetic functions */ |
---|
5048 | n/a | DecCtx_BinaryFunc(mpd_qadd) |
---|
5049 | n/a | DecCtx_BinaryFunc(mpd_qcompare) |
---|
5050 | n/a | DecCtx_BinaryFunc(mpd_qcompare_signal) |
---|
5051 | n/a | DecCtx_BinaryFunc(mpd_qdiv) |
---|
5052 | n/a | DecCtx_BinaryFunc(mpd_qdivint) |
---|
5053 | n/a | DecCtx_BinaryFunc(mpd_qmax) |
---|
5054 | n/a | DecCtx_BinaryFunc(mpd_qmax_mag) |
---|
5055 | n/a | DecCtx_BinaryFunc(mpd_qmin) |
---|
5056 | n/a | DecCtx_BinaryFunc(mpd_qmin_mag) |
---|
5057 | n/a | DecCtx_BinaryFunc(mpd_qmul) |
---|
5058 | n/a | DecCtx_BinaryFunc(mpd_qnext_toward) |
---|
5059 | n/a | DecCtx_BinaryFunc(mpd_qquantize) |
---|
5060 | n/a | DecCtx_BinaryFunc(mpd_qrem) |
---|
5061 | n/a | DecCtx_BinaryFunc(mpd_qrem_near) |
---|
5062 | n/a | DecCtx_BinaryFunc(mpd_qsub) |
---|
5063 | n/a | |
---|
5064 | n/a | static PyObject * |
---|
5065 | n/a | ctx_mpd_qdivmod(PyObject *context, PyObject *args) |
---|
5066 | n/a | { |
---|
5067 | n/a | PyObject *v, *w; |
---|
5068 | n/a | PyObject *a, *b; |
---|
5069 | n/a | PyObject *q, *r; |
---|
5070 | n/a | uint32_t status = 0; |
---|
5071 | n/a | PyObject *ret; |
---|
5072 | n/a | |
---|
5073 | n/a | if (!PyArg_ParseTuple(args, "OO", &v, &w)) { |
---|
5074 | n/a | return NULL; |
---|
5075 | n/a | } |
---|
5076 | n/a | |
---|
5077 | n/a | CONVERT_BINOP_RAISE(&a, &b, v, w, context); |
---|
5078 | n/a | |
---|
5079 | n/a | q = dec_alloc(); |
---|
5080 | n/a | if (q == NULL) { |
---|
5081 | n/a | Py_DECREF(a); |
---|
5082 | n/a | Py_DECREF(b); |
---|
5083 | n/a | return NULL; |
---|
5084 | n/a | } |
---|
5085 | n/a | r = dec_alloc(); |
---|
5086 | n/a | if (r == NULL) { |
---|
5087 | n/a | Py_DECREF(a); |
---|
5088 | n/a | Py_DECREF(b); |
---|
5089 | n/a | Py_DECREF(q); |
---|
5090 | n/a | return NULL; |
---|
5091 | n/a | } |
---|
5092 | n/a | |
---|
5093 | n/a | mpd_qdivmod(MPD(q), MPD(r), MPD(a), MPD(b), CTX(context), &status); |
---|
5094 | n/a | Py_DECREF(a); |
---|
5095 | n/a | Py_DECREF(b); |
---|
5096 | n/a | if (dec_addstatus(context, status)) { |
---|
5097 | n/a | Py_DECREF(r); |
---|
5098 | n/a | Py_DECREF(q); |
---|
5099 | n/a | return NULL; |
---|
5100 | n/a | } |
---|
5101 | n/a | |
---|
5102 | n/a | ret = Py_BuildValue("(OO)", q, r); |
---|
5103 | n/a | Py_DECREF(r); |
---|
5104 | n/a | Py_DECREF(q); |
---|
5105 | n/a | return ret; |
---|
5106 | n/a | } |
---|
5107 | n/a | |
---|
5108 | n/a | /* Binary or ternary arithmetic functions */ |
---|
5109 | n/a | static PyObject * |
---|
5110 | n/a | ctx_mpd_qpow(PyObject *context, PyObject *args, PyObject *kwds) |
---|
5111 | n/a | { |
---|
5112 | n/a | static char *kwlist[] = {"a", "b", "modulo", NULL}; |
---|
5113 | n/a | PyObject *base, *exp, *mod = Py_None; |
---|
5114 | n/a | PyObject *a, *b, *c = NULL; |
---|
5115 | n/a | PyObject *result; |
---|
5116 | n/a | uint32_t status = 0; |
---|
5117 | n/a | |
---|
5118 | n/a | if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO|O", kwlist, |
---|
5119 | n/a | &base, &exp, &mod)) { |
---|
5120 | n/a | return NULL; |
---|
5121 | n/a | } |
---|
5122 | n/a | |
---|
5123 | n/a | CONVERT_BINOP_RAISE(&a, &b, base, exp, context); |
---|
5124 | n/a | |
---|
5125 | n/a | if (mod != Py_None) { |
---|
5126 | n/a | if (!convert_op(TYPE_ERR, &c, mod, context)) { |
---|
5127 | n/a | Py_DECREF(a); |
---|
5128 | n/a | Py_DECREF(b); |
---|
5129 | n/a | return c; |
---|
5130 | n/a | } |
---|
5131 | n/a | } |
---|
5132 | n/a | |
---|
5133 | n/a | result = dec_alloc(); |
---|
5134 | n/a | if (result == NULL) { |
---|
5135 | n/a | Py_DECREF(a); |
---|
5136 | n/a | Py_DECREF(b); |
---|
5137 | n/a | Py_XDECREF(c); |
---|
5138 | n/a | return NULL; |
---|
5139 | n/a | } |
---|
5140 | n/a | |
---|
5141 | n/a | if (c == NULL) { |
---|
5142 | n/a | mpd_qpow(MPD(result), MPD(a), MPD(b), |
---|
5143 | n/a | CTX(context), &status); |
---|
5144 | n/a | } |
---|
5145 | n/a | else { |
---|
5146 | n/a | mpd_qpowmod(MPD(result), MPD(a), MPD(b), MPD(c), |
---|
5147 | n/a | CTX(context), &status); |
---|
5148 | n/a | Py_DECREF(c); |
---|
5149 | n/a | } |
---|
5150 | n/a | Py_DECREF(a); |
---|
5151 | n/a | Py_DECREF(b); |
---|
5152 | n/a | if (dec_addstatus(context, status)) { |
---|
5153 | n/a | Py_DECREF(result); |
---|
5154 | n/a | return NULL; |
---|
5155 | n/a | } |
---|
5156 | n/a | |
---|
5157 | n/a | return result; |
---|
5158 | n/a | } |
---|
5159 | n/a | |
---|
5160 | n/a | /* Ternary arithmetic functions */ |
---|
5161 | n/a | DecCtx_TernaryFunc(mpd_qfma) |
---|
5162 | n/a | |
---|
5163 | n/a | /* No argument */ |
---|
5164 | n/a | static PyObject * |
---|
5165 | n/a | ctx_mpd_radix(PyObject *context, PyObject *dummy) |
---|
5166 | n/a | { |
---|
5167 | n/a | return dec_mpd_radix(context, dummy); |
---|
5168 | n/a | } |
---|
5169 | n/a | |
---|
5170 | n/a | /* Boolean functions: single decimal argument */ |
---|
5171 | n/a | DecCtx_BoolFunc(mpd_isnormal) |
---|
5172 | n/a | DecCtx_BoolFunc(mpd_issubnormal) |
---|
5173 | n/a | DecCtx_BoolFunc_NO_CTX(mpd_isfinite) |
---|
5174 | n/a | DecCtx_BoolFunc_NO_CTX(mpd_isinfinite) |
---|
5175 | n/a | DecCtx_BoolFunc_NO_CTX(mpd_isnan) |
---|
5176 | n/a | DecCtx_BoolFunc_NO_CTX(mpd_isqnan) |
---|
5177 | n/a | DecCtx_BoolFunc_NO_CTX(mpd_issigned) |
---|
5178 | n/a | DecCtx_BoolFunc_NO_CTX(mpd_issnan) |
---|
5179 | n/a | DecCtx_BoolFunc_NO_CTX(mpd_iszero) |
---|
5180 | n/a | |
---|
5181 | n/a | static PyObject * |
---|
5182 | n/a | ctx_iscanonical(PyObject *context UNUSED, PyObject *v) |
---|
5183 | n/a | { |
---|
5184 | n/a | if (!PyDec_Check(v)) { |
---|
5185 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
5186 | n/a | "argument must be a Decimal"); |
---|
5187 | n/a | return NULL; |
---|
5188 | n/a | } |
---|
5189 | n/a | |
---|
5190 | n/a | return mpd_iscanonical(MPD(v)) ? incr_true() : incr_false(); |
---|
5191 | n/a | } |
---|
5192 | n/a | |
---|
5193 | n/a | /* Functions with a single decimal argument */ |
---|
5194 | n/a | static PyObject * |
---|
5195 | n/a | PyDecContext_Apply(PyObject *context, PyObject *v) |
---|
5196 | n/a | { |
---|
5197 | n/a | PyObject *result, *a; |
---|
5198 | n/a | |
---|
5199 | n/a | CONVERT_OP_RAISE(&a, v, context); |
---|
5200 | n/a | |
---|
5201 | n/a | result = dec_apply(a, context); |
---|
5202 | n/a | Py_DECREF(a); |
---|
5203 | n/a | return result; |
---|
5204 | n/a | } |
---|
5205 | n/a | |
---|
5206 | n/a | static PyObject * |
---|
5207 | n/a | ctx_canonical(PyObject *context UNUSED, PyObject *v) |
---|
5208 | n/a | { |
---|
5209 | n/a | if (!PyDec_Check(v)) { |
---|
5210 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
5211 | n/a | "argument must be a Decimal"); |
---|
5212 | n/a | return NULL; |
---|
5213 | n/a | } |
---|
5214 | n/a | |
---|
5215 | n/a | Py_INCREF(v); |
---|
5216 | n/a | return v; |
---|
5217 | n/a | } |
---|
5218 | n/a | |
---|
5219 | n/a | static PyObject * |
---|
5220 | n/a | ctx_mpd_qcopy_abs(PyObject *context, PyObject *v) |
---|
5221 | n/a | { |
---|
5222 | n/a | PyObject *result, *a; |
---|
5223 | n/a | uint32_t status = 0; |
---|
5224 | n/a | |
---|
5225 | n/a | CONVERT_OP_RAISE(&a, v, context); |
---|
5226 | n/a | |
---|
5227 | n/a | result = dec_alloc(); |
---|
5228 | n/a | if (result == NULL) { |
---|
5229 | n/a | Py_DECREF(a); |
---|
5230 | n/a | return NULL; |
---|
5231 | n/a | } |
---|
5232 | n/a | |
---|
5233 | n/a | mpd_qcopy_abs(MPD(result), MPD(a), &status); |
---|
5234 | n/a | Py_DECREF(a); |
---|
5235 | n/a | if (dec_addstatus(context, status)) { |
---|
5236 | n/a | Py_DECREF(result); |
---|
5237 | n/a | return NULL; |
---|
5238 | n/a | } |
---|
5239 | n/a | |
---|
5240 | n/a | return result; |
---|
5241 | n/a | } |
---|
5242 | n/a | |
---|
5243 | n/a | static PyObject * |
---|
5244 | n/a | ctx_copy_decimal(PyObject *context, PyObject *v) |
---|
5245 | n/a | { |
---|
5246 | n/a | PyObject *result; |
---|
5247 | n/a | |
---|
5248 | n/a | CONVERT_OP_RAISE(&result, v, context); |
---|
5249 | n/a | return result; |
---|
5250 | n/a | } |
---|
5251 | n/a | |
---|
5252 | n/a | static PyObject * |
---|
5253 | n/a | ctx_mpd_qcopy_negate(PyObject *context, PyObject *v) |
---|
5254 | n/a | { |
---|
5255 | n/a | PyObject *result, *a; |
---|
5256 | n/a | uint32_t status = 0; |
---|
5257 | n/a | |
---|
5258 | n/a | CONVERT_OP_RAISE(&a, v, context); |
---|
5259 | n/a | |
---|
5260 | n/a | result = dec_alloc(); |
---|
5261 | n/a | if (result == NULL) { |
---|
5262 | n/a | Py_DECREF(a); |
---|
5263 | n/a | return NULL; |
---|
5264 | n/a | } |
---|
5265 | n/a | |
---|
5266 | n/a | mpd_qcopy_negate(MPD(result), MPD(a), &status); |
---|
5267 | n/a | Py_DECREF(a); |
---|
5268 | n/a | if (dec_addstatus(context, status)) { |
---|
5269 | n/a | Py_DECREF(result); |
---|
5270 | n/a | return NULL; |
---|
5271 | n/a | } |
---|
5272 | n/a | |
---|
5273 | n/a | return result; |
---|
5274 | n/a | } |
---|
5275 | n/a | |
---|
5276 | n/a | DecCtx_UnaryFunc(mpd_qlogb) |
---|
5277 | n/a | DecCtx_UnaryFunc(mpd_qinvert) |
---|
5278 | n/a | |
---|
5279 | n/a | static PyObject * |
---|
5280 | n/a | ctx_mpd_class(PyObject *context, PyObject *v) |
---|
5281 | n/a | { |
---|
5282 | n/a | PyObject *a; |
---|
5283 | n/a | const char *cp; |
---|
5284 | n/a | |
---|
5285 | n/a | CONVERT_OP_RAISE(&a, v, context); |
---|
5286 | n/a | |
---|
5287 | n/a | cp = mpd_class(MPD(a), CTX(context)); |
---|
5288 | n/a | Py_DECREF(a); |
---|
5289 | n/a | |
---|
5290 | n/a | return PyUnicode_FromString(cp); |
---|
5291 | n/a | } |
---|
5292 | n/a | |
---|
5293 | n/a | static PyObject * |
---|
5294 | n/a | ctx_mpd_to_sci(PyObject *context, PyObject *v) |
---|
5295 | n/a | { |
---|
5296 | n/a | PyObject *result; |
---|
5297 | n/a | PyObject *a; |
---|
5298 | n/a | mpd_ssize_t size; |
---|
5299 | n/a | char *s; |
---|
5300 | n/a | |
---|
5301 | n/a | CONVERT_OP_RAISE(&a, v, context); |
---|
5302 | n/a | |
---|
5303 | n/a | size = mpd_to_sci_size(&s, MPD(a), CtxCaps(context)); |
---|
5304 | n/a | Py_DECREF(a); |
---|
5305 | n/a | if (size < 0) { |
---|
5306 | n/a | PyErr_NoMemory(); |
---|
5307 | n/a | return NULL; |
---|
5308 | n/a | } |
---|
5309 | n/a | |
---|
5310 | n/a | result = unicode_fromascii(s, size); |
---|
5311 | n/a | mpd_free(s); |
---|
5312 | n/a | |
---|
5313 | n/a | return result; |
---|
5314 | n/a | } |
---|
5315 | n/a | |
---|
5316 | n/a | static PyObject * |
---|
5317 | n/a | ctx_mpd_to_eng(PyObject *context, PyObject *v) |
---|
5318 | n/a | { |
---|
5319 | n/a | PyObject *result; |
---|
5320 | n/a | PyObject *a; |
---|
5321 | n/a | mpd_ssize_t size; |
---|
5322 | n/a | char *s; |
---|
5323 | n/a | |
---|
5324 | n/a | CONVERT_OP_RAISE(&a, v, context); |
---|
5325 | n/a | |
---|
5326 | n/a | size = mpd_to_eng_size(&s, MPD(a), CtxCaps(context)); |
---|
5327 | n/a | Py_DECREF(a); |
---|
5328 | n/a | if (size < 0) { |
---|
5329 | n/a | PyErr_NoMemory(); |
---|
5330 | n/a | return NULL; |
---|
5331 | n/a | } |
---|
5332 | n/a | |
---|
5333 | n/a | result = unicode_fromascii(s, size); |
---|
5334 | n/a | mpd_free(s); |
---|
5335 | n/a | |
---|
5336 | n/a | return result; |
---|
5337 | n/a | } |
---|
5338 | n/a | |
---|
5339 | n/a | /* Functions with two decimal arguments */ |
---|
5340 | n/a | DecCtx_BinaryFunc_NO_CTX(mpd_compare_total) |
---|
5341 | n/a | DecCtx_BinaryFunc_NO_CTX(mpd_compare_total_mag) |
---|
5342 | n/a | |
---|
5343 | n/a | static PyObject * |
---|
5344 | n/a | ctx_mpd_qcopy_sign(PyObject *context, PyObject *args) |
---|
5345 | n/a | { |
---|
5346 | n/a | PyObject *v, *w; |
---|
5347 | n/a | PyObject *a, *b; |
---|
5348 | n/a | PyObject *result; |
---|
5349 | n/a | uint32_t status = 0; |
---|
5350 | n/a | |
---|
5351 | n/a | if (!PyArg_ParseTuple(args, "OO", &v, &w)) { |
---|
5352 | n/a | return NULL; |
---|
5353 | n/a | } |
---|
5354 | n/a | |
---|
5355 | n/a | CONVERT_BINOP_RAISE(&a, &b, v, w, context); |
---|
5356 | n/a | |
---|
5357 | n/a | result = dec_alloc(); |
---|
5358 | n/a | if (result == NULL) { |
---|
5359 | n/a | Py_DECREF(a); |
---|
5360 | n/a | Py_DECREF(b); |
---|
5361 | n/a | return NULL; |
---|
5362 | n/a | } |
---|
5363 | n/a | |
---|
5364 | n/a | mpd_qcopy_sign(MPD(result), MPD(a), MPD(b), &status); |
---|
5365 | n/a | Py_DECREF(a); |
---|
5366 | n/a | Py_DECREF(b); |
---|
5367 | n/a | if (dec_addstatus(context, status)) { |
---|
5368 | n/a | Py_DECREF(result); |
---|
5369 | n/a | return NULL; |
---|
5370 | n/a | } |
---|
5371 | n/a | |
---|
5372 | n/a | return result; |
---|
5373 | n/a | } |
---|
5374 | n/a | |
---|
5375 | n/a | DecCtx_BinaryFunc(mpd_qand) |
---|
5376 | n/a | DecCtx_BinaryFunc(mpd_qor) |
---|
5377 | n/a | DecCtx_BinaryFunc(mpd_qxor) |
---|
5378 | n/a | |
---|
5379 | n/a | DecCtx_BinaryFunc(mpd_qrotate) |
---|
5380 | n/a | DecCtx_BinaryFunc(mpd_qscaleb) |
---|
5381 | n/a | DecCtx_BinaryFunc(mpd_qshift) |
---|
5382 | n/a | |
---|
5383 | n/a | static PyObject * |
---|
5384 | n/a | ctx_mpd_same_quantum(PyObject *context, PyObject *args) |
---|
5385 | n/a | { |
---|
5386 | n/a | PyObject *v, *w; |
---|
5387 | n/a | PyObject *a, *b; |
---|
5388 | n/a | PyObject *result; |
---|
5389 | n/a | |
---|
5390 | n/a | if (!PyArg_ParseTuple(args, "OO", &v, &w)) { |
---|
5391 | n/a | return NULL; |
---|
5392 | n/a | } |
---|
5393 | n/a | |
---|
5394 | n/a | CONVERT_BINOP_RAISE(&a, &b, v, w, context); |
---|
5395 | n/a | |
---|
5396 | n/a | result = mpd_same_quantum(MPD(a), MPD(b)) ? incr_true() : incr_false(); |
---|
5397 | n/a | Py_DECREF(a); |
---|
5398 | n/a | Py_DECREF(b); |
---|
5399 | n/a | |
---|
5400 | n/a | return result; |
---|
5401 | n/a | } |
---|
5402 | n/a | |
---|
5403 | n/a | |
---|
5404 | n/a | static PyMethodDef context_methods [] = |
---|
5405 | n/a | { |
---|
5406 | n/a | /* Unary arithmetic functions */ |
---|
5407 | n/a | { "abs", ctx_mpd_qabs, METH_O, doc_ctx_abs }, |
---|
5408 | n/a | { "exp", ctx_mpd_qexp, METH_O, doc_ctx_exp }, |
---|
5409 | n/a | { "ln", ctx_mpd_qln, METH_O, doc_ctx_ln }, |
---|
5410 | n/a | { "log10", ctx_mpd_qlog10, METH_O, doc_ctx_log10 }, |
---|
5411 | n/a | { "minus", ctx_mpd_qminus, METH_O, doc_ctx_minus }, |
---|
5412 | n/a | { "next_minus", ctx_mpd_qnext_minus, METH_O, doc_ctx_next_minus }, |
---|
5413 | n/a | { "next_plus", ctx_mpd_qnext_plus, METH_O, doc_ctx_next_plus }, |
---|
5414 | n/a | { "normalize", ctx_mpd_qreduce, METH_O, doc_ctx_normalize }, |
---|
5415 | n/a | { "plus", ctx_mpd_qplus, METH_O, doc_ctx_plus }, |
---|
5416 | n/a | { "to_integral", ctx_mpd_qround_to_int, METH_O, doc_ctx_to_integral }, |
---|
5417 | n/a | { "to_integral_exact", ctx_mpd_qround_to_intx, METH_O, doc_ctx_to_integral_exact }, |
---|
5418 | n/a | { "to_integral_value", ctx_mpd_qround_to_int, METH_O, doc_ctx_to_integral_value }, |
---|
5419 | n/a | { "sqrt", ctx_mpd_qsqrt, METH_O, doc_ctx_sqrt }, |
---|
5420 | n/a | |
---|
5421 | n/a | /* Binary arithmetic functions */ |
---|
5422 | n/a | { "add", ctx_mpd_qadd, METH_VARARGS, doc_ctx_add }, |
---|
5423 | n/a | { "compare", ctx_mpd_qcompare, METH_VARARGS, doc_ctx_compare }, |
---|
5424 | n/a | { "compare_signal", ctx_mpd_qcompare_signal, METH_VARARGS, doc_ctx_compare_signal }, |
---|
5425 | n/a | { "divide", ctx_mpd_qdiv, METH_VARARGS, doc_ctx_divide }, |
---|
5426 | n/a | { "divide_int", ctx_mpd_qdivint, METH_VARARGS, doc_ctx_divide_int }, |
---|
5427 | n/a | { "divmod", ctx_mpd_qdivmod, METH_VARARGS, doc_ctx_divmod }, |
---|
5428 | n/a | { "max", ctx_mpd_qmax, METH_VARARGS, doc_ctx_max }, |
---|
5429 | n/a | { "max_mag", ctx_mpd_qmax_mag, METH_VARARGS, doc_ctx_max_mag }, |
---|
5430 | n/a | { "min", ctx_mpd_qmin, METH_VARARGS, doc_ctx_min }, |
---|
5431 | n/a | { "min_mag", ctx_mpd_qmin_mag, METH_VARARGS, doc_ctx_min_mag }, |
---|
5432 | n/a | { "multiply", ctx_mpd_qmul, METH_VARARGS, doc_ctx_multiply }, |
---|
5433 | n/a | { "next_toward", ctx_mpd_qnext_toward, METH_VARARGS, doc_ctx_next_toward }, |
---|
5434 | n/a | { "quantize", ctx_mpd_qquantize, METH_VARARGS, doc_ctx_quantize }, |
---|
5435 | n/a | { "remainder", ctx_mpd_qrem, METH_VARARGS, doc_ctx_remainder }, |
---|
5436 | n/a | { "remainder_near", ctx_mpd_qrem_near, METH_VARARGS, doc_ctx_remainder_near }, |
---|
5437 | n/a | { "subtract", ctx_mpd_qsub, METH_VARARGS, doc_ctx_subtract }, |
---|
5438 | n/a | |
---|
5439 | n/a | /* Binary or ternary arithmetic functions */ |
---|
5440 | n/a | { "power", (PyCFunction)ctx_mpd_qpow, METH_VARARGS|METH_KEYWORDS, doc_ctx_power }, |
---|
5441 | n/a | |
---|
5442 | n/a | /* Ternary arithmetic functions */ |
---|
5443 | n/a | { "fma", ctx_mpd_qfma, METH_VARARGS, doc_ctx_fma }, |
---|
5444 | n/a | |
---|
5445 | n/a | /* No argument */ |
---|
5446 | n/a | { "Etiny", context_getetiny, METH_NOARGS, doc_ctx_Etiny }, |
---|
5447 | n/a | { "Etop", context_getetop, METH_NOARGS, doc_ctx_Etop }, |
---|
5448 | n/a | { "radix", ctx_mpd_radix, METH_NOARGS, doc_ctx_radix }, |
---|
5449 | n/a | |
---|
5450 | n/a | /* Boolean functions */ |
---|
5451 | n/a | { "is_canonical", ctx_iscanonical, METH_O, doc_ctx_is_canonical }, |
---|
5452 | n/a | { "is_finite", ctx_mpd_isfinite, METH_O, doc_ctx_is_finite }, |
---|
5453 | n/a | { "is_infinite", ctx_mpd_isinfinite, METH_O, doc_ctx_is_infinite }, |
---|
5454 | n/a | { "is_nan", ctx_mpd_isnan, METH_O, doc_ctx_is_nan }, |
---|
5455 | n/a | { "is_normal", ctx_mpd_isnormal, METH_O, doc_ctx_is_normal }, |
---|
5456 | n/a | { "is_qnan", ctx_mpd_isqnan, METH_O, doc_ctx_is_qnan }, |
---|
5457 | n/a | { "is_signed", ctx_mpd_issigned, METH_O, doc_ctx_is_signed }, |
---|
5458 | n/a | { "is_snan", ctx_mpd_issnan, METH_O, doc_ctx_is_snan }, |
---|
5459 | n/a | { "is_subnormal", ctx_mpd_issubnormal, METH_O, doc_ctx_is_subnormal }, |
---|
5460 | n/a | { "is_zero", ctx_mpd_iszero, METH_O, doc_ctx_is_zero }, |
---|
5461 | n/a | |
---|
5462 | n/a | /* Functions with a single decimal argument */ |
---|
5463 | n/a | { "_apply", PyDecContext_Apply, METH_O, NULL }, /* alias for apply */ |
---|
5464 | n/a | #ifdef EXTRA_FUNCTIONALITY |
---|
5465 | n/a | { "apply", PyDecContext_Apply, METH_O, doc_ctx_apply }, |
---|
5466 | n/a | #endif |
---|
5467 | n/a | { "canonical", ctx_canonical, METH_O, doc_ctx_canonical }, |
---|
5468 | n/a | { "copy_abs", ctx_mpd_qcopy_abs, METH_O, doc_ctx_copy_abs }, |
---|
5469 | n/a | { "copy_decimal", ctx_copy_decimal, METH_O, doc_ctx_copy_decimal }, |
---|
5470 | n/a | { "copy_negate", ctx_mpd_qcopy_negate, METH_O, doc_ctx_copy_negate }, |
---|
5471 | n/a | { "logb", ctx_mpd_qlogb, METH_O, doc_ctx_logb }, |
---|
5472 | n/a | { "logical_invert", ctx_mpd_qinvert, METH_O, doc_ctx_logical_invert }, |
---|
5473 | n/a | { "number_class", ctx_mpd_class, METH_O, doc_ctx_number_class }, |
---|
5474 | n/a | { "to_sci_string", ctx_mpd_to_sci, METH_O, doc_ctx_to_sci_string }, |
---|
5475 | n/a | { "to_eng_string", ctx_mpd_to_eng, METH_O, doc_ctx_to_eng_string }, |
---|
5476 | n/a | |
---|
5477 | n/a | /* Functions with two decimal arguments */ |
---|
5478 | n/a | { "compare_total", ctx_mpd_compare_total, METH_VARARGS, doc_ctx_compare_total }, |
---|
5479 | n/a | { "compare_total_mag", ctx_mpd_compare_total_mag, METH_VARARGS, doc_ctx_compare_total_mag }, |
---|
5480 | n/a | { "copy_sign", ctx_mpd_qcopy_sign, METH_VARARGS, doc_ctx_copy_sign }, |
---|
5481 | n/a | { "logical_and", ctx_mpd_qand, METH_VARARGS, doc_ctx_logical_and }, |
---|
5482 | n/a | { "logical_or", ctx_mpd_qor, METH_VARARGS, doc_ctx_logical_or }, |
---|
5483 | n/a | { "logical_xor", ctx_mpd_qxor, METH_VARARGS, doc_ctx_logical_xor }, |
---|
5484 | n/a | { "rotate", ctx_mpd_qrotate, METH_VARARGS, doc_ctx_rotate }, |
---|
5485 | n/a | { "same_quantum", ctx_mpd_same_quantum, METH_VARARGS, doc_ctx_same_quantum }, |
---|
5486 | n/a | { "scaleb", ctx_mpd_qscaleb, METH_VARARGS, doc_ctx_scaleb }, |
---|
5487 | n/a | { "shift", ctx_mpd_qshift, METH_VARARGS, doc_ctx_shift }, |
---|
5488 | n/a | |
---|
5489 | n/a | /* Set context values */ |
---|
5490 | n/a | { "clear_flags", context_clear_flags, METH_NOARGS, doc_ctx_clear_flags }, |
---|
5491 | n/a | { "clear_traps", context_clear_traps, METH_NOARGS, doc_ctx_clear_traps }, |
---|
5492 | n/a | |
---|
5493 | n/a | #ifdef CONFIG_32 |
---|
5494 | n/a | /* Unsafe set functions with relaxed range checks */ |
---|
5495 | n/a | { "_unsafe_setprec", context_unsafe_setprec, METH_O, NULL }, |
---|
5496 | n/a | { "_unsafe_setemin", context_unsafe_setemin, METH_O, NULL }, |
---|
5497 | n/a | { "_unsafe_setemax", context_unsafe_setemax, METH_O, NULL }, |
---|
5498 | n/a | #endif |
---|
5499 | n/a | |
---|
5500 | n/a | /* Miscellaneous */ |
---|
5501 | n/a | { "__copy__", (PyCFunction)context_copy, METH_NOARGS, NULL }, |
---|
5502 | n/a | { "__reduce__", context_reduce, METH_NOARGS, NULL }, |
---|
5503 | n/a | { "copy", (PyCFunction)context_copy, METH_NOARGS, doc_ctx_copy }, |
---|
5504 | n/a | { "create_decimal", ctx_create_decimal, METH_VARARGS, doc_ctx_create_decimal }, |
---|
5505 | n/a | { "create_decimal_from_float", ctx_from_float, METH_O, doc_ctx_create_decimal_from_float }, |
---|
5506 | n/a | |
---|
5507 | n/a | { NULL, NULL, 1 } |
---|
5508 | n/a | }; |
---|
5509 | n/a | |
---|
5510 | n/a | static PyTypeObject PyDecContext_Type = |
---|
5511 | n/a | { |
---|
5512 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
---|
5513 | n/a | "decimal.Context", /* tp_name */ |
---|
5514 | n/a | sizeof(PyDecContextObject), /* tp_basicsize */ |
---|
5515 | n/a | 0, /* tp_itemsize */ |
---|
5516 | n/a | (destructor) context_dealloc, /* tp_dealloc */ |
---|
5517 | n/a | 0, /* tp_print */ |
---|
5518 | n/a | (getattrfunc) 0, /* tp_getattr */ |
---|
5519 | n/a | (setattrfunc) 0, /* tp_setattr */ |
---|
5520 | n/a | 0, /* tp_reserved */ |
---|
5521 | n/a | (reprfunc) context_repr, /* tp_repr */ |
---|
5522 | n/a | 0, /* tp_as_number */ |
---|
5523 | n/a | 0, /* tp_as_sequence */ |
---|
5524 | n/a | 0, /* tp_as_mapping */ |
---|
5525 | n/a | (hashfunc) 0, /* tp_hash */ |
---|
5526 | n/a | 0, /* tp_call */ |
---|
5527 | n/a | (reprfunc) context_repr, /* tp_str */ |
---|
5528 | n/a | (getattrofunc) context_getattr, /* tp_getattro */ |
---|
5529 | n/a | (setattrofunc) context_setattr, /* tp_setattro */ |
---|
5530 | n/a | (PyBufferProcs *) 0, /* tp_as_buffer */ |
---|
5531 | n/a | Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */ |
---|
5532 | n/a | doc_context, /* tp_doc */ |
---|
5533 | n/a | 0, /* tp_traverse */ |
---|
5534 | n/a | 0, /* tp_clear */ |
---|
5535 | n/a | 0, /* tp_richcompare */ |
---|
5536 | n/a | 0, /* tp_weaklistoffset */ |
---|
5537 | n/a | 0, /* tp_iter */ |
---|
5538 | n/a | 0, /* tp_iternext */ |
---|
5539 | n/a | context_methods, /* tp_methods */ |
---|
5540 | n/a | 0, /* tp_members */ |
---|
5541 | n/a | context_getsets, /* tp_getset */ |
---|
5542 | n/a | 0, /* tp_base */ |
---|
5543 | n/a | 0, /* tp_dict */ |
---|
5544 | n/a | 0, /* tp_descr_get */ |
---|
5545 | n/a | 0, /* tp_descr_set */ |
---|
5546 | n/a | 0, /* tp_dictoffset */ |
---|
5547 | n/a | context_init, /* tp_init */ |
---|
5548 | n/a | 0, /* tp_alloc */ |
---|
5549 | n/a | context_new, /* tp_new */ |
---|
5550 | n/a | PyObject_Del, /* tp_free */ |
---|
5551 | n/a | }; |
---|
5552 | n/a | |
---|
5553 | n/a | |
---|
5554 | n/a | static PyMethodDef _decimal_methods [] = |
---|
5555 | n/a | { |
---|
5556 | n/a | { "getcontext", (PyCFunction)PyDec_GetCurrentContext, METH_NOARGS, doc_getcontext}, |
---|
5557 | n/a | { "setcontext", (PyCFunction)PyDec_SetCurrentContext, METH_O, doc_setcontext}, |
---|
5558 | n/a | { "localcontext", (PyCFunction)ctxmanager_new, METH_VARARGS|METH_KEYWORDS, doc_localcontext}, |
---|
5559 | n/a | #ifdef EXTRA_FUNCTIONALITY |
---|
5560 | n/a | { "IEEEContext", (PyCFunction)ieee_context, METH_O, doc_ieee_context}, |
---|
5561 | n/a | #endif |
---|
5562 | n/a | { NULL, NULL, 1, NULL } |
---|
5563 | n/a | }; |
---|
5564 | n/a | |
---|
5565 | n/a | static struct PyModuleDef _decimal_module = { |
---|
5566 | n/a | PyModuleDef_HEAD_INIT, |
---|
5567 | n/a | "decimal", |
---|
5568 | n/a | doc__decimal, |
---|
5569 | n/a | -1, |
---|
5570 | n/a | _decimal_methods, |
---|
5571 | n/a | NULL, |
---|
5572 | n/a | NULL, |
---|
5573 | n/a | NULL, |
---|
5574 | n/a | NULL |
---|
5575 | n/a | }; |
---|
5576 | n/a | |
---|
5577 | n/a | struct ssize_constmap { const char *name; mpd_ssize_t val; }; |
---|
5578 | n/a | static struct ssize_constmap ssize_constants [] = { |
---|
5579 | n/a | {"MAX_PREC", MPD_MAX_PREC}, |
---|
5580 | n/a | {"MAX_EMAX", MPD_MAX_EMAX}, |
---|
5581 | n/a | {"MIN_EMIN", MPD_MIN_EMIN}, |
---|
5582 | n/a | {"MIN_ETINY", MPD_MIN_ETINY}, |
---|
5583 | n/a | {NULL} |
---|
5584 | n/a | }; |
---|
5585 | n/a | |
---|
5586 | n/a | struct int_constmap { const char *name; int val; }; |
---|
5587 | n/a | static struct int_constmap int_constants [] = { |
---|
5588 | n/a | /* int constants */ |
---|
5589 | n/a | #ifdef EXTRA_FUNCTIONALITY |
---|
5590 | n/a | {"DECIMAL32", MPD_DECIMAL32}, |
---|
5591 | n/a | {"DECIMAL64", MPD_DECIMAL64}, |
---|
5592 | n/a | {"DECIMAL128", MPD_DECIMAL128}, |
---|
5593 | n/a | {"IEEE_CONTEXT_MAX_BITS", MPD_IEEE_CONTEXT_MAX_BITS}, |
---|
5594 | n/a | /* int condition flags */ |
---|
5595 | n/a | {"DecClamped", MPD_Clamped}, |
---|
5596 | n/a | {"DecConversionSyntax", MPD_Conversion_syntax}, |
---|
5597 | n/a | {"DecDivisionByZero", MPD_Division_by_zero}, |
---|
5598 | n/a | {"DecDivisionImpossible", MPD_Division_impossible}, |
---|
5599 | n/a | {"DecDivisionUndefined", MPD_Division_undefined}, |
---|
5600 | n/a | {"DecFpuError", MPD_Fpu_error}, |
---|
5601 | n/a | {"DecInexact", MPD_Inexact}, |
---|
5602 | n/a | {"DecInvalidContext", MPD_Invalid_context}, |
---|
5603 | n/a | {"DecInvalidOperation", MPD_Invalid_operation}, |
---|
5604 | n/a | {"DecIEEEInvalidOperation", MPD_IEEE_Invalid_operation}, |
---|
5605 | n/a | {"DecMallocError", MPD_Malloc_error}, |
---|
5606 | n/a | {"DecFloatOperation", MPD_Float_operation}, |
---|
5607 | n/a | {"DecOverflow", MPD_Overflow}, |
---|
5608 | n/a | {"DecRounded", MPD_Rounded}, |
---|
5609 | n/a | {"DecSubnormal", MPD_Subnormal}, |
---|
5610 | n/a | {"DecUnderflow", MPD_Underflow}, |
---|
5611 | n/a | {"DecErrors", MPD_Errors}, |
---|
5612 | n/a | {"DecTraps", MPD_Traps}, |
---|
5613 | n/a | #endif |
---|
5614 | n/a | {NULL} |
---|
5615 | n/a | }; |
---|
5616 | n/a | |
---|
5617 | n/a | |
---|
5618 | n/a | #define CHECK_INT(expr) \ |
---|
5619 | n/a | do { if ((expr) < 0) goto error; } while (0) |
---|
5620 | n/a | #define ASSIGN_PTR(result, expr) \ |
---|
5621 | n/a | do { result = (expr); if (result == NULL) goto error; } while (0) |
---|
5622 | n/a | #define CHECK_PTR(expr) \ |
---|
5623 | n/a | do { if ((expr) == NULL) goto error; } while (0) |
---|
5624 | n/a | |
---|
5625 | n/a | |
---|
5626 | n/a | static PyCFunction |
---|
5627 | n/a | cfunc_noargs(PyTypeObject *t, const char *name) |
---|
5628 | n/a | { |
---|
5629 | n/a | struct PyMethodDef *m; |
---|
5630 | n/a | |
---|
5631 | n/a | if (t->tp_methods == NULL) { |
---|
5632 | n/a | goto error; |
---|
5633 | n/a | } |
---|
5634 | n/a | |
---|
5635 | n/a | for (m = t->tp_methods; m->ml_name != NULL; m++) { |
---|
5636 | n/a | if (strcmp(name, m->ml_name) == 0) { |
---|
5637 | n/a | if (!(m->ml_flags & METH_NOARGS)) { |
---|
5638 | n/a | goto error; |
---|
5639 | n/a | } |
---|
5640 | n/a | return m->ml_meth; |
---|
5641 | n/a | } |
---|
5642 | n/a | } |
---|
5643 | n/a | |
---|
5644 | n/a | error: |
---|
5645 | n/a | PyErr_Format(PyExc_RuntimeError, |
---|
5646 | n/a | "internal error: could not find method %s", name); |
---|
5647 | n/a | return NULL; |
---|
5648 | n/a | } |
---|
5649 | n/a | |
---|
5650 | n/a | |
---|
5651 | n/a | PyMODINIT_FUNC |
---|
5652 | n/a | PyInit__decimal(void) |
---|
5653 | n/a | { |
---|
5654 | n/a | PyObject *m = NULL; |
---|
5655 | n/a | PyObject *numbers = NULL; |
---|
5656 | n/a | PyObject *Number = NULL; |
---|
5657 | n/a | PyObject *collections = NULL; |
---|
5658 | n/a | PyObject *MutableMapping = NULL; |
---|
5659 | n/a | PyObject *obj = NULL; |
---|
5660 | n/a | DecCondMap *cm; |
---|
5661 | n/a | struct ssize_constmap *ssize_cm; |
---|
5662 | n/a | struct int_constmap *int_cm; |
---|
5663 | n/a | int i; |
---|
5664 | n/a | |
---|
5665 | n/a | |
---|
5666 | n/a | /* Init libmpdec */ |
---|
5667 | n/a | mpd_traphandler = dec_traphandler; |
---|
5668 | n/a | mpd_mallocfunc = PyMem_Malloc; |
---|
5669 | n/a | mpd_reallocfunc = PyMem_Realloc; |
---|
5670 | n/a | mpd_callocfunc = mpd_callocfunc_em; |
---|
5671 | n/a | mpd_free = PyMem_Free; |
---|
5672 | n/a | mpd_setminalloc(_Py_DEC_MINALLOC); |
---|
5673 | n/a | |
---|
5674 | n/a | |
---|
5675 | n/a | /* Init external C-API functions */ |
---|
5676 | n/a | _py_long_multiply = PyLong_Type.tp_as_number->nb_multiply; |
---|
5677 | n/a | _py_long_floor_divide = PyLong_Type.tp_as_number->nb_floor_divide; |
---|
5678 | n/a | _py_long_power = PyLong_Type.tp_as_number->nb_power; |
---|
5679 | n/a | _py_float_abs = PyFloat_Type.tp_as_number->nb_absolute; |
---|
5680 | n/a | ASSIGN_PTR(_py_float_as_integer_ratio, cfunc_noargs(&PyFloat_Type, |
---|
5681 | n/a | "as_integer_ratio")); |
---|
5682 | n/a | ASSIGN_PTR(_py_long_bit_length, cfunc_noargs(&PyLong_Type, "bit_length")); |
---|
5683 | n/a | |
---|
5684 | n/a | |
---|
5685 | n/a | /* Init types */ |
---|
5686 | n/a | PyDec_Type.tp_base = &PyBaseObject_Type; |
---|
5687 | n/a | PyDecContext_Type.tp_base = &PyBaseObject_Type; |
---|
5688 | n/a | PyDecContextManager_Type.tp_base = &PyBaseObject_Type; |
---|
5689 | n/a | PyDecSignalDictMixin_Type.tp_base = &PyBaseObject_Type; |
---|
5690 | n/a | |
---|
5691 | n/a | CHECK_INT(PyType_Ready(&PyDec_Type)); |
---|
5692 | n/a | CHECK_INT(PyType_Ready(&PyDecContext_Type)); |
---|
5693 | n/a | CHECK_INT(PyType_Ready(&PyDecSignalDictMixin_Type)); |
---|
5694 | n/a | CHECK_INT(PyType_Ready(&PyDecContextManager_Type)); |
---|
5695 | n/a | |
---|
5696 | n/a | ASSIGN_PTR(obj, PyUnicode_FromString("decimal")); |
---|
5697 | n/a | CHECK_INT(PyDict_SetItemString(PyDec_Type.tp_dict, "__module__", obj)); |
---|
5698 | n/a | CHECK_INT(PyDict_SetItemString(PyDecContext_Type.tp_dict, |
---|
5699 | n/a | "__module__", obj)); |
---|
5700 | n/a | Py_CLEAR(obj); |
---|
5701 | n/a | |
---|
5702 | n/a | |
---|
5703 | n/a | /* Numeric abstract base classes */ |
---|
5704 | n/a | ASSIGN_PTR(numbers, PyImport_ImportModule("numbers")); |
---|
5705 | n/a | ASSIGN_PTR(Number, PyObject_GetAttrString(numbers, "Number")); |
---|
5706 | n/a | /* Register Decimal with the Number abstract base class */ |
---|
5707 | n/a | ASSIGN_PTR(obj, PyObject_CallMethod(Number, "register", "(O)", |
---|
5708 | n/a | (PyObject *)&PyDec_Type)); |
---|
5709 | n/a | Py_CLEAR(obj); |
---|
5710 | n/a | /* Rational is a global variable used for fraction comparisons. */ |
---|
5711 | n/a | ASSIGN_PTR(Rational, PyObject_GetAttrString(numbers, "Rational")); |
---|
5712 | n/a | /* Done with numbers, Number */ |
---|
5713 | n/a | Py_CLEAR(numbers); |
---|
5714 | n/a | Py_CLEAR(Number); |
---|
5715 | n/a | |
---|
5716 | n/a | /* DecimalTuple */ |
---|
5717 | n/a | ASSIGN_PTR(collections, PyImport_ImportModule("collections")); |
---|
5718 | n/a | ASSIGN_PTR(DecimalTuple, (PyTypeObject *)PyObject_CallMethod(collections, |
---|
5719 | n/a | "namedtuple", "(ss)", "DecimalTuple", |
---|
5720 | n/a | "sign digits exponent")); |
---|
5721 | n/a | |
---|
5722 | n/a | ASSIGN_PTR(obj, PyUnicode_FromString("decimal")); |
---|
5723 | n/a | CHECK_INT(PyDict_SetItemString(DecimalTuple->tp_dict, "__module__", obj)); |
---|
5724 | n/a | Py_CLEAR(obj); |
---|
5725 | n/a | |
---|
5726 | n/a | /* MutableMapping */ |
---|
5727 | n/a | ASSIGN_PTR(MutableMapping, PyObject_GetAttrString(collections, |
---|
5728 | n/a | "MutableMapping")); |
---|
5729 | n/a | /* Create SignalDict type */ |
---|
5730 | n/a | ASSIGN_PTR(PyDecSignalDict_Type, |
---|
5731 | n/a | (PyTypeObject *)PyObject_CallFunction( |
---|
5732 | n/a | (PyObject *)&PyType_Type, "s(OO){}", |
---|
5733 | n/a | "SignalDict", &PyDecSignalDictMixin_Type, |
---|
5734 | n/a | MutableMapping)); |
---|
5735 | n/a | |
---|
5736 | n/a | /* Done with collections, MutableMapping */ |
---|
5737 | n/a | Py_CLEAR(collections); |
---|
5738 | n/a | Py_CLEAR(MutableMapping); |
---|
5739 | n/a | |
---|
5740 | n/a | |
---|
5741 | n/a | /* Create the module */ |
---|
5742 | n/a | ASSIGN_PTR(m, PyModule_Create(&_decimal_module)); |
---|
5743 | n/a | |
---|
5744 | n/a | |
---|
5745 | n/a | /* Add types to the module */ |
---|
5746 | n/a | Py_INCREF(&PyDec_Type); |
---|
5747 | n/a | CHECK_INT(PyModule_AddObject(m, "Decimal", (PyObject *)&PyDec_Type)); |
---|
5748 | n/a | Py_INCREF(&PyDecContext_Type); |
---|
5749 | n/a | CHECK_INT(PyModule_AddObject(m, "Context", |
---|
5750 | n/a | (PyObject *)&PyDecContext_Type)); |
---|
5751 | n/a | Py_INCREF(DecimalTuple); |
---|
5752 | n/a | CHECK_INT(PyModule_AddObject(m, "DecimalTuple", (PyObject *)DecimalTuple)); |
---|
5753 | n/a | |
---|
5754 | n/a | |
---|
5755 | n/a | /* Create top level exception */ |
---|
5756 | n/a | ASSIGN_PTR(DecimalException, PyErr_NewException( |
---|
5757 | n/a | "decimal.DecimalException", |
---|
5758 | n/a | PyExc_ArithmeticError, NULL)); |
---|
5759 | n/a | Py_INCREF(DecimalException); |
---|
5760 | n/a | CHECK_INT(PyModule_AddObject(m, "DecimalException", DecimalException)); |
---|
5761 | n/a | |
---|
5762 | n/a | /* Create signal tuple */ |
---|
5763 | n/a | ASSIGN_PTR(SignalTuple, PyTuple_New(SIGNAL_MAP_LEN)); |
---|
5764 | n/a | |
---|
5765 | n/a | /* Add exceptions that correspond to IEEE signals */ |
---|
5766 | n/a | for (i = SIGNAL_MAP_LEN-1; i >= 0; i--) { |
---|
5767 | n/a | PyObject *base; |
---|
5768 | n/a | |
---|
5769 | n/a | cm = signal_map + i; |
---|
5770 | n/a | |
---|
5771 | n/a | switch (cm->flag) { |
---|
5772 | n/a | case MPD_Float_operation: |
---|
5773 | n/a | base = PyTuple_Pack(2, DecimalException, PyExc_TypeError); |
---|
5774 | n/a | break; |
---|
5775 | n/a | case MPD_Division_by_zero: |
---|
5776 | n/a | base = PyTuple_Pack(2, DecimalException, PyExc_ZeroDivisionError); |
---|
5777 | n/a | break; |
---|
5778 | n/a | case MPD_Overflow: |
---|
5779 | n/a | base = PyTuple_Pack(2, signal_map[INEXACT].ex, |
---|
5780 | n/a | signal_map[ROUNDED].ex); |
---|
5781 | n/a | break; |
---|
5782 | n/a | case MPD_Underflow: |
---|
5783 | n/a | base = PyTuple_Pack(3, signal_map[INEXACT].ex, |
---|
5784 | n/a | signal_map[ROUNDED].ex, |
---|
5785 | n/a | signal_map[SUBNORMAL].ex); |
---|
5786 | n/a | break; |
---|
5787 | n/a | default: |
---|
5788 | n/a | base = PyTuple_Pack(1, DecimalException); |
---|
5789 | n/a | break; |
---|
5790 | n/a | } |
---|
5791 | n/a | |
---|
5792 | n/a | if (base == NULL) { |
---|
5793 | n/a | goto error; /* GCOV_NOT_REACHED */ |
---|
5794 | n/a | } |
---|
5795 | n/a | |
---|
5796 | n/a | ASSIGN_PTR(cm->ex, PyErr_NewException(cm->fqname, base, NULL)); |
---|
5797 | n/a | Py_DECREF(base); |
---|
5798 | n/a | |
---|
5799 | n/a | /* add to module */ |
---|
5800 | n/a | Py_INCREF(cm->ex); |
---|
5801 | n/a | CHECK_INT(PyModule_AddObject(m, cm->name, cm->ex)); |
---|
5802 | n/a | |
---|
5803 | n/a | /* add to signal tuple */ |
---|
5804 | n/a | Py_INCREF(cm->ex); |
---|
5805 | n/a | PyTuple_SET_ITEM(SignalTuple, i, cm->ex); |
---|
5806 | n/a | } |
---|
5807 | n/a | |
---|
5808 | n/a | /* |
---|
5809 | n/a | * Unfortunately, InvalidOperation is a signal that comprises |
---|
5810 | n/a | * several conditions, including InvalidOperation! Naming the |
---|
5811 | n/a | * signal IEEEInvalidOperation would prevent the confusion. |
---|
5812 | n/a | */ |
---|
5813 | n/a | cond_map[0].ex = signal_map[0].ex; |
---|
5814 | n/a | |
---|
5815 | n/a | /* Add remaining exceptions, inherit from InvalidOperation */ |
---|
5816 | n/a | for (cm = cond_map+1; cm->name != NULL; cm++) { |
---|
5817 | n/a | PyObject *base; |
---|
5818 | n/a | if (cm->flag == MPD_Division_undefined) { |
---|
5819 | n/a | base = PyTuple_Pack(2, signal_map[0].ex, PyExc_ZeroDivisionError); |
---|
5820 | n/a | } |
---|
5821 | n/a | else { |
---|
5822 | n/a | base = PyTuple_Pack(1, signal_map[0].ex); |
---|
5823 | n/a | } |
---|
5824 | n/a | if (base == NULL) { |
---|
5825 | n/a | goto error; /* GCOV_NOT_REACHED */ |
---|
5826 | n/a | } |
---|
5827 | n/a | |
---|
5828 | n/a | ASSIGN_PTR(cm->ex, PyErr_NewException(cm->fqname, base, NULL)); |
---|
5829 | n/a | Py_DECREF(base); |
---|
5830 | n/a | |
---|
5831 | n/a | Py_INCREF(cm->ex); |
---|
5832 | n/a | CHECK_INT(PyModule_AddObject(m, cm->name, cm->ex)); |
---|
5833 | n/a | } |
---|
5834 | n/a | |
---|
5835 | n/a | |
---|
5836 | n/a | /* Init default context template first */ |
---|
5837 | n/a | ASSIGN_PTR(default_context_template, |
---|
5838 | n/a | PyObject_CallObject((PyObject *)&PyDecContext_Type, NULL)); |
---|
5839 | n/a | Py_INCREF(default_context_template); |
---|
5840 | n/a | CHECK_INT(PyModule_AddObject(m, "DefaultContext", |
---|
5841 | n/a | default_context_template)); |
---|
5842 | n/a | |
---|
5843 | n/a | #ifdef WITHOUT_THREADS |
---|
5844 | n/a | /* Init module context */ |
---|
5845 | n/a | ASSIGN_PTR(module_context, |
---|
5846 | n/a | PyObject_CallObject((PyObject *)&PyDecContext_Type, NULL)); |
---|
5847 | n/a | Py_INCREF(Py_False); |
---|
5848 | n/a | CHECK_INT(PyModule_AddObject(m, "HAVE_THREADS", Py_False)); |
---|
5849 | n/a | #else |
---|
5850 | n/a | ASSIGN_PTR(tls_context_key, PyUnicode_FromString("___DECIMAL_CTX__")); |
---|
5851 | n/a | Py_INCREF(Py_True); |
---|
5852 | n/a | CHECK_INT(PyModule_AddObject(m, "HAVE_THREADS", Py_True)); |
---|
5853 | n/a | #endif |
---|
5854 | n/a | |
---|
5855 | n/a | /* Init basic context template */ |
---|
5856 | n/a | ASSIGN_PTR(basic_context_template, |
---|
5857 | n/a | PyObject_CallObject((PyObject *)&PyDecContext_Type, NULL)); |
---|
5858 | n/a | init_basic_context(basic_context_template); |
---|
5859 | n/a | Py_INCREF(basic_context_template); |
---|
5860 | n/a | CHECK_INT(PyModule_AddObject(m, "BasicContext", |
---|
5861 | n/a | basic_context_template)); |
---|
5862 | n/a | |
---|
5863 | n/a | /* Init extended context template */ |
---|
5864 | n/a | ASSIGN_PTR(extended_context_template, |
---|
5865 | n/a | PyObject_CallObject((PyObject *)&PyDecContext_Type, NULL)); |
---|
5866 | n/a | init_extended_context(extended_context_template); |
---|
5867 | n/a | Py_INCREF(extended_context_template); |
---|
5868 | n/a | CHECK_INT(PyModule_AddObject(m, "ExtendedContext", |
---|
5869 | n/a | extended_context_template)); |
---|
5870 | n/a | |
---|
5871 | n/a | |
---|
5872 | n/a | /* Init mpd_ssize_t constants */ |
---|
5873 | n/a | for (ssize_cm = ssize_constants; ssize_cm->name != NULL; ssize_cm++) { |
---|
5874 | n/a | ASSIGN_PTR(obj, PyLong_FromSsize_t(ssize_cm->val)); |
---|
5875 | n/a | CHECK_INT(PyModule_AddObject(m, ssize_cm->name, obj)); |
---|
5876 | n/a | obj = NULL; |
---|
5877 | n/a | } |
---|
5878 | n/a | |
---|
5879 | n/a | /* Init int constants */ |
---|
5880 | n/a | for (int_cm = int_constants; int_cm->name != NULL; int_cm++) { |
---|
5881 | n/a | CHECK_INT(PyModule_AddIntConstant(m, int_cm->name, |
---|
5882 | n/a | int_cm->val)); |
---|
5883 | n/a | } |
---|
5884 | n/a | |
---|
5885 | n/a | /* Init string constants */ |
---|
5886 | n/a | for (i = 0; i < _PY_DEC_ROUND_GUARD; i++) { |
---|
5887 | n/a | ASSIGN_PTR(round_map[i], PyUnicode_InternFromString(mpd_round_string[i])); |
---|
5888 | n/a | Py_INCREF(round_map[i]); |
---|
5889 | n/a | CHECK_INT(PyModule_AddObject(m, mpd_round_string[i], round_map[i])); |
---|
5890 | n/a | } |
---|
5891 | n/a | |
---|
5892 | n/a | /* Add specification version number */ |
---|
5893 | n/a | CHECK_INT(PyModule_AddStringConstant(m, "__version__", "1.70")); |
---|
5894 | n/a | CHECK_INT(PyModule_AddStringConstant(m, "__libmpdec_version__", mpd_version())); |
---|
5895 | n/a | |
---|
5896 | n/a | |
---|
5897 | n/a | return m; |
---|
5898 | n/a | |
---|
5899 | n/a | |
---|
5900 | n/a | error: |
---|
5901 | n/a | Py_CLEAR(obj); /* GCOV_NOT_REACHED */ |
---|
5902 | n/a | Py_CLEAR(numbers); /* GCOV_NOT_REACHED */ |
---|
5903 | n/a | Py_CLEAR(Number); /* GCOV_NOT_REACHED */ |
---|
5904 | n/a | Py_CLEAR(Rational); /* GCOV_NOT_REACHED */ |
---|
5905 | n/a | Py_CLEAR(collections); /* GCOV_NOT_REACHED */ |
---|
5906 | n/a | Py_CLEAR(MutableMapping); /* GCOV_NOT_REACHED */ |
---|
5907 | n/a | Py_CLEAR(SignalTuple); /* GCOV_NOT_REACHED */ |
---|
5908 | n/a | Py_CLEAR(DecimalTuple); /* GCOV_NOT_REACHED */ |
---|
5909 | n/a | #ifdef WITHOUT_THREADS |
---|
5910 | n/a | Py_CLEAR(module_context); /* GCOV_NOT_REACHED */ |
---|
5911 | n/a | #else |
---|
5912 | n/a | Py_CLEAR(default_context_template); /* GCOV_NOT_REACHED */ |
---|
5913 | n/a | Py_CLEAR(tls_context_key); /* GCOV_NOT_REACHED */ |
---|
5914 | n/a | #endif |
---|
5915 | n/a | Py_CLEAR(basic_context_template); /* GCOV_NOT_REACHED */ |
---|
5916 | n/a | Py_CLEAR(extended_context_template); /* GCOV_NOT_REACHED */ |
---|
5917 | n/a | Py_CLEAR(m); /* GCOV_NOT_REACHED */ |
---|
5918 | n/a | |
---|
5919 | n/a | return NULL; /* GCOV_NOT_REACHED */ |
---|
5920 | n/a | } |
---|
5921 | n/a | |
---|
5922 | n/a | |
---|