| 641 | create_file(self.fname, b"ABC") |
| 642 | |
| 643 | def check_stat_attributes(self, fname): |
| 644 | result = os.stat(fname) |
| 645 | |
| 646 | # Make sure direct access works |
| 647 | self.assertEqual(result[stat.ST_SIZE], 3) |
| 648 | self.assertEqual(result.st_size, 3) |
| 649 | |
| 650 | # Make sure all the attributes are there |
| 651 | members = dir(result) |
| 652 | for name in dir(stat): |
| 653 | if name[:3] == 'ST_': |
| 654 | attr = name.lower() |
| 655 | if name.endswith("TIME"): |
| 656 | def trunc(x): return int(x) |
| 657 | else: |
| 658 | def trunc(x): return x |
| 659 | self.assertEqual(trunc(getattr(result, attr)), |
| 660 | result[getattr(stat, name)]) |
| 661 | self.assertIn(attr, members) |
| 662 | |
| 663 | # Make sure that the st_?time and st_?time_ns fields roughly agree |
| 664 | # (they should always agree up to around tens-of-microseconds) |
| 665 | for name in 'st_atime st_mtime st_ctime'.split(): |
| 666 | floaty = int(getattr(result, name) * 100000) |
| 667 | nanosecondy = getattr(result, name + "_ns") // 10000 |
| 668 | self.assertAlmostEqual(floaty, nanosecondy, delta=2) |
| 669 | |
| 670 | # Ensure both birthtime and birthtime_ns roughly agree, if present |
| 671 | try: |
| 672 | floaty = int(result.st_birthtime * 100000) |
| 673 | nanosecondy = result.st_birthtime_ns // 10000 |
| 674 | except AttributeError: |
| 675 | pass |
| 676 | else: |
| 677 | self.assertAlmostEqual(floaty, nanosecondy, delta=2) |
| 678 | |
| 679 | try: |
| 680 | result[200] |
| 681 | self.fail("No exception raised") |
| 682 | except IndexError: |
| 683 | pass |
| 684 | |
| 685 | # Make sure that assignment fails |
| 686 | try: |
| 687 | result.st_mode = 1 |
| 688 | self.fail("No exception raised") |
| 689 | except AttributeError: |
| 690 | pass |
| 691 | |
| 692 | try: |
| 693 | result.st_rdev = 1 |
| 694 | self.fail("No exception raised") |
| 695 | except (AttributeError, TypeError): |
| 696 | pass |
| 697 | |
| 698 | try: |
| 699 | result.parrot = 1 |
| 700 | self.fail("No exception raised") |