Private
Public Access
feat(scripts): Phase 12.1+12.2+12.3 - remove Heuristic #19; fix visit_Try; add Heuristic D
Phase 12.1: REMOVE Heuristic #19 (narrow except + log = INTERNAL_COMPLIANT). Per error_handling.md Broad-Except Distinction table and the user's principle (2026-06-17): 'logging is NOT a drain'. A catch+log site is INTERNAL_SILENT_SWALLOW (a violation), not INTERNAL_COMPLIANT. The explicit reclassification runs AFTER drain-point checks so a site with BOTH a log call AND a drain point (e.g., sys.stderr.write + sys.exit) is classified by the drain point (which wins). Phase 12.2: FIX the visit_Try audit bug. The walker did NOT recurse into node.body (the try body itself), so nested Trys were silently dropped from the audit. Verified against src/api_hooks.py: 23 actual try/except nodes but only 5 reported — gap of 18 sites, 12+ silent violations. Fix: added 'for child in node.body: self.visit(child)' to ExceptionVisitor.visit_Try (placed before the handlers loop). Phase 12.3: ADD Heuristic D (5 drain-point patterns) with TDD: - D.1 HTTP error response (BaseHTTPRequestHandler.send_response) - D.2 GUI error display (imgui.open_popup) - D.3 Intentional app termination (sys.exit) - D.4 Telemetry emission (telemetry.emit_*) - D.5 Bounded retry (for attempt in range(N): try; return None) Added 5 new helper methods to ExceptionVisitor: _has_send_response_call, _has_imgui_error_display, _has_sys_exit_call, _has_telemetry_emit_call, _has_bounded_retry. Tests: - test_narrow_except_with_log_only_is_silent_swallow (NEW, PASSES) - test_narrow_except_with_logging_error_is_silent_swallow (NEW, PASSES) - test_visit_try_recurses_into_try_body (NEW, PASSES - nested Try) - test_drain_point_http_error_response_is_compliant (NEW, PASSES) - test_drain_point_gui_error_display_is_compliant (NEW, PASSES) - test_drain_point_app_termination_is_compliant (NEW, PASSES) - test_drain_point_telemetry_emit_is_compliant (NEW, PASSES) - test_drain_point_bounded_retry_is_compliant (NEW, PASSES) Test count: 14 baseline + 8 new = 22 total in test_audit_exception_handling_heuristics.py. All 22 pass (20 PASSED + 2 XFAIL from Phase 11's #22/#23 laundering heuristics).
This commit is contained in:
+230
@@ -0,0 +1,230 @@
|
||||
from pathlib import Path
|
||||
|
||||
p = Path(r"C:\projects\manual_slop_tier2\tests\test_audit_exception_handling_heuristics.py")
|
||||
with open(p, "rb") as f:
|
||||
existing = f.read()
|
||||
|
||||
# New tests content. Use byte concatenation to avoid Python string escaping.
|
||||
nl = b"\r\n" # match CRLF
|
||||
new = b""
|
||||
|
||||
new += nl * 2
|
||||
new += b"# ---------------------------------------------------------------------------" + nl
|
||||
new += b"# Phase 12.1: Heuristic #19 REMOVED - narrow except + log is INTERNAL_SILENT_SWALLOW" + nl
|
||||
new += b"# ---------------------------------------------------------------------------" + nl
|
||||
new += b"def test_narrow_except_with_log_only_is_silent_swallow():" + nl
|
||||
new += b' """try: ...; except (SpecificError): sys.stderr.write(...) is INTERNAL_SILENT_SWALLOW (a violation).' + nl
|
||||
new += b"" + nl
|
||||
new += b' Per error_handling.md "The Broad-Except Distinction" table and the user\'s' + nl
|
||||
new += b' principle (2026-06-17): "logging is NOT a drain". sys.stderr.write alone' + nl
|
||||
new += b" loses the error context; the propagation does NOT terminate visibly to" + nl
|
||||
new += b" the user. The convention requires Result[T] propagation to a true drain" + nl
|
||||
new += b" point. Heuristic #19 (which classified this as compliant) was REMOVED" + nl
|
||||
new += b" in Phase 12.1." + nl
|
||||
new += b' """' + nl
|
||||
new += b" src = (" + nl
|
||||
new += b" 'def log_failure(path, e):\\n'" + nl
|
||||
new += b" ' try:\\n'" + nl
|
||||
new += b" ' path.write_text(\"x\", encoding=\"utf-8\")\\n'" + nl
|
||||
new += b" ' except (OSError, UnicodeEncodeError):\\n'" + nl
|
||||
new += b" ' sys.stderr.write(f\"write failed: {e}\")\\n'" + nl
|
||||
new += b" )" + nl
|
||||
new += b" data = _run_audit_on_fixture(src)" + nl
|
||||
new += b' findings = _classifications_for_file(data, "audit_heuristic_fixture.py")' + nl
|
||||
new += b' excepts = [f for f in findings if f["kind"] == "EXCEPT"]' + nl
|
||||
new += b" assert len(excepts) == 1" + nl
|
||||
new += b' assert excepts[0]["category"] == "INTERNAL_SILENT_SWALLOW", (' + nl
|
||||
new += b' f"narrow except + log only should be INTERNAL_SILENT_SWALLOW (logging is NOT a drain), got {excepts[0][\'category\']}"' + nl
|
||||
new += b" )" + nl
|
||||
new += nl * 2
|
||||
new += b"def test_narrow_except_with_logging_error_is_silent_swallow():" + nl
|
||||
new += b' """try: ...; except (SpecificError): logging.error(...) is INTERNAL_SILENT_SWALLOW (a violation).' + nl
|
||||
new += b"" + nl
|
||||
new += b" Same principle as test_narrow_except_with_log_only_is_silent_swallow" + nl
|
||||
new += b" but with the logging module. Logging alone loses the error context." + nl
|
||||
new += b' """' + nl
|
||||
new += b" src = (" + nl
|
||||
new += b" 'def log_failure_via_logging(path):\\n'" + nl
|
||||
new += b" ' try:\\n'" + nl
|
||||
new += b" ' path.write_text(\"x\", encoding=\"utf-8\")\\n'" + nl
|
||||
new += b" ' except (OSError, UnicodeEncodeError) as e:\\n'" + nl
|
||||
new += b" ' logging.error(f\"write failed: {e}\")\\n'" + nl
|
||||
new += b" )" + nl
|
||||
new += b" data = _run_audit_on_fixture(src)" + nl
|
||||
new += b' findings = _classifications_for_file(data, "audit_heuristic_fixture.py")' + nl
|
||||
new += b' excepts = [f for f in findings if f["kind"] == "EXCEPT"]' + nl
|
||||
new += b" assert len(excepts) == 1" + nl
|
||||
new += b' assert excepts[0]["category"] == "INTERNAL_SILENT_SWALLOW", (' + nl
|
||||
new += b' f"narrow except + logging.error should be INTERNAL_SILENT_SWALLOW, got {excepts[0][\'category\']}"' + nl
|
||||
new += b" )" + nl
|
||||
new += nl * 2
|
||||
new += b"# ---------------------------------------------------------------------------" + nl
|
||||
new += b"# Phase 12.2: visit_Try recursion fix - nested Trys in try body are visited" + nl
|
||||
new += b"# ---------------------------------------------------------------------------" + nl
|
||||
new += b"def test_visit_try_recurses_into_try_body():" + nl
|
||||
new += b' """A nested try inside the try body should be visited and its handlers recorded.' + nl
|
||||
new += b"" + nl
|
||||
new += b" The audit's visit_Try had a bug where it did NOT recurse into node.body." + nl
|
||||
new += b" This test constructs a source with an outer try containing an inner try," + nl
|
||||
new += b" and asserts BOTH outer and inner handlers appear in the findings." + nl
|
||||
new += b' """' + nl
|
||||
new += b" src = (" + nl
|
||||
new += b" 'def outer():\\n'" + nl
|
||||
new += b" ' try:\\n'" + nl
|
||||
new += b" ' try:\\n'" + nl
|
||||
new += b" ' do_inner()\\n'" + nl
|
||||
new += b" ' except ValueError:\\n'" + nl
|
||||
new += b" ' handle_inner()\\n'" + nl
|
||||
new += b" ' do_outer_thing()\\n'" + nl
|
||||
new += b" ' except (OSError, IOError):\\n'" + nl
|
||||
new += b" ' handle_outer()\\n'" + nl
|
||||
new += b" )" + nl
|
||||
new += b" data = _run_audit_on_fixture(src)" + nl
|
||||
new += b' findings = _classifications_for_file(data, "audit_heuristic_fixture.py")' + nl
|
||||
new += b' excepts = [f for f in findings if f["kind"] == "EXCEPT"]' + nl
|
||||
new += b" assert len(excepts) == 2, (" + nl
|
||||
new += b' f"visit_Try should recurse into try body; expected 2 EXCEPT findings, got {len(excepts)}: {excepts}"' + nl
|
||||
new += b" )" + nl
|
||||
new += nl * 2
|
||||
new += b"# ---------------------------------------------------------------------------" + nl
|
||||
new += b"# Phase 12.3: Heuristic D.1 - HTTP error response drain point" + nl
|
||||
new += b"# ---------------------------------------------------------------------------" + nl
|
||||
new += b"def test_drain_point_http_error_response_is_compliant():" + nl
|
||||
new += b' """try: ...; except (SpecificError): self.send_response(500, ...) is INTERNAL_COMPLIANT (drain point D.1).' + nl
|
||||
new += b"" + nl
|
||||
new += b" Per error_handling.md Drain Points section, Pattern 1: HTTP error" + nl
|
||||
new += b" response in a BaseHTTPRequestHandler subclass IS a drain point. The" + nl
|
||||
new += b" HTTP status code IS the visible user feedback; the propagation" + nl
|
||||
new += b" terminates at the HTTP response. Heuristic D.1 recognizes this pattern." + nl
|
||||
new += b' """' + nl
|
||||
new += b" src = (" + nl
|
||||
new += b" 'class Handler(BaseHTTPRequestHandler):\\n'" + nl
|
||||
new += b" ' def do_GET(self):\\n'" + nl
|
||||
new += b" ' try:\\n'" + nl
|
||||
new += b" ' self._read_body()\\n'" + nl
|
||||
new += b" ' except (OSError, ValueError) as e:\\n'" + nl
|
||||
new += b" ' self.send_response(500)\\n'" + nl
|
||||
new += b" ' self.send_header(\"Content-Type\", \"application/json\")\\n'" + nl
|
||||
new += b" ' self.wfile.write(b\\'{\"error\": \"internal\"}\\')\\n'" + nl
|
||||
new += b" )" + nl
|
||||
new += b" data = _run_audit_on_fixture(src)" + nl
|
||||
new += b' findings = _classifications_for_file(data, "audit_heuristic_fixture.py")' + nl
|
||||
new += b' excepts = [f for f in findings if f["kind"] == "EXCEPT"]' + nl
|
||||
new += b" assert len(excepts) == 1" + nl
|
||||
new += b' assert excepts[0]["category"] == "INTERNAL_COMPLIANT", (' + nl
|
||||
new += b' f"HTTP error response should be INTERNAL_COMPLIANT (drain point D.1), got {excepts[0][\'category\']}: {excepts[0].get(\'note\', \'\')}"' + nl
|
||||
new += b" )" + nl
|
||||
new += nl * 2
|
||||
new += b"# ---------------------------------------------------------------------------" + nl
|
||||
new += b"# Phase 12.3: Heuristic D.2 - GUI error display drain point" + nl
|
||||
new += b"# ---------------------------------------------------------------------------" + nl
|
||||
new += b"def test_drain_point_gui_error_display_is_compliant():" + nl
|
||||
new += b' """try: ...; except (SpecificError): imgui.open_popup(...) is INTERNAL_COMPLIANT (drain point D.2).' + nl
|
||||
new += b"" + nl
|
||||
new += b" Per error_handling.md Drain Points section, Pattern 2: GUI error" + nl
|
||||
new += b" display via imgui.open_popup IS a drain point. The user sees the" + nl
|
||||
new += b" error modal." + nl
|
||||
new += b' """' + nl
|
||||
new += b" src = (" + nl
|
||||
new += b" 'def show_load_error():\\n'" + nl
|
||||
new += b" ' try:\\n'" + nl
|
||||
new += b" ' do_load()\\n'" + nl
|
||||
new += b" ' except (OSError, ValueError):\\n'" + nl
|
||||
new += b" ' imgui.open_popup(\"Load Error\")\\n'" + nl
|
||||
new += b" )" + nl
|
||||
new += b" data = _run_audit_on_fixture(src)" + nl
|
||||
new += b' findings = _classifications_for_file(data, "audit_heuristic_fixture.py")' + nl
|
||||
new += b' excepts = [f for f in findings if f["kind"] == "EXCEPT"]' + nl
|
||||
new += b" assert len(excepts) == 1" + nl
|
||||
new += b' assert excepts[0]["category"] == "INTERNAL_COMPLIANT", (' + nl
|
||||
new += b' f"GUI error display should be INTERNAL_COMPLIANT (drain point D.2), got {excepts[0][\'category\']}"' + nl
|
||||
new += b" )" + nl
|
||||
new += nl * 2
|
||||
new += b"# ---------------------------------------------------------------------------" + nl
|
||||
new += b"# Phase 12.3: Heuristic D.3 - Intentional app termination drain point" + nl
|
||||
new += b"# ---------------------------------------------------------------------------" + nl
|
||||
new += b"def test_drain_point_app_termination_is_compliant():" + nl
|
||||
new += b' """try: ...; except (SpecificError): sys.exit(1) is INTERNAL_COMPLIANT (drain point D.3).' + nl
|
||||
new += b"" + nl
|
||||
new += b" Per error_handling.md Drain Points section, Pattern 3: intentional" + nl
|
||||
new += b" app termination via sys.exit IS a drain point. The process exit IS" + nl
|
||||
new += b" the termination of the propagation." + nl
|
||||
new += b' """' + nl
|
||||
new += b" src = (" + nl
|
||||
new += b" 'def critical_init():\\n'" + nl
|
||||
new += b" ' try:\\n'" + nl
|
||||
new += b" ' load_config()\\n'" + nl
|
||||
new += b" ' except (OSError, ValueError):\\n'" + nl
|
||||
new += b" ' sys.stderr.write(\"FATAL: config missing\\n\")\\n'" + nl
|
||||
new += b" ' sys.exit(1)\\n'" + nl
|
||||
new += b" )" + nl
|
||||
new += b" data = _run_audit_on_fixture(src)" + nl
|
||||
new += b' findings = _classifications_for_file(data, "audit_heuristic_fixture.py")' + nl
|
||||
new += b' excepts = [f for f in findings if f["kind"] == "EXCEPT"]' + nl
|
||||
new += b" assert len(excepts) == 1" + nl
|
||||
new += b' assert excepts[0]["category"] == "INTERNAL_COMPLIANT", (' + nl
|
||||
new += b' f"app termination should be INTERNAL_COMPLIANT (drain point D.3), got {excepts[0][\'category\']}"' + nl
|
||||
new += b" )" + nl
|
||||
new += nl * 2
|
||||
new += b"# ---------------------------------------------------------------------------" + nl
|
||||
new += b"# Phase 12.3: Heuristic D.4 - Telemetry emission drain point" + nl
|
||||
new += b"# ---------------------------------------------------------------------------" + nl
|
||||
new += b"def test_drain_point_telemetry_emit_is_compliant():" + nl
|
||||
new += b' """try: ...; except (SpecificError): telemetry.emit_error(...) is INTERNAL_COMPLIANT (drain point D.4).' + nl
|
||||
new += b"" + nl
|
||||
new += b" Per error_handling.md Drain Points section, Pattern 4: telemetry" + nl
|
||||
new += b" emission IS a drain point. The error reaches the monitoring system." + nl
|
||||
new += b' """' + nl
|
||||
new += b" src = (" + nl
|
||||
new += b" 'def report_failure():\\n'" + nl
|
||||
new += b" ' try:\\n'" + nl
|
||||
new += b" ' do_thing()\\n'" + nl
|
||||
new += b" ' except (OSError, ValueError):\\n'" + nl
|
||||
new += b" ' telemetry.emit_error(operation=\"do_thing\", kind=\"INTERNAL\", message=\"failed\")\\n'" + nl
|
||||
new += b" )" + nl
|
||||
new += b" data = _run_audit_on_fixture(src)" + nl
|
||||
new += b' findings = _classifications_for_file(data, "audit_heuristic_fixture.py")' + nl
|
||||
new += b' excepts = [f for f in findings if f["kind"] == "EXCEPT"]' + nl
|
||||
new += b" assert len(excepts) == 1" + nl
|
||||
new += b' assert excepts[0]["category"] == "INTERNAL_COMPLIANT", (' + nl
|
||||
new += b' f"telemetry emit should be INTERNAL_COMPLIANT (drain point D.4), got {excepts[0][\'category\']}"' + nl
|
||||
new += b" )" + nl
|
||||
new += nl * 2
|
||||
new += b"# ---------------------------------------------------------------------------" + nl
|
||||
new += b"# Phase 12.3: Heuristic D.5 - Bounded retry drain point" + nl
|
||||
new += b"# ---------------------------------------------------------------------------" + nl
|
||||
new += b"def test_drain_point_bounded_retry_is_compliant():" + nl
|
||||
new += b' """try: ...; except (SpecificError): for attempt in range(3): ...; return None is INTERNAL_COMPLIANT (drain point D.5).' + nl
|
||||
new += b"" + nl
|
||||
new += b" Per error_handling.md Drain Points section, Pattern 5: bounded retry" + nl
|
||||
new += b" followed by return None IS a drain point. The retry is bounded (no" + nl
|
||||
new += b" infinite loop); the final None propagates to a visible error UI." + nl
|
||||
new += b' """' + nl
|
||||
new += b" src = (" + nl
|
||||
new += b" 'def load_with_retry():\\n'" + nl
|
||||
new += b" ' for attempt in range(3):\\n'" + nl
|
||||
new += b" ' try:\\n'" + nl
|
||||
new += b" ' do_load()\\n'" + nl
|
||||
new += b" ' return \"ok\"\\n'" + nl
|
||||
new += b" ' except (OSError, ValueError):\\n'" + nl
|
||||
new += b" ' time.sleep(1)\\n'" + nl
|
||||
new += b" ' return None\\n'" + nl
|
||||
new += b" )" + nl
|
||||
new += b" data = _run_audit_on_fixture(src)" + nl
|
||||
new += b' findings = _classifications_for_file(data, "audit_heuristic_fixture.py")' + nl
|
||||
new += b' excepts = [f for f in findings if f["kind"] == "EXCEPT"]' + nl
|
||||
new += b" assert len(excepts) == 1" + nl
|
||||
new += b' assert excepts[0]["category"] == "INTERNAL_COMPLIANT", (' + nl
|
||||
new += b' f"bounded retry should be INTERNAL_COMPLIANT (drain point D.5), got {excepts[0][\'category\']}"' + nl
|
||||
new += b" )" + nl
|
||||
|
||||
# Append
|
||||
result = existing + new
|
||||
with open(p, "wb") as f:
|
||||
f.write(result)
|
||||
|
||||
print(f"wrote {len(result)} chars (added {len(new)} chars)")
|
||||
# Verify parses
|
||||
import ast
|
||||
ast.parse(result.decode("utf-8"))
|
||||
print("parses ok")
|
||||
@@ -0,0 +1,23 @@
|
||||
from pathlib import Path
|
||||
p = Path(r"C:\projects\manual_slop_tier2\tests\test_audit_exception_handling_heuristics.py")
|
||||
data = p.read_bytes()
|
||||
|
||||
# In the test file source (Python source code), the test source string is:
|
||||
# ' sys.stderr.write("FATAL: config missing\\n")\n'
|
||||
# When Python parses this, it becomes the actual string:
|
||||
# ' sys.stderr.write("FATAL: config missing\n")\n' (with real \n in string literal)
|
||||
# When this is written to a fixture file, the file gets a real newline INSIDE the
|
||||
# string literal, breaking the syntax.
|
||||
#
|
||||
# Fix: change "\\n" to "" (no newline in the message string).
|
||||
needle = b' sys.stderr.write("FATAL: config missing\\\\n")\\n'
|
||||
replacement = b' sys.stderr.write("FATAL: config missing")\\n'
|
||||
if needle in data:
|
||||
data = data.replace(needle, replacement)
|
||||
p.write_bytes(data)
|
||||
print("ok: removed \\n from sys.stderr.write message")
|
||||
else:
|
||||
print(f"NOT FOUND; bytes: {needle!r}")
|
||||
idx = data.find(b"FATAL")
|
||||
if idx > 0:
|
||||
print(f"context: {data[idx-20:idx+50]!r}")
|
||||
Reference in New Issue
Block a user