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

collections.namedtuple uses exec to create new classes #48224

Closed
audax mannequin opened this issue Sep 26, 2008 · 4 comments
Closed

collections.namedtuple uses exec to create new classes #48224

audax mannequin opened this issue Sep 26, 2008 · 4 comments
Assignees
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@audax
Copy link
Mannequin

audax mannequin commented Sep 26, 2008

BPO 3974
Nosy @rhettinger, @jcea, @pitrou, @kristjanvalur, @giampaolo, @merwok, @florentx
Files
  • new_namedtuples.diff: new collections.namedtuples
  • 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 = 'https://github.com/rhettinger'
    closed_at = <Date 2008-10-01.16:22:26.243>
    created_at = <Date 2008-09-26.12:01:53.973>
    labels = ['type-feature', 'library']
    title = 'collections.namedtuple uses exec to create new classes'
    updated_at = <Date 2011-12-24.03:49:16.631>
    user = 'https://bugs.python.org/audax'

    bugs.python.org fields:

    activity = <Date 2011-12-24.03:49:16.631>
    actor = 'jcea'
    assignee = 'rhettinger'
    closed = True
    closed_date = <Date 2008-10-01.16:22:26.243>
    closer = 'rhettinger'
    components = ['Library (Lib)']
    creation = <Date 2008-09-26.12:01:53.973>
    creator = 'audax'
    dependencies = []
    files = ['11608']
    hgrepos = []
    issue_num = 3974
    keywords = ['patch']
    message_count = 4.0
    messages = ['73844', '74123', '74136', '135835']
    nosy_count = 8.0
    nosy_names = ['rhettinger', 'jcea', 'pitrou', 'kristjan.jonsson', 'giampaolo.rodola', 'eric.araujo', 'audax', 'flox']
    pr_nums = []
    priority = 'normal'
    resolution = 'rejected'
    stage = None
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue3974'
    versions = ['Python 2.6', 'Python 3.0']

    @audax
    Copy link
    Mannequin Author

    audax mannequin commented Sep 26, 2008

    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.

    @audax audax mannequin added the stdlib Python modules in the Lib dir label Sep 26, 2008
    @tiran tiran added the type-feature A feature request or enhancement label Sep 26, 2008
    @pitrou
    Copy link
    Member

    pitrou commented Oct 1, 2008

    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.

    @rhettinger
    Copy link
    Contributor

    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.

    @kristjanvalur
    Copy link
    Mannequin

    kristjanvalur mannequin commented May 12, 2011

    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.

    @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
    stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants