1. Antoine Pitrou
  2. pathlib

Commits

Antoine Pitrou  committed d98273f

Add st_* attributes

  • Participants
  • Parent commits 104a583
  • Branches default

Comments (0)

Files changed (4)

File VERSION.txt

View file
  • Ignore whitespace
-0.2
+0.3

File docs/index.rst

View file
  • Ignore whitespace
       >>> p.stat().st_mtime
       1327883547.852554
 
+   This information can also be accessed through :ref:`helper attributes <st_attrs>`.
+
 
 .. method:: Path.restat()
 
    use :func:`Path.rmdir` instead.
 
 
+.. _st_attrs:
+
+Attributes
+^^^^^^^^^^
+
+Concrete paths provide the following attributes:
+
+.. data::
+      Path.st_mode
+      Path.st_ino
+      Path.st_dev
+      Path.st_nlink
+      Path.st_uid
+      Path.st_gid
+      Path.st_size
+      Path.st_atime
+      Path.st_mtime
+      Path.st_ctime
+      ...
+
+   Helper attributes returning the corresponding fields on :meth:`Path.stat`'s
+   result::
+
+      >>> p = Path('setup.py')
+      >>> p.st_size
+      956
+      >>> p.st_mtime
+      1327939910.2178059

File pathlib.py

View file
  • Ignore whitespace
             if self._closed:
                 self._raise_closed()
 
+    def __getattr__(self, name):
+        if name.startswith('st_'):
+            return getattr(self._stat, name)
+        return super().__getattribute__(name)
+
     def abspath(self):
         """Return an absolute version of this path.  This function works
         even if the path doesn't point to anything.

File test_pathlib.py

View file
  • Ignore whitespace
         self.addCleanup(support.rmtree, BASE)
         os.mkdir(join('dirA'))
         os.mkdir(join('dirB'))
-        with open(join('fileA'), 'w') as f:
-            f.write("this is file A\n")
-        with open(join('dirB', 'fileB'), 'w') as f:
-            f.write("this is file B\n")
+        with open(join('fileA'), 'wb') as f:
+            f.write(b"this is file A\n")
+        with open(join('dirB', 'fileB'), 'wb') as f:
+            f.write(b"this is file B\n")
         if not symlink_skip_reason:
             if os.name == 'nt':
                 # Workaround for http://bugs.python.org/issue13772
         st = p.stat()
         self.assertEqual(st, p.lstat())
 
+    def test_st_fields(self):
+        p = self.cls(BASE)['fileA']
+        self.assertEqual(p.st_size, 15)
+        p.st_mtime
+        p.st_mode
+        with self.assertRaises(AttributeError):
+            p.st_foo
+        with self.assertRaises(AttributeError):
+            p.foo
+
     def test_unlink(self):
         p = self.cls(BASE)['fileA']
         p.unlink()