mirror of
https://github.com/python/cpython.git
synced 2025-11-01 06:01:29 +00:00
[3.9] bpo-42163, bpo-42189, bpo-42659: Support uname_tuple._replace (for all but processor) (GH-23010) (#24232)
* Add test capturing missed expectation with uname_result._replace.
* bpo-42163: Override uname_result._make to allow uname_result._replace to work (for everything but 'processor'.
* Replace hard-coded length with one derived from the definition.
* Add test capturing missed expectation with copy/deepcopy on namedtuple (bpo-42189).
* bpo-42189: Exclude processor parameter when constructing uname_result.
* In _make, rely on __new__ to strip processor.
* Add blurb.
* iter is not necessary here.
* Rely on num_fields in __new__
* Add test for slices on uname
* Add test for copy and pickle.
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
* import pickle
* Fix equality test after pickling.
* Simply rely on __reduce__ for pickling.
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
(cherry picked from commit a6fd0f414c)
Co-authored-by: Jason R. Coombs <jaraco@jaraco.com>
This commit is contained in:
parent
17c1f0c8cb
commit
799722cb0d
3 changed files with 50 additions and 2 deletions
|
|
@ -782,7 +782,7 @@ class uname_result(
|
|||
):
|
||||
"""
|
||||
A uname_result that's largely compatible with a
|
||||
simple namedtuple except that 'platform' is
|
||||
simple namedtuple except that 'processor' is
|
||||
resolved late and cached to avoid calling "uname"
|
||||
except when needed.
|
||||
"""
|
||||
|
|
@ -797,12 +797,25 @@ def __iter__(self):
|
|||
(self.processor,)
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def _make(cls, iterable):
|
||||
# override factory to affect length check
|
||||
num_fields = len(cls._fields)
|
||||
result = cls.__new__(cls, *iterable)
|
||||
if len(result) != num_fields + 1:
|
||||
msg = f'Expected {num_fields} arguments, got {len(result)}'
|
||||
raise TypeError(msg)
|
||||
return result
|
||||
|
||||
def __getitem__(self, key):
|
||||
return tuple(iter(self))[key]
|
||||
return tuple(self)[key]
|
||||
|
||||
def __len__(self):
|
||||
return len(tuple(iter(self)))
|
||||
|
||||
def __reduce__(self):
|
||||
return uname_result, tuple(self)[:len(self._fields)]
|
||||
|
||||
|
||||
_uname_cache = None
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue