A uname_result that's largely compatible with a simple namedtuple except that 'processor' is resolved late and cached to avoid calling "uname" except when needed.
| 776 | ### Portable uname() interface |
| 777 | |
| 778 | class uname_result( |
| 779 | collections.namedtuple( |
| 780 | "uname_result_base", |
| 781 | "system node release version machine") |
| 782 | ): |
| 783 | """ |
| 784 | A uname_result that's largely compatible with a |
| 785 | simple namedtuple except that 'processor' is |
| 786 | resolved late and cached to avoid calling "uname" |
| 787 | except when needed. |
| 788 | """ |
| 789 | |
| 790 | _fields = ('system', 'node', 'release', 'version', 'machine', 'processor') |
| 791 | |
| 792 | @functools.cached_property |
| 793 | def processor(self): |
| 794 | return _unknown_as_blank(_Processor.get()) |
| 795 | |
| 796 | def __iter__(self): |
| 797 | return itertools.chain( |
| 798 | super().__iter__(), |
| 799 | (self.processor,) |
| 800 | ) |
| 801 | |
| 802 | @classmethod |
| 803 | def _make(cls, iterable): |
| 804 | # override factory to affect length check |
| 805 | num_fields = len(cls._fields) - 1 |
| 806 | result = cls.__new__(cls, *iterable) |
| 807 | if len(result) != num_fields + 1: |
| 808 | msg = f'Expected {num_fields} arguments, got {len(result)}' |
| 809 | raise TypeError(msg) |
| 810 | return result |
| 811 | |
| 812 | def __getitem__(self, key): |
| 813 | return tuple(self)[key] |
| 814 | |
| 815 | def __len__(self): |
| 816 | return len(tuple(iter(self))) |
| 817 | |
| 818 | def __reduce__(self): |
| 819 | return uname_result, tuple(self)[:len(self._fields) - 1] |
| 820 | |
| 821 | |
| 822 | _uname_cache = None |