[3.6] bpo-32964: Reuse a testing implementation of the path protocol … · python/cpython@fbdd075 (original) (raw)

`@@ -64,7 +64,7 @@

`

64

64

`INT_MAX = PY_SSIZE_T_MAX = sys.maxsize

`

65

65

``

66

66

`from test.support.script_helper import assert_python_ok

`

67

``

`-

from test.support import unix_shell

`

``

67

`+

from test.support import unix_shell, FakePath

`

68

68

``

69

69

``

70

70

`root_in_posix = False

`

`@@ -94,21 +94,6 @@ def requires_os_func(name):

`

94

94

`return unittest.skipUnless(hasattr(os, name), 'requires os.%s' % name)

`

95

95

``

96

96

``

97

``

`-

class _PathLike(os.PathLike):

`

98

``

-

99

``

`-

def init(self, path=""):

`

100

``

`-

self.path = path

`

101

``

-

102

``

`-

def str(self):

`

103

``

`-

return str(self.path)

`

104

``

-

105

``

`-

def fspath(self):

`

106

``

`-

if isinstance(self.path, BaseException):

`

107

``

`-

raise self.path

`

108

``

`-

else:

`

109

``

`-

return self.path

`

110

``

-

111

``

-

112

97

`def create_file(filename, content=b'content'):

`

113

98

`with open(filename, "xb", 0) as fp:

`

114

99

`fp.write(content)

`

`@@ -970,15 +955,14 @@ def test_walk_prune(self, walk_path=None):

`

970

955

`dirs.remove('SUB1')

`

971

956

``

972

957

`self.assertEqual(len(all), 2)

`

973

``

`-

self.assertEqual(all[0],

`

974

``

`-

(str(walk_path), ["SUB2"], ["tmp1"]))

`

``

958

`+

self.assertEqual(all[0], (self.walk_path, ["SUB2"], ["tmp1"]))

`

975

959

``

976

960

`all[1][-1].sort()

`

977

961

`all[1][1].sort()

`

978

962

`self.assertEqual(all[1], self.sub2_tree)

`

979

963

``

980

964

`def test_file_like_path(self):

`

981

``

`-

self.test_walk_prune(_PathLike(self.walk_path))

`

``

965

`+

self.test_walk_prune(FakePath(self.walk_path))

`

982

966

``

983

967

`def test_walk_bottom_up(self):

`

984

968

`# Walk bottom-up.

`

`@@ -2294,7 +2278,7 @@ def test_getppid(self):

`

2294

2278

`def test_waitpid(self):

`

2295

2279

`args = [sys.executable, '-c', 'pass']

`

2296

2280

`# Add an implicit test for PyUnicode_FSConverter().

`

2297

``

`-

pid = os.spawnv(os.P_NOWAIT, _PathLike(args[0]), args)

`

``

2281

`+

pid = os.spawnv(os.P_NOWAIT, FakePath(args[0]), args)

`

2298

2282

`status = os.waitpid(pid, 0)

`

2299

2283

`self.assertEqual(status, (pid, 0))

`

2300

2284

``

`@@ -3140,13 +3124,13 @@ def test_path_t_converter(self):

`

3140

3124

`bytes_fspath = bytes_filename = None

`

3141

3125

`else:

`

3142

3126

`bytes_filename = support.TESTFN.encode('ascii')

`

3143

``

`-

bytes_fspath = _PathLike(bytes_filename)

`

3144

``

`-

fd = os.open(_PathLike(str_filename), os.O_WRONLY|os.O_CREAT)

`

``

3127

`+

bytes_fspath = FakePath(bytes_filename)

`

``

3128

`+

fd = os.open(FakePath(str_filename), os.O_WRONLY|os.O_CREAT)

`

3145

3129

`self.addCleanup(support.unlink, support.TESTFN)

`

3146

3130

`self.addCleanup(os.close, fd)

`

3147

3131

``

3148

``

`-

int_fspath = _PathLike(fd)

`

3149

``

`-

str_fspath = _PathLike(str_filename)

`

``

3132

`+

int_fspath = FakePath(fd)

`

``

3133

`+

str_fspath = FakePath(str_filename)

`

3150

3134

``

3151

3135

`for name, allow_fd, extra_args, cleanup_fn in self.functions:

`

3152

3136

`with self.subTest(name=name):

`

`@@ -3519,16 +3503,16 @@ def test_return_string(self):

`

3519

3503

``

3520

3504

`def test_fsencode_fsdecode(self):

`

3521

3505

`for p in "path/like/object", b"path/like/object":

`

3522

``

`-

pathlike = _PathLike(p)

`

``

3506

`+

pathlike = FakePath(p)

`

3523

3507

``

3524

3508

`self.assertEqual(p, self.fspath(pathlike))

`

3525

3509

`self.assertEqual(b"path/like/object", os.fsencode(pathlike))

`

3526

3510

`self.assertEqual("path/like/object", os.fsdecode(pathlike))

`

3527

3511

``

3528

3512

`def test_pathlike(self):

`

3529

``

`-

self.assertEqual('#feelthegil', self.fspath(_PathLike('#feelthegil')))

`

3530

``

`-

self.assertTrue(issubclass(_PathLike, os.PathLike))

`

3531

``

`-

self.assertTrue(isinstance(_PathLike(), os.PathLike))

`

``

3513

`+

self.assertEqual('#feelthegil', self.fspath(FakePath('#feelthegil')))

`

``

3514

`+

self.assertTrue(issubclass(FakePath, os.PathLike))

`

``

3515

`+

self.assertTrue(isinstance(FakePath('x'), os.PathLike))

`

3532

3516

``

3533

3517

`def test_garbage_in_exception_out(self):

`

3534

3518

`vapor = type('blah', (), {})

`

`@@ -3540,14 +3524,14 @@ def test_argument_required(self):

`

3540

3524

``

3541

3525

`def test_bad_pathlike(self):

`

3542

3526

`# fspath returns a value other than str or bytes.

`

3543

``

`-

self.assertRaises(TypeError, self.fspath, _PathLike(42))

`

``

3527

`+

self.assertRaises(TypeError, self.fspath, FakePath(42))

`

3544

3528

`# fspath attribute that is not callable.

`

3545

3529

`c = type('foo', (), {})

`

3546

3530

`c.fspath = 1

`

3547

3531

`self.assertRaises(TypeError, self.fspath, c())

`

3548

3532

`# fspath raises an exception.

`

3549

3533

`self.assertRaises(ZeroDivisionError, self.fspath,

`

3550

``

`-

_PathLike(ZeroDivisionError()))

`

``

3534

`+

FakePath(ZeroDivisionError()))

`

3551

3535

``

3552

3536

`# Only test if the C version is provided, otherwise TestPEP519 already tested

`

3553

3537

`# the pure Python implementation.

`