Commits

Anonymous committed 107bf33

Add a cwd() classmethod.

Comments (0)

Files changed (2)

 
     # Public API
 
+    @classmethod
+    def cwd(cls, use_openat=False):
+        """Return a new path pointing to the current working directory
+        (as returned by os.getcwd()).
+        """
+        return cls(os.getcwd(), use_openat=use_openat)
+
     def __iter__(self):
         """Iterate over the files in this directory.  Does not yield any
         result for the special paths '.' and '..'.
             return self
         # FIXME this must defer to the specific flavour (and, under Windows,
         # use nt._getfullpathname())
-        obj = self._from_parts((os.getcwd(),), self, init=False)
+        obj = self._from_parts((os.getcwd(),) + self._parts, init=False)
         obj._init(template=self, parent_fd=self._parent_fd, fd=self._cached_fd)
         return obj
 

Lib/test/test_pathlib.py

                         "%r and %r don't point to the same file" %
                         (path_a, path_b))
 
+    def _test_cwd(self, p):
+        q = self.cls(os.getcwd())
+        self.assertEqual(p, q)
+        self.assertEqual(str(p), str(q))
+        self.assertIs(type(p), type(q))
+        self.assertTrue(p.is_absolute())
+
+    def test_cwd(self):
+        p = self.cls.cwd()
+        self._test_cwd(p)
+
     def test_open(self):
         p = self.cls(BASE)
         with p['fileA'].open('r') as f:
         self.assertEqual(mock_fdlistdir.calls, 1)
         self.assertEqual(mock_listdir.calls, 0)
 
+    def test_cwd(self):
+        p = pathlib.PosixPath.cwd(use_openat=True)
+        self._test_cwd(p)
+
     # XXX can't mock os.openat since _OpenatAccessor caches the os.openat lookup.