Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed
- Non-UTF-8 `.dmai` files now return a dedicated CLI error instead of a
raw Python traceback. Closes #27.

## [0.4.0] — 2026-07-23

First release published to PyPI since 0.2.0 (0.3.0 was an internal
Expand Down
12 changes: 10 additions & 2 deletions maithili_dsl/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ def _validate_imports(python_code, translated_modules):
EXIT_LINT_ERROR = 4
EXIT_IMPORT_BLOCKED = 5
EXIT_RUNTIME_ERROR = 6
EXIT_BAD_ENCODING = 7


def run_dmai_file(file_path):
Expand All @@ -116,8 +117,15 @@ def run_dmai_file(file_path):
print(f"Error: File '{file_path}' not found.")
return EXIT_NOT_FOUND

with open(path, 'r', encoding='utf-8') as file:
maithili_code = file.read()
try:
with open(path, 'r', encoding='utf-8') as file:
maithili_code = file.read()
except UnicodeDecodeError:
print("⚠️ त्रुटि: फाइल UTF-8 में नहि अछि।")
return EXIT_BAD_ENCODING
except OSError:
print("⚠️ त्रुटि: फाइल पढ़ल नहि जा सकल।")
return EXIT_NOT_FOUND

errors = lint_maithili_code(maithili_code)
if errors:
Expand Down
29 changes: 29 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import pytest

from maithili_dsl.cli import (
EXIT_BAD_ENCODING,
EXIT_IMPORT_BLOCKED,
EXIT_LINT_ERROR,
EXIT_NOT_FOUND,
Expand All @@ -30,6 +31,34 @@ def test_missing_file_returns_not_found(capsys):
assert "not found" in captured.out.lower()


def test_non_utf8_file_returns_bad_encoding(tmp_path, capsys):
path = tmp_path / "invalid.dmai"
path.write_bytes(b"\xff\xfe")

code = run_dmai_file(str(path))
captured = capsys.readouterr()

assert code == EXIT_BAD_ENCODING
assert "UTF-8" in captured.out
assert "Traceback" not in captured.out


def test_unreadable_file_returns_not_found(tmp_path, monkeypatch, capsys):
path = tmp_path / "unreadable.dmai"
path.write_text("छपाउ(\"hello\")\n", encoding="utf-8")

def deny_read(*args, **kwargs):
raise PermissionError("private path details")

monkeypatch.setattr("builtins.open", deny_read)
code = run_dmai_file(str(path))
captured = capsys.readouterr()

assert code == EXIT_NOT_FOUND
assert "फाइल पढ़ल नहि जा सकल" in captured.out
assert "private path details" not in captured.out


def test_lint_error_returns_lint_exit_code(tmp_dmai_file, capsys):
# The leading = triggers the "'=' चिह्नक पहिले" lint error.
path = tmp_dmai_file("= ५\n")
Expand Down
14 changes: 14 additions & 0 deletions tests/test_security.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
_validate_imports,
run_dmai_file,
translate_imports,
EXIT_BAD_ENCODING,
EXIT_OK,
EXIT_IMPORT_BLOCKED,
EXIT_RUNTIME_ERROR,
Expand Down Expand Up @@ -291,3 +292,16 @@ def test_actual_dangerous_code_outside_strings_still_blocked():
transpiled = transpile_maithili_code(code)
errors = _validate_imports(transpiled, translated_modules=set())
assert errors, f"raw 'import os' must be blocked by import validator: {errors}"


@pytest.mark.security
def test_invalid_encoding_is_rejected_before_execution(tmp_path, capsys):
"""Malformed source must fail closed without reaching the sandbox."""
path = tmp_path / "invalid.dmai"
path.write_bytes(b"\xff\xfe")

code = run_dmai_file(str(path))
captured = capsys.readouterr()

assert code == EXIT_BAD_ENCODING
assert "UTF-8" in captured.out