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.
| 919 | ### Portable uname() interface |
| 920 | |
| 921 | class uname_result( |
| 922 | collections.namedtuple( |
| 923 | "uname_result_base", |
| 924 | "system node release version machine") |
| 925 | ): |
| 926 | """ |
| 927 | A uname_result that's largely compatible with a |
| 928 | simple namedtuple except that 'processor' is |
| 929 | resolved late and cached to avoid calling "uname" |
| 930 | except when needed. |
| 931 | """ |
| 932 | |
| 933 | _fields = ('system', 'node', 'release', 'version', 'machine', 'processor') |
| 934 | |
| 935 | @functools.cached_property |
| 936 | def processor(self): |
| 937 | return _unknown_as_blank(_Processor.get()) |
| 938 | |
| 939 | def __iter__(self): |
| 940 | return itertools.chain( |
| 941 | super().__iter__(), |
| 942 | (self.processor,) |
| 943 | ) |
| 944 | |
| 945 | @classmethod |
| 946 | def _make(cls, iterable): |
| 947 | # override factory to affect length check |
| 948 | num_fields = len(cls._fields) - 1 |
| 949 | result = cls.__new__(cls, *iterable) |
| 950 | if len(result) != num_fields + 1: |
| 951 | msg = f'Expected {num_fields} arguments, got {len(result)}' |
| 952 | raise TypeError(msg) |
| 953 | return result |
| 954 | |
| 955 | def __getitem__(self, key): |
| 956 | return tuple(self)[key] |
| 957 | |
| 958 | def __len__(self): |
| 959 | return len(tuple(iter(self))) |
| 960 | |
| 961 | def __reduce__(self): |
| 962 | return uname_result, tuple(self)[:len(self._fields) - 1] |
| 963 | |
| 964 | |
| 965 | _uname_cache = None |