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

SystemError in class creation in case of a metaclass with a bad __prepare__() method #75769

Closed
orenmn mannequin opened this issue Sep 26, 2017 · 8 comments
Closed

SystemError in class creation in case of a metaclass with a bad __prepare__() method #75769

orenmn mannequin opened this issue Sep 26, 2017 · 8 comments
Labels
3.7 (EOL) end of life interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@orenmn
Copy link
Mannequin

orenmn mannequin commented Sep 26, 2017

BPO 31588
Nosy @ncoghlan, @ericsnowcurrently, @serhiy-storchaka, @orenmn
PRs
  • bpo-31588: Prevent raising a SystemError in class creation in case of a metaclass with a bad __prepare__() method #3764
  • [3.6] bpo-31588: Validate return value of __prepare__() methods (GH-3764) #3790
  • 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 2017-09-27.16:28:47.445>
    created_at = <Date 2017-09-26.07:38:12.271>
    labels = ['interpreter-core', 'type-bug', '3.7']
    title = 'SystemError in class creation in case of a metaclass with a bad __prepare__() method'
    updated_at = <Date 2017-09-27.16:28:47.445>
    user = 'https://github.com/orenmn'

    bugs.python.org fields:

    activity = <Date 2017-09-27.16:28:47.445>
    actor = 'ncoghlan'
    assignee = 'none'
    closed = True
    closed_date = <Date 2017-09-27.16:28:47.445>
    closer = 'ncoghlan'
    components = ['Interpreter Core']
    creation = <Date 2017-09-26.07:38:12.271>
    creator = 'Oren Milman'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 31588
    keywords = ['patch']
    message_count = 8.0
    messages = ['303019', '303118', '303121', '303122', '303143', '303144', '303146', '303159']
    nosy_count = 4.0
    nosy_names = ['ncoghlan', 'eric.snow', 'serhiy.storchaka', 'Oren Milman']
    pr_nums = ['3764', '3790']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue31588'
    versions = ['Python 3.6', 'Python 3.7']

    @orenmn
    Copy link
    Mannequin Author

    orenmn mannequin commented Sep 26, 2017

    The following code causes a SystemError:
    class BadMetaclass(type):
    def __prepare__(*args):
    pass

    class Foo(metaclass=BadMetaclass):
        pass

    This is because builtin___build_class__() assumes that __prepare__() returned a
    mapping, and passes it to PyEval_EvalCodeEx(), which passes it to
    _PyEval_EvalCodeWithName(), which passes it to _PyFrame_New_NoTrack(), which
    raises the SystemError.

    This issue seems related to bpo-17421.

    @orenmn orenmn mannequin added 3.7 (EOL) end of life interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Sep 26, 2017
    @serhiy-storchaka
    Copy link
    Member

    I'm not a metaclass expert. Eric, Nick, could you please take a look at the patch?

    @ncoghlan
    Copy link
    Contributor

    There's no __prepare__ method in 2.7, so that version can't be affected by this.

    For 3.6 and 3.7, I can't reproduce the reported SystemError. Instead, I get:

    >>> class BadMetaclass(type):
    ...     def __prepare__(*args):
    ...         pass
    ... 
    >>> class Foo(metaclass=BadMetaclass):
    ...     pass
    ... 
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<stdin>", line 1, in Foo
    TypeError: 'NoneType' object is not subscriptable
    
    

    The error remains the same if I add @classmethod to the prepare definition.

    That's still thoroughly cryptic and worth changing, but indicates the test should be checking the error message details, not just the error type.

    @orenmn
    Copy link
    Mannequin Author

    orenmn mannequin commented Sep 27, 2017

    Nick, maybe you tried to reproduce in release?
    In debug (where I got the SystemError), you have in the beginning of _PyFrame_New_NoTrack():
    #ifdef Py_DEBUG
        if (code == NULL || globals == NULL || !PyDict_Check(globals) ||
            (locals != NULL && !PyMapping_Check(locals))) {
            PyErr_BadInternalCall();
            return NULL;
        }

    @ncoghlan
    Copy link
    Contributor

    Aye, that's exactly what I did.

    @ncoghlan
    Copy link
    Contributor

    New changeset 5837d04 by Nick Coghlan (Oren Milman) in branch 'master':
    bpo-31588: Validate return value of __prepare__() methods (GH-3764)
    5837d04

    @ncoghlan
    Copy link
    Contributor

    CI is still running for the backport, so I'll merge that tomorrow.

    Thanks for the patch!

    @ncoghlan
    Copy link
    Contributor

    New changeset 084f80b by Nick Coghlan (Miss Islington (bot)) in branch '3.6':
    [3.6] bpo-31588: Validate return value of __prepare__() methods (GH-3790)
    084f80b

    @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
    3.7 (EOL) end of life 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

    2 participants