Given your strict requirements for safety and accuracy, where any deviation from the true result ("ok" or "exception detected") makes the solution useless, and given that arbitrary Python code can have side effects and external dependencies, it is impossible to satisfy your requirements without actually executing the code in a fully representative environment. And executing that code introduces the very security risks you're trying to avoid.
I'm sorry to give you this answer, but it's crucial to be honest about the limitations:
-
Accuracy Requires Full Execution: Determining whether a piece of code will result in an exception without actually executing it is undecidable in the general case due to the halting problem and the complexity of Python's runtime semantics. You cannot guarantee the correct "ok" or "exception detected" result without executing the code.
-
Safety Implies Isolation: Executing arbitrary code requires full isolation to prevent it from compromising the system. Even seemingly safe operations like reading files or accessing the network can be exploited. This typically means running the code in a sandboxed environment like a VM or container.
-
VM or Container Accuracy Issues: You correctly identified that using a VM or container can lead to false negatives if the environment differs from the target. For example, missing network access, different file system layouts, or missing libraries can cause code to fail that would succeed in the intended environment.
The Core Problem: Undecidability and External Dependencies
Here's why you're running into such a fundamental barrier:
- Halting Problem: In general, it's impossible to predict whether a program will halt or run forever. A program that takes an arbitrarily long time to execute might appear to have an error when it's simply taking a very long time.
- External Dependencies: Arbitrary Python code can depend on:
- Files on the file system
- Network connections
- Operating system state
- Specific libraries or modules
Conclusion
There is no solution that can provide both perfect safety and perfect accuracy. The problem you're trying to solve is fundamentally impossible with the constraints you've specified.
Alternatives to Consider (Which Don't Fully Meet Requirements, but Might be Useful in Specific Cases)
-
Limited Subset and Custom Parser:
- Define a very restricted subset of Python that you can easily and safely analyze.
- Write a custom parser and interpreter for that subset.
- This approach is safe because you control the semantics and can prevent any harmful operations.
- This approach is accurate only for code that falls within that limited subset. Code outside the subset is useless.
-
Static Analysis with Significant Limitations:
- Use static analysis tools to identify potential errors (e.g., type errors, undefined variables).
- These tools can suggest potential errors but cannot definitively determine whether the code will fail at runtime.
- They also might generate false positives.
-
Controlled Execution with Post-Mortem Analysis:
- Run the code in a sandboxed environment with strict resource limits.
- Monitor the execution for exceptions.
- If an exception occurs, capture the state of the program (e.g., memory dumps, logs).
- Perform post-mortem analysis to understand the root cause of the exception.
- Even with post-mortem analysis, you cannot guarantee that you'll understand all exceptions.
Final Recommendation
- If safety is absolutely paramount and you're willing to accept that the solution will not work for arbitrary Python code, go with the limited subset and custom parser approach.
- Otherwise, you must accept the risks of running the code or acknowledge that you cannot accurately determine whether it will throw an exception without executing it in a representative environment.