-
-
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
SyntaxError when free variable name is also an exception target #48867
Comments
This issue comes from bpo-4613. The following code raises a def f():
e = None
def g():
e
The reason is because of The above code is correct, and should work. After all, the "variable referenced" has no value before it is set, The Attached patch adds a DELETE_DEREF opcode, that removes the value of Some compiler experts should review it. Few regressions are possible, Tests are to come, but I'd like other's suggestions. |
-1 as I understand the proposal. Your code is bugged and should fail as If I understand correctly, you agree that the SyntaxError is correct as If the latter, you want def f():
e = 1
del e
def g(): print(e)
return g to compile. I would not. Your reason "After all, the "variable |
Not sure the "del e" idea was a good solution to the garbage collection Phillip, any thoughts? |
Terry, my motivation is that the sample code above runs correctly with First I thought to turn the implicit "del e" into something else (and change |
I could argue either way on this one; it's true that deleting a In neither case, however, do I think it's appropriate to drop the (Btw, I'm not sure why this one's assigned to me; ISTM I might have |
Guido, any thoughts? |
I think being able to delete free variables is reasonable and brings |
I don't think this has much to do with try/except. That it works in 2.6 It has to do with deletion of a variable that's held in a cell for def outer():
x = 0
def inner(): return x
del x # SyntaxError I suspect (but do not know for sure) that the reason this is considered I think it's fine to fix this in 2.7 and 3.1, but I don't see it as a I don't see a reason to declare this a release blocker just because the |
There's also this one which caught me out: def outer():
x = 0
y = (x for i in range(10))
del x # SyntaxError |
It's an interesting bug. Maybe the compiler shouldn't allow you to On Thu, Feb 11, 2010 at 9:09 PM, Craig McQueen <report@bugs.python.org> wrote:
|
All examples so far (*) have to do with our inability to have properly nested blocks. If we did, I'd make the except clause a block, and I'd issue a syntax warning or error if a nested block shadowed a variable referenced outside it. Ditto for generator expressions and comprehensions. As long as we don't have nested blocks, I think it's okay to see the limitation on (implicit or explicit) "del" of a cell variable as a compiler deficiency and fix that deficiency. (*) However there's also this example: >>> def f():
... try: 1/0
... except Exception as a:
... def g(): return a
... return g
...
SyntaxError: can not delete variable 'a' referenced in nested scope
>>> |
On Mon, Feb 22, 2010 at 6:10 PM, Guido van Rossum
There's no reason we couldn't revise the language spec to explain that y = 10
try:
...
except SomeError as err:
y = 12
print y # prints 10 In the example above, y would be a local variable in the scope of the
The general request here is to remove all the SyntaxErrors about hoping-for-some-bdfl-pronouncements-ly y'rs,
|
On Mon, Feb 22, 2010 at 6:51 PM, Jeremy Hylton <report@bugs.python.org> wrote:
However (even apart from the below example) it would be tough to
Yeah, there are all sorts of problems with less-conspicuous nested
Yeah, if we could kill those SyntaxErrors we can leave the rest as is. |
The above patch adds a new opcode (DELETE_DEREF), does the Moratorium apply here? |
I don't think so. It's very marginal. --Guido (on Android) On Feb 23, 2010 8:52 AM, "Amaury Forgeot d'Arc" <report@bugs.python.org> Amaury Forgeot d'Arc <amauryfa@gmail.com> added the comment: The above patch adds a new opcode (DELETE_DEREF), does the Moratorium apply ---------- Python tracker <report@bugs.python.org> |
The patch looks pretty good. I'd factor out the common error-checking code (common between It would also be good to add some test cases. Jeremy On Tue, Feb 23, 2010 at 9:38 AM, Guido van Rossum
|
This bug is waiting for unit tests and a small patch cleanup. |
I have changed my mind on this issue. Since e = 1
del e
def g(): print(e)
g() compiles and raises a run-time name error, so should the same code embedded within a function. In either case, the premature deletion is a logic error, not a syntax error. However, changing the language definition, even to fix what is considered a design bug, is a feature request. For both 2.7 and 3.1, section 6.5. "The del statement", says "It is illegal to delete a name from the local namespace if it occurs as a free variable in a nested block." So this seems too late for 2.7. On the other hand, Guido has allowed it for 3.2 in spite of the moratorium, but I think it should go in the initial release. |
Yeah, please fix in 3.2, don't fix in 2.7. |
Fixed in r84685, with tests and doc updates. |
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: