SyntaxError when free variable name is also an exception target #48867
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
assignee = 'https://github.com/amauryfa' closed_at = <Date 2010-09-10.22:02:17.749> created_at = <Date 2008-12-10.12:18:42.495> labels = ['interpreter-core', 'type-feature'] title = 'SyntaxError when free variable name is also an exception target' updated_at = <Date 2013-04-19.00:49:14.320> user = 'https://github.com/amauryfa'
activity = <Date 2013-04-19.00:49:14.320> actor = 'barry' assignee = 'amaury.forgeotdarc' closed = True closed_date = <Date 2010-09-10.22:02:17.749> closer = 'amaury.forgeotdarc' components = ['Interpreter Core'] creation = <Date 2008-12-10.12:18:42.495> creator = 'amaury.forgeotdarc' dependencies =  files = ['12318'] hgrepos =  issue_num = 4617 keywords = ['patch'] message_count = 20.0 messages = ['77536', '77691', '77693', '77696', '77703', '77704', '78434', '79228', '99247', '99855', '99866', '99880', '99911', '99915', '99918', '99950', '113312', '113335', '113340', '116048'] nosy_count = 11.0 nosy_names = ['gvanrossum', 'jhylton', 'barry', 'rhettinger', 'terry.reedy', 'pje', 'amaury.forgeotdarc', 'benjamin.peterson', 'ezio.melotti', 'cmcqueen1975', 'flox'] pr_nums =  priority = 'normal' resolution = 'fixed' stage = 'resolved' status = 'closed' superseder = None type = 'enhancement' url = 'https://bugs.python.org/issue4617' versions = ['Python 3.2']
The text was updated successfully, but these errors were encountered:
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
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
It's an interesting bug. Maybe the compiler shouldn't allow you to
On Thu, Feb 11, 2010 at 9:09 PM, Craig McQueen <email@example.com> 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
On Mon, Feb 22, 2010 at 6:51 PM, Jeremy Hylton <firstname.lastname@example.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.
I don't think so. It's very marginal.
--Guido (on Android)
On Feb 23, 2010 8:52 AM, "Amaury Forgeot d'Arc" <email@example.com>
Amaury Forgeot d'Arc <firstname.lastname@example.org> added the comment:
The above patch adds a new opcode (DELETE_DEREF), does the Moratorium apply
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.
On Tue, Feb 23, 2010 at 9:38 AM, Guido van Rossum
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.