Skip to content

Commit 5649e5e

Browse files
committed
Initial support for __rdiv__ and __rtruediv__
1 parent d140006 commit 5649e5e

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

‎path.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,20 @@ def __div__(self, rel):
239239
# Make the / operator work even when true division is enabled.
240240
__truediv__ = __div__
241241

242+
# The / operator joins Paths the other way around
243+
def __rdiv__(self, rel):
244+
""" fp.__rdiv__(rel) == rel / fp
245+
246+
Join two path components, adding a separator character if
247+
needed.
248+
249+
.. seealso:: :func:`os.path.join`
250+
"""
251+
return self._next_class(self.module.join(rel, self))
252+
253+
# Make the / operator work even when true division is enabled.
254+
__rtruediv__ = __rdiv__
255+
242256
def __enter__(self):
243257
self._old_dir = self.getcwd()
244258
os.chdir(self)

‎test_path.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ def testRelpath(self):
5454
self.assertEqual(boz.relpathto(quux), up/up/up/'quux')
5555
self.assertEqual(boz.relpathto(bar), up/up)
5656

57+
# Path is not the first element in concatenation
58+
self.assertEqual(root.relpathto(boz), 'foo'/Path('bar')/'Baz'/'Boz')
59+
5760
# x.relpathto(x) == curdir
5861
self.assertEqual(root.relpathto(root), os.curdir)
5962
self.assertEqual(boz.relpathto(boz), os.curdir)

0 commit comments

Comments
 (0)