Skip to content

Commit

Permalink
Fix test on invalid expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
FrancescAlted committed Oct 18, 2024
1 parent 4cc3947 commit 8042d94
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 8 deletions.
6 changes: 1 addition & 5 deletions src/blosc2/lazyexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ def validate_expr(expr: str) -> None:

# Check for forbidden patterns
if _blacklist_re.search(skip_quotes) is not None:
raise ValueError(f"Expression {expr} has forbidden control characters.")
raise ValueError(f"'{expr}' is not a valid expression.")

# Check for invalid characters not covered by the tokenizer
invalid_chars = re.compile(r"[^\w\s+\-*/%().,=<>!&|~^]")
Expand Down Expand Up @@ -1968,10 +1968,6 @@ def save(self, **kwargs):
if value.schunk.urlpath is None:
raise ValueError("To save a LazyArray, all operands must be stored on disk/network")
operands[key] = value.schunk.urlpath
# Check that the expression is valid
# ne.validate(self.expression, locals=operands)
# Is that necessary here? I think this has been done already.
validate_expr(self.expression)
array.schunk.vlmeta["_LazyArray"] = {
"expression": self.expression,
"UDF": None,
Expand Down
6 changes: 3 additions & 3 deletions tests/ndarray/test_lazyexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,16 +543,16 @@ def test_save_unsafe():
expr = blosc2.open(urlpath)
# Replace expression by a (potentially) unsafe expression
expr.expression = "import os; os.system('touch /tmp/unsafe')"
with pytest.raises(Exception) as excinfo:
with pytest.raises(ValueError) as excinfo:
expr.compute()
assert expr.expression in str(excinfo.value)

# Check that an invalid expression cannot be easily saved.
# As this can easily be worked around, the best protection is
# during loading time (tested above).
with pytest.raises(Exception) as excinfo:
with pytest.raises(ValueError) as excinfo:
expr.save(urlpath=urlpath)
assert "invalid syntax" in str(excinfo.value)
assert expr.expression in str(excinfo.value)

for urlpath in disk_arrays:
blosc2.remove_urlpath(urlpath)
Expand Down

0 comments on commit 8042d94

Please sign in to comment.