Skip to content

Commit f1496f8

Browse files
committed
gh-102431: clarify constraints on arguments of logical_xxx methods
1 parent 5e6661b commit f1496f8

File tree

2 files changed

+43
-16
lines changed

2 files changed

+43
-16
lines changed

Lib/_pydecimal.py

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3388,7 +3388,11 @@ def _fill_logical(self, context, opa, opb):
33883388
return opa, opb
33893389

33903390
def logical_and(self, other, context=None):
3391-
"""Applies an 'and' operation between self and other's digits."""
3391+
"""Applies an 'and' operation between self and other's digits.
3392+
3393+
Both self and other should have zero sign and exponent,
3394+
and digits either 0 or 1.
3395+
"""
33923396
if context is None:
33933397
context = getcontext()
33943398

@@ -3405,14 +3409,21 @@ def logical_and(self, other, context=None):
34053409
return _dec_from_triple(0, result.lstrip('0') or '0', 0)
34063410

34073411
def logical_invert(self, context=None):
3408-
"""Invert all its digits."""
3412+
"""Invert all its digits.
3413+
3414+
The self should have zero sign and exponent, and digits either 0 or 1.
3415+
"""
34093416
if context is None:
34103417
context = getcontext()
34113418
return self.logical_xor(_dec_from_triple(0,'1'*context.prec,0),
34123419
context)
34133420

34143421
def logical_or(self, other, context=None):
3415-
"""Applies an 'or' operation between self and other's digits."""
3422+
"""Applies an 'or' operation between self and other's digits.
3423+
3424+
Both self and other should have zero sign and exponent,
3425+
and digits either 0 or 1.
3426+
"""
34163427
if context is None:
34173428
context = getcontext()
34183429

@@ -3429,7 +3440,11 @@ def logical_or(self, other, context=None):
34293440
return _dec_from_triple(0, result.lstrip('0') or '0', 0)
34303441

34313442
def logical_xor(self, other, context=None):
3432-
"""Applies an 'xor' operation between self and other's digits."""
3443+
"""Applies an 'xor' operation between self and other's digits.
3444+
3445+
Both self and other should have zero sign and exponent,
3446+
and digits either 0 or 1.
3447+
"""
34333448
if context is None:
34343449
context = getcontext()
34353450

@@ -4752,7 +4767,8 @@ def logb(self, a):
47524767
def logical_and(self, a, b):
47534768
"""Applies the logical operation 'and' between each operand's digits.
47544769
4755-
The operands must be both logical numbers.
4770+
Both operands should have zero sign and exponent, and
4771+
digits either 0 or 1.
47564772
47574773
>>> ExtendedContext.logical_and(Decimal('0'), Decimal('0'))
47584774
Decimal('0')
@@ -4779,7 +4795,8 @@ def logical_and(self, a, b):
47794795
def logical_invert(self, a):
47804796
"""Invert all the digits in the operand.
47814797
4782-
The operand must be a logical number.
4798+
The operand should have zero sign and exponent, and digits
4799+
either 0 or 1.
47834800
47844801
>>> ExtendedContext.logical_invert(Decimal('0'))
47854802
Decimal('111111111')
@@ -4798,7 +4815,8 @@ def logical_invert(self, a):
47984815
def logical_or(self, a, b):
47994816
"""Applies the logical operation 'or' between each operand's digits.
48004817
4801-
The operands must be both logical numbers.
4818+
Both operands should have zero sign and exponent, and digits
4819+
either 0 or 1.
48024820
48034821
>>> ExtendedContext.logical_or(Decimal('0'), Decimal('0'))
48044822
Decimal('0')
@@ -4825,7 +4843,8 @@ def logical_or(self, a, b):
48254843
def logical_xor(self, a, b):
48264844
"""Applies the logical operation 'xor' between each operand's digits.
48274845
4828-
The operands must be both logical numbers.
4846+
Both operands should have zero sign and exponent, and digits
4847+
either 0 or 1.
48294848
48304849
>>> ExtendedContext.logical_xor(Decimal('0'), Decimal('0'))
48314850
Decimal('0')

Modules/_decimal/docstrings.h

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -282,22 +282,26 @@ an infinity then Decimal('Infinity') is returned.\n\
282282

283283
PyDoc_STRVAR(doc_logical_and,
284284
"logical_and($self, /, other, context=None)\n--\n\n\
285-
Return the digit-wise 'and' of the two (logical) operands.\n\
285+
Return the digit-wise 'and' of the two (logical) operands,\n\
286+
which both have zero sign and exponent, and digits either 0 or 1.\n\
286287
\n");
287288

288289
PyDoc_STRVAR(doc_logical_invert,
289290
"logical_invert($self, /, context=None)\n--\n\n\
290-
Return the digit-wise inversion of the (logical) operand.\n\
291+
Return the digit-wise inversion of the (logical) operand,\n\
292+
which has zero sign and exponent, and digits either 0 or 1.\n\
291293
\n");
292294

293295
PyDoc_STRVAR(doc_logical_or,
294296
"logical_or($self, /, other, context=None)\n--\n\n\
295-
Return the digit-wise 'or' of the two (logical) operands.\n\
297+
Return the digit-wise 'or' of the two (logical) operands,\n\
298+
which both have zero sign and exponent, and digits either 0 or 1.\n\
296299
\n");
297300

298301
PyDoc_STRVAR(doc_logical_xor,
299302
"logical_xor($self, /, other, context=None)\n--\n\n\
300-
Return the digit-wise 'exclusive or' of the two (logical) operands.\n\
303+
Return the digit-wise 'exclusive or' of the two (logical) operands,\n\
304+
which both have zero sign and exponent, and digits either 0 or 1.\n\
301305
\n");
302306

303307
PyDoc_STRVAR(doc_max,
@@ -702,22 +706,26 @@ Return the exponent of the magnitude of the operand's MSD.\n\
702706

703707
PyDoc_STRVAR(doc_ctx_logical_and,
704708
"logical_and($self, x, y, /)\n--\n\n\
705-
Digit-wise and of x and y.\n\
709+
Digit-wise and of x and y, which both have zero sign\n\
710+
and exponent, and digits either 0 or 1.\n\
706711
\n");
707712

708713
PyDoc_STRVAR(doc_ctx_logical_invert,
709714
"logical_invert($self, x, /)\n--\n\n\
710-
Invert all digits of x.\n\
715+
Invert all digits of x, which has zero sign and exponent,\n\
716+
and digits either 0 or 1.\n\
711717
\n");
712718

713719
PyDoc_STRVAR(doc_ctx_logical_or,
714720
"logical_or($self, x, y, /)\n--\n\n\
715-
Digit-wise or of x and y.\n\
721+
Digit-wise or of x and y, which both have zero sign\n\
722+
and exponent, and digits either 0 or 1.\n\
716723
\n");
717724

718725
PyDoc_STRVAR(doc_ctx_logical_xor,
719726
"logical_xor($self, x, y, /)\n--\n\n\
720-
Digit-wise xor of x and y.\n\
727+
Digit-wise xor of x and y, which both have zero sign\n\
728+
and exponent, and digits either 0 or 1.\n\
721729
\n");
722730

723731
PyDoc_STRVAR(doc_ctx_max,

0 commit comments

Comments
 (0)