LEFT | RIGHT |
1 /* | 1 /* |
2 * Copyright (c) 2008-2010 Stefan Krah. All rights reserved. | 2 * Copyright (c) 2008-2010 Stefan Krah. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * | 7 * |
8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * | 10 * |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 {"Clamped", "decimal.Clamped", MPD_Clamped, NULL}, | 157 {"Clamped", "decimal.Clamped", MPD_Clamped, NULL}, |
158 {NULL} | 158 {NULL} |
159 }; | 159 }; |
160 | 160 |
161 /* Exceptions that inherit from InvalidOperation */ | 161 /* Exceptions that inherit from InvalidOperation */ |
162 static DecCondMap cond_map[] = { | 162 static DecCondMap cond_map[] = { |
163 {"InvalidOperation", "decimal.InvalidOperation", MPD_Invalid_operation, NULL}, | 163 {"InvalidOperation", "decimal.InvalidOperation", MPD_Invalid_operation, NULL}, |
164 {"ConversionSyntax", "decimal.ConversionSyntax", MPD_Conversion_syntax, NULL}, | 164 {"ConversionSyntax", "decimal.ConversionSyntax", MPD_Conversion_syntax, NULL}, |
165 {"DivisionImpossible", "decimal.DivisionImpossible", MPD_Division_impossible,
NULL}, | 165 {"DivisionImpossible", "decimal.DivisionImpossible", MPD_Division_impossible,
NULL}, |
166 {"DivisionUndefined", "decimal.DivisionUndefined", MPD_Division_undefined, NUL
L}, | 166 {"DivisionUndefined", "decimal.DivisionUndefined", MPD_Division_undefined, NUL
L}, |
167 {"FpuError", "decimal.FpuError", MPD_Fpu_error, NULL}, | |
168 {"InvalidContext", "decimal.InvalidContext", MPD_Invalid_context, NULL}, | 167 {"InvalidContext", "decimal.InvalidContext", MPD_Invalid_context, NULL}, |
169 {"MallocError", "decimal.MallocError", MPD_Malloc_error, NULL}, | 168 {"MallocError", "decimal.MallocError", MPD_Malloc_error, NULL}, |
170 {NULL} | 169 {NULL} |
171 }; | 170 }; |
172 | 171 |
173 static const char *dec_signal_string[MPD_NUM_FLAGS] = { | 172 static const char *dec_signal_string[MPD_NUM_FLAGS] = { |
174 "Clamped", | 173 "Clamped", |
175 "InvalidOperation", | 174 "InvalidOperation", |
176 "DivisionByZero", | 175 "DivisionByZero", |
177 "InvalidOperation", | 176 "InvalidOperation", |
(...skipping 15 matching lines...) Expand all Loading... |
193 [ROUND_CEILING, ROUND_FLOOR, ROUND_UP, ROUND_DOWN,\n\ | 192 [ROUND_CEILING, ROUND_FLOOR, ROUND_UP, ROUND_DOWN,\n\ |
194 ROUND_HALF_UP, ROUND_HALF_DOWN, ROUND_HALF_EVEN,\n\ | 193 ROUND_HALF_UP, ROUND_HALF_DOWN, ROUND_HALF_EVEN,\n\ |
195 ROUND_05UP]"; | 194 ROUND_05UP]"; |
196 | 195 |
197 static const char *invalid_signals_err = | 196 static const char *invalid_signals_err = |
198 "valid values for signals are:\n\ | 197 "valid values for signals are:\n\ |
199 [InvalidOperation, FloatOperation, DivisionByZero,\n\ | 198 [InvalidOperation, FloatOperation, DivisionByZero,\n\ |
200 Overflow, Underflow, Subnormal, Inexact, Rounded,\n\ | 199 Overflow, Underflow, Subnormal, Inexact, Rounded,\n\ |
201 Clamped]"; | 200 Clamped]"; |
202 | 201 |
| 202 #ifdef EXTRA_FUNCTIONALITY |
203 static const char *invalid_flags_err = | 203 static const char *invalid_flags_err = |
204 "valid values for _flags or _traps are:\n\ | 204 "valid values for _flags or _traps are:\n\ |
205 signals:\n\ | 205 signals:\n\ |
206 [DecIEEEInvalidOperation, DecFloatOperation, DecDivisionByZero,\n\ | 206 [DecIEEEInvalidOperation, DecFloatOperation, DecDivisionByZero,\n\ |
207 DecOverflow, DecUnderflow, DecSubnormal, DecInexact, DecRounded,\n\ | 207 DecOverflow, DecUnderflow, DecSubnormal, DecInexact, DecRounded,\n\ |
208 DecClamped]\n\ | 208 DecClamped]\n\ |
209 conditions which trigger DecIEEEInvalidOperation:\n\ | 209 conditions which trigger DecIEEEInvalidOperation:\n\ |
210 [DecInvalidOperation, DecConversionSyntax, DecDivisionImpossible,\n\ | 210 [DecInvalidOperation, DecConversionSyntax, DecDivisionImpossible,\n\ |
211 DecDivisionUndefined, DecFpuError, DecInvalidContext, DecMallocError]"; | 211 DecDivisionUndefined, DecFpuError, DecInvalidContext, DecMallocError]"; |
| 212 #endif |
212 | 213 |
213 static int | 214 static int |
214 value_error_int(const char *mesg) | 215 value_error_int(const char *mesg) |
215 { | 216 { |
216 PyErr_SetString(PyExc_ValueError, mesg); | 217 PyErr_SetString(PyExc_ValueError, mesg); |
217 return -1; | 218 return -1; |
| 219 } |
| 220 |
| 221 static PyObject * |
| 222 value_error_ptr(const char *mesg) |
| 223 { |
| 224 PyErr_SetString(PyExc_ValueError, mesg); |
| 225 return NULL; |
218 } | 226 } |
219 | 227 |
220 static int | 228 static int |
221 type_error_int(const char *mesg) | 229 type_error_int(const char *mesg) |
222 { | 230 { |
223 PyErr_SetString(PyExc_TypeError, mesg); | 231 PyErr_SetString(PyExc_TypeError, mesg); |
224 return -1; | 232 return -1; |
225 } | 233 } |
226 | 234 |
227 static PyObject * | 235 static PyObject * |
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
415 return DEC_ERR_OCCURRED; | 423 return DEC_ERR_OCCURRED; |
416 } | 424 } |
417 if (x == 1) { | 425 if (x == 1) { |
418 flags |= cm->flag; | 426 flags |= cm->flag; |
419 } | 427 } |
420 } | 428 } |
421 | 429 |
422 return flags; | 430 return flags; |
423 } | 431 } |
424 | 432 |
| 433 #ifdef EXTRA_FUNCTIONALITY |
425 static uint32_t | 434 static uint32_t |
426 long_as_flags(PyObject *v) | 435 long_as_flags(PyObject *v) |
427 { | 436 { |
428 long x; | 437 long x; |
429 | 438 |
430 x = PyLong_AsLong(v); | 439 x = PyLong_AsLong(v); |
431 if (x == -1 && PyErr_Occurred()) { | 440 if (x == -1 && PyErr_Occurred()) { |
432 return DEC_ERR_OCCURRED; | 441 return DEC_ERR_OCCURRED; |
433 } | 442 } |
434 if (x < 0 || x > (long)MPD_Max_status) { | 443 if (x < 0 || x > (long)MPD_Max_status) { |
435 PyErr_SetString(PyExc_TypeError, invalid_flags_err); | 444 PyErr_SetString(PyExc_TypeError, invalid_flags_err); |
436 return DEC_INVALID_SIGNALS; | 445 return DEC_INVALID_SIGNALS; |
437 } | 446 } |
438 | 447 |
439 return x; | 448 return x; |
440 } | 449 } |
| 450 #endif |
441 | 451 |
442 static int | 452 static int |
443 dec_addstatus(PyObject *context, uint32_t status) | 453 dec_addstatus(PyObject *context, uint32_t status) |
444 { | 454 { |
445 mpd_context_t *ctx = CTX(context); | 455 mpd_context_t *ctx = CTX(context); |
446 | 456 |
447 ctx->status |= status; | 457 ctx->status |= status; |
448 if (ctx->traps&status) { | 458 if (ctx->traps&status) { |
449 PyObject *ex, *siglist; | 459 PyObject *ex, *siglist; |
450 | 460 |
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
673 { \ | 683 { \ |
674 return PyLong_FromUnsignedLong(mpd_get##mem(CTX(self))); \ | 684 return PyLong_FromUnsignedLong(mpd_get##mem(CTX(self))); \ |
675 } | 685 } |
676 | 686 |
677 Dec_CONTEXT_GET_SSIZE(prec) | 687 Dec_CONTEXT_GET_SSIZE(prec) |
678 Dec_CONTEXT_GET_SSIZE(emax) | 688 Dec_CONTEXT_GET_SSIZE(emax) |
679 Dec_CONTEXT_GET_SSIZE(emin) | 689 Dec_CONTEXT_GET_SSIZE(emin) |
680 Dec_CONTEXT_GET_SSIZE(round) | 690 Dec_CONTEXT_GET_SSIZE(round) |
681 Dec_CONTEXT_GET_SSIZE(clamp) | 691 Dec_CONTEXT_GET_SSIZE(clamp) |
682 | 692 |
| 693 #ifdef EXTRA_FUNCTIONALITY |
683 Dec_CONTEXT_GET_ULONG(traps) | 694 Dec_CONTEXT_GET_ULONG(traps) |
684 Dec_CONTEXT_GET_ULONG(status) | 695 Dec_CONTEXT_GET_ULONG(status) |
| 696 #endif |
685 | 697 |
686 static PyObject * | 698 static PyObject * |
687 context_getcapitals(PyObject *self, void *closure UNUSED) | 699 context_getcapitals(PyObject *self, void *closure UNUSED) |
688 { | 700 { |
689 return PyLong_FromLong(CtxCaps(self)); | 701 return PyLong_FromLong(CtxCaps(self)); |
690 } | 702 } |
691 | 703 |
| 704 #ifdef EXTRA_FUNCTIONALITY |
692 static PyObject * | 705 static PyObject * |
693 context_getallcr(PyObject *self, void *closure UNUSED) | 706 context_getallcr(PyObject *self, void *closure UNUSED) |
694 { | 707 { |
695 return PyLong_FromLong(mpd_getcr(CTX(self))); | 708 return PyLong_FromLong(mpd_getcr(CTX(self))); |
696 } | 709 } |
| 710 #endif |
697 | 711 |
698 static PyObject * | 712 static PyObject * |
699 context_getetiny(PyObject *self, PyObject *dummy UNUSED) | 713 context_getetiny(PyObject *self, PyObject *dummy UNUSED) |
700 { | 714 { |
701 return PyLong_FromSsize_t(mpd_etiny(CTX(self))); | 715 return PyLong_FromSsize_t(mpd_etiny(CTX(self))); |
702 } | 716 } |
703 | 717 |
704 static PyObject * | 718 static PyObject * |
705 context_getetop(PyObject *self, PyObject *dummy UNUSED) | 719 context_getetop(PyObject *self, PyObject *dummy UNUSED) |
706 { | 720 { |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
760 | 774 |
761 ctx = CTX(self); | 775 ctx = CTX(self); |
762 if (!mpd_qsetemax(ctx, x)) { | 776 if (!mpd_qsetemax(ctx, x)) { |
763 return value_error_int( | 777 return value_error_int( |
764 "valid range for Emax is [0, MAX_EMAX]"); | 778 "valid range for Emax is [0, MAX_EMAX]"); |
765 } | 779 } |
766 | 780 |
767 return 0; | 781 return 0; |
768 } | 782 } |
769 | 783 |
| 784 #ifdef CONFIG_32 |
770 static PyObject * | 785 static PyObject * |
771 context_unsafe_setprec(PyObject *self, PyObject *value) | 786 context_unsafe_setprec(PyObject *self, PyObject *value) |
772 { | 787 { |
773 mpd_context_t *ctx = CTX(self); | 788 mpd_context_t *ctx = CTX(self); |
774 mpd_ssize_t x; | 789 mpd_ssize_t x; |
775 | 790 |
776 x = PyLong_AsSsize_t(value); | 791 x = PyLong_AsSsize_t(value); |
777 if (x == -1 && PyErr_Occurred()) { | 792 if (x == -1 && PyErr_Occurred()) { |
778 return NULL; | 793 return NULL; |
779 } | 794 } |
780 | 795 |
| 796 if (x < 1 || x > 1070000000L) { |
| 797 return value_error_ptr( |
| 798 "valid range for unsafe prec is [1, 1070000000]"); |
| 799 } |
| 800 |
781 ctx->prec = x; | 801 ctx->prec = x; |
782 Py_RETURN_NONE; | 802 Py_RETURN_NONE; |
783 } | 803 } |
784 | 804 |
785 static PyObject * | 805 static PyObject * |
786 context_unsafe_setemin(PyObject *self, PyObject *value) | 806 context_unsafe_setemin(PyObject *self, PyObject *value) |
787 { | 807 { |
788 mpd_context_t *ctx = CTX(self); | 808 mpd_context_t *ctx = CTX(self); |
789 mpd_ssize_t x; | 809 mpd_ssize_t x; |
790 | 810 |
791 x = PyLong_AsSsize_t(value); | 811 x = PyLong_AsSsize_t(value); |
792 if (x == -1 && PyErr_Occurred()) { | 812 if (x == -1 && PyErr_Occurred()) { |
793 return NULL; | 813 return NULL; |
794 } | 814 } |
795 | 815 |
| 816 if (x < -1070000000L || x > 0) { |
| 817 return value_error_ptr( |
| 818 "valid range for unsafe emin is [-1070000000, 0]"); |
| 819 } |
| 820 |
796 ctx->emin = x; | 821 ctx->emin = x; |
797 Py_RETURN_NONE; | 822 Py_RETURN_NONE; |
798 } | 823 } |
799 | 824 |
800 static PyObject * | 825 static PyObject * |
801 context_unsafe_setemax(PyObject *self, PyObject *value) | 826 context_unsafe_setemax(PyObject *self, PyObject *value) |
802 { | 827 { |
803 mpd_context_t *ctx = CTX(self); | 828 mpd_context_t *ctx = CTX(self); |
804 mpd_ssize_t x; | 829 mpd_ssize_t x; |
805 | 830 |
806 x = PyLong_AsSsize_t(value); | 831 x = PyLong_AsSsize_t(value); |
807 if (x == -1 && PyErr_Occurred()) { | 832 if (x == -1 && PyErr_Occurred()) { |
808 return NULL; | 833 return NULL; |
809 } | 834 } |
810 | 835 |
| 836 if (x < 0 || x > 1070000000L) { |
| 837 return value_error_ptr( |
| 838 "valid range for unsafe emax is [0, 1070000000]"); |
| 839 } |
| 840 |
811 ctx->emax = x; | 841 ctx->emax = x; |
812 Py_RETURN_NONE; | 842 Py_RETURN_NONE; |
813 } | 843 } |
| 844 #endif |
814 | 845 |
815 static int | 846 static int |
816 context_setround(PyObject *self, PyObject *value, void *closure UNUSED) | 847 context_setround(PyObject *self, PyObject *value, void *closure UNUSED) |
817 { | 848 { |
818 mpd_context_t *ctx; | 849 mpd_context_t *ctx; |
819 mpd_ssize_t x; | 850 mpd_ssize_t x; |
820 | 851 |
821 x = PyLong_AsSsize_t(value); | 852 x = PyLong_AsSsize_t(value); |
822 if (x == -1 && PyErr_Occurred()) { | 853 if (x == -1 && PyErr_Occurred()) { |
823 return -1; | 854 return -1; |
(...skipping 20 matching lines...) Expand all Loading... |
844 | 875 |
845 if (x != 0 && x != 1) { | 876 if (x != 0 && x != 1) { |
846 return value_error_int( | 877 return value_error_int( |
847 "valid values for capitals are 0 or 1"); | 878 "valid values for capitals are 0 or 1"); |
848 } | 879 } |
849 CtxCaps(self) = (int)x; | 880 CtxCaps(self) = (int)x; |
850 | 881 |
851 return 0; | 882 return 0; |
852 } | 883 } |
853 | 884 |
| 885 #ifdef EXTRA_FUNCTIONALITY |
854 static int | 886 static int |
855 context_settraps(PyObject *self, PyObject *value, void *closure UNUSED) | 887 context_settraps(PyObject *self, PyObject *value, void *closure UNUSED) |
856 { | 888 { |
857 mpd_context_t *ctx; | 889 mpd_context_t *ctx; |
858 uint32_t flags; | 890 uint32_t flags; |
859 | 891 |
860 flags = long_as_flags(value); | 892 flags = long_as_flags(value); |
861 if (flags & DEC_ERRORS) { | 893 if (flags & DEC_ERRORS) { |
862 return -1; | 894 return -1; |
863 } | 895 } |
864 | 896 |
865 ctx = CTX(self); | 897 ctx = CTX(self); |
866 if (!mpd_qsettraps(ctx, flags)) { | 898 if (!mpd_qsettraps(ctx, flags)) { |
867 INTERNAL_ERROR_INT("context_settraps"); | 899 INTERNAL_ERROR_INT("context_settraps"); |
868 } | 900 } |
869 | 901 |
870 return 0; | 902 return 0; |
871 } | 903 } |
| 904 #endif |
872 | 905 |
873 static int | 906 static int |
874 context_settraps_list(PyObject *self, PyObject *value) | 907 context_settraps_list(PyObject *self, PyObject *value) |
875 { | 908 { |
876 mpd_context_t *ctx; | 909 mpd_context_t *ctx; |
877 uint32_t flags; | 910 uint32_t flags; |
878 | 911 |
879 flags = list_as_flags(value); | 912 flags = list_as_flags(value); |
880 if (flags & DEC_ERRORS) { | 913 if (flags & DEC_ERRORS) { |
881 return -1; | 914 return -1; |
(...skipping 24 matching lines...) Expand all Loading... |
906 } | 939 } |
907 | 940 |
908 ctx = CTX(self); | 941 ctx = CTX(self); |
909 if (!mpd_qsettraps(ctx, flags)) { | 942 if (!mpd_qsettraps(ctx, flags)) { |
910 INTERNAL_ERROR_INT("context_settraps_dict"); | 943 INTERNAL_ERROR_INT("context_settraps_dict"); |
911 } | 944 } |
912 | 945 |
913 return 0; | 946 return 0; |
914 } | 947 } |
915 | 948 |
| 949 #ifdef EXTRA_FUNCTIONALITY |
916 static int | 950 static int |
917 context_setstatus(PyObject *self, PyObject *value, void *closure UNUSED) | 951 context_setstatus(PyObject *self, PyObject *value, void *closure UNUSED) |
918 { | 952 { |
919 mpd_context_t *ctx; | 953 mpd_context_t *ctx; |
920 uint32_t flags; | 954 uint32_t flags; |
921 | 955 |
922 flags = long_as_flags(value); | 956 flags = long_as_flags(value); |
923 if (flags & DEC_ERRORS) { | 957 if (flags & DEC_ERRORS) { |
924 return -1; | 958 return -1; |
925 } | 959 } |
926 | 960 |
927 ctx = CTX(self); | 961 ctx = CTX(self); |
928 if (!mpd_qsetstatus(ctx, flags)) { | 962 if (!mpd_qsetstatus(ctx, flags)) { |
929 INTERNAL_ERROR_INT("context_setstatus"); | 963 INTERNAL_ERROR_INT("context_setstatus"); |
930 } | 964 } |
931 | 965 |
932 return 0; | 966 return 0; |
933 } | 967 } |
| 968 #endif |
934 | 969 |
935 static int | 970 static int |
936 context_setstatus_list(PyObject *self, PyObject *value) | 971 context_setstatus_list(PyObject *self, PyObject *value) |
937 { | 972 { |
938 mpd_context_t *ctx; | 973 mpd_context_t *ctx; |
939 uint32_t flags; | 974 uint32_t flags; |
940 | 975 |
941 flags = list_as_flags(value); | 976 flags = list_as_flags(value); |
942 if (flags & DEC_ERRORS) { | 977 if (flags & DEC_ERRORS) { |
943 return -1; | 978 return -1; |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
988 BOUNDS_CHECK(x, INT_MIN, INT_MAX); | 1023 BOUNDS_CHECK(x, INT_MIN, INT_MAX); |
989 | 1024 |
990 ctx = CTX(self); | 1025 ctx = CTX(self); |
991 if (!mpd_qsetclamp(ctx, (int)x)) { | 1026 if (!mpd_qsetclamp(ctx, (int)x)) { |
992 return value_error_int("valid values for clamp are 0 or 1"); | 1027 return value_error_int("valid values for clamp are 0 or 1"); |
993 } | 1028 } |
994 | 1029 |
995 return 0; | 1030 return 0; |
996 } | 1031 } |
997 | 1032 |
| 1033 #ifdef EXTRA_FUNCTIONALITY |
998 static int | 1034 static int |
999 context_setallcr(PyObject *self, PyObject *value, void *closure UNUSED) | 1035 context_setallcr(PyObject *self, PyObject *value, void *closure UNUSED) |
1000 { | 1036 { |
1001 mpd_context_t *ctx; | 1037 mpd_context_t *ctx; |
1002 mpd_ssize_t x; | 1038 mpd_ssize_t x; |
1003 | 1039 |
1004 x = PyLong_AsSsize_t(value); | 1040 x = PyLong_AsSsize_t(value); |
1005 if (x == -1 && PyErr_Occurred()) { | 1041 if (x == -1 && PyErr_Occurred()) { |
1006 return -1; | 1042 return -1; |
1007 } | 1043 } |
1008 BOUNDS_CHECK(x, INT_MIN, INT_MAX); | 1044 BOUNDS_CHECK(x, INT_MIN, INT_MAX); |
1009 | 1045 |
1010 ctx = CTX(self); | 1046 ctx = CTX(self); |
1011 if (!mpd_qsetcr(ctx, (int)x)) { | 1047 if (!mpd_qsetcr(ctx, (int)x)) { |
1012 return value_error_int("valid values for _allcr are 0 or 1"); | 1048 return value_error_int("valid values for _allcr are 0 or 1"); |
1013 } | 1049 } |
1014 | 1050 |
1015 return 0; | 1051 return 0; |
1016 } | 1052 } |
| 1053 #endif |
1017 | 1054 |
1018 static PyObject * | 1055 static PyObject * |
1019 context_getattr(PyObject *self, PyObject *name) | 1056 context_getattr(PyObject *self, PyObject *name) |
1020 { | 1057 { |
1021 PyObject *retval; | 1058 PyObject *retval; |
1022 | 1059 |
1023 if (PyUnicode_Check(name)) { | 1060 if (PyUnicode_Check(name)) { |
1024 if (PyUnicode_CompareWithASCIIString(name, "traps") == 0) { | 1061 if (PyUnicode_CompareWithASCIIString(name, "traps") == 0) { |
1025 retval = ((PyDecContextObject *)self)->traps; | 1062 retval = ((PyDecContextObject *)self)->traps; |
1026 Py_INCREF(retval); | 1063 Py_INCREF(retval); |
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1198 !mpd_qsetstatus(ctx, t.status)) { | 1235 !mpd_qsetstatus(ctx, t.status)) { |
1199 return type_error_int("invalid context"); | 1236 return type_error_int("invalid context"); |
1200 } | 1237 } |
1201 | 1238 |
1202 if (capitals != 0 && capitals != 1) { | 1239 if (capitals != 0 && capitals != 1) { |
1203 return value_error_int("invalid context"); | 1240 return value_error_int("invalid context"); |
1204 } | 1241 } |
1205 CtxCaps(self) = capitals; | 1242 CtxCaps(self) = capitals; |
1206 | 1243 |
1207 if (traps != NULL) { | 1244 if (traps != NULL) { |
1208 » » if (PyLong_Check(traps)) { | 1245 » » if (PyList_Check(traps)) { |
| 1246 » » » ret = context_settraps_list(self, traps); |
| 1247 » » } |
| 1248 #ifdef EXTRA_FUNCTIONALITY |
| 1249 » » else if (PyLong_Check(traps)) { |
1209 ret = context_settraps(self, traps, NULL); | 1250 ret = context_settraps(self, traps, NULL); |
1210 } | 1251 } |
1211 » » else if (PyList_Check(traps)) { | 1252 #endif |
1212 » » » ret = context_settraps_list(self, traps); | |
1213 » » } | |
1214 else { | 1253 else { |
1215 ret = context_settraps_dict(self, traps); | 1254 ret = context_settraps_dict(self, traps); |
1216 } | 1255 } |
1217 if (ret < 0) { | 1256 if (ret < 0) { |
1218 return ret; | 1257 return ret; |
1219 } | 1258 } |
1220 } | 1259 } |
1221 if (status != NULL) { | 1260 if (status != NULL) { |
1222 » » if (PyLong_Check(status)) { | 1261 » » if (PyList_Check(status)) { |
| 1262 » » » ret = context_setstatus_list(self, status); |
| 1263 » » } |
| 1264 #ifdef EXTRA_FUNCTIONALITY |
| 1265 » » else if (PyLong_Check(status)) { |
1223 ret = context_setstatus(self, status, NULL); | 1266 ret = context_setstatus(self, status, NULL); |
1224 } | 1267 } |
1225 » » else if (PyList_Check(status)) { | 1268 #endif |
1226 » » » ret = context_setstatus_list(self, status); | |
1227 » » } | |
1228 else { | 1269 else { |
1229 ret = context_setstatus_dict(self, status); | 1270 ret = context_setstatus_dict(self, status); |
1230 } | 1271 } |
1231 if (ret < 0) { | 1272 if (ret < 0) { |
1232 return ret; | 1273 return ret; |
1233 } | 1274 } |
1234 } | 1275 } |
1235 | 1276 |
1236 return 0; | 1277 return 0; |
1237 } | 1278 } |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1283 { | 1324 { |
1284 mpd_context_t ctx = dflt_ctx; | 1325 mpd_context_t ctx = dflt_ctx; |
1285 | 1326 |
1286 ctx.prec = 9; | 1327 ctx.prec = 9; |
1287 ctx.traps = 0; | 1328 ctx.traps = 0; |
1288 | 1329 |
1289 *CTX(v) = ctx; | 1330 *CTX(v) = ctx; |
1290 CtxCaps(v) = 1; | 1331 CtxCaps(v) = 1; |
1291 } | 1332 } |
1292 | 1333 |
| 1334 #ifdef EXTRA_FUNCTIONALITY |
1293 /* Factory function for creating IEEE interchange format contexts */ | 1335 /* Factory function for creating IEEE interchange format contexts */ |
1294 static PyObject * | 1336 static PyObject * |
1295 ieee_context(PyObject *dummy UNUSED, PyObject *v) | 1337 ieee_context(PyObject *dummy UNUSED, PyObject *v) |
1296 { | 1338 { |
1297 PyObject *context; | 1339 PyObject *context; |
1298 mpd_ssize_t bits; | 1340 mpd_ssize_t bits; |
1299 mpd_context_t ctx; | 1341 mpd_context_t ctx; |
1300 | 1342 |
1301 bits = PyLong_AsSsize_t(v); | 1343 bits = PyLong_AsSsize_t(v); |
1302 if (bits == -1 && PyErr_Occurred()) { | 1344 if (bits == -1 && PyErr_Occurred()) { |
(...skipping 14 matching lines...) Expand all Loading... |
1317 | 1359 |
1318 return context; | 1360 return context; |
1319 | 1361 |
1320 error: | 1362 error: |
1321 PyErr_Format(PyExc_ValueError, | 1363 PyErr_Format(PyExc_ValueError, |
1322 "argument must be a multiple of 32, with a maximum of %d", | 1364 "argument must be a multiple of 32, with a maximum of %d", |
1323 MPD_IEEE_CONTEXT_MAX_BITS); | 1365 MPD_IEEE_CONTEXT_MAX_BITS); |
1324 | 1366 |
1325 return NULL; | 1367 return NULL; |
1326 } | 1368 } |
| 1369 #endif |
1327 | 1370 |
1328 static PyObject * | 1371 static PyObject * |
1329 context_copy(PyObject *self) | 1372 context_copy(PyObject *self) |
1330 { | 1373 { |
1331 PyObject *copy; | 1374 PyObject *copy; |
1332 | 1375 |
1333 copy = PyObject_CallObject((PyObject *)&PyDecContext_Type, NULL); | 1376 copy = PyObject_CallObject((PyObject *)&PyDecContext_Type, NULL); |
1334 if (copy == NULL) { | 1377 if (copy == NULL) { |
1335 return NULL; | 1378 return NULL; |
1336 } | 1379 } |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1376 | 1419 |
1377 | 1420 |
1378 static PyGetSetDef context_getsets [] = | 1421 static PyGetSetDef context_getsets [] = |
1379 { | 1422 { |
1380 { "prec", (getter)context_getprec, (setter)context_setprec, NULL, NULL}, | 1423 { "prec", (getter)context_getprec, (setter)context_setprec, NULL, NULL}, |
1381 { "Emax", (getter)context_getemax, (setter)context_setemax, NULL, NULL}, | 1424 { "Emax", (getter)context_getemax, (setter)context_setemax, NULL, NULL}, |
1382 { "Emin", (getter)context_getemin, (setter)context_setemin, NULL, NULL}, | 1425 { "Emin", (getter)context_getemin, (setter)context_setemin, NULL, NULL}, |
1383 { "rounding", (getter)context_getround, (setter)context_setround, NULL, NULL}, | 1426 { "rounding", (getter)context_getround, (setter)context_setround, NULL, NULL}, |
1384 { "capitals", (getter)context_getcapitals, (setter)context_setcapitals, NULL,
NULL}, | 1427 { "capitals", (getter)context_getcapitals, (setter)context_setcapitals, NULL,
NULL}, |
1385 { "clamp", (getter)context_getclamp, (setter)context_setclamp, NULL, NULL}, | 1428 { "clamp", (getter)context_getclamp, (setter)context_setclamp, NULL, NULL}, |
| 1429 #ifdef EXTRA_FUNCTIONALITY |
1386 { "_allcr", (getter)context_getallcr, (setter)context_setallcr, NULL, NULL}, | 1430 { "_allcr", (getter)context_getallcr, (setter)context_setallcr, NULL, NULL}, |
1387 { "_traps", (getter)context_gettraps, (setter)context_settraps, NULL, NULL}, | 1431 { "_traps", (getter)context_gettraps, (setter)context_settraps, NULL, NULL}, |
1388 { "_flags", (getter)context_getstatus, (setter)context_setstatus, NULL, NULL}, | 1432 { "_flags", (getter)context_getstatus, (setter)context_setstatus, NULL, NULL}, |
| 1433 #endif |
1389 {NULL} | 1434 {NULL} |
1390 }; | 1435 }; |
1391 | 1436 |
1392 | 1437 |
1393 #define CONTEXT_CHECK(obj) \ | 1438 #define CONTEXT_CHECK(obj) \ |
1394 if (!PyDecContext_Check(obj)) { \ | 1439 if (!PyDecContext_Check(obj)) { \ |
1395 PyErr_SetString(PyExc_TypeError, \ | 1440 PyErr_SetString(PyExc_TypeError, \ |
1396 "argument must be a context"); \ | 1441 "argument must be a context"); \ |
1397 return NULL; \ | 1442 return NULL; \ |
1398 } | 1443 } |
(...skipping 1625 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3024 if ((sep = PyUnicode_AsUTF8String(sep)) == NULL) { | 3069 if ((sep = PyUnicode_AsUTF8String(sep)) == NULL) { |
3025 goto finish; | 3070 goto finish; |
3026 } | 3071 } |
3027 spec.sep = PyBytes_AS_STRING(sep); | 3072 spec.sep = PyBytes_AS_STRING(sep); |
3028 } | 3073 } |
3029 if ((grouping = PyDict_GetItemString(override, "grouping"))) { | 3074 if ((grouping = PyDict_GetItemString(override, "grouping"))) { |
3030 if ((grouping = PyUnicode_AsUTF8String(grouping)) == NUL
L) { | 3075 if ((grouping = PyUnicode_AsUTF8String(grouping)) == NUL
L) { |
3031 goto finish; | 3076 goto finish; |
3032 } | 3077 } |
3033 spec.grouping = PyBytes_AS_STRING(grouping); | 3078 spec.grouping = PyBytes_AS_STRING(grouping); |
| 3079 } |
| 3080 if (mpd_validate_lconv(&spec) < 0) { |
| 3081 PyErr_SetString(PyExc_ValueError, |
| 3082 "invalid override dict"); |
| 3083 goto finish; |
3034 } | 3084 } |
3035 } | 3085 } |
3036 else { | 3086 else { |
3037 n = strlen(spec.dot); | 3087 n = strlen(spec.dot); |
3038 if (n > 1 || (n == 1 && !isascii((uchar)spec.dot[0]))) { | 3088 if (n > 1 || (n == 1 && !isascii((uchar)spec.dot[0]))) { |
3039 /* fix locale dependent non-ascii characters */ | 3089 /* fix locale dependent non-ascii characters */ |
3040 dot = dotsep_as_utf8(spec.dot); | 3090 dot = dotsep_as_utf8(spec.dot); |
3041 if (dot == NULL) { | 3091 if (dot == NULL) { |
3042 goto finish; | 3092 goto finish; |
3043 } | 3093 } |
(...skipping 692 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3736 Py_DECREF(b); | 3786 Py_DECREF(b); |
3737 Py_XDECREF(c); | 3787 Py_XDECREF(c); |
3738 return NULL; | 3788 return NULL; |
3739 } | 3789 } |
3740 | 3790 |
3741 if (c == NULL) { | 3791 if (c == NULL) { |
3742 mpd_qpow(MPD(result), MPD(a), MPD(b), | 3792 mpd_qpow(MPD(result), MPD(a), MPD(b), |
3743 CTX(context), &status); | 3793 CTX(context), &status); |
3744 } | 3794 } |
3745 else { | 3795 else { |
3746 mpd_context_t maxctx; | |
3747 mpd_maxcontext(&maxctx); | |
3748 mpd_qpowmod(MPD(result), MPD(a), MPD(b), MPD(c), | 3796 mpd_qpowmod(MPD(result), MPD(a), MPD(b), MPD(c), |
3749 CTX(context), &status); | 3797 CTX(context), &status); |
| 3798 status = (status == MPD_Clamped) ? 0 : status; |
3750 /* remove ideal exponent for compatibility with decimal.py */ | 3799 /* remove ideal exponent for compatibility with decimal.py */ |
3751 mpd_qquantize(MPD(result), MPD(result), &zero, | 3800 mpd_qquantize(MPD(result), MPD(result), &zero, |
3752 » » &maxctx, &status); | 3801 » » CTX(context), &status); |
3753 Py_DECREF(c); | 3802 Py_DECREF(c); |
3754 } | 3803 } |
3755 Py_DECREF(a); | 3804 Py_DECREF(a); |
3756 Py_DECREF(b); | 3805 Py_DECREF(b); |
3757 if (dec_addstatus(context, status)) { | 3806 if (dec_addstatus(context, status)) { |
3758 Py_DECREF(result); | 3807 Py_DECREF(result); |
3759 return NULL; | 3808 return NULL; |
3760 } | 3809 } |
3761 | 3810 |
3762 return result; | 3811 return result; |
(...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4068 r = (r == -1); | 4117 r = (r == -1); |
4069 break; | 4118 break; |
4070 case Py_GT: | 4119 case Py_GT: |
4071 r = (r == 1); | 4120 r = (r == 1); |
4072 break; | 4121 break; |
4073 } | 4122 } |
4074 | 4123 |
4075 return PyBool_FromLong(r); | 4124 return PyBool_FromLong(r); |
4076 } | 4125 } |
4077 | 4126 |
| 4127 /* __ceil__ */ |
| 4128 static PyObject * |
| 4129 dec_ceil(PyObject *self, PyObject *dummy UNUSED) |
| 4130 { |
| 4131 PyObject *context; |
| 4132 |
| 4133 CURRENT_CONTEXT(context); |
| 4134 return dec_as_long(self, context, MPD_ROUND_CEILING); |
| 4135 } |
| 4136 |
4078 /* __complex__ */ | 4137 /* __complex__ */ |
4079 static PyObject * | 4138 static PyObject * |
4080 dec_complex(PyObject *self, PyObject *dummy UNUSED) | 4139 dec_complex(PyObject *self, PyObject *dummy UNUSED) |
4081 { | 4140 { |
4082 PyObject *f; | 4141 PyObject *f; |
4083 double x; | 4142 double x; |
4084 | 4143 |
4085 f = PyDec_AsFloat(self); | 4144 f = PyDec_AsFloat(self); |
4086 if (f == NULL) { | 4145 if (f == NULL) { |
4087 return NULL; | 4146 return NULL; |
4088 } | 4147 } |
4089 | 4148 |
4090 x = PyFloat_AsDouble(f); | 4149 x = PyFloat_AsDouble(f); |
4091 Py_DECREF(f); | 4150 Py_DECREF(f); |
4092 if (x == -1.0 && PyErr_Occurred()) { | 4151 if (x == -1.0 && PyErr_Occurred()) { |
4093 return NULL; | 4152 return NULL; |
4094 } | 4153 } |
4095 | 4154 |
4096 return PyComplex_FromDoubles(x, 0); | 4155 return PyComplex_FromDoubles(x, 0); |
4097 } | 4156 } |
4098 | 4157 |
4099 /* __copy__ and __deepcopy__ */ | 4158 /* __copy__ and __deepcopy__ */ |
4100 static PyObject * | 4159 static PyObject * |
4101 dec_copy(PyObject *self, PyObject *dummy UNUSED) | 4160 dec_copy(PyObject *self, PyObject *dummy UNUSED) |
4102 { | 4161 { |
4103 Py_INCREF(self); | 4162 Py_INCREF(self); |
4104 return self; | 4163 return self; |
| 4164 } |
| 4165 |
| 4166 /* __floor__ */ |
| 4167 static PyObject * |
| 4168 dec_floor(PyObject *self, PyObject *dummy UNUSED) |
| 4169 { |
| 4170 PyObject *context; |
| 4171 |
| 4172 CURRENT_CONTEXT(context); |
| 4173 return dec_as_long(self, context, MPD_ROUND_FLOOR); |
4105 } | 4174 } |
4106 | 4175 |
4107 /* Always uses the module context */ | 4176 /* Always uses the module context */ |
4108 static Py_hash_t | 4177 static Py_hash_t |
4109 dec_hash(PyObject *v) | 4178 dec_hash(PyObject *v) |
4110 { | 4179 { |
4111 #if defined(CONFIG_64) && _PyHASH_BITS == 61 | 4180 #if defined(CONFIG_64) && _PyHASH_BITS == 61 |
4112 /* 2**61 - 1 */ | 4181 /* 2**61 - 1 */ |
4113 mpd_uint_t p_data[1] = {2305843009213693951ULL}; | 4182 mpd_uint_t p_data[1] = {2305843009213693951ULL}; |
4114 mpd_t p = {MPD_POS|MPD_STATIC|MPD_CONST_DATA, 0, 19, 1, 1, p_data}; | 4183 mpd_t p = {MPD_POS|MPD_STATIC|MPD_CONST_DATA, 0, 19, 1, 1, p_data}; |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4231 } | 4300 } |
4232 | 4301 |
4233 result = Py_BuildValue("O(O)", Py_TYPE(self), str); | 4302 result = Py_BuildValue("O(O)", Py_TYPE(self), str); |
4234 Py_DECREF(str); | 4303 Py_DECREF(str); |
4235 | 4304 |
4236 return result; | 4305 return result; |
4237 } | 4306 } |
4238 | 4307 |
4239 /* __trunc__ */ | 4308 /* __trunc__ */ |
4240 static PyObject * | 4309 static PyObject * |
4241 dec_trunc(PyObject *dec, PyObject *dummy UNUSED) | 4310 dec_trunc(PyObject *self, PyObject *dummy UNUSED) |
4242 { | 4311 { |
4243 PyObject *context; | 4312 PyObject *context; |
4244 | 4313 |
4245 CURRENT_CONTEXT(context); | 4314 CURRENT_CONTEXT(context); |
4246 » return dec_as_long(dec, context, MPD_ROUND_DOWN); | 4315 » return dec_as_long(self, context, MPD_ROUND_DOWN); |
4247 } | 4316 } |
4248 | 4317 |
4249 /* real and imag */ | 4318 /* real and imag */ |
4250 static PyObject * | 4319 static PyObject * |
4251 dec_real(PyObject *self, void *closure UNUSED) | 4320 dec_real(PyObject *self, void *closure UNUSED) |
4252 { | 4321 { |
4253 Py_INCREF(self); | 4322 Py_INCREF(self); |
4254 return self; | 4323 return self; |
4255 } | 4324 } |
4256 | 4325 |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4388 /* Miscellaneous */ | 4457 /* Miscellaneous */ |
4389 { "from_float", dec_from_float, METH_O|METH_CLASS, doc_from_float }, | 4458 { "from_float", dec_from_float, METH_O|METH_CLASS, doc_from_float }, |
4390 { "as_tuple", PyDec_AsTuple, METH_NOARGS, doc_as_tuple }, | 4459 { "as_tuple", PyDec_AsTuple, METH_NOARGS, doc_as_tuple }, |
4391 | 4460 |
4392 /* Special methods */ | 4461 /* Special methods */ |
4393 { "__copy__", dec_copy, METH_NOARGS, NULL }, | 4462 { "__copy__", dec_copy, METH_NOARGS, NULL }, |
4394 { "__deepcopy__", dec_copy, METH_O, NULL }, | 4463 { "__deepcopy__", dec_copy, METH_O, NULL }, |
4395 { "__format__", dec_format, METH_VARARGS, NULL }, | 4464 { "__format__", dec_format, METH_VARARGS, NULL }, |
4396 { "__reduce__", dec_reduce, METH_NOARGS, NULL }, | 4465 { "__reduce__", dec_reduce, METH_NOARGS, NULL }, |
4397 { "__round__", PyDec_Round, METH_VARARGS, NULL }, | 4466 { "__round__", PyDec_Round, METH_VARARGS, NULL }, |
| 4467 { "__ceil__", dec_ceil, METH_NOARGS, NULL }, |
| 4468 { "__floor__", dec_floor, METH_NOARGS, NULL }, |
4398 { "__trunc__", dec_trunc, METH_NOARGS, NULL }, | 4469 { "__trunc__", dec_trunc, METH_NOARGS, NULL }, |
4399 { "__complex__", dec_complex, METH_NOARGS, NULL }, | 4470 { "__complex__", dec_complex, METH_NOARGS, NULL }, |
4400 | 4471 |
4401 { NULL, NULL, 1 } | 4472 { NULL, NULL, 1 } |
4402 }; | 4473 }; |
4403 | 4474 |
4404 static PyTypeObject PyDec_Type = | 4475 static PyTypeObject PyDec_Type = |
4405 { | 4476 { |
4406 PyVarObject_HEAD_INIT(NULL, 0) | 4477 PyVarObject_HEAD_INIT(NULL, 0) |
4407 "decimal.Decimal", /* tp_name */ | 4478 "decimal.Decimal", /* tp_name */ |
(...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4716 Py_DECREF(b); | 4787 Py_DECREF(b); |
4717 Py_XDECREF(c); | 4788 Py_XDECREF(c); |
4718 return NULL; | 4789 return NULL; |
4719 } | 4790 } |
4720 | 4791 |
4721 if (c == NULL) { | 4792 if (c == NULL) { |
4722 mpd_qpow(MPD(result), MPD(a), MPD(b), | 4793 mpd_qpow(MPD(result), MPD(a), MPD(b), |
4723 CTX(context), &status); | 4794 CTX(context), &status); |
4724 } | 4795 } |
4725 else { | 4796 else { |
4726 mpd_context_t maxctx; | |
4727 mpd_maxcontext(&maxctx); | |
4728 mpd_qpowmod(MPD(result), MPD(a), MPD(b), MPD(c), | 4797 mpd_qpowmod(MPD(result), MPD(a), MPD(b), MPD(c), |
4729 CTX(context), &status); | 4798 CTX(context), &status); |
| 4799 status = (status == MPD_Clamped) ? 0 : status; |
4730 /* remove ideal exponent for compatibility with decimal.py */ | 4800 /* remove ideal exponent for compatibility with decimal.py */ |
4731 mpd_qquantize(MPD(result), MPD(result), &zero, | 4801 mpd_qquantize(MPD(result), MPD(result), &zero, |
4732 » » &maxctx, &status); | 4802 » » CTX(context), &status); |
4733 Py_DECREF(c); | 4803 Py_DECREF(c); |
4734 } | 4804 } |
4735 Py_DECREF(a); | 4805 Py_DECREF(a); |
4736 Py_DECREF(b); | 4806 Py_DECREF(b); |
4737 if (dec_addstatus(context, status)) { | 4807 if (dec_addstatus(context, status)) { |
4738 Py_DECREF(result); | 4808 Py_DECREF(result); |
4739 return NULL; | 4809 return NULL; |
4740 } | 4810 } |
4741 | 4811 |
4742 return result; | 4812 return result; |
(...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5039 { "is_nan", ctx_mpd_isnan, METH_O, doc_ctx_is_nan }, | 5109 { "is_nan", ctx_mpd_isnan, METH_O, doc_ctx_is_nan }, |
5040 { "is_normal", ctx_mpd_isnormal, METH_O, doc_ctx_is_normal }, | 5110 { "is_normal", ctx_mpd_isnormal, METH_O, doc_ctx_is_normal }, |
5041 { "is_qnan", ctx_mpd_isqnan, METH_O, doc_ctx_is_qnan }, | 5111 { "is_qnan", ctx_mpd_isqnan, METH_O, doc_ctx_is_qnan }, |
5042 { "is_signed", ctx_mpd_issigned, METH_O, doc_ctx_is_signed }, | 5112 { "is_signed", ctx_mpd_issigned, METH_O, doc_ctx_is_signed }, |
5043 { "is_snan", ctx_mpd_issnan, METH_O, doc_ctx_is_snan }, | 5113 { "is_snan", ctx_mpd_issnan, METH_O, doc_ctx_is_snan }, |
5044 { "is_subnormal", ctx_mpd_issubnormal, METH_O, doc_ctx_is_subnormal }, | 5114 { "is_subnormal", ctx_mpd_issubnormal, METH_O, doc_ctx_is_subnormal }, |
5045 { "is_zero", ctx_mpd_iszero, METH_O, doc_ctx_is_zero }, | 5115 { "is_zero", ctx_mpd_iszero, METH_O, doc_ctx_is_zero }, |
5046 | 5116 |
5047 /* Functions with a single decimal argument */ | 5117 /* Functions with a single decimal argument */ |
5048 { "_apply", PyDecContext_Apply, METH_O, NULL }, /* alias for apply */ | 5118 { "_apply", PyDecContext_Apply, METH_O, NULL }, /* alias for apply */ |
| 5119 #ifdef EXTRA_FUNCTIONALITY |
5049 { "apply", PyDecContext_Apply, METH_O, doc_ctx_apply }, | 5120 { "apply", PyDecContext_Apply, METH_O, doc_ctx_apply }, |
| 5121 #endif |
5050 { "canonical", ctx_canonical, METH_O, doc_ctx_canonical }, | 5122 { "canonical", ctx_canonical, METH_O, doc_ctx_canonical }, |
5051 { "copy_abs", ctx_mpd_qcopy_abs, METH_O, doc_ctx_copy_abs }, | 5123 { "copy_abs", ctx_mpd_qcopy_abs, METH_O, doc_ctx_copy_abs }, |
5052 { "copy_decimal", ctx_copy_decimal, METH_O, doc_ctx_copy_decimal }, | 5124 { "copy_decimal", ctx_copy_decimal, METH_O, doc_ctx_copy_decimal }, |
5053 { "copy_negate", ctx_mpd_qcopy_negate, METH_O, doc_ctx_copy_negate }, | 5125 { "copy_negate", ctx_mpd_qcopy_negate, METH_O, doc_ctx_copy_negate }, |
5054 { "logb", ctx_mpd_qlogb, METH_O, doc_ctx_logb }, | 5126 { "logb", ctx_mpd_qlogb, METH_O, doc_ctx_logb }, |
5055 { "logical_invert", ctx_mpd_qinvert, METH_O, doc_ctx_logical_invert }, | 5127 { "logical_invert", ctx_mpd_qinvert, METH_O, doc_ctx_logical_invert }, |
5056 { "number_class", ctx_mpd_class, METH_O, doc_ctx_number_class }, | 5128 { "number_class", ctx_mpd_class, METH_O, doc_ctx_number_class }, |
5057 { "to_sci_string", ctx_mpd_to_sci, METH_O, doc_ctx_to_sci_string }, | 5129 { "to_sci_string", ctx_mpd_to_sci, METH_O, doc_ctx_to_sci_string }, |
5058 { "to_eng_string", ctx_mpd_to_eng, METH_O, doc_ctx_to_eng_string }, | 5130 { "to_eng_string", ctx_mpd_to_eng, METH_O, doc_ctx_to_eng_string }, |
5059 | 5131 |
5060 /* Functions with two decimal arguments */ | 5132 /* Functions with two decimal arguments */ |
5061 { "compare_total", ctx_mpd_compare_total, METH_VARARGS, doc_ctx_compare_total
}, | 5133 { "compare_total", ctx_mpd_compare_total, METH_VARARGS, doc_ctx_compare_total
}, |
5062 { "compare_total_mag", ctx_mpd_compare_total_mag, METH_VARARGS, doc_ctx_compar
e_total_mag }, | 5134 { "compare_total_mag", ctx_mpd_compare_total_mag, METH_VARARGS, doc_ctx_compar
e_total_mag }, |
5063 { "copy_sign", ctx_mpd_qcopy_sign, METH_VARARGS, doc_ctx_copy_sign }, | 5135 { "copy_sign", ctx_mpd_qcopy_sign, METH_VARARGS, doc_ctx_copy_sign }, |
5064 { "logical_and", ctx_mpd_qand, METH_VARARGS, doc_ctx_logical_and }, | 5136 { "logical_and", ctx_mpd_qand, METH_VARARGS, doc_ctx_logical_and }, |
5065 { "logical_or", ctx_mpd_qor, METH_VARARGS, doc_ctx_logical_or }, | 5137 { "logical_or", ctx_mpd_qor, METH_VARARGS, doc_ctx_logical_or }, |
5066 { "logical_xor", ctx_mpd_qxor, METH_VARARGS, doc_ctx_logical_xor }, | 5138 { "logical_xor", ctx_mpd_qxor, METH_VARARGS, doc_ctx_logical_xor }, |
5067 { "rotate", ctx_mpd_qrotate, METH_VARARGS, doc_ctx_rotate }, | 5139 { "rotate", ctx_mpd_qrotate, METH_VARARGS, doc_ctx_rotate }, |
5068 { "same_quantum", ctx_mpd_same_quantum, METH_VARARGS, doc_ctx_same_quantum }, | 5140 { "same_quantum", ctx_mpd_same_quantum, METH_VARARGS, doc_ctx_same_quantum }, |
5069 { "scaleb", ctx_mpd_qscaleb, METH_VARARGS, doc_ctx_scaleb }, | 5141 { "scaleb", ctx_mpd_qscaleb, METH_VARARGS, doc_ctx_scaleb }, |
5070 { "shift", ctx_mpd_qshift, METH_VARARGS, doc_ctx_shift }, | 5142 { "shift", ctx_mpd_qshift, METH_VARARGS, doc_ctx_shift }, |
5071 | 5143 |
5072 /* Set context values */ | 5144 /* Set context values */ |
5073 { "clear_flags", context_clear_flags, METH_NOARGS, doc_ctx_clear_flags }, | 5145 { "clear_flags", context_clear_flags, METH_NOARGS, doc_ctx_clear_flags }, |
5074 { "clear_traps", context_clear_traps, METH_NOARGS, doc_ctx_clear_traps }, | 5146 { "clear_traps", context_clear_traps, METH_NOARGS, doc_ctx_clear_traps }, |
5075 | 5147 |
5076 /* Unsafe set functions with no range checks */ | 5148 #ifdef CONFIG_32 |
5077 { "unsafe_setprec", context_unsafe_setprec, METH_O, NULL }, | 5149 /* Unsafe set functions with relaxed range checks */ |
5078 { "unsafe_setemin", context_unsafe_setemin, METH_O, NULL }, | 5150 { "_unsafe_setprec", context_unsafe_setprec, METH_O, NULL }, |
5079 { "unsafe_setemax", context_unsafe_setemax, METH_O, NULL }, | 5151 { "_unsafe_setemin", context_unsafe_setemin, METH_O, NULL }, |
| 5152 { "_unsafe_setemax", context_unsafe_setemax, METH_O, NULL }, |
| 5153 #endif |
5080 | 5154 |
5081 /* Miscellaneous */ | 5155 /* Miscellaneous */ |
5082 { "__copy__", (PyCFunction)context_copy, METH_NOARGS, NULL }, | 5156 { "__copy__", (PyCFunction)context_copy, METH_NOARGS, NULL }, |
5083 { "__reduce__", context_reduce, METH_NOARGS, NULL }, | 5157 { "__reduce__", context_reduce, METH_NOARGS, NULL }, |
5084 { "copy", (PyCFunction)context_copy, METH_NOARGS, doc_ctx_copy }, | 5158 { "copy", (PyCFunction)context_copy, METH_NOARGS, doc_ctx_copy }, |
5085 { "create_decimal", ctx_create_decimal, METH_VARARGS, doc_ctx_create_decimal }
, | 5159 { "create_decimal", ctx_create_decimal, METH_VARARGS, doc_ctx_create_decimal }
, |
5086 { "create_decimal_from_float", ctx_from_float, METH_O, doc_ctx_create_decimal_
from_float }, | 5160 { "create_decimal_from_float", ctx_from_float, METH_O, doc_ctx_create_decimal_
from_float }, |
5087 | 5161 |
5088 { NULL, NULL, 1 } | 5162 { NULL, NULL, 1 } |
5089 }; | 5163 }; |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5130 context_new, /* tp_new */ | 5204 context_new, /* tp_new */ |
5131 0 /* tp_free */ | 5205 0 /* tp_free */ |
5132 }; | 5206 }; |
5133 | 5207 |
5134 | 5208 |
5135 static PyMethodDef _decimal_methods [] = | 5209 static PyMethodDef _decimal_methods [] = |
5136 { | 5210 { |
5137 { "getcontext", (PyCFunction)PyDec_GetCurrentContext, METH_NOARGS, doc_getcont
ext}, | 5211 { "getcontext", (PyCFunction)PyDec_GetCurrentContext, METH_NOARGS, doc_getcont
ext}, |
5138 { "setcontext", (PyCFunction)PyDec_SetCurrentContext, METH_O, doc_setcontext}, | 5212 { "setcontext", (PyCFunction)PyDec_SetCurrentContext, METH_O, doc_setcontext}, |
5139 { "localcontext", (PyCFunction)ctxmanager_new, METH_VARARGS, doc_localcontext}
, | 5213 { "localcontext", (PyCFunction)ctxmanager_new, METH_VARARGS, doc_localcontext}
, |
| 5214 #ifdef EXTRA_FUNCTIONALITY |
5140 { "IEEEContext", (PyCFunction)ieee_context, METH_O, doc_ieee_context}, | 5215 { "IEEEContext", (PyCFunction)ieee_context, METH_O, doc_ieee_context}, |
| 5216 #endif |
5141 { NULL, NULL, 1, NULL } | 5217 { NULL, NULL, 1, NULL } |
5142 }; | 5218 }; |
5143 | 5219 |
5144 static struct PyModuleDef _decimal_module = { | 5220 static struct PyModuleDef _decimal_module = { |
5145 PyModuleDef_HEAD_INIT, | 5221 PyModuleDef_HEAD_INIT, |
5146 "decimal", | 5222 "decimal", |
5147 doc__decimal, | 5223 doc__decimal, |
5148 -1, | 5224 -1, |
5149 _decimal_methods, | 5225 _decimal_methods, |
5150 NULL, | 5226 NULL, |
5151 NULL, | 5227 NULL, |
5152 NULL, | 5228 NULL, |
5153 NULL | 5229 NULL |
5154 }; | 5230 }; |
5155 | 5231 |
5156 struct ssize_constmap { const char *name; mpd_ssize_t val; }; | 5232 struct ssize_constmap { const char *name; mpd_ssize_t val; }; |
5157 static struct ssize_constmap ssize_constants [] = { | 5233 static struct ssize_constmap ssize_constants [] = { |
5158 {"MAX_PREC", MPD_MAX_PREC}, | 5234 {"MAX_PREC", MPD_MAX_PREC}, |
5159 {"MAX_EMAX", MPD_MAX_EMAX}, | 5235 {"MAX_EMAX", MPD_MAX_EMAX}, |
5160 {"MIN_EMIN", MPD_MIN_EMIN}, | 5236 {"MIN_EMIN", MPD_MIN_EMIN}, |
5161 {"MIN_ETINY", MPD_MIN_ETINY}, | 5237 {"MIN_ETINY", MPD_MIN_ETINY}, |
5162 {NULL} | 5238 {NULL} |
5163 }; | 5239 }; |
5164 | 5240 |
5165 struct int_constmap { const char *name; int val; }; | 5241 struct int_constmap { const char *name; int val; }; |
5166 static struct int_constmap int_constants [] = { | 5242 static struct int_constmap int_constants [] = { |
5167 /* int constants */ | 5243 /* int constants */ |
| 5244 #ifdef EXTRA_FUNCTIONALITY |
5168 {"DECIMAL32", MPD_DECIMAL32}, | 5245 {"DECIMAL32", MPD_DECIMAL32}, |
5169 {"DECIMAL64", MPD_DECIMAL64}, | 5246 {"DECIMAL64", MPD_DECIMAL64}, |
5170 {"DECIMAL128", MPD_DECIMAL128}, | 5247 {"DECIMAL128", MPD_DECIMAL128}, |
5171 {"IEEE_CONTEXT_MAX_BITS", MPD_IEEE_CONTEXT_MAX_BITS}, | 5248 {"IEEE_CONTEXT_MAX_BITS", MPD_IEEE_CONTEXT_MAX_BITS}, |
| 5249 #endif |
5172 {"ROUND_CEILING", MPD_ROUND_CEILING}, | 5250 {"ROUND_CEILING", MPD_ROUND_CEILING}, |
5173 {"ROUND_FLOOR", MPD_ROUND_FLOOR}, | 5251 {"ROUND_FLOOR", MPD_ROUND_FLOOR}, |
5174 {"ROUND_UP", MPD_ROUND_UP}, | 5252 {"ROUND_UP", MPD_ROUND_UP}, |
5175 {"ROUND_DOWN", MPD_ROUND_DOWN}, | 5253 {"ROUND_DOWN", MPD_ROUND_DOWN}, |
5176 {"ROUND_HALF_UP", MPD_ROUND_HALF_UP}, | 5254 {"ROUND_HALF_UP", MPD_ROUND_HALF_UP}, |
5177 {"ROUND_HALF_DOWN", MPD_ROUND_HALF_DOWN}, | 5255 {"ROUND_HALF_DOWN", MPD_ROUND_HALF_DOWN}, |
5178 {"ROUND_HALF_EVEN", MPD_ROUND_HALF_EVEN}, | 5256 {"ROUND_HALF_EVEN", MPD_ROUND_HALF_EVEN}, |
5179 {"ROUND_05UP", MPD_ROUND_05UP}, | 5257 {"ROUND_05UP", MPD_ROUND_05UP}, |
| 5258 #ifdef EXTRA_FUNCTIONALITY |
5180 {"ROUND_TRUNC", MPD_ROUND_TRUNC}, | 5259 {"ROUND_TRUNC", MPD_ROUND_TRUNC}, |
5181 /* int condition flags */ | 5260 /* int condition flags */ |
5182 {"DecClamped", MPD_Clamped}, | 5261 {"DecClamped", MPD_Clamped}, |
5183 {"DecConversionSyntax", MPD_Conversion_syntax}, | 5262 {"DecConversionSyntax", MPD_Conversion_syntax}, |
5184 {"DecDivisionByZero", MPD_Division_by_zero}, | 5263 {"DecDivisionByZero", MPD_Division_by_zero}, |
5185 {"DecDivisionImpossible", MPD_Division_impossible}, | 5264 {"DecDivisionImpossible", MPD_Division_impossible}, |
5186 {"DecDivisionUndefined", MPD_Division_undefined}, | 5265 {"DecDivisionUndefined", MPD_Division_undefined}, |
5187 {"DecFpuError", MPD_Fpu_error}, | 5266 {"DecFpuError", MPD_Fpu_error}, |
5188 {"DecInexact", MPD_Inexact}, | 5267 {"DecInexact", MPD_Inexact}, |
5189 {"DecInvalidContext", MPD_Invalid_context}, | 5268 {"DecInvalidContext", MPD_Invalid_context}, |
5190 {"DecInvalidOperation", MPD_Invalid_operation}, | 5269 {"DecInvalidOperation", MPD_Invalid_operation}, |
5191 {"DecIEEEInvalidOperation", MPD_IEEE_Invalid_operation}, | 5270 {"DecIEEEInvalidOperation", MPD_IEEE_Invalid_operation}, |
5192 {"DecMallocError", MPD_Malloc_error}, | 5271 {"DecMallocError", MPD_Malloc_error}, |
5193 {"DecFloatOperation", MPD_Float_operation}, | 5272 {"DecFloatOperation", MPD_Float_operation}, |
5194 {"DecOverflow", MPD_Overflow}, | 5273 {"DecOverflow", MPD_Overflow}, |
5195 {"DecRounded", MPD_Rounded}, | 5274 {"DecRounded", MPD_Rounded}, |
5196 {"DecSubnormal", MPD_Subnormal}, | 5275 {"DecSubnormal", MPD_Subnormal}, |
5197 {"DecUnderflow", MPD_Underflow}, | 5276 {"DecUnderflow", MPD_Underflow}, |
5198 {"DecErrors", MPD_Errors}, | 5277 {"DecErrors", MPD_Errors}, |
5199 {"DecTraps", MPD_Traps}, | 5278 {"DecTraps", MPD_Traps}, |
| 5279 #endif |
5200 {NULL} | 5280 {NULL} |
5201 }; | 5281 }; |
5202 | 5282 |
5203 | 5283 |
5204 #define CHECK_INT(expr) \ | 5284 #define CHECK_INT(expr) \ |
5205 do { if ((expr) < 0) goto error; } while (0) | 5285 do { if ((expr) < 0) goto error; } while (0) |
5206 #define ASSIGN_PTR(result, expr) \ | 5286 #define ASSIGN_PTR(result, expr) \ |
5207 do { result = (expr); if (result == NULL) goto error; } while (0) | 5287 do { result = (expr); if (result == NULL) goto error; } while (0) |
5208 #define CHECK_PTR(expr) \ | 5288 #define CHECK_PTR(expr) \ |
5209 do { if ((expr) == NULL) goto error; } while (0) | 5289 do { if ((expr) == NULL) goto error; } while (0) |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5286 /* Create the module */ | 5366 /* Create the module */ |
5287 ASSIGN_PTR(m, PyModule_Create(&_decimal_module)); | 5367 ASSIGN_PTR(m, PyModule_Create(&_decimal_module)); |
5288 | 5368 |
5289 | 5369 |
5290 /* Add types to the module */ | 5370 /* Add types to the module */ |
5291 Py_INCREF(&PyDec_Type); | 5371 Py_INCREF(&PyDec_Type); |
5292 CHECK_INT(PyModule_AddObject(m, "Decimal", (PyObject *)&PyDec_Type)); | 5372 CHECK_INT(PyModule_AddObject(m, "Decimal", (PyObject *)&PyDec_Type)); |
5293 Py_INCREF(&PyDecContext_Type); | 5373 Py_INCREF(&PyDecContext_Type); |
5294 CHECK_INT(PyModule_AddObject(m, "Context", | 5374 CHECK_INT(PyModule_AddObject(m, "Context", |
5295 (PyObject *)&PyDecContext_Type)); | 5375 (PyObject *)&PyDecContext_Type)); |
| 5376 Py_INCREF(DecimalTuple); |
| 5377 CHECK_INT(PyModule_AddObject(m, "DecimalTuple", DecimalTuple)); |
5296 | 5378 |
5297 | 5379 |
5298 /* Create top level exception */ | 5380 /* Create top level exception */ |
5299 ASSIGN_PTR(DecimalException, PyErr_NewException( | 5381 ASSIGN_PTR(DecimalException, PyErr_NewException( |
5300 "decimal.DecimalException", | 5382 "decimal.DecimalException", |
5301 PyExc_ArithmeticError, NULL)); | 5383 PyExc_ArithmeticError, NULL)); |
5302 Py_INCREF(DecimalException); | 5384 Py_INCREF(DecimalException); |
5303 CHECK_INT(PyModule_AddObject(m, "DecimalException", DecimalException)); | 5385 CHECK_INT(PyModule_AddObject(m, "DecimalException", DecimalException)); |
5304 | 5386 |
5305 /* Create signal tuple */ | 5387 /* Create signal tuple */ |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5339 ASSIGN_PTR(default_context_template, | 5421 ASSIGN_PTR(default_context_template, |
5340 PyObject_CallObject((PyObject *)&PyDecContext_Type, NULL)); | 5422 PyObject_CallObject((PyObject *)&PyDecContext_Type, NULL)); |
5341 Py_INCREF(default_context_template); | 5423 Py_INCREF(default_context_template); |
5342 CHECK_INT(PyModule_AddObject(m, "DefaultContext", | 5424 CHECK_INT(PyModule_AddObject(m, "DefaultContext", |
5343 default_context_template)); | 5425 default_context_template)); |
5344 | 5426 |
5345 #ifdef WITHOUT_THREADS | 5427 #ifdef WITHOUT_THREADS |
5346 /* Init module context */ | 5428 /* Init module context */ |
5347 ASSIGN_PTR(module_context, | 5429 ASSIGN_PTR(module_context, |
5348 PyObject_CallObject((PyObject *)&PyDecContext_Type, NULL)); | 5430 PyObject_CallObject((PyObject *)&PyDecContext_Type, NULL)); |
5349 » CHECK_INT(PyModule_AddIntConstant(m, "HAVE_THREADS", 0)); | 5431 » Py_INCREF(Py_False); |
| 5432 » CHECK_INT(PyModule_AddObject(m, "HAVE_THREADS", Py_False)); |
5350 #else | 5433 #else |
5351 ASSIGN_PTR(tls_context_key, PyUnicode_FromString("___DECIMAL_CTX__")); | 5434 ASSIGN_PTR(tls_context_key, PyUnicode_FromString("___DECIMAL_CTX__")); |
5352 » CHECK_INT(PyModule_AddIntConstant(m, "HAVE_THREADS", 1)); | 5435 » Py_INCREF(Py_True); |
| 5436 » CHECK_INT(PyModule_AddObject(m, "HAVE_THREADS", Py_True)); |
5353 #endif | 5437 #endif |
5354 | 5438 |
5355 /* Init basic context template */ | 5439 /* Init basic context template */ |
5356 ASSIGN_PTR(basic_context_template, | 5440 ASSIGN_PTR(basic_context_template, |
5357 PyObject_CallObject((PyObject *)&PyDecContext_Type, NULL)); | 5441 PyObject_CallObject((PyObject *)&PyDecContext_Type, NULL)); |
5358 init_basic_context(basic_context_template); | 5442 init_basic_context(basic_context_template); |
5359 Py_INCREF(basic_context_template); | 5443 Py_INCREF(basic_context_template); |
5360 CHECK_INT(PyModule_AddObject(m, "BasicContext", | 5444 CHECK_INT(PyModule_AddObject(m, "BasicContext", |
5361 basic_context_template)); | 5445 basic_context_template)); |
5362 | 5446 |
(...skipping 10 matching lines...) Expand all Loading... |
5373 for (ssize_cm = ssize_constants; ssize_cm->name != NULL; ssize_cm++) { | 5457 for (ssize_cm = ssize_constants; ssize_cm->name != NULL; ssize_cm++) { |
5374 ASSIGN_PTR(obj, PyLong_FromSsize_t(ssize_cm->val)); | 5458 ASSIGN_PTR(obj, PyLong_FromSsize_t(ssize_cm->val)); |
5375 CHECK_INT(PyModule_AddObject(m, ssize_cm->name, obj)); | 5459 CHECK_INT(PyModule_AddObject(m, ssize_cm->name, obj)); |
5376 } | 5460 } |
5377 | 5461 |
5378 /* Init int constants */ | 5462 /* Init int constants */ |
5379 for (int_cm = int_constants; int_cm->name != NULL; int_cm++) { | 5463 for (int_cm = int_constants; int_cm->name != NULL; int_cm++) { |
5380 CHECK_INT(PyModule_AddIntConstant(m, int_cm->name, | 5464 CHECK_INT(PyModule_AddIntConstant(m, int_cm->name, |
5381 int_cm->val)); | 5465 int_cm->val)); |
5382 } | 5466 } |
| 5467 |
| 5468 /* Add specification version number */ |
| 5469 CHECK_INT(PyModule_AddStringConstant(m, "__version__", " 1.70")); |
5383 | 5470 |
5384 | 5471 |
5385 return m; | 5472 return m; |
5386 | 5473 |
5387 | 5474 |
5388 error: | 5475 error: |
5389 Py_XDECREF(obj); /* GCOV_NOT_REACHED */ | 5476 Py_XDECREF(obj); /* GCOV_NOT_REACHED */ |
5390 Py_XDECREF(numbers); /* GCOV_NOT_REACHED */ | 5477 Py_XDECREF(numbers); /* GCOV_NOT_REACHED */ |
5391 Py_XDECREF(Number); /* GCOV_NOT_REACHED */ | 5478 Py_XDECREF(Number); /* GCOV_NOT_REACHED */ |
5392 Py_XDECREF(Rational); /* GCOV_NOT_REACHED */ | 5479 Py_XDECREF(Rational); /* GCOV_NOT_REACHED */ |
5393 Py_XDECREF(collections); /* GCOV_NOT_REACHED */ | 5480 Py_XDECREF(collections); /* GCOV_NOT_REACHED */ |
5394 Py_XDECREF(MutableMapping); /* GCOV_NOT_REACHED */ | 5481 Py_XDECREF(MutableMapping); /* GCOV_NOT_REACHED */ |
5395 Py_XDECREF(SignalTuple); /* GCOV_NOT_REACHED */ | 5482 Py_XDECREF(SignalTuple); /* GCOV_NOT_REACHED */ |
5396 Py_XDECREF(DecimalTuple); /* GCOV_NOT_REACHED */ | 5483 Py_XDECREF(DecimalTuple); /* GCOV_NOT_REACHED */ |
5397 #ifdef WITHOUT_THREADS | 5484 #ifdef WITHOUT_THREADS |
5398 Py_XDECREF(module_context); /* GCOV_NOT_REACHED */ | 5485 Py_XDECREF(module_context); /* GCOV_NOT_REACHED */ |
5399 #else | 5486 #else |
5400 Py_XDECREF(default_context_template); /* GCOV_NOT_REACHED */ | 5487 Py_XDECREF(default_context_template); /* GCOV_NOT_REACHED */ |
5401 Py_XDECREF(tls_context_key); /* GCOV_NOT_REACHED */ | 5488 Py_XDECREF(tls_context_key); /* GCOV_NOT_REACHED */ |
5402 #endif | 5489 #endif |
5403 Py_XDECREF(basic_context_template); /* GCOV_NOT_REACHED */ | 5490 Py_XDECREF(basic_context_template); /* GCOV_NOT_REACHED */ |
5404 Py_XDECREF(extended_context_template); /* GCOV_NOT_REACHED */ | 5491 Py_XDECREF(extended_context_template); /* GCOV_NOT_REACHED */ |
5405 Py_XDECREF(m); /* GCOV_NOT_REACHED */ | 5492 Py_XDECREF(m); /* GCOV_NOT_REACHED */ |
5406 | 5493 |
5407 return NULL; /* GCOV_NOT_REACHED */ | 5494 return NULL; /* GCOV_NOT_REACHED */ |
5408 } | 5495 } |
5409 | 5496 |
5410 | 5497 |
LEFT | RIGHT |