From 66afc8921e4f5d3a41e407ab6d95ce7e4ec5383a Mon Sep 17 00:00:00 2001 From: Ilya Etingof Date: Sun, 25 Aug 2019 14:35:44 +0200 Subject: Add `isInconsistent` property hook to all constructed types (#170) Added `isInconsistent` property to all constructed types. This property conceptually replaces `verifySizeSpec` method to serve a more general purpose e.g. ensuring all required fields are in a good shape. By default this check invokes subtype constraints verification and is run by codecs on value de/serialisation. --- pyasn1/type/base.py | 23 +++++++++++++++++++++-- pyasn1/type/univ.py | 2 +- 2 files changed, 22 insertions(+), 3 deletions(-) (limited to 'pyasn1/type') diff --git a/pyasn1/type/base.py b/pyasn1/type/base.py index 21e4041..1fd69f9 100644 --- a/pyasn1/type/base.py +++ b/pyasn1/type/base.py @@ -655,8 +655,23 @@ class ConstructedAsn1Type(Asn1Type): return clone - def verifySizeSpec(self): - self.sizeSpec(self) + @property + def isInconsistent(self): + """Run necessary checks to ensure object consistency. + + Default action is to verify |ASN.1| object against constraints imposed + by `subtypeSpec`. + + Raises + ------ + :py:class:`~pyasn1.error.PyAsn1tError` on any inconsistencies found + """ + try: + self.sizeSpec(self) + + except error.PyAsn1Error: + exc = sys.exc_info()[1] + return exc def getComponentByPosition(self, idx): raise error.PyAsn1Error('Method not implemented') @@ -679,5 +694,9 @@ class ConstructedAsn1Type(Asn1Type): def getComponentType(self): return self.componentType + def verifySizeSpec(self): + self.sizeSpec(self) + + # Backward compatibility AbstractConstructedAsn1Item = ConstructedAsn1Type diff --git a/pyasn1/type/univ.py b/pyasn1/type/univ.py index b39c533..4f305f6 100644 --- a/pyasn1/type/univ.py +++ b/pyasn1/type/univ.py @@ -3004,7 +3004,7 @@ class Choice(Set): if self._currentIdx is not None: yield self.componentType[self._currentIdx].getName(), self[self._currentIdx] - def verifySizeSpec(self): + def checkConsistency(self): if self._currentIdx is None: raise error.PyAsn1Error('Component not chosen') -- cgit v1.2.3