Skip to content

Commit b335e44

Browse files
committed
+1
1 parent 860a942 commit b335e44

File tree

2 files changed

+314
-86
lines changed

2 files changed

+314
-86
lines changed

Modules/_decimal/_decimal.c

Lines changed: 91 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1651,16 +1651,18 @@ _decimal_IEEEContext_impl(PyObject *module, Py_ssize_t bits)
16511651
/*[clinic input]
16521652
_decimal.Context.copy
16531653
1654+
cls: defining_class
1655+
16541656
Return a duplicate of the context with all flags cleared.
16551657
[clinic start generated code]*/
16561658

16571659
static PyObject *
1658-
_decimal_Context_copy_impl(PyObject *self)
1659-
/*[clinic end generated code: output=f99649a60a9c10f8 input=2589aa46b77cbc28]*/
1660+
_decimal_Context_copy_impl(PyObject *self, PyTypeObject *cls)
1661+
/*[clinic end generated code: output=31c9c8eeb0c0cf77 input=aef1c0bddabdf8f0]*/
16601662
{
16611663
PyObject *copy;
16621664

1663-
decimal_state *state = get_module_state_from_ctx(self);
1665+
decimal_state *state = PyType_GetModuleState(cls);
16641666
copy = PyObject_CallObject((PyObject *)state->PyDecContext_Type, NULL);
16651667
if (copy == NULL) {
16661668
return NULL;
@@ -1673,20 +1675,51 @@ _decimal_Context_copy_impl(PyObject *self)
16731675
return copy;
16741676
}
16751677

1678+
/*[clinic input]
1679+
_decimal.Context.__copy__ = _decimal.Context.copy
1680+
1681+
[clinic start generated code]*/
1682+
1683+
static PyObject *
1684+
_decimal_Context___copy___impl(PyObject *self, PyTypeObject *cls)
1685+
/*[clinic end generated code: output=93552486e5fb0ab4 input=4a55dd22f6d31bcc]*/
1686+
{
1687+
return _decimal_Context_copy_impl(self, cls);
1688+
}
1689+
16761690
static PyObject *
16771691
context_copy(PyObject *self, PyObject *Py_UNUSED(dummy))
16781692
{
1679-
return _decimal_Context_copy_impl(self);
1693+
PyObject *copy;
1694+
1695+
decimal_state *state = get_module_state_from_ctx(self);
1696+
copy = PyObject_CallObject((PyObject *)state->PyDecContext_Type, NULL);
1697+
if (copy == NULL) {
1698+
return NULL;
1699+
}
1700+
1701+
*CTX(copy) = *CTX(self);
1702+
CTX(copy)->newtrap = 0;
1703+
CtxCaps(copy) = CtxCaps(self);
1704+
1705+
return copy;
16801706
}
16811707

1708+
1709+
/*[clinic input]
1710+
_decimal.Context.__reduce__ = _decimal.Context.copy
1711+
1712+
[clinic start generated code]*/
1713+
16821714
static PyObject *
1683-
context_reduce(PyObject *self, PyObject *Py_UNUSED(dummy))
1715+
_decimal_Context___reduce___impl(PyObject *self, PyTypeObject *cls)
1716+
/*[clinic end generated code: output=4e77de55efdbb56a input=787683f13d047ce8]*/
16841717
{
16851718
PyObject *flags;
16861719
PyObject *traps;
16871720
PyObject *ret;
16881721
mpd_context_t *ctx;
1689-
decimal_state *state = get_module_state_from_ctx(self);
1722+
decimal_state *state = PyType_GetModuleState(cls);
16901723

16911724
ctx = CTX(self);
16921725

@@ -2978,6 +3011,7 @@ PyDecType_FromSequenceExact(PyTypeObject *type, PyObject *v,
29783011
@classmethod
29793012
_decimal.Decimal.from_float
29803013
3014+
cls: defining_class
29813015
f as pyfloat: object
29823016
/
29833017
@@ -2997,13 +3031,14 @@ Decimal.from_float(0.1) is not the same as Decimal('0.1').
29973031
[clinic start generated code]*/
29983032

29993033
static PyObject *
3000-
_decimal_Decimal_from_float_impl(PyTypeObject *type, PyObject *pyfloat)
3001-
/*[clinic end generated code: output=e62775271ac469e6 input=052036648342f8c8]*/
3034+
_decimal_Decimal_from_float_impl(PyTypeObject *type, PyTypeObject *cls,
3035+
PyObject *pyfloat)
3036+
/*[clinic end generated code: output=fcb7d55d2f9dc790 input=03bc8dbe963e52ca]*/
30023037
{
30033038
PyObject *context;
30043039
PyObject *result;
30053040

3006-
decimal_state *state = get_module_state_by_def(type);
3041+
decimal_state *state = PyType_GetModuleState(cls);
30073042
CURRENT_CONTEXT(state, context);
30083043
result = PyDecType_FromFloatExact(state->PyDec_Type, pyfloat, context);
30093044
if (type != state->PyDec_Type && result != NULL) {
@@ -3018,9 +3053,10 @@ _decimal_Decimal_from_float_impl(PyTypeObject *type, PyObject *pyfloat)
30183053
an exact conversion. If the result does not meet the restrictions
30193054
for an mpd_t, fail with InvalidOperation. */
30203055
static PyObject *
3021-
PyDecType_FromNumberExact(PyTypeObject *type, PyObject *v, PyObject *context)
3056+
PyDecType_FromNumberExact(PyTypeObject *type, PyTypeObject *cls,
3057+
PyObject *v, PyObject *context)
30223058
{
3023-
decimal_state *state = get_module_state_by_def(type);
3059+
decimal_state *state = PyType_GetModuleState(cls);
30243060
assert(v != NULL);
30253061
if (PyDec_Check(state, v)) {
30263062
return PyDecType_FromDecimalExact(type, v, context);
@@ -3046,6 +3082,7 @@ PyDecType_FromNumberExact(PyTypeObject *type, PyObject *v, PyObject *context)
30463082
@classmethod
30473083
_decimal.Decimal.from_number
30483084
3085+
cls: defining_class
30493086
number: object
30503087
/
30513088
@@ -3060,15 +3097,16 @@ Class method that converts a real number to a decimal number, exactly.
30603097
[clinic start generated code]*/
30613098

30623099
static PyObject *
3063-
_decimal_Decimal_from_number_impl(PyTypeObject *type, PyObject *number)
3064-
/*[clinic end generated code: output=41885304e5beea0a input=c58b678e8916f66b]*/
3100+
_decimal_Decimal_from_number_impl(PyTypeObject *type, PyTypeObject *cls,
3101+
PyObject *number)
3102+
/*[clinic end generated code: output=4d3ec722b7acfd8b input=271cb4feb3148804]*/
30653103
{
30663104
PyObject *context;
30673105
PyObject *result;
30683106

3069-
decimal_state *state = get_module_state_by_def(type);
3107+
decimal_state *state = PyType_GetModuleState(cls);
30703108
CURRENT_CONTEXT(state, context);
3071-
result = PyDecType_FromNumberExact(state->PyDec_Type, number, context);
3109+
result = PyDecType_FromNumberExact(state->PyDec_Type, cls, number, context);
30723110
if (type != state->PyDec_Type && result != NULL) {
30733111
Py_SETREF(result,
30743112
PyObject_CallFunctionObjArgs((PyObject *)type, result, NULL));
@@ -3083,6 +3121,7 @@ _decimal_Decimal_from_number_impl(PyTypeObject *type, PyObject *number)
30833121
_decimal.Context.create_decimal_from_float
30843122
30853123
self as context: self
3124+
cls: defining_class
30863125
f: object
30873126
/
30883127
@@ -3093,10 +3132,12 @@ the context limits.
30933132
[clinic start generated code]*/
30943133

30953134
static PyObject *
3096-
_decimal_Context_create_decimal_from_float(PyObject *context, PyObject *f)
3097-
/*[clinic end generated code: output=c660c343f6f7158b input=05a8c54b7a5b457b]*/
3135+
_decimal_Context_create_decimal_from_float_impl(PyObject *context,
3136+
PyTypeObject *cls,
3137+
PyObject *f)
3138+
/*[clinic end generated code: output=a5548f5140fa0870 input=8c66eeb22b01ddd4]*/
30983139
{
3099-
decimal_state *state = get_module_state_from_ctx(context);
3140+
decimal_state *state = PyType_GetModuleState(cls);
31003141
return PyDec_FromFloat(state, f, context);
31013142
}
31023143

@@ -3702,6 +3743,7 @@ pydec_format(PyObject *dec, PyObject *context, PyObject *fmt, decimal_state *sta
37023743
_decimal.Decimal.__format__
37033744
37043745
self as dec: self
3746+
cls: defining_class
37053747
format_spec as fmtarg: unicode
37063748
override: object = NULL
37073749
/
@@ -3710,9 +3752,9 @@ Formats the Decimal according to format_spec.
37103752
[clinic start generated code]*/
37113753

37123754
static PyObject *
3713-
_decimal_Decimal___format___impl(PyObject *dec, PyObject *fmtarg,
3714-
PyObject *override)
3715-
/*[clinic end generated code: output=4b3640b7f0c8b6a5 input=e53488e49a0fff00]*/
3755+
_decimal_Decimal___format___impl(PyObject *dec, PyTypeObject *cls,
3756+
PyObject *fmtarg, PyObject *override)
3757+
/*[clinic end generated code: output=6d95f91bbb28b3ed input=2dbfaa0cbe243e9e]*/
37163758
{
37173759
PyObject *result = NULL;
37183760
PyObject *dot = NULL;
@@ -3725,7 +3767,7 @@ _decimal_Decimal___format___impl(PyObject *dec, PyObject *fmtarg,
37253767
uint32_t status = 0;
37263768
int replace_fillchar = 0;
37273769
Py_ssize_t size;
3728-
decimal_state *state = get_module_state_by_def(Py_TYPE(dec));
3770+
decimal_state *state = PyType_GetModuleState(cls);
37293771
CURRENT_CONTEXT(state, context);
37303772
fmt = (char *)PyUnicode_AsUTF8AndSize(fmtarg, &size);
37313773
if (fmt == NULL) {
@@ -3929,15 +3971,17 @@ dec_as_long(PyObject *dec, PyObject *context, int round)
39293971
/*[clinic input]
39303972
_decimal.Decimal.as_integer_ratio
39313973
3974+
cls: defining_class
3975+
39323976
Return a pair of integers whose ratio is exactly equal to the original.
39333977
39343978
The ratio is in lowest terms and with a positive denominator.
39353979
Raise OverflowError on infinities and a ValueError on NaNs.
39363980
[clinic start generated code]*/
39373981

39383982
static PyObject *
3939-
_decimal_Decimal_as_integer_ratio_impl(PyObject *self)
3940-
/*[clinic end generated code: output=c5d88e900080c264 input=7861cb643f01525a]*/
3983+
_decimal_Decimal_as_integer_ratio_impl(PyObject *self, PyTypeObject *cls)
3984+
/*[clinic end generated code: output=eb49c512701f844b input=07e33d8852184761]*/
39413985
{
39423986
PyObject *numerator = NULL;
39433987
PyObject *denominator = NULL;
@@ -3960,7 +4004,7 @@ _decimal_Decimal_as_integer_ratio_impl(PyObject *self)
39604004
return NULL;
39614005
}
39624006

3963-
decimal_state *state = get_module_state_by_def(Py_TYPE(self));
4007+
decimal_state *state = PyType_GetModuleState(cls);
39644008
CURRENT_CONTEXT(state, context);
39654009

39664010
tmp = dec_alloc(state);
@@ -4130,7 +4174,7 @@ _decimal_Decimal_to_integral_exact_impl(PyObject *self, PyTypeObject *cls,
41304174
uint32_t status = 0;
41314175
mpd_context_t workctx;
41324176

4133-
decimal_state *state = get_module_state_by_def(Py_TYPE(self));
4177+
decimal_state *state = PyType_GetModuleState(cls);
41344178
CONTEXT_CHECK_VA(state, context);
41354179

41364180
workctx = *CTX(context);
@@ -4193,20 +4237,22 @@ PyDec_AsFloat(PyObject *dec)
41934237
/*[clinic input]
41944238
_decimal.Decimal.__round__
41954239
4240+
cls: defining_class
41964241
ndigits: object = NULL
41974242
/
41984243
41994244
Return the Integral closest to self, rounding half toward even.
42004245
[clinic start generated code]*/
42014246

42024247
static PyObject *
4203-
_decimal_Decimal___round___impl(PyObject *self, PyObject *ndigits)
4204-
/*[clinic end generated code: output=ca6b3570a8df0c91 input=dc72084114f59380]*/
4248+
_decimal_Decimal___round___impl(PyObject *self, PyTypeObject *cls,
4249+
PyObject *ndigits)
4250+
/*[clinic end generated code: output=790c2c6bd57890e6 input=d69e7178a58a66b1]*/
42054251
{
42064252
PyObject *result;
42074253
uint32_t status = 0;
42084254
PyObject *context;
4209-
decimal_state *state = get_module_state_by_def(Py_TYPE(self));
4255+
decimal_state *state = PyType_GetModuleState(cls);
42104256
CURRENT_CONTEXT(state, context);
42114257
if (ndigits) {
42124258
mpd_uint_t dq[1] = {1};
@@ -5101,37 +5147,41 @@ _dec_mpd_radix(decimal_state *state)
51015147
/*[clinic input]
51025148
_decimal.Decimal.radix
51035149
5150+
cls: defining_class
5151+
51045152
Return Decimal(10).
51055153
51065154
This is the radix (base) in which the Decimal class does
51075155
all its arithmetic. Included for compatibility with the specification.
51085156
[clinic start generated code]*/
51095157

51105158
static PyObject *
5111-
_decimal_Decimal_radix_impl(PyObject *self)
5112-
/*[clinic end generated code: output=6b1db4c3fcdb5ee1 input=18b72393549ca8fd]*/
5159+
_decimal_Decimal_radix_impl(PyObject *self, PyTypeObject *cls)
5160+
/*[clinic end generated code: output=40a3bc7ec3d99228 input=b0d4cb9f870bbac1]*/
51135161
{
5114-
decimal_state *state = get_module_state_by_def(Py_TYPE(self));
5162+
decimal_state *state = PyType_GetModuleState(cls);
51155163
return _dec_mpd_radix(state);
51165164
}
51175165

51185166
/*[clinic input]
51195167
_decimal.Decimal.copy_abs
51205168
5169+
cls: defining_class
5170+
51215171
Return the absolute value of the argument.
51225172
51235173
This operation is unaffected by context and is quiet: no flags are
51245174
changed and no rounding is performed.
51255175
[clinic start generated code]*/
51265176

51275177
static PyObject *
5128-
_decimal_Decimal_copy_abs_impl(PyObject *self)
5129-
/*[clinic end generated code: output=fff53742cca94d70 input=a263c2e71d421f1b]*/
5178+
_decimal_Decimal_copy_abs_impl(PyObject *self, PyTypeObject *cls)
5179+
/*[clinic end generated code: output=081cb7fb4230676e input=676d7c62b1795512]*/
51305180
{
51315181
PyObject *result;
51325182
uint32_t status = 0;
5183+
decimal_state *state = PyType_GetModuleState(cls);
51335184

5134-
decimal_state *state = get_module_state_by_def(Py_TYPE(self));
51355185
if ((result = dec_alloc(state)) == NULL) {
51365186
return NULL;
51375187
}
@@ -5147,7 +5197,7 @@ _decimal_Decimal_copy_abs_impl(PyObject *self)
51475197
}
51485198

51495199
/*[clinic input]
5150-
_decimal.Decimal.copy_negate
5200+
_decimal.Decimal.copy_negate = _decimal.Decimal.copy_abs
51515201
51525202
Return the negation of the argument.
51535203
@@ -5156,13 +5206,13 @@ changed and no rounding is performed.
51565206
[clinic start generated code]*/
51575207

51585208
static PyObject *
5159-
_decimal_Decimal_copy_negate_impl(PyObject *self)
5160-
/*[clinic end generated code: output=8551bc26dbc5d01d input=13d47ed3a5d228b1]*/
5209+
_decimal_Decimal_copy_negate_impl(PyObject *self, PyTypeObject *cls)
5210+
/*[clinic end generated code: output=04fed82c17d4e28b input=23f41ee8899f3891]*/
51615211
{
51625212
PyObject *result;
51635213
uint32_t status = 0;
5214+
decimal_state *state = PyType_GetModuleState(cls);
51645215

5165-
decimal_state *state = get_module_state_by_def(Py_TYPE(self));
51665216
if ((result = dec_alloc(state)) == NULL) {
51675217
return NULL;
51685218
}
@@ -7336,8 +7386,8 @@ static PyMethodDef context_methods [] =
73367386
#endif
73377387

73387388
/* Miscellaneous */
7339-
{ "__copy__", context_copy, METH_NOARGS, NULL },
7340-
{ "__reduce__", context_reduce, METH_NOARGS, NULL },
7389+
_DECIMAL_CONTEXT___COPY___METHODDEF
7390+
_DECIMAL_CONTEXT___REDUCE___METHODDEF
73417391
_DECIMAL_CONTEXT_COPY_METHODDEF
73427392
_DECIMAL_CONTEXT_CREATE_DECIMAL_METHODDEF
73437393
_DECIMAL_CONTEXT_CREATE_DECIMAL_FROM_FLOAT_METHODDEF

0 commit comments

Comments
 (0)