Skip to content

Commit 6dae59b

Browse files
committed
bugfix in filter_needs
1 parent c73c44f commit 6dae59b

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

docs/changelog.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ Released: under development
1616
for debugging purposes.
1717
(`#917 <https://github.com/useblocks/sphinx-needs/pull/917>`_)
1818

19+
* Bugfix: Check filter strings for correctness.
20+
(`#964 <https://github.com/useblocks/sphinx-needs/pull/964>`_)
1921
* Bugfix: Replace hardcoded `index` with config value `root_doc`.
2022
(`#877 <https://github.com/useblocks/sphinx-needs/pull/877>`_)
2123
* Bugfix: Fix unbounded memory usage in pickle environment.

sphinx_needs/filter_common.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,10 @@ def filter_needs(app: Sphinx, needs, filter_string: str = "", current_need=None)
274274
return found_needs
275275

276276

277+
def need_search(*args, **kwargs):
278+
return bool(re.search(*args, **kwargs))
279+
280+
277281
@measure_time("filtering")
278282
def filter_single_need(
279283
app: Sphinx, need, filter_string: str = "", needs=None, current_need=None, filter_compiled=None
@@ -300,15 +304,20 @@ def filter_single_need(
300304
# Get needs external filter data and merge to filter_context
301305
filter_context.update(app.config.needs_filter_data)
302306

303-
filter_context["search"] = re.search
307+
filter_context["search"] = need_search
304308
result = False
305309
try:
306310
# Set filter_context as globals and not only locals in eval()!
307311
# Otherwise, the vars not be accessed in list comprehensions.
308312
if filter_compiled:
309-
result = bool(eval(filter_compiled, filter_context))
313+
result = eval(filter_compiled, filter_context)
310314
else:
311-
result = bool(eval(filter_string, filter_context))
315+
result = eval(filter_string, filter_context)
316+
if not isinstance(result, bool):
317+
# Raises NeedsInvalidFilter if the result is a string type
318+
raise NeedsInvalidFilter(
319+
f"Error when evaluating filter: expected output to have True/False but got a string <{result}>"
320+
)
312321
except Exception as e:
313322
raise NeedsInvalidFilter(f"Filter {filter_string} not valid. Error: {e}.")
314323
return result

0 commit comments

Comments
 (0)