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

inspect.signature() doesn't parse __text_signature__ containing a newline character #85267

Closed
vstinner opened this issue Jun 23, 2020 · 6 comments
Labels
3.10 stdlib Python modules in the Lib dir

Comments

@vstinner
Copy link
Member

vstinner commented Jun 23, 2020

BPO 41095
Nosy @vstinner, @serhiy-storchaka, @1st1, @asottile
PRs
  • bpo-31938: Fix default-value signatures of several functions in the select module #21066
  • 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 = None
    created_at = <Date 2020-06-23.22:26:29.252>
    labels = ['library', '3.10']
    title = "inspect.signature() doesn't parse __text_signature__ containing a newline character"
    updated_at = <Date 2020-06-24.01:12:14.804>
    user = 'https://github.com/vstinner'

    bugs.python.org fields:

    activity = <Date 2020-06-24.01:12:14.804>
    actor = 'Anthony Sottile'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2020-06-23.22:26:29.252>
    creator = 'vstinner'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 41095
    keywords = ['patch']
    message_count = 2.0
    messages = ['372213', '372222']
    nosy_count = 4.0
    nosy_names = ['vstinner', 'serhiy.storchaka', 'yselivanov', 'Anthony Sottile']
    pr_nums = ['21066']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue41095'
    versions = ['Python 3.10']

    @vstinner
    Copy link
    Member Author

    vstinner commented Jun 23, 2020

    $ ./python
    Python 3.10.0a0 (heads/unicode_latin1:40855c7064, Jun 24 2020, 00:20:07) 
    >>> import select
    >>> select.epoll.register.__text_signature__
    '($self, /, fd,\n         eventmask=select.EPOLLIN | select.EPOLLPRI | select.EPOLLOUT)'
    
    >>> import inspect
    >>> inspect.signature(select.epoll.register)
    <Signature (self, /, fd)>

    => eventmask parameter is gone!

    Either signature() must raise an exception, or it must handle a __text_signature__ containing a newline character.

    Issue spotted on bpo-31938 when fixing "./python -m pydoc select".

    By the way, as expected, pydoc shows:

    Help on method_descriptor in select.epoll:
    ---

    $ ./python -m pydoc select.epoll.register
    select.epoll.register = register(self, /, fd)
        Registers a new fd or raises an OSError if the fd is already registered.
    (...)

    @vstinner vstinner added 3.10 stdlib Python modules in the Lib dir labels Jun 23, 2020
    @asottile
    Copy link
    Mannequin

    asottile mannequin commented Jun 24, 2020

    Looking into this, it appears to be due to the default value and not due to the newline

    I've stumbled upon two simplifications to the routines in inspect but not a fix for this

    1. bpo-20684: Remove unused inspect._signature_get_bound_param #21100
    2. gh-99815: remove unused 'invalid' sentinel value and code that checks for it in inspect.signature parsing #21104

    the following code is hit because ast.literal_eval(...) fails for the |d expression:

    cpython/Lib/inspect.py

    Lines 2069 to 2070 in 2f9ada9

    if o is invalid:
    return None

    this causes the parameter to be skipped entirely

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    hauntsaninja added a commit to hauntsaninja/cpython that referenced this issue Oct 28, 2022
    …handling
    
    This makes a couple related changes to inspect.signature's behaviour
    when parsing a signature from `__text_signature__`.
    
    First, `inspect.signature` is documented as only raising ValueError or
    TypeError. However, in some cases, we could raise RuntimeError.  This PR
    changes that, thereby fixing python#83685.
    
    (Note that the new ValueErrors in RewriteSymbolics are caught and then
    reraised with a message)
    
    Second, `inspect.signature` could randomly drop parameters that it
    didn't understand (corresponding to `return None` in the `p` function).
    This is the core issue in python#85267. I think this is very surprising
    behaviour and it seems better to fail outright.
    
    Third, adding this new failure broke a couple tests. To fix them (and to
    e.g. allow `inspect.signature(select.epoll.register)` as in python#85267), I
    add constant folding of a couple binary operations to RewriteSymbolics.
    
    (There's some discussion of making signature expression evaluation
    arbitrary powerful in python#68155. I think that's out of scope. The
    additional constant folding here is pretty straightforward, useful, and
    not much of a slippery slope)
    
    Fourth, while python#85267 is incorrect about the cause of the issue, it turns
    out if you had consecutive newlines in __text_signature__, you'd get
    `tokenize.TokenError`.
    
    Finally, the `if name is invalid:` code path was dead, since
    `parse_name` never returned `invalid`.
    @vstinner
    Copy link
    Member Author

    vstinner commented Nov 3, 2022

    Sadly, I don't have the bandwidth to work on this issue, so I just close it.

    @vstinner vstinner closed this as completed Nov 3, 2022
    @JelleZijlstra
    Copy link
    Member

    JelleZijlstra commented Nov 8, 2022

    Reopening as there's an active PR fixing this.

    @JelleZijlstra JelleZijlstra reopened this Nov 8, 2022
    @vstinner
    Copy link
    Member Author

    vstinner commented Nov 8, 2022

    What is the active PR?

    @JelleZijlstra
    Copy link
    Member

    JelleZijlstra commented Nov 8, 2022

    #98796

    JelleZijlstra pushed a commit that referenced this issue Dec 21, 2022
    …ng (#98796)
    
    This makes a couple related changes to inspect.signature's behaviour
    when parsing a signature from `__text_signature__`.
    
    First, `inspect.signature` is documented as only raising ValueError or
    TypeError. However, in some cases, we could raise RuntimeError.  This PR
    changes that, thereby fixing #83685.
    
    (Note that the new ValueErrors in RewriteSymbolics are caught and then
    reraised with a message)
    
    Second, `inspect.signature` could randomly drop parameters that it
    didn't understand (corresponding to `return None` in the `p` function).
    This is the core issue in #85267. I think this is very surprising
    behaviour and it seems better to fail outright.
    
    Third, adding this new failure broke a couple tests. To fix them (and to
    e.g. allow `inspect.signature(select.epoll.register)` as in #85267), I
    add constant folding of a couple binary operations to RewriteSymbolics.
    
    (There's some discussion of making signature expression evaluation
    arbitrary powerful in #68155. I think that's out of scope. The
    additional constant folding here is pretty straightforward, useful, and
    not much of a slippery slope)
    
    Fourth, while #85267 is incorrect about the cause of the issue, it turns
    out if you had consecutive newlines in __text_signature__, you'd get
    `tokenize.TokenError`.
    
    Finally, the `if name is invalid:` code path was dead, since
    `parse_name` never returned `invalid`.
    hauntsaninja added a commit to hauntsaninja/cpython that referenced this issue Dec 21, 2022
    …ture__ handling (pythonGH-98796)
    
    This makes a couple related changes to inspect.signature's behaviour
    when parsing a signature from `__text_signature__`.
    
    First, `inspect.signature` is documented as only raising ValueError or
    TypeError. However, in some cases, we could raise RuntimeError.  This PR
    changes that, thereby fixing pythonGH-83685.
    
    (Note that the new ValueErrors in RewriteSymbolics are caught and then
    reraised with a message)
    
    Second, `inspect.signature` could randomly drop parameters that it
    didn't understand (corresponding to `return None` in the `p` function).
    This is the core issue in pythonGH-85267. I think this is very surprising
    behaviour and it seems better to fail outright.
    
    Third, adding this new failure broke a couple tests. To fix them (and to
    e.g. allow `inspect.signature(select.epoll.register)` as in pythonGH-85267), I
    add constant folding of a couple binary operations to RewriteSymbolics.
    
    (There's some discussion of making signature expression evaluation
    arbitrary powerful in pythonGH-68155. I think that's out of scope. The
    additional constant folding here is pretty straightforward, useful, and
    not much of a slippery slope)
    
    Fourth, while pythonGH-85267 is incorrect about the cause of the issue, it turns
    out if you had consecutive newlines in __text_signature__, you'd get
    `tokenize.TokenError`.
    
    Finally, the `if name is invalid:` code path was dead, since
    `parse_name` never returned `invalid`..
    (cherry picked from commit 79311cb)
    
    Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
    hauntsaninja added a commit to hauntsaninja/cpython that referenced this issue Dec 21, 2022
    …ture__ handling (pythonGH-98796)
    
    This makes a couple related changes to inspect.signature's behaviour
    when parsing a signature from `__text_signature__`.
    
    First, `inspect.signature` is documented as only raising ValueError or
    TypeError. However, in some cases, we could raise RuntimeError.  This PR
    changes that, thereby fixing pythonGH-83685.
    
    (Note that the new ValueErrors in RewriteSymbolics are caught and then
    reraised with a message)
    
    Second, `inspect.signature` could randomly drop parameters that it
    didn't understand (corresponding to `return None` in the `p` function).
    This is the core issue in pythonGH-85267. I think this is very surprising
    behaviour and it seems better to fail outright.
    
    Third, adding this new failure broke a couple tests. To fix them (and to
    e.g. allow `inspect.signature(select.epoll.register)` as in pythonGH-85267), I
    add constant folding of a couple binary operations to RewriteSymbolics.
    
    (There's some discussion of making signature expression evaluation
    arbitrary powerful in pythonGH-68155. I think that's out of scope. The
    additional constant folding here is pretty straightforward, useful, and
    not much of a slippery slope)
    
    Fourth, while pythonGH-85267 is incorrect about the cause of the issue, it turns
    out if you had consecutive newlines in __text_signature__, you'd get
    `tokenize.TokenError`.
    
    Finally, the `if name is invalid:` code path was dead, since
    `parse_name` never returned `invalid`..
    (cherry picked from commit 79311cb)
    
    Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
    JelleZijlstra pushed a commit that referenced this issue Dec 21, 2022
    … handling (GH-98796) (#100392)
    
    This makes a couple related changes to inspect.signature's behaviour
    when parsing a signature from `__text_signature__`.
    
    First, `inspect.signature` is documented as only raising ValueError or
    TypeError. However, in some cases, we could raise RuntimeError.  This PR
    changes that, thereby fixing GH-83685.
    
    (Note that the new ValueErrors in RewriteSymbolics are caught and then
    reraised with a message)
    
    Second, `inspect.signature` could randomly drop parameters that it
    didn't understand (corresponding to `return None` in the `p` function).
    This is the core issue in GH-85267. I think this is very surprising
    behaviour and it seems better to fail outright.
    
    Third, adding this new failure broke a couple tests. To fix them (and to
    e.g. allow `inspect.signature(select.epoll.register)` as in GH-85267), I
    add constant folding of a couple binary operations to RewriteSymbolics.
    
    (There's some discussion of making signature expression evaluation
    arbitrary powerful in GH-68155. I think that's out of scope. The
    additional constant folding here is pretty straightforward, useful, and
    not much of a slippery slope)
    
    Fourth, while GH-85267 is incorrect about the cause of the issue, it turns
    out if you had consecutive newlines in __text_signature__, you'd get
    `tokenize.TokenError`.
    
    Finally, the `if name is invalid:` code path was dead, since
    `parse_name` never returned `invalid`..
    (cherry picked from commit 79311cb)
    
    Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
    JelleZijlstra pushed a commit that referenced this issue Dec 21, 2022
    … handling (GH-98796) (#100393)
    
    This makes a couple related changes to inspect.signature's behaviour
    when parsing a signature from `__text_signature__`.
    
    First, `inspect.signature` is documented as only raising ValueError or
    TypeError. However, in some cases, we could raise RuntimeError.  This PR
    changes that, thereby fixing GH-83685.
    
    (Note that the new ValueErrors in RewriteSymbolics are caught and then
    reraised with a message)
    
    Second, `inspect.signature` could randomly drop parameters that it
    didn't understand (corresponding to `return None` in the `p` function).
    This is the core issue in GH-85267. I think this is very surprising
    behaviour and it seems better to fail outright.
    
    Third, adding this new failure broke a couple tests. To fix them (and to
    e.g. allow `inspect.signature(select.epoll.register)` as in GH-85267), I
    add constant folding of a couple binary operations to RewriteSymbolics.
    
    (There's some discussion of making signature expression evaluation
    arbitrary powerful in GH-68155. I think that's out of scope. The
    additional constant folding here is pretty straightforward, useful, and
    not much of a slippery slope)
    
    Fourth, while GH-85267 is incorrect about the cause of the issue, it turns
    out if you had consecutive newlines in __text_signature__, you'd get
    `tokenize.TokenError`.
    
    Finally, the `if name is invalid:` code path was dead, since
    `parse_name` never returned `invalid`..
    (cherry picked from commit 79311cb)
    
    Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
    @hauntsaninja hauntsaninja reopened this Dec 21, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.10 stdlib Python modules in the Lib dir
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants