Skip to content
Closed
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
13 changes: 7 additions & 6 deletions src/mqt/bench/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def generate_header(
"The Python package `mqt.bench` is not installed in the current "
"environment. Install it with\n\n"
" pip install mqt.bench\n\n"
f"and try again. (Original error: {exc})"
"and try again."
)
raise MQTBenchExporterError(msg) from exc

Expand Down Expand Up @@ -155,7 +155,7 @@ def write_circuit(
destination.write(header)
(dump2 if fmt is OutputFormat.QASM2 else dump3)(qc, destination)
except Exception as exc: # pragma: no cover - unforeseen I/O
msg = f"Failed to write QASM stream. (Original error: {exc})"
msg = "Failed to write QASM stream."
raise MQTBenchExporterError(msg) from exc
Comment thread
coderabbitai[bot] marked this conversation as resolved.
return

Expand All @@ -166,7 +166,7 @@ def write_circuit(
try:
dump_qpy(_attach_metadata(qc, header), destination)
except Exception as exc:
msg = f"Failed to write QPY stream. (Original error: {exc})"
msg = "Failed to write QPY stream."
raise MQTBenchExporterError(msg) from exc
return

Expand All @@ -179,15 +179,15 @@ def write_circuit(
f.write(header)
(dump2 if fmt is OutputFormat.QASM2 else dump3)(qc, f)
except Exception as exc:
msg = f"Failed to write {fmt.value.upper()} file to {destination}. (Original error: {exc})"
msg = f"Failed to write {fmt.value.upper()} file to {destination}."
raise MQTBenchExporterError(msg) from exc
Comment on lines 181 to 183
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Removing {exc} from the error message breaks test_write_circuit_io_error.

test_write_circuit_io_error in tests/test_bench.py (line 787) asserts:

assert "disk full" in msg.lower()   # msg = str(exc.value)

str(exc.value) is the MQTBenchExporterError message string only — Python does not include __cause__ in str(). With the current message f"Failed to write {fmt.value.upper()} file to {destination}.", the string does not contain "disk full", so the assertion fails.

The same applies to lines 158, 169, and 190 (stream and QPY paths), though those are not exercised by this particular test case.

Either update the test to drop the "disk full" assertion and rely solely on the generic message check at line 786, or re-embed the cause in the message:

🛠️ Option A — Update the test (aligned with PR intent)
 msg = str(exc.value)
 assert "failed to write qasm2 file" in msg.lower()
-assert "disk full" in msg.lower()
🛠️ Option B — Re-embed the exception in the message
-            msg = f"Failed to write {fmt.value.upper()} file to {destination}."
+            msg = f"Failed to write {fmt.value.upper()} file to {destination}: {exc}."
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/mqt/bench/output.py` around lines 181 - 183, The raised
MQTBenchExporterError currently omits the original exception text, breaking
tests that expect the cause (e.g., "disk full"); update the except handlers that
raise MQTBenchExporterError to include the original exception message (exc) in
the new message (e.g., f"Failed to write {fmt.value.upper()} file to
{destination}: {exc}"), and apply the same change to the similar handlers
referenced for stream and QPY paths so the original error cause is preserved in
the MQTBenchExporterError message.


elif fmt is OutputFormat.QPY:
try:
with destination.open("wb") as f:
dump_qpy(_attach_metadata(qc, header), f)
except Exception as exc:
msg = f"Failed to write QPY file to {destination}. (Original error: {exc})"
msg = f"Failed to write QPY file to {destination}."
raise MQTBenchExporterError(msg) from exc

else:
Expand Down Expand Up @@ -220,7 +220,8 @@ def save_circuit(
try:
write_circuit(qc, path, level, output_format, target)
except MQTBenchExporterError as e:
print(e)
cause = f": {e.__cause__}" if e.__cause__ else ""
print(f"{e}{cause}")
return False

return True
Expand Down
8 changes: 4 additions & 4 deletions tests/test_bench.py
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,8 @@ def test_generate_header_minimal(monkeypatch: pytest.MonkeyPatch) -> None:
hdr = generate_header(OutputFormat.QASM3, BenchmarkLevel.INDEP)
lines = hdr.splitlines()
# first line has today's date
assert lines[0] == f"// Benchmark created by MQT Bench on {datetime.datetime.now(tz=datetime.timezone.utc).date()}"
today = datetime.datetime.now(tz=datetime.timezone.utc).date()
assert lines[0] == f"// Benchmark created by MQT Bench on {today}"
# contains the fixed info lines
assert "// For more info: https://mqt-bench.app/" in hdr
assert "// MQT Bench version: 9.9.9" in hdr
Expand Down Expand Up @@ -756,9 +757,8 @@ def test_write_circuit_qpy(tmp_path: Path) -> None:
assert isinstance(circ, QuantumCircuit)

header = circ.metadata["mqt_bench"]
assert header.startswith(
f"// Benchmark created by MQT Bench on {datetime.datetime.now(tz=datetime.timezone.utc).date()}"
)
today = datetime.datetime.now(tz=datetime.timezone.utc).date()
assert header.startswith(f"// Benchmark created by MQT Bench on {today}")
assert "// MQT Bench version:" in header
assert "// Output format: qpy" in header

Expand Down
Loading