[New-bugs-announce] [issue35942] posixmodule.c:path_converter() returns an invalid exception message for broken PathLike objects

Łukasz Langa report at bugs.python.org
Fri Feb 8 11:34:39 EST 2019


New submission from Łukasz Langa <lukasz at langa.pl>:

>>> 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

----------
messages: 335094
nosy: lukasz.langa
priority: normal
severity: normal
stage: needs patch
status: open
title: posixmodule.c:path_converter() returns an invalid exception message for broken PathLike objects
type: behavior
versions: Python 3.7, Python 3.8

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue35942>
_______________________________________


More information about the New-bugs-announce mailing list