| 1 |
from typing import SupportsInt, Union |
| 2 |
|
| 3 |
from .. import debug_helper |
| 4 |
from ..ft_gettext import current_lang |
| 5 |
from ..message_parser import get_parser |
| 6 |
from ..tb_data import TracebackData |
| 7 |
from ..typing_info import CauseInfo |
| 8 |
|
| 9 |
parser = get_parser(ZeroDivisionError) |
| 10 |
_ = current_lang.translate |
| 11 |
|
| 12 |
|
| 13 |
def expression_is_zero( |
| 14 |
expression: Union[str, bytes, SupportsInt], modulo: bool = False |
| 15 |
) -> str: |
| 16 |
"""Simpler message when the denominator is a literal 0.""" |
| 17 |
try: |
| 18 |
if int(expression) == 0: |
| 19 |
if modulo: |
| 20 |
return _("Using the modulo operator, `%`, you are dividing by zero.\n") |
| 21 |
return _("You are dividing by zero.\n") |
| 22 |
except Exception: |
| 23 |
return "" |
| 24 |
debug_helper.log("New case to consider for expression_is_zero") |
| 25 |
|
| 26 |
|
| 27 |
@parser._add |
| 28 |
def division_by_zero(message: str, tb_data: TracebackData) -> CauseInfo: |
| 29 |
if message not in ( |
| 30 |
"division by zero", |
| 31 |
"float division by zero", |
| 32 |
"complex division by zero", |
| 33 |
): |
| 34 |
return {} |
| 35 |
|
| 36 |
expression = tb_data.bad_line |
| 37 |
if expression.count("/") == 1: |
| 38 |
expression = expression.split("/")[1] |
| 39 |
cause = expression_is_zero(expression) |
| 40 |
if not cause: |
| 41 |
cause = _( |
| 42 |
"You are dividing by the following term\n\n" |
| 43 |
" {expression}\n\n" |
| 44 |
"which is equal to zero.\n" |
| 45 |
).format(expression=expression) |
| 46 |
else: |
| 47 |
cause = expression_includes_division_by_zero(expression) |
| 48 |
return {"cause": cause} |
| 49 |
|
| 50 |
|
| 51 |
def expression_includes_division_by_zero(expression): |
| 52 |
if not expression.strip(): |
| 53 |
expression = _("<'expression not found'>") |
| 54 |
return _( |
| 55 |
"The following mathematical expression includes a division by zero:\n\n" |
| 56 |
" {expression}\n" |
| 57 |
).format(expression=expression) |
| 58 |
|
| 59 |
|
| 60 |
@parser._add |
| 61 |
def integer_division_or_modulo(message: str, tb_data: TracebackData) -> CauseInfo: |
| 62 |
if message not in ["integer division or modulo by zero", "integer modulo by zero"]: |
| 63 |
return {} |
| 64 |
expression = tb_data.bad_line |
| 65 |
nb_div = expression.count("//") |
| 66 |
nb_mod = expression.count("%") |
| 67 |
nb_divmod = expression.count("divmod") |
| 68 |
if nb_div and not nb_mod and not nb_divmod: |
| 69 |
if nb_div == 1: |
| 70 |
expression = expression.split("//")[1] |
| 71 |
cause = expression_is_zero(expression) |
| 72 |
if not cause: |
| 73 |
cause = _( |
| 74 |
"You are dividing by the following term\n\n" |
| 75 |
" {expression}\n\n" |
| 76 |
"which is equal to zero.\n" |
| 77 |
).format(expression=expression) |
| 78 |
else: |
| 79 |
cause = expression_includes_division_by_zero(expression) |
| 80 |
elif nb_mod and not nb_div and not nb_divmod: |
| 81 |
if nb_mod == 1: |
| 82 |
expression = expression.split("%")[1] |
| 83 |
cause = expression_is_zero(expression, modulo=True) |
| 84 |
if not cause: |
| 85 |
cause = _( |
| 86 |
"Using the modulo operator, `%`, you are dividing by the following term\n\n" |
| 87 |
" {expression}\n\n" |
| 88 |
"which is equal to zero.\n" |
| 89 |
).format(expression=expression) |
| 90 |
else: |
| 91 |
cause = expression_includes_division_by_zero(expression) |
| 92 |
elif nb_divmod and not nb_div and not nb_mod: |
| 93 |
cause = _("The second argument of the `divmod()` function is zero.\n") |
| 94 |
else: |
| 95 |
cause = expression_includes_division_by_zero(expression) |
| 96 |
|
| 97 |
return {"cause": cause} |
| 98 |
|
| 99 |
|
| 100 |
@parser._add |
| 101 |
def zero_negative_power(message: str, _tb_data: TracebackData) -> CauseInfo: |
| 102 |
if message != "0.0 cannot be raised to a negative power": |
| 103 |
return {} |
| 104 |
cause = _( |
| 105 |
"You are attempting to raise the number 0 to a negative power\n" |
| 106 |
"which is equivalent to dividing by zero.\n" |
| 107 |
) |
| 108 |
return {"cause": cause} |
| 109 |
|
| 110 |
|
| 111 |
@parser._add |
| 112 |
def float_modulo(message: str, tb_data: TracebackData) -> CauseInfo: |
| 113 |
if message != "float modulo": |
| 114 |
return {} |
| 115 |
expression = tb_data.bad_line |
| 116 |
if expression.count("%") == 1: |
| 117 |
expression = expression.split("%")[1] |
| 118 |
cause = expression_is_zero(expression, modulo=True) |
| 119 |
if not cause: |
| 120 |
cause = _( |
| 121 |
"Using the modulo operator, `%`, you are dividing by the following term\n\n" |
| 122 |
" {expression}\n\n" |
| 123 |
"which is equal to zero.\n" |
| 124 |
).format(expression=expression) |
| 125 |
else: |
| 126 |
if not expression.strip(): |
| 127 |
expression = _("<'expression not found'>") |
| 128 |
cause = _( |
| 129 |
"The following mathematical expression includes a division by zero\n" |
| 130 |
"done using the modulo operator:\n\n" |
| 131 |
" {expression}\n" |
| 132 |
).format(expression=expression) |
| 133 |
|
| 134 |
return {"cause": cause} |
| 135 |
|
| 136 |
|
| 137 |
@parser._add |
| 138 |
def float_divmod(message: str, _tb_data: TracebackData) -> CauseInfo: |
| 139 |
if message != "float divmod()": |
| 140 |
debug_helper.log("new case to consider") |
| 141 |
return {} |
| 142 |
|
| 143 |
cause = _("The second argument of the `divmod()` function is equal to zero.\n") |
| 144 |
return {"cause": cause} |