Skip to content

PyType_FromSpecWithBases tp_new bugfix #59932

Closed

Description

@RobinSchreiber
BPO 15727
Nosy @loewis, @jcea, @ncoghlan, @abalkin, @pitrou, @encukou, @serhiy-storchaka, @Dormouse759
Superseder
  • bpo-26979: The danger of PyType_FromSpec()
  • Files
  • PyType_FromSpecWithBases_tp_new_fix.patch
  • PyType_FromSpecWithBases_tp_new_fix_v1.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 = None
    closed_at = <Date 2021-10-18.22:57:46.804>
    created_at = <Date 2012-08-19.15:57:45.398>
    labels = ['interpreter-core', '3.7', 'type-crash']
    title = 'PyType_FromSpecWithBases tp_new bugfix'
    updated_at = <Date 2021-10-18.22:57:46.804>
    user = 'https://bugs.python.org/RobinSchreiber'

    bugs.python.org fields:

    activity = <Date 2021-10-18.22:57:46.804>
    actor = 'iritkatriel'
    assignee = 'none'
    closed = True
    closed_date = <Date 2021-10-18.22:57:46.804>
    closer = 'iritkatriel'
    components = ['Interpreter Core']
    creation = <Date 2012-08-19.15:57:45.398>
    creator = 'Robin.Schreiber'
    dependencies = []
    files = ['26897', '28167']
    hgrepos = []
    issue_num = 15727
    keywords = ['patch', 'pep3121']
    message_count = 6.0
    messages = ['168579', '170157', '176695', '176805', '277880', '325059']
    nosy_count = 9.0
    nosy_names = ['loewis', 'jcea', 'ncoghlan', 'belopolsky', 'pitrou', 'petr.viktorin', 'serhiy.storchaka', 'Robin.Schreiber', 'Dormouse759']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = 'resolved'
    status = 'closed'
    superseder = '26979'
    type = 'crash'
    url = 'https://bugs.python.org/issue15727'
    versions = ['Python 3.5', 'Python 3.6', 'Python 3.7']

    Activity

    1. RobinSchreiber commented on Aug 19, 2012

      RobinSchreibermannequin
      MannequinAuthor

      As with every type, that has been created and initialized, HeapTypes created form PyType_FromSpecWithBases() have to pass through PyType_Ready(). Here the function inherit_special might be called, which, among other things, does the following:
      ....
      3892 if (base != &PyBaseObject_Type ||
      3893 (type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
      3894 if (type->tp_new == NULL)
      3895 type->tp_new = base->tp_new;
      3896 }
      ....
      The code does not know of Heaptypes that have been created from extension-types by the PEP-384 refactorings. This includes extension-types that do not specify a tp_new method but instead have seperate factory methods, that are only used within the extension module. inherit_special() might consequently assign inappropriate new-methods to these type objects.

      To circumvent this issue, I propose to enhance PyType_FromSpecWithBases in the following way: If no tp_new has been specified, we assign the newly defined PySpec_New() method to tp_new which simply denies the user to create an instance of this type. This also prohibits inherit_special to falsely inherit inappropriate new-methods.

    2. added
      type-bug
      interpreter-core
      on Aug 19, 2012
    3. abalkin commented on Sep 10, 2012

      @abalkin
      Member

      Can you give an example of the situation that you described? Perhaps you encountered it while refactoring some particular extension module. Which?

      In your patch new code is commented out.

      PySpec_New() is not a good name. It should be something like PyObject_FailingNew().

    4. RobinSchreiber commented on Nov 30, 2012

      RobinSchreibermannequin
      MannequinAuthor

      Here is revised version of the patch.

      Martin von Löwis and I had discovered a way to reproduce problems with refactored modules, that
      occur without this patch. This is was several months ago, however I will try to give you a code sample! :-)

    5. pitrou commented on Dec 2, 2012

      @pitrou
      Member

      Robin, do you remember which extension types were affected by this issue?

    6. serhiy-storchaka commented on Oct 2, 2016

      @serhiy-storchaka
      Member

      Affected modules were tkinter and curses (see bpo-23815). This was fixed by explicit nullifying tp_new after calling PyType_FromSpec(). bpo-26979 was open for documenting this danger effect.

    7. added
      3.7 (EOL)
      type-crash
      and removed
      type-bug
      on Oct 2, 2016
    8. encukou commented on Sep 11, 2018

      @encukou
      Member

      Converting static types to heap ones is just one of the reasons to use PyType_FromSpec*. Another ones are writing such classes from scratch, and converting pure-Python classes to C. I don't think PyObject_FailingNew is a good default for either of those.

      I think Sehriy's solution in bpo-26979 is better: document this, and allow setting a slot to NULL explicitly.

    9. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.7 (EOL)interpreter-coretype-crash

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions