summaryrefslogtreecommitdiff
path: root/searx/plugins/calculator.py
diff options
context:
space:
mode:
authorMarkus Heiser <markus.heiser@darmarIT.de>2025-07-17 19:50:02 +0200
committerGitHub <noreply@github.com>2025-07-17 19:50:02 +0200
commite851bc1269916a5a8c7a822946eccf71ce138403 (patch)
treebaedd5b6ffbe2d4df5a46407839fbc7fe3a927a6 /searx/plugins/calculator.py
parent62fac1c6a9db94682f8ef686f0424a482663b288 (diff)
[fix] calculator plugin: filtering real calculation tasks (#5016)
Whether the query is a real calculation tasks is currently only detected in the AST, resulting in unnecessary creatins of subprocesses. This problem is mitigated with this patch: if the query contains letters, it is obviously not a math problem, and the plugin can return without further action. Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>
Diffstat (limited to 'searx/plugins/calculator.py')
-rw-r--r--searx/plugins/calculator.py16
1 files changed, 16 insertions, 0 deletions
diff --git a/searx/plugins/calculator.py b/searx/plugins/calculator.py
index 5adaaaf22..428f734c1 100644
--- a/searx/plugins/calculator.py
+++ b/searx/plugins/calculator.py
@@ -71,6 +71,22 @@ class SXNGPlugin(Plugin):
# replace commonly used math operators with their proper Python operator
query = query.replace("x", "*").replace(":", "/")
+ # Is this a term that can be calculated?
+ word, constants = "", set()
+ for x in query:
+ # Alphabetic characters are defined as "Letters" in the Unicode
+ # character database and are the constants in an equation.
+ if x.isalpha():
+ word += x.strip()
+ elif word:
+ constants.add(word)
+ word = ""
+
+ # In the term of an arithmetic operation there should be no other
+ # alphabetic characters besides the constants
+ if constants - set(math_constants):
+ return results
+
# use UI language
ui_locale = babel.Locale.parse(request.preferences.get_value("locale"), sep="-")