mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
gh-106046: Improve error message from os.fspath
if __fspath__
is set to None
(#106082)
This commit is contained in:
parent
8c24a83737
commit
93a970ffbc
5 changed files with 52 additions and 4 deletions
|
@ -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()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue