classification
Title: collections.namedtuple uses exec to create new classes
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.0, Python 2.6
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: audax, eric.araujo, flox, giampaolo.rodola, jcea, kristjan.jonsson, pitrou, rhettinger
Priority: normal Keywords: patch

Created on 2008-09-26 12:01 by audax, last changed 2011-12-24 03:49 by jcea. This issue is now closed.

Files
File name Uploaded Description Edit
new_namedtuples.diff audax, 2008-09-26 12:01 new collections.namedtuples
Messages (4)
msg73844 - (view) Author: Jens Kadenbach (audax) Date: 2008-09-26 12:01
Rewrite of the namedtuple implementation to avoid the use of exec for
class generation.  The new code uses a custom class dictionary and the
builtin type to create new classes dynamically.
msg74123 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2008-10-01 10:39
Have you run benchmarks of both versions? It seems the benefit of the
original version is that it's faster, thanks to hardcoding critical methods.
msg74136 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2008-10-01 16:22
The current version is clear and maintainable.  There is nothing unholy
about using exec.  Earlier versions used other approaches and they
proved unnecessarily complex and had unexpected problems.  It is a key
feature for named tuples that they are exactly equivalent to a
hand-written class.

I appreciate your effort but am rejecting it flat-out.
msg135835 - (view) Author: Kristján Valur Jónsson (kristjan.jonsson) * (Python committer) Date: 2011-05-12 13:53
Yes, exec is unholy:)

For embedding Python into a console game we have removed the python compiler.  exec and eval don't work.  This saves space and is also a security feature.  I had to modify the collections module so that namedtuple() just returns tuple.  Fortunately, no part of the standard library that uses namedtuple actually uses the actual names, everyone just uses it as a tuple.  So that approach works.

Another problem with the Eval approach is code duplication.

A third could arguably be that there is no namedtuple base class.

Now, replacing namedtuples with tuples works in the standardlib _currentlyt_, but I'm happy to have found an alternative implementation, for future safety.  I only wish it were not hidden away as a diff in a feature request.
History
Date User Action Args
2011-12-24 03:49:16jceasetnosy: + jcea
2011-11-11 13:20:16floxsetnosy: + flox
2011-11-11 12:39:17giampaolo.rodolasetnosy: + giampaolo.rodola
2011-06-03 16:07:40eric.araujosetnosy: + eric.araujo
2011-05-12 13:53:23kristjan.jonssonsetnosy: + kristjan.jonsson
messages: + msg135835
2008-10-01 16:22:26rhettingersetstatus: open -> closed
resolution: rejected
messages: + msg74136
2008-10-01 10:39:51pitrousetnosy: + pitrou
messages: + msg74123
2008-09-26 12:06:49christian.heimessetpriority: normal
assignee: rhettinger
type: enhancement
nosy: + rhettinger
versions: + Python 3.0
2008-09-26 12:01:54audaxcreate