gh-106046: Improve error message from os.fspath if __fspath__ is set to None (#106082)

This commit is contained in:
Alex Waygood 2023-06-26 00:06:12 +01:00 committed by GitHub
parent 8c24a83737
commit 93a970ffbc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 52 additions and 4 deletions

View file

@ -4647,6 +4647,45 @@ class TestPEP519(unittest.TestCase):
return ''
self.assertFalse(hasattr(A(), '__dict__'))
def test_fspath_set_to_None(self):
class Foo:
__fspath__ = None
class Bar:
def __fspath__(self):
return 'bar'
class Baz(Bar):
__fspath__ = None
good_error_msg = (
r"expected str, bytes or os.PathLike object, not {}".format
)
with self.assertRaisesRegex(TypeError, good_error_msg("Foo")):
self.fspath(Foo())
self.assertEqual(self.fspath(Bar()), 'bar')
with self.assertRaisesRegex(TypeError, good_error_msg("Baz")):
self.fspath(Baz())
with self.assertRaisesRegex(TypeError, good_error_msg("Foo")):
open(Foo())
with self.assertRaisesRegex(TypeError, good_error_msg("Baz")):
open(Baz())
other_good_error_msg = (
r"should be string, bytes or os.PathLike, not {}".format
)
with self.assertRaisesRegex(TypeError, other_good_error_msg("Foo")):
os.rename(Foo(), "foooo")
with self.assertRaisesRegex(TypeError, other_good_error_msg("Baz")):
os.rename(Baz(), "bazzz")
class TimesTests(unittest.TestCase):
def test_times(self):
times = os.times()