Skip to content

issue a warning when populating a CPython type dict with non-string keys #55664

Closed

Description

@voidspace
BPO 11455
Nosy @loewis, @Yhg1s, @ncoghlan, @pitrou, @vstinner, @alex, @voidspace, @briancurtin, @durban, @serhiy-storchaka, @iritkatriel
Files
  • issue11455.patch
  • 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/Yhg1s'
    closed_at = None
    created_at = <Date 2011-03-09.20:53:47.152>
    labels = ['interpreter-core', 'type-feature', '3.11']
    title = 'issue a warning when populating a CPython type dict with non-string keys'
    updated_at = <Date 2021-12-02.09:24:17.831>
    user = 'https://github.com/voidspace'

    bugs.python.org fields:

    activity = <Date 2021-12-02.09:24:17.831>
    actor = 'serhiy.storchaka'
    assignee = 'twouters'
    closed = False
    closed_date = None
    closer = None
    components = ['Interpreter Core']
    creation = <Date 2011-03-09.20:53:47.152>
    creator = 'michael.foord'
    dependencies = []
    files = ['21072']
    hgrepos = []
    issue_num = 11455
    keywords = ['patch']
    message_count = 16.0
    messages = ['130462', '130464', '130465', '130484', '130510', '130512', '130520', '130521', '130522', '130523', '131974', '131975', '132032', '222753', '407472', '407520']
    nosy_count = 12.0
    nosy_names = ['loewis', 'twouters', 'ncoghlan', 'pitrou', 'vstinner', 'stutzbach', 'alex', 'michael.foord', 'brian.curtin', 'daniel.urban', 'serhiy.storchaka', 'iritkatriel']
    pr_nums = []
    priority = 'normal'
    resolution = None
    stage = None
    status = 'open'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue11455'
    versions = ['Python 3.11']

    Linked PRs

    Activity

    1. voidspace commented on Mar 9, 2011

      @voidspace
      ContributorAuthor

      It is valid in CPython to create a new type with non-string keys in the dict. This is a problem for other implementations (neither pypy nor jython support it).

      This should raise a warning.

    2. added
      type-bugAn unexpected behavior, bug, or error
      interpreter-core(Objects, Python, Grammar, and Parser dirs)
      on Mar 9, 2011
    3. alex commented on Mar 9, 2011

      @alex
      Member

      2 ways to do it:

      class A(object):
          locals()[42] = "abc"

      or

      type("A", (object,), {42: "abc"})

    4. voidspace commented on Mar 9, 2011

      @voidspace
      ContributorAuthor

      Note that other implementations not supporting this has been agreed by Guido. The language spec says that the class dict is a namespace and should have string keys.

    5. loewis commented on Mar 10, 2011

      loewismannequin
      Mannequin

      I fail to see the need to warn about this, though. Users using the feature are likely aware that this violates the language specification, and will find out quickly when they do port it to another Python implementation.

      There are many many other aspects of CPython that don't, and often even can't, work on other Python implementations that are not warned about (such as usage of extensions modules, or development of extension modules).

    6. stutzbach commented on Mar 10, 2011

      stutzbachmannequin
      Mannequin

      For what it's worth, I believe this could be implemented easily by calling _PyDict_HasOnlyStringKeys at the end of the class creation process.

    7. loewis commented on Mar 10, 2011

      loewismannequin
      Mannequin

      Can somebody propose a patch?

    8. durban commented on Mar 10, 2011

      durbanmannequin
      Mannequin

      Can somebody propose a patch?

      Yes, here it is. (I'm not sure if the test is in the correct file.)

    9. pitrou commented on Mar 10, 2011

      @pitrou
      Member

      How about the case where non-string keys are added after the class is created?
      (I think I've heard some third-party lib does this, perhaps a generic functions implementation?)

    10. alex commented on Mar 10, 2011

      @alex
      Member

      How can they be set afterwords?

      alex@alex-laptop:~/projects/pypy$ python3.1 
      Python 3.1.2 (release31-maint, Sep 17 2010, 20:34:23) 
      [GCC 4.4.5] on linux2
      Type "help", "copyright", "credits" or "license" for more information.
      >>> class A(object):
      ...     pass
      ... 
      >>> A.__dict__[32] = "heh"
      Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
      TypeError: 'dict_proxy' object does not support item assignment
      >>> setattr(A, 32, "heh")
      Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
      TypeError: attribute name must be string, not 'int'
    11. pitrou commented on Mar 10, 2011

      @pitrou
      Member

      Hmm, that's true (although there's a trick using gc.get_referers(), IIRC).
      I was probably thinking about instance dicts rather than type dicts, then.

    12. ncoghlan commented on Mar 24, 2011

      @ncoghlan
      Contributor

      Thomas, I know you've been working on this post-Pycon. Could you please take a look at Daniel's patch and/or publish your own.

    13. vstinner commented on Mar 24, 2011

      @vstinner
      Member

      Cool, someone uses my PyErr_WarnFormat() function! :-) I didn't know that NULL can be used for the category: I would prefer an explicit PyExc_RuntimeWarning to not have to read the source of PyErr_WarnFormat() or its documentation.

    14. 1 remaining item

    15. BreamoreBoy commented on Jul 11, 2014

      BreamoreBoymannequin
      Mannequin

      The patch is short and sweet. Assuming it is acceptable do we commit or don't we? See also the reference to bpo-11470 in msg132032.

    16. iritkatriel commented on Dec 1, 2021

      @iritkatriel
      Member

      Reproduced on 3.11.

      >>> A = type("A", (object,), {42: "abc"})
      >>> dir(A())
      Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
      TypeError: '<' not supported between instances of 'int' and 'str'
    17. added
      3.11only security fixes
      type-featureA feature request or enhancement
      and removed
      type-bugAn unexpected behavior, bug, or error
      on Dec 1, 2021
    18. serhiy-storchaka commented on Dec 2, 2021

      @serhiy-storchaka
      Member

      Why not raise an error if it contradicts language spec?

    19. transferred this issue fromon Apr 10, 2022
    20. added
      3.12only security fixes
      and removed
      3.11only security fixes
      on Oct 7, 2022
    21. furkanonder commented on Jun 7, 2023

      @furkanonder
      Contributor

      The PR is ready for review.

    22. added
      3.13only security fixes
      and removed
      3.12only security fixes
      on Jan 5, 2024
    23. added a commit that references this issue on Jan 28, 2024
    24. added a commit that references this issue on Feb 11, 2024
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    Labels

    3.13only security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions