calculator.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """Calculate mathematical expressions using :py:obj:`ast.parse` (mode="eval")."""
  3. from __future__ import annotations
  4. import typing
  5. import ast
  6. import math
  7. import re
  8. import operator
  9. import multiprocessing
  10. import babel
  11. import babel.numbers
  12. from flask_babel import gettext
  13. from searx.result_types import EngineResults
  14. from searx.plugins import Plugin, PluginInfo
  15. if typing.TYPE_CHECKING:
  16. from searx.search import SearchWithPlugins
  17. from searx.extended_types import SXNG_Request
  18. from searx.plugins import PluginCfg
  19. class SXNGPlugin(Plugin):
  20. """Plugin converts strings to different hash digests. The results are
  21. displayed in area for the "answers".
  22. """
  23. id = "calculator"
  24. def __init__(self, plg_cfg: "PluginCfg") -> None:
  25. super().__init__(plg_cfg)
  26. self.info = PluginInfo(
  27. id=self.id,
  28. name=gettext("Basic Calculator"),
  29. description=gettext("Calculate mathematical expressions via the search bar"),
  30. preference_section="general",
  31. )
  32. def post_search(self, request: "SXNG_Request", search: "SearchWithPlugins") -> EngineResults:
  33. results = EngineResults()
  34. # only show the result of the expression on the first page
  35. if search.search_query.pageno > 1:
  36. return results
  37. query = search.search_query.query
  38. # in order to avoid DoS attacks with long expressions, ignore long expressions
  39. if len(query) > 100:
  40. return results
  41. # replace commonly used math operators with their proper Python operator
  42. query = query.replace("x", "*").replace(":", "/")
  43. # use UI language
  44. ui_locale = babel.Locale.parse(request.preferences.get_value("locale"), sep="-")
  45. # parse the number system in a localized way
  46. def _decimal(match: re.Match) -> str:
  47. val = match.string[match.start() : match.end()]
  48. val = babel.numbers.parse_decimal(val, ui_locale, numbering_system="latn")
  49. return str(val)
  50. decimal = ui_locale.number_symbols["latn"]["decimal"]
  51. group = ui_locale.number_symbols["latn"]["group"]
  52. query = re.sub(f"[0-9]+[{decimal}|{group}][0-9]+[{decimal}|{group}]?[0-9]?", _decimal, query)
  53. # in python, powers are calculated via **
  54. query_py_formatted = query.replace("^", "**")
  55. # Prevent the runtime from being longer than 50 ms
  56. res = timeout_func(0.05, _eval_expr, query_py_formatted)
  57. if res is None or res[0] == "":
  58. return results
  59. res, is_boolean = res
  60. if is_boolean:
  61. res = "True" if res != 0 else "False"
  62. else:
  63. res = babel.numbers.format_decimal(res, locale=ui_locale)
  64. results.add(results.types.Answer(answer=f"{search.search_query.query} = {res}"))
  65. return results
  66. def _compare(ops: list[ast.cmpop], values: list[int | float]) -> int:
  67. """
  68. 2 < 3 becomes ops=[ast.Lt] and values=[2,3]
  69. 2 < 3 <= 4 becomes ops=[ast.Lt, ast.LtE] and values=[2,3, 4]
  70. """
  71. for op, a, b in zip(ops, values, values[1:]): # pylint: disable=invalid-name
  72. if isinstance(op, ast.Eq) and a == b:
  73. continue
  74. if isinstance(op, ast.NotEq) and a != b:
  75. continue
  76. if isinstance(op, ast.Lt) and a < b:
  77. continue
  78. if isinstance(op, ast.LtE) and a <= b:
  79. continue
  80. if isinstance(op, ast.Gt) and a > b:
  81. continue
  82. if isinstance(op, ast.GtE) and a >= b:
  83. continue
  84. # Ignore impossible ops:
  85. # * ast.Is
  86. # * ast.IsNot
  87. # * ast.In
  88. # * ast.NotIn
  89. # the result is False for a and b and operation op
  90. return 0
  91. # the results for all the ops are True
  92. return 1
  93. operators: dict[type, typing.Callable] = {
  94. ast.Add: operator.add,
  95. ast.Sub: operator.sub,
  96. ast.Mult: operator.mul,
  97. ast.Div: operator.truediv,
  98. ast.Pow: operator.pow,
  99. ast.BitXor: operator.xor,
  100. ast.BitOr: operator.or_,
  101. ast.BitAnd: operator.and_,
  102. ast.USub: operator.neg,
  103. ast.RShift: operator.rshift,
  104. ast.LShift: operator.lshift,
  105. ast.Mod: operator.mod,
  106. ast.Compare: _compare,
  107. }
  108. math_constants = {
  109. 'e': math.e,
  110. 'pi': math.pi,
  111. }
  112. # with multiprocessing.get_context("fork") we are ready for Py3.14 (by emulating
  113. # the old behavior "fork") but it will not solve the core problem of fork, nor
  114. # will it remove the deprecation warnings in py3.12 & py3.13. Issue is
  115. # ddiscussed here: https://github.com/searxng/searxng/issues/4159
  116. mp_fork = multiprocessing.get_context("fork")
  117. def _eval_expr(expr):
  118. """
  119. Evaluates the given textual expression.
  120. Returns a tuple of (numericResult, isBooleanResult).
  121. >>> _eval_expr('2^6')
  122. 64, False
  123. >>> _eval_expr('2**6')
  124. 64, False
  125. >>> _eval_expr('1 + 2*3**(4^5) / (6 + -7)')
  126. -5.0, False
  127. >>> _eval_expr('1 < 3')
  128. 1, True
  129. >>> _eval_expr('5 < 3')
  130. 0, True
  131. >>> _eval_expr('17 == 11+1+5 == 7+5+5')
  132. 1, True
  133. """
  134. try:
  135. root_expr = ast.parse(expr, mode='eval').body
  136. return _eval(root_expr), isinstance(root_expr, ast.Compare)
  137. except (SyntaxError, TypeError, ZeroDivisionError):
  138. # Expression that can't be evaluated (i.e. not a math expression)
  139. return "", False
  140. def _eval(node):
  141. if isinstance(node, ast.Constant) and isinstance(node.value, (int, float)):
  142. return node.value
  143. if isinstance(node, ast.BinOp):
  144. return operators[type(node.op)](_eval(node.left), _eval(node.right))
  145. if isinstance(node, ast.UnaryOp):
  146. return operators[type(node.op)](_eval(node.operand))
  147. if isinstance(node, ast.Compare):
  148. return _compare(node.ops, [_eval(node.left)] + [_eval(c) for c in node.comparators])
  149. if isinstance(node, ast.Name) and node.id in math_constants:
  150. return math_constants[node.id]
  151. raise TypeError(node)
  152. def handler(q: multiprocessing.Queue, func, args, **kwargs): # pylint:disable=invalid-name
  153. try:
  154. q.put(func(*args, **kwargs))
  155. except:
  156. q.put(None)
  157. raise
  158. def timeout_func(timeout, func, *args, **kwargs):
  159. que = mp_fork.Queue()
  160. p = mp_fork.Process(target=handler, args=(que, func, args), kwargs=kwargs)
  161. p.start()
  162. p.join(timeout=timeout)
  163. ret_val = None
  164. # pylint: disable=used-before-assignment,undefined-variable
  165. if not p.is_alive():
  166. ret_val = que.get()
  167. else:
  168. logger.debug("terminate function after timeout is exceeded") # type: ignore
  169. p.terminate()
  170. p.join()
  171. p.close()
  172. return ret_val