diff options
author | Ilya Etingof <etingof@gmail.com> | 2019-07-12 22:31:41 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-07-12 22:31:41 +0200 |
commit | 7b3f79cac2ce765537baa406762d59eae47de04c (patch) | |
tree | 6942818a0fb1266825e93ce8b9f4aaf594b881f9 /pyasn1/type | |
parent | cf9b3f5f67501b85296c05456bbd324352177718 (diff) | |
download | pyasn1-7b3f79cac2ce765537baa406762d59eae47de04c.tar.gz |
Add `SequenceOf`/`SetOf` list-like slicing support (#168)
Diffstat (limited to 'pyasn1/type')
-rw-r--r-- | pyasn1/type/univ.py | 28 |
1 files changed, 25 insertions, 3 deletions
diff --git a/pyasn1/type/univ.py b/pyasn1/type/univ.py index 5634f94..0ba59a7 100644 --- a/pyasn1/type/univ.py +++ b/pyasn1/type/univ.py @@ -1677,7 +1677,7 @@ class SequenceOfAndSetOfBase(base.AbstractConstructedAsn1Item): key=key, reverse=reverse))) def __len__(self): - if not self._componentValues: + if self._componentValues is noValue or not self._componentValues: return 0 return max(self._componentValues) + 1 @@ -1762,6 +1762,17 @@ class SequenceOfAndSetOfBase(base.AbstractConstructedAsn1Item): # returns noValue s.getComponentByPosition(0, instantiate=False) """ + if isinstance(idx, slice): + indices = tuple(range(len(self))) + return [self.getComponentByPosition(subidx, default, instantiate) + for subidx in indices[idx]] + + if idx < 0: + idx = len(self) + idx + if idx < 0: + raise error.PyAsn1Error( + 'SequenceOf/SetOf index is out of range') + try: componentValue = self._componentValues[idx] @@ -1819,9 +1830,20 @@ class SequenceOfAndSetOfBase(base.AbstractConstructedAsn1Item): IndexError: When idx > len(self) """ + if isinstance(idx, slice): + indices = tuple(range(len(self))) + startIdx = indices and indices[idx][0] or 0 + for subIdx, subValue in enumerate(value): + self.setComponentByPosition( + startIdx + subIdx, subValue, verifyConstraints, + matchTags, matchConstraints) + return self + if idx < 0: - raise error.PyAsn1Error( - 'SequenceOf/SetOf index must not be negative') + idx = len(self) + idx + if idx < 0: + raise error.PyAsn1Error( + 'SequenceOf/SetOf index is out of range') componentType = self.componentType |