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

posixmodule.c:path_converter() returns an invalid exception message for broken PathLike objects #80123

Closed
ambv opened this issue Feb 8, 2019 · 6 comments
Labels
3.7 (EOL) end of life 3.8 only security fixes type-bug An unexpected behavior, bug, or error

Comments

@ambv
Copy link
Contributor

ambv commented Feb 8, 2019

BPO 35942
Nosy @ambv, @serhiy-storchaka, @pablogsal, @miss-islington
PRs
  • bpo-35942: Improve the error message if __fspath__ returns invalid types in path_converter #11831
  • [3.7] bpo-35942: Improve the error message if __fspath__ returns invalid types in path_converter (GH-11831) #11912
  • Fix syntax warnings in tests introduced in bpo-35942. #11934
  • Fix syntax warnings in tests introduced in bpo-15248. #11932
  • [3.7] Fix syntax warnings in tests introduced in bpo-35942. (GH-11934) #11937
  • 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 2019-02-18.11:38:39.756>
    created_at = <Date 2019-02-08.16:34:39.069>
    labels = ['3.8', 'type-bug', '3.7']
    title = 'posixmodule.c:path_converter() returns an invalid exception message for broken PathLike objects'
    updated_at = <Date 2019-02-19.12:16:41.732>
    user = 'https://github.com/ambv'

    bugs.python.org fields:

    activity = <Date 2019-02-19.12:16:41.732>
    actor = 'miss-islington'
    assignee = 'none'
    closed = True
    closed_date = <Date 2019-02-18.11:38:39.756>
    closer = 'pablogsal'
    components = []
    creation = <Date 2019-02-08.16:34:39.069>
    creator = 'lukasz.langa'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 35942
    keywords = ['patch']
    message_count = 6.0
    messages = ['335094', '335756', '335804', '335809', '335935', '335939']
    nosy_count = 4.0
    nosy_names = ['lukasz.langa', 'serhiy.storchaka', 'pablogsal', 'miss-islington']
    pr_nums = ['11831', '11912', '11934', '11932', '11937']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue35942'
    versions = ['Python 3.7', 'Python 3.8']

    @ambv
    Copy link
    Contributor Author

    ambv commented Feb 8, 2019

    >>> class K:
    ...   def __fspath__(self):
    ...     return 1
    ...
    >>> import os
    >>> os.stat(K())
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: stat: path should be string, bytes, os.PathLike or integer, not int

    This error message is internally inconsistent:

    • it suggests that the error is about the path argument whereas it's in fact about the value returned from __fspath__()
    • it hilariously states "should be integer, not int"
    • it claims os.PathLike is fine as a return value from __fspath__() whereas it's not

    I would advise removing the custom __fspath__() handling from path_converter and just directly using PyOS_FSPath which returns a valid error in this case (example from pypy3):

    >>>> class K:
    ....   def __fspath__(self):
    ....     return 1
    ....
    >>>> import os
    >>>> os.open(K(), os.O_RDONLY)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: expected K.__fspath__() to return str or bytes, not int

    @ambv ambv added 3.7 (EOL) end of life 3.8 only security fixes type-bug An unexpected behavior, bug, or error labels Feb 8, 2019
    @serhiy-storchaka
    Copy link
    Member

    The custom __fspath__() handling is used in path_converter for better error reporting. The error message can include names of the function and the argument. PyOS_FSPath() can not provides this information.

    I agree that error reporting about incorrect type of the __fspath__() result should be improved.

    @pablogsal
    Copy link
    Member

    New changeset 09fbcd6 by Pablo Galindo in branch 'master':
    bpo-35942: Improve the error message if __fspath__ returns invalid types in path_converter (GH-11831)
    09fbcd6

    @miss-islington
    Copy link
    Contributor

    New changeset a01065a by Miss Islington (bot) in branch '3.7':
    bpo-35942: Improve the error message if __fspath__ returns invalid types in path_converter (GH-11831)
    a01065a

    @serhiy-storchaka
    Copy link
    Member

    New changeset 8d01eb4 by Serhiy Storchaka in branch 'master':
    Fix syntax warnings in tests introduced in bpo-35942. (GH-11934)
    8d01eb4

    @miss-islington
    Copy link
    Contributor

    New changeset a883490 by Miss Islington (bot) in branch '3.7':
    Fix syntax warnings in tests introduced in bpo-35942. (GH-11934)
    a883490

    @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 3.8 only security fixes type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    4 participants