summaryrefslogtreecommitdiff
path: root/searx/plugins/calculator.py
diff options
context:
space:
mode:
authorBnyro <bnyro@tutanota.com>2025-06-26 15:22:53 +0200
committerMarkus Heiser <markus.heiser@darmarIT.de>2025-07-03 13:57:31 +0200
commit99033f548ec3852e1cd1368046cfeb6d1fb74557 (patch)
tree0bc748c7da2a200a92187bcc58fe1d33fcf00013 /searx/plugins/calculator.py
parent27466faadb83598737354b3550b6a05380f2fafa (diff)
[feat] calculator: add support for math constants (e, pi)
Diffstat (limited to 'searx/plugins/calculator.py')
-rw-r--r--searx/plugins/calculator.py15
1 files changed, 11 insertions, 4 deletions
diff --git a/searx/plugins/calculator.py b/searx/plugins/calculator.py
index 30c414771..ba9dc8721 100644
--- a/searx/plugins/calculator.py
+++ b/searx/plugins/calculator.py
@@ -5,6 +5,7 @@ from __future__ import annotations
import typing
import ast
+import math
import re
import operator
import multiprocessing
@@ -67,10 +68,6 @@ class SXNGPlugin(Plugin):
group = ui_locale.number_symbols["latn"]["group"]
query = re.sub(f"[0-9]+[{decimal}|{group}][0-9]+[{decimal}|{group}]?[0-9]?", _decimal, query)
- # only numbers and math operators are accepted
- if any(str.isalpha(c) for c in query):
- return results
-
# in python, powers are calculated via **
query_py_formatted = query.replace("^", "**")
@@ -136,6 +133,13 @@ operators: dict[type, typing.Callable] = {
ast.Compare: _compare,
}
+
+math_constants = {
+ 'e': math.e,
+ 'pi': math.pi,
+}
+
+
# with multiprocessing.get_context("fork") we are ready for Py3.14 (by emulating
# the old behavior "fork") but it will not solve the core problem of fork, nor
# will it remove the deprecation warnings in py3.12 & py3.13. Issue is
@@ -184,6 +188,9 @@ def _eval(node):
if isinstance(node, ast.Compare):
return _compare(node.ops, [_eval(node.left)] + [_eval(c) for c in node.comparators])
+ if isinstance(node, ast.Name) and node.id in math_constants:
+ return math_constants[node.id]
+
raise TypeError(node)