This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: posixmodule.c:path_converter() returns an invalid exception message for broken PathLike objects
Type: behavior Stage: resolved
Components: Versions: Python 3.8, Python 3.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: lukasz.langa, miss-islington, pablogsal, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2019-02-08 16:34 by lukasz.langa, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 11831 merged pablogsal, 2019-02-12 22:40
PR 11912 merged miss-islington, 2019-02-18 10:46
PR 11934 merged serhiy.storchaka, 2019-02-19 11:23
PR 11932 merged serhiy.storchaka, 2019-02-19 11:50
PR 11937 merged miss-islington, 2019-02-19 11:55
Messages (6)
msg335094 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2019-02-08 16:34
>>> 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
msg335756 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-02-17 06:39
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.
msg335804 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2019-02-18 10:46
New changeset 09fbcd6085e18b534fd4161844ff39f77eb4a082 by Pablo Galindo in branch 'master':
bpo-35942: Improve the error message if __fspath__ returns invalid types in path_converter (GH-11831)
https://github.com/python/cpython/commit/09fbcd6085e18b534fd4161844ff39f77eb4a082
msg335809 - (view) Author: miss-islington (miss-islington) Date: 2019-02-18 11:05
New changeset a01065a3588d3f0d0c57ea35107aa97e722fe2b2 by Miss Islington (bot) in branch '3.7':
bpo-35942: Improve the error message if __fspath__ returns invalid types in path_converter (GH-11831)
https://github.com/python/cpython/commit/a01065a3588d3f0d0c57ea35107aa97e722fe2b2
msg335935 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-02-19 11:52
New changeset 8d01eb49fc7ff914feeb2e2090d1d6ef15c932ab by Serhiy Storchaka in branch 'master':
Fix syntax warnings in tests introduced in bpo-35942. (GH-11934)
https://github.com/python/cpython/commit/8d01eb49fc7ff914feeb2e2090d1d6ef15c932ab
msg335939 - (view) Author: miss-islington (miss-islington) Date: 2019-02-19 12:16
New changeset a8834905df853510f38d4e2d628bed8229fd7482 by Miss Islington (bot) in branch '3.7':
Fix syntax warnings in tests introduced in bpo-35942. (GH-11934)
https://github.com/python/cpython/commit/a8834905df853510f38d4e2d628bed8229fd7482
History
Date User Action Args
2022-04-11 14:59:11adminsetgithub: 80123
2019-02-19 12:16:41miss-islingtonsetmessages: + msg335939
2019-02-19 11:55:22miss-islingtonsetpull_requests: + pull_request11962
2019-02-19 11:52:37serhiy.storchakasetmessages: + msg335935
2019-02-19 11:50:38serhiy.storchakasetpull_requests: + pull_request11961
2019-02-19 11:24:56serhiy.storchakasetpull_requests: - pull_request11956
2019-02-19 11:23:24serhiy.storchakasetpull_requests: + pull_request11958
2019-02-19 11:08:26serhiy.storchakasetpull_requests: + pull_request11956
2019-02-18 11:38:39pablogsalsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2019-02-18 11:05:55miss-islingtonsetnosy: + miss-islington
messages: + msg335809
2019-02-18 10:46:48miss-islingtonsetpull_requests: + pull_request11938
2019-02-18 10:46:48pablogsalsetnosy: + pablogsal
messages: + msg335804
2019-02-17 06:39:18serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg335756
2019-02-12 22:40:57pablogsalsetkeywords: + patch
stage: needs patch -> patch review
pull_requests: + pull_request11862
2019-02-08 16:34:39lukasz.langacreate