Skip to content
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

List comprehensions are leaking variables #47201

Closed
donmez mannequin opened this issue May 23, 2008 · 4 comments
Closed

List comprehensions are leaking variables #47201

donmez mannequin opened this issue May 23, 2008 · 4 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@donmez
Copy link
Mannequin

donmez mannequin commented May 23, 2008

BPO 2952
Nosy @loewis

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:

assignee = None
closed_at = <Date 2008-05-23.19:21:32.018>
created_at = <Date 2008-05-23.19:09:24.803>
labels = ['interpreter-core', 'type-bug']
title = 'List comprehensions are leaking variables'
updated_at = <Date 2008-05-23.19:33:47.290>
user = 'https://bugs.python.org/donmez'

bugs.python.org fields:

activity = <Date 2008-05-23.19:33:47.290>
actor = 'loewis'
assignee = 'none'
closed = True
closed_date = <Date 2008-05-23.19:21:32.018>
closer = 'loewis'
components = ['Interpreter Core']
creation = <Date 2008-05-23.19:09:24.803>
creator = 'donmez'
dependencies = []
files = []
hgrepos = []
issue_num = 2952
keywords = []
message_count = 4.0
messages = ['67261', '67262', '67263', '67264']
nosy_count = 2.0
nosy_names = ['loewis', 'donmez']
pr_nums = []
priority = 'normal'
resolution = 'fixed'
stage = None
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue2952'
versions = ['Python 2.5']

@donmez
Copy link
Mannequin Author

donmez mannequin commented May 23, 2008

Originally found at http://www.randombit.net/weblog/programming/variable_leak_in_list_compre
hensions.html

First example :

[~]> python
Python 2.5.1 (r251:54863, Jan 17 2008, 19:35:17) 
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> x=1
>>> [x for x in [1,2,3]]
[1, 2, 3]
>>> x
3

whoops x changed.

Another example from original post:

$ python
Python 2.4.4 (#1, Mar  7 2008, 14:54:19)
[GCC 4.1.2 (Gentoo 4.1.2 p1.0.2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> x = { 1:2, 3:4 }
>>> nothere
Traceback (most recent call last):
  File "", line 1, in ?
NameError: name 'nothere' is not defined
>>> [nothere for nothere in x.keys()]
[1, 3]
>>> nothere
3

This bug doesn't seem to affect py3k but it does python 2.4/2.5. Either
this should be fixed or documented as a caveat.

@donmez donmez mannequin added stdlib Python modules in the Lib dir interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error and removed stdlib Python modules in the Lib dir labels May 23, 2008
@loewis
Copy link
Mannequin

loewis mannequin commented May 23, 2008

The behaviour is documented, e.g. in footnote 5.1 of

http://docs.python.org/ref/lists.html

It is fixed in Python 3.

@loewis loewis mannequin closed this as completed May 23, 2008
@donmez
Copy link
Mannequin Author

donmez mannequin commented May 23, 2008

But it only mentions python 2.3, time to update the footnote.

@loewis
Copy link
Mannequin

loewis mannequin commented May 23, 2008

This is now fixed in r63569

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

0 participants