Skip to content

Commit 3fa3727

Browse files
committed
gh-73487: Convert _decimal to use Argument Clinic (part 3)
1 parent c01448e commit 3fa3727

File tree

3 files changed

+1472
-339
lines changed

3 files changed

+1472
-339
lines changed

Modules/_decimal/_decimal.c

Lines changed: 294 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4382,19 +4382,11 @@ dec_##MPDFUNC(PyObject *self, PyObject *context) \
43824382
/* Binary function with an optional context arg. */
43834383
#define Dec_BinaryFuncVA(MPDFUNC) \
43844384
static PyObject * \
4385-
dec_##MPDFUNC(PyObject *self, PyObject *args, PyObject *kwds) \
4385+
dec_##MPDFUNC(PyObject *self, PyObject *other, PyObject *context)\
43864386
{ \
4387-
static char *kwlist[] = {"other", "context", NULL}; \
4388-
PyObject *other; \
43894387
PyObject *a, *b; \
43904388
PyObject *result; \
4391-
PyObject *context = Py_None; \
43924389
uint32_t status = 0; \
4393-
\
4394-
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O", kwlist, \
4395-
&other, &context)) { \
4396-
return NULL; \
4397-
} \
43984390
decimal_state *state = \
43994391
get_module_state_by_def(Py_TYPE(self)); \
44004392
CONTEXT_CHECK_VA(state, context); \
@@ -4748,6 +4740,166 @@ Dec_BinaryFuncVA(mpd_qmin_mag)
47484740
Dec_BinaryFuncVA(mpd_qnext_toward)
47494741
Dec_BinaryFuncVA(mpd_qrem_near)
47504742

4743+
/*[clinic input]
4744+
_decimal.Decimal.compare
4745+
4746+
other: object
4747+
context: object = None
4748+
4749+
Compare self to other.
4750+
4751+
Return a decimal value:
4752+
4753+
a or b is a NaN ==> Decimal('NaN')
4754+
a < b ==> Decimal('-1')
4755+
a == b ==> Decimal('0')
4756+
a > b ==> Decimal('1')
4757+
[clinic start generated code]*/
4758+
4759+
static PyObject *
4760+
_decimal_Decimal_compare_impl(PyObject *self, PyObject *other,
4761+
PyObject *context)
4762+
/*[clinic end generated code: output=d6967aa3578b9d48 input=1b7b75a2a154e520]*/
4763+
{
4764+
return dec_mpd_qcompare(self, other, context);
4765+
}
4766+
4767+
/*[clinic input]
4768+
_decimal.Decimal.compare_signal
4769+
4770+
other: object
4771+
context: object = None
4772+
4773+
Identical to compare, except that all NaNs signal.
4774+
[clinic start generated code]*/
4775+
4776+
static PyObject *
4777+
_decimal_Decimal_compare_signal_impl(PyObject *self, PyObject *other,
4778+
PyObject *context)
4779+
/*[clinic end generated code: output=0b8d0ff43f6c8a95 input=daf40eeaec81606f]*/
4780+
{
4781+
return dec_mpd_qcompare_signal(self, other, context);
4782+
}
4783+
4784+
/*[clinic input]
4785+
_decimal.Decimal.max
4786+
4787+
other: object
4788+
context: object = None
4789+
4790+
Maximum of self and other.
4791+
4792+
If one operand is a quiet NaN and the other is numeric, the numeric
4793+
operand is returned.
4794+
[clinic start generated code]*/
4795+
4796+
static PyObject *
4797+
_decimal_Decimal_max_impl(PyObject *self, PyObject *other, PyObject *context)
4798+
/*[clinic end generated code: output=f3a5c5d76761c9ff input=3a04c62c08c1189a]*/
4799+
{
4800+
return dec_mpd_qmax(self, other, context);
4801+
}
4802+
4803+
/*[clinic input]
4804+
_decimal.Decimal.max_mag
4805+
4806+
other: object
4807+
context: object = None
4808+
4809+
As the max() method, but compares the absolute values of the operands.
4810+
[clinic start generated code]*/
4811+
4812+
static PyObject *
4813+
_decimal_Decimal_max_mag_impl(PyObject *self, PyObject *other,
4814+
PyObject *context)
4815+
/*[clinic end generated code: output=52b0451987bac65f input=2eebf864d8659574]*/
4816+
{
4817+
return dec_mpd_qmax_mag(self, other, context);
4818+
}
4819+
4820+
/*[clinic input]
4821+
_decimal.Decimal.min
4822+
4823+
other: object
4824+
context: object = None
4825+
4826+
Minimum of self and other.
4827+
4828+
If one operand is a quiet NaN and the other is numeric, the numeric
4829+
operand is returned.
4830+
[clinic start generated code]*/
4831+
4832+
static PyObject *
4833+
_decimal_Decimal_min_impl(PyObject *self, PyObject *other, PyObject *context)
4834+
/*[clinic end generated code: output=d2f38ecb9d6f0493 input=01f01cd974c5441d]*/
4835+
{
4836+
return dec_mpd_qmin(self, other, context);
4837+
}
4838+
4839+
/*[clinic input]
4840+
_decimal.Decimal.min_mag
4841+
4842+
other: object
4843+
context: object = None
4844+
4845+
As the min() method, but compares the absolute values of the operands.
4846+
[clinic start generated code]*/
4847+
4848+
static PyObject *
4849+
_decimal_Decimal_min_mag_impl(PyObject *self, PyObject *other,
4850+
PyObject *context)
4851+
/*[clinic end generated code: output=aa3391935f6c8fc9 input=38f70dd313c8db83]*/
4852+
{
4853+
return dec_mpd_qmin_mag(self, other, context);
4854+
}
4855+
4856+
/*[clinic input]
4857+
_decimal.Decimal.next_toward
4858+
4859+
other: object
4860+
context: object = None
4861+
4862+
Returns the number closest to self, in the direction towards other.
4863+
4864+
If the two operands are unequal, return the number closest to the first
4865+
operand in the direction of the second operand. If both operands are
4866+
numerically equal, return a copy of the first operand with the sign set
4867+
to be the same as the sign of the second operand.
4868+
[clinic start generated code]*/
4869+
4870+
static PyObject *
4871+
_decimal_Decimal_next_toward_impl(PyObject *self, PyObject *other,
4872+
PyObject *context)
4873+
/*[clinic end generated code: output=edb933755644af69 input=b03dc5ae977012a5]*/
4874+
{
4875+
return dec_mpd_qnext_toward(self, other, context);
4876+
}
4877+
4878+
/*[clinic input]
4879+
_decimal.Decimal.remainder_near
4880+
4881+
other: object
4882+
context: object = None
4883+
4884+
Return the remainder from dividing self by other.
4885+
4886+
This differs from self % other in that the sign of the remainder is
4887+
chosen so as to minimize its absolute value. More precisely, the return
4888+
value is self - n * other where n is the integer nearest to the exact
4889+
value of self / other, and if two integers are equally near then the
4890+
even one is chosen.
4891+
4892+
If the result is zero then its sign will be the sign of self.
4893+
[clinic start generated code]*/
4894+
4895+
static PyObject *
4896+
_decimal_Decimal_remainder_near_impl(PyObject *self, PyObject *other,
4897+
PyObject *context)
4898+
/*[clinic end generated code: output=6ce0fb3b0faff2f9 input=6f603d9f21378661]*/
4899+
{
4900+
return dec_mpd_qrem_near(self, other, context);
4901+
}
4902+
47514903
/* Ternary arithmetic functions, optional context arg */
47524904
Dec_TernaryFuncVA(mpd_qfma)
47534905

@@ -5146,6 +5298,125 @@ Dec_BinaryFuncVA(mpd_qrotate)
51465298
Dec_BinaryFuncVA(mpd_qscaleb)
51475299
Dec_BinaryFuncVA(mpd_qshift)
51485300

5301+
/*[clinic input]
5302+
_decimal.Decimal.logical_and
5303+
5304+
other: object
5305+
context: object = None
5306+
5307+
Return the digit-wise 'and' of the two (logical) operands.
5308+
[clinic start generated code]*/
5309+
5310+
static PyObject *
5311+
_decimal_Decimal_logical_and_impl(PyObject *self, PyObject *other,
5312+
PyObject *context)
5313+
/*[clinic end generated code: output=1526a357f97eaf71 input=84d2729d67fca057]*/
5314+
{
5315+
return dec_mpd_qand(self, other, context);
5316+
}
5317+
5318+
/*[clinic input]
5319+
_decimal.Decimal.logical_or
5320+
5321+
other: object
5322+
context: object = None
5323+
5324+
Return the digit-wise 'or' of the two (logical) operands.
5325+
[clinic start generated code]*/
5326+
5327+
static PyObject *
5328+
_decimal_Decimal_logical_or_impl(PyObject *self, PyObject *other,
5329+
PyObject *context)
5330+
/*[clinic end generated code: output=e57a72acf0982f56 input=03c8141eb7ff8020]*/
5331+
{
5332+
return dec_mpd_qor(self, other, context);
5333+
}
5334+
5335+
/*[clinic input]
5336+
_decimal.Decimal.logical_xor
5337+
5338+
other: object
5339+
context: object = None
5340+
5341+
Return the digit-wise 'xor' of the two (logical) operands.
5342+
[clinic start generated code]*/
5343+
5344+
static PyObject *
5345+
_decimal_Decimal_logical_xor_impl(PyObject *self, PyObject *other,
5346+
PyObject *context)
5347+
/*[clinic end generated code: output=ae3a7aeddde5a1a8 input=d154ad76a12595a7]*/
5348+
{
5349+
return dec_mpd_qxor(self, other, context);
5350+
}
5351+
5352+
/*[clinic input]
5353+
_decimal.Decimal.rotate
5354+
5355+
other: object
5356+
context: object = None
5357+
5358+
Returns a rotated copy of self's digits, value-of-other times.
5359+
5360+
The second operand must be an integer in the range -precision through
5361+
precision. The absolute value of the second operand gives the number of
5362+
places to rotate. If the second operand is positive then rotation is to
5363+
the left; otherwise rotation is to the right. The coefficient of the
5364+
first operand is padded on the left with zeros to length precision if
5365+
necessary. The sign and exponent of the first operand are unchanged.
5366+
[clinic start generated code]*/
5367+
5368+
static PyObject *
5369+
_decimal_Decimal_rotate_impl(PyObject *self, PyObject *other,
5370+
PyObject *context)
5371+
/*[clinic end generated code: output=e59e757e70a8416a input=36574269f4b31d0c]*/
5372+
{
5373+
return dec_mpd_qrotate(self, other, context);
5374+
}
5375+
5376+
/*[clinic input]
5377+
_decimal.Decimal.scaleb
5378+
5379+
other: object
5380+
context: object = None
5381+
5382+
Return the first operand with the exponent adjusted the second.
5383+
5384+
Equivalently, return the first operand multiplied by 10**other. The
5385+
second operand must be an integer.
5386+
[clinic start generated code]*/
5387+
5388+
static PyObject *
5389+
_decimal_Decimal_scaleb_impl(PyObject *self, PyObject *other,
5390+
PyObject *context)
5391+
/*[clinic end generated code: output=f01e99600eda34d7 input=de4ac7eb39f95991]*/
5392+
{
5393+
return dec_mpd_qscaleb(self, other, context);
5394+
}
5395+
5396+
/*[clinic input]
5397+
_decimal.Decimal.shift
5398+
5399+
other: object
5400+
context: object = None
5401+
5402+
Returns a shifted copy of self's digits, value-of-other times.
5403+
5404+
The second operand must be an integer in the range -precision through
5405+
precision. The absolute value of the second operand gives the number
5406+
of places to shift. If the second operand is positive, then the shift
5407+
is to the left; otherwise the shift is to the right. Digits shifted
5408+
into the coefficient are zeros. The sign and exponent of the first
5409+
operand are unchanged.
5410+
[clinic start generated code]*/
5411+
5412+
static PyObject *
5413+
_decimal_Decimal_shift_impl(PyObject *self, PyObject *other,
5414+
PyObject *context)
5415+
/*[clinic end generated code: output=f79ff9ce6d5b05ed input=899b4d64fd3f76b8]*/
5416+
{
5417+
return dec_mpd_qshift(self, other, context);
5418+
}
5419+
51495420
/*[clinic input]
51505421
_decimal.Decimal.quantize
51515422
@@ -5542,15 +5813,15 @@ static PyMethodDef dec_methods [] =
55425813
_DECIMAL_DECIMAL_SQRT_METHODDEF
55435814

55445815
/* Binary arithmetic functions, optional context arg */
5545-
{ "compare", _PyCFunction_CAST(dec_mpd_qcompare), METH_VARARGS|METH_KEYWORDS, doc_compare },
5546-
{ "compare_signal", _PyCFunction_CAST(dec_mpd_qcompare_signal), METH_VARARGS|METH_KEYWORDS, doc_compare_signal },
5547-
{ "max", _PyCFunction_CAST(dec_mpd_qmax), METH_VARARGS|METH_KEYWORDS, doc_max },
5548-
{ "max_mag", _PyCFunction_CAST(dec_mpd_qmax_mag), METH_VARARGS|METH_KEYWORDS, doc_max_mag },
5549-
{ "min", _PyCFunction_CAST(dec_mpd_qmin), METH_VARARGS|METH_KEYWORDS, doc_min },
5550-
{ "min_mag", _PyCFunction_CAST(dec_mpd_qmin_mag), METH_VARARGS|METH_KEYWORDS, doc_min_mag },
5551-
{ "next_toward", _PyCFunction_CAST(dec_mpd_qnext_toward), METH_VARARGS|METH_KEYWORDS, doc_next_toward },
5816+
_DECIMAL_DECIMAL_COMPARE_METHODDEF
5817+
_DECIMAL_DECIMAL_COMPARE_SIGNAL_METHODDEF
5818+
_DECIMAL_DECIMAL_MAX_METHODDEF
5819+
_DECIMAL_DECIMAL_MAX_MAG_METHODDEF
5820+
_DECIMAL_DECIMAL_MIN_METHODDEF
5821+
_DECIMAL_DECIMAL_MIN_MAG_METHODDEF
5822+
_DECIMAL_DECIMAL_NEXT_TOWARD_METHODDEF
55525823
_DECIMAL_DECIMAL_QUANTIZE_METHODDEF
5553-
{ "remainder_near", _PyCFunction_CAST(dec_mpd_qrem_near), METH_VARARGS|METH_KEYWORDS, doc_remainder_near },
5824+
_DECIMAL_DECIMAL_REMAINDER_NEAR_METHODDEF
55545825

55555826
/* Ternary arithmetic functions, optional context arg */
55565827
_DECIMAL_DECIMAL_FMA_METHODDEF
@@ -5592,12 +5863,12 @@ static PyMethodDef dec_methods [] =
55925863
_DECIMAL_DECIMAL_SAME_QUANTUM_METHODDEF
55935864

55945865
/* Binary functions, optional context arg */
5595-
{ "logical_and", _PyCFunction_CAST(dec_mpd_qand), METH_VARARGS|METH_KEYWORDS, doc_logical_and },
5596-
{ "logical_or", _PyCFunction_CAST(dec_mpd_qor), METH_VARARGS|METH_KEYWORDS, doc_logical_or },
5597-
{ "logical_xor", _PyCFunction_CAST(dec_mpd_qxor), METH_VARARGS|METH_KEYWORDS, doc_logical_xor },
5598-
{ "rotate", _PyCFunction_CAST(dec_mpd_qrotate), METH_VARARGS|METH_KEYWORDS, doc_rotate },
5599-
{ "scaleb", _PyCFunction_CAST(dec_mpd_qscaleb), METH_VARARGS|METH_KEYWORDS, doc_scaleb },
5600-
{ "shift", _PyCFunction_CAST(dec_mpd_qshift), METH_VARARGS|METH_KEYWORDS, doc_shift },
5866+
_DECIMAL_DECIMAL_LOGICAL_AND_METHODDEF
5867+
_DECIMAL_DECIMAL_LOGICAL_OR_METHODDEF
5868+
_DECIMAL_DECIMAL_LOGICAL_XOR_METHODDEF
5869+
_DECIMAL_DECIMAL_ROTATE_METHODDEF
5870+
_DECIMAL_DECIMAL_SCALEB_METHODDEF
5871+
_DECIMAL_DECIMAL_SHIFT_METHODDEF
56015872

56025873
/* Miscellaneous */
56035874
_DECIMAL_DECIMAL_FROM_FLOAT_METHODDEF

0 commit comments

Comments
 (0)