Skip to content

Promote warnings to errors? #114

@aaaaalbert

Description

@aaaaalbert

(Note: The mechanics of the problem were first discussed under the tangentially related #69.)

There are a few code constructs that can raise Warnings (see Python docs), which can cause trouble in nested VirtualNamespaces (as used by dylink for example).

For example, consider the snippet

assert(False, "A message")

This does not raise an AssertionError, because assert is a keyword (not a function), so the parenthesized expression evaluates as a tuple (rather than as arguments), which is truthy. This generates SyntaxWarning: assertion is always true, perhaps remove parentheses? on stderr as a notification about this problem, but the program will continue to run.

If you run RepyV2 from the command line, you will see the SyntaxWarning. If you run the above snippet on a vessel, you won't see the message. This might or might not be a problem for you.

However, if you run the snippet inside of a VirtualNamespace, or in a program that uses dylink, the SyntaxWarning causes this weird traceback, courtesy of Python's warning library that gets invoked when the nested VirtualNamespace is checked for safety and found to contain the potential syntax problem (see #69):

python repy.py restrictions.default dylink.r2py test-assert.r2py

---
Uncaught exception!

---
Following is a full traceback, and a user traceback.
The user traceback excludes non-user modules. The most recent call is displayed last.

Full debugging traceback:
  "repyV2/repy.py", line 177, in execute_namespace_until_completion
  "/Users/albert/Desktop/seattle/seattle_repy/repyV2/virtual_namespace.py", line 117, in evaluate
  "/Users/albert/Desktop/seattle/seattle_repy/repyV2/safe.py", line 588, in safe_run
  "dylink.r2py", line 546, in <module>
  "dylink.r2py", line 397, in dylink_dispatch
  "dylink.r2py", line 155, in _dy_module_code

User traceback:
  "dylink.r2py", line 546, in <module>
  "dylink.r2py", line 397, in dylink_dispatch
  "dylink.r2py", line 155, in _dy_module_code

Exception (with type 'exceptions.Exception'): Failed to initialize the module (test-assert.r2py)! Got the following exception: 'RunBuiltinException("Unsafe call 'open' with args '('test-assert.r2py', 'rU')', kwargs '{}'",)'

---

We could probably stash the unsafe calls required by warnings in safe.py somehow, as we do with a couple of others (like getattr), and through this enable the behavior of SyntaxWarning (and other warnings) as in the non-dylinked / non-VirtualNamespace case.

I would personally prefer to promote warnings to errors instead, so that proper exceptions are raised for every warning. This requires a simple one-line patch,

--- a/safe.py
+++ b/safe.py
@@ -92,7 +92,7 @@ except ImportError:
 import warnings
 warnings.simplefilter('ignore')
 import compiler     # Required for the code safety check
-warnings.resetwarnings()
+warnings.simplefilter('error')

and does two things: It stops the program, and outputs the warning message in a visible manner, regardless of whether you can see stderr:

Exception (with type 'exceptions.Exception'): Failed to initialize the module (test-assert.r2py)! Got the following exception: 'SyntaxWarning('assertion is always true, perhaps remove parentheses?',)'

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions