-
-
Notifications
You must be signed in to change notification settings - Fork 29.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
About the "assert" bytecode #79061
Comments
When I was involved in the YaPyPy project, I found a problem, An source "assert 0" will compile to an bytecode, On that bytecode sequence, it always raise a "AssertionError" from global, when we rewrite global "AssertionError" like here:
so, "assert" is meta-programming? |
If I have understood you correctly, you are asking whether it is expected behaviour for assert to use the global AssertionError instead of the built-in AssertionError. I don't see why it shouldn't. In any case, that behaviour goes back to Python 1.5 and perhaps older. I don't think this is a bug, so I'm going to close this. If you disagree, please re-open it with more detail explaining why you think it is a bug. |
Steven, this problem is quite interesting because it just allows users to cause fatal errors without any invalid operations.
It's better to deal with it seriously. |
If we could generate
|
On Wed, Oct 03, 2018 at 09:49:06AM +0000, thautwarm wrote:
How is that different from every other case of shadowing a builtin? len = 45
print(len("hello world")) The ability to shadow builtins is a feature, not a bug. I have no specific objection to changing assert so that it raises the (To be honest, that's what I assumed it would do, until I tried it.) |
Reply to this:
Another difference is that shadow builtins could be resumed in the
|
I think this is a bug that should be fixed. This is similar to how f-strings used to work: the generated byte code would call format(something-or-other), and if you'd defined a name "format" in your code it would fail. Now admittedly "format" is more common than "AssertionError", but in any event I don't think assert should fail because of a name you happen to be using. That's a surprising action-at-a-distance, in my mind. And I especially think that's true in the case of assert: when an assert fires, the last thing I want is something that I normally wouldn't have tested for causing me to not see what the assertion is. And, I think a broader discussion on python-dev might be useful, too, in order to get more opinions. |
On Wed, Oct 03, 2018 at 01:56:00PM +0000, Eric V. Smith wrote:
Supporting this position, shadowing other exceptions doesn't change the py> TypeError = None
py> 1 + "a"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str' On the other hand, this has been the behaviour going back to Python 1.5, Hence this would be an enhancement rather than a bug fix.
I agree. I think we need to clarify the intent here, and then decide |
Done: https://mail.python.org/pipermail/python-dev/2018-October/155410.html Changing the status to Pending until we have more clarity on whether this is a bug, feature, accident or whatnot. |
Assuming it is not crazy complicated to fix, I would like to to be changed. It should work like the TypeError example. That fact that it has worked the current way since Python 1.5 isn't a strong argument. I think no one should be surprised if it changes. Also, expecting other Python implementations to match the LOAD_GLOBAL behavior seems to put unnecessary burden on them. If someone writes code that relies on that behavior, they should not be surprised if it gets broken, IMHO. |
A middle ground might be to copy the behavior of |
This can't be changed in 3.6. There are two ways of changing it:
The latter way is easier. Bytecode is changed in every feature release, and it was already changed in 3.8. New marshal versions are added less often. There was similar issue with StopAsyncIteration. See bpo-33041. |
Hi, Serhiy, there could be an another way to fix all this sort of problems IMO. We can figure out all the cases that implicitly require shadow builtins, and then change symtable visitor to add corresponding free variables with name mangling, for instance:
where ".AssertionError" is a name-mangled free variable and is assigned once the module is executed. The same to |
Thank you Zackery for your contribution.
But this still allows to overriding builtin AssertionError before importing a module. And this solution would be much more complex that adding a new opcode. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: