summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorelie <elie>2013-12-07 21:33:18 +0000
committerelie <elie>2013-12-07 21:33:18 +0000
commitdaa20434ae095855b28c63469cebff3f79bfe15a (patch)
tree4d5b68a92bd78f555cb3ff2fa00a38688e3e96d6
parent4ca0e11991c5045e3e70b164ed9ede979028853a (diff)
downloadpyasn1-daa20434ae095855b28c63469cebff3f79bfe15a.tar.gz
* constructed types now always verify their consistensy with
isSameTypeWith() rather then with isSuperTypeOf() * optional flags to setComponentByPosition(), introduced in previous revision, withdrawn as lame
-rw-r--r--CHANGES5
-rw-r--r--pyasn1/type/base.py10
-rw-r--r--pyasn1/type/univ.py68
-rw-r--r--test/type/test_univ.py49
4 files changed, 29 insertions, 103 deletions
diff --git a/CHANGES b/CHANGES
index 38ccf7c..d6d617a 100644
--- a/CHANGES
+++ b/CHANGES
@@ -18,9 +18,8 @@ Revision 0.1.8
- When comparing ASN.1 types, by-tag and/or by-constraints matching
can now be performed with the isSuperTypeOf()/isSameTypeWith() optional
flags.
-- The setComponentBy*() methods of all CONSTRUCTED objects now accept optional
- exactTypes, matchTags and matchConstraints flags to give apps finer control
- on when not to perform relevant data consistency checks.
+- Constructed types now verify their consistency by invoking isSameTypeWith()
+ for their components rather than isSuperTypeOf() as it used to be.
- Fix to NamedType.__repr__() to work properly.
- Fixes to __repr__() implementation of many built-in ASN.1 types to take into
account all of their initializers such as tagSet, subtypeSpec etc.
diff --git a/pyasn1/type/base.py b/pyasn1/type/base.py
index 817aeb6..8efa178 100644
--- a/pyasn1/type/base.py
+++ b/pyasn1/type/base.py
@@ -244,19 +244,13 @@ class AbstractConstructedAsn1Item(Asn1ItemBase):
self._cloneComponentValues(r, cloneValueFlag)
return r
- def _verifyComponent(self, idx, value, exactTypes=False,
- matchTags=True, matchConstraints=True):
- pass
+ def _verifyComponent(self, idx, value): pass
def verifySizeSpec(self): self._sizeSpec(self)
def getComponentByPosition(self, idx):
raise error.PyAsn1Error('Method not implemented')
- def setComponentByPosition(self, idx, value,
- verifyConstraints=True,
- exactTypes=False,
- matchTags=True,
- matchConstraints=True):
+ def setComponentByPosition(self, idx, value, verifyConstraints=True):
raise error.PyAsn1Error('Method not implemented')
def getComponentType(self): return self._componentType
diff --git a/pyasn1/type/univ.py b/pyasn1/type/univ.py
index e118468..594a308 100644
--- a/pyasn1/type/univ.py
+++ b/pyasn1/type/univ.py
@@ -689,21 +689,13 @@ class SetOf(base.AbstractConstructedAsn1Item):
myClone.setComponentByPosition(idx, c.clone())
idx = idx + 1
- def _verifyComponent(self, idx, value, exactTypes=False,
- matchTags=True, matchConstraints=True):
- if self._componentType is not None:
- f = exactTypes and \
- self._componentType.isSameTypeWith or \
- self._componentType.isSuperTypeOf
- if not f(value, matchTags, matchConstraints):
- raise error.PyAsn1Error('Component type error %r' % (value,))
+ def _verifyComponent(self, idx, value):
+ if self._componentType is not None and \
+ not self._componentType.isSameTypeWith(value):
+ raise error.PyAsn1Error('Component type error %r' % (value,))
def getComponentByPosition(self, idx): return self._componentValues[idx]
- def setComponentByPosition(self, idx, value=None,
- verifyConstraints=True,
- exactTypes=False,
- matchTags=True,
- matchConstraints=True):
+ def setComponentByPosition(self, idx, value=None, verifyConstraints=True):
l = len(self._componentValues)
if idx >= l:
self._componentValues = self._componentValues + (idx-l+1)*[None]
@@ -723,10 +715,7 @@ class SetOf(base.AbstractConstructedAsn1Item):
raise error.PyAsn1Error('Instance value required')
if verifyConstraints:
if self._componentType is not None:
- self._verifyComponent(idx, value,
- exactTypes=exactTypes,
- matchTags=matchTags,
- matchConstraints=matchConstraints)
+ self._verifyComponent(idx, value)
self._verifySubtypeSpec(value, idx)
if self._componentValues[idx] is None:
self._componentValuesSet = self._componentValuesSet + 1
@@ -790,29 +779,22 @@ class SequenceAndSetBase(base.AbstractConstructedAsn1Item):
myClone.setComponentByPosition(idx, c.clone())
idx = idx + 1
- def _verifyComponent(self, idx, value, exactTypes=False,
- matchTags=True, matchConstraints=True):
+ def _verifyComponent(self, idx, value):
if idx >= self._componentTypeLen:
raise error.PyAsn1Error(
'Component type error out of range'
)
t = self._componentType[idx].getType()
- f = exactTypes and t.isSameTypeWith or t.isSuperTypeOf
- if not f(value, matchTags, matchConstraints):
+ if not t.isSameTypeWith(value):
raise error.PyAsn1Error('Component type error %r vs %r' % (t, value))
def getComponentByName(self, name):
return self.getComponentByPosition(
self._componentType.getPositionByName(name)
)
- def setComponentByName(self, name, value=None,
- verifyConstraints=True,
- exactTypes=False,
- matchTags=True,
- matchConstraints=True):
+ def setComponentByName(self, name, value=None, verifyConstraints=True):
return self.setComponentByPosition(
- self._componentType.getPositionByName(name), value,
- verifyConstraints, exactTypes, matchTags, matchConstraints
+ self._componentType.getPositionByName(name),value,verifyConstraints
)
def getComponentByPosition(self, idx):
@@ -843,10 +825,7 @@ class SequenceAndSetBase(base.AbstractConstructedAsn1Item):
raise error.PyAsn1Error('Instance value required')
if verifyConstraints:
if self._componentTypeLen:
- self._verifyComponent(idx, value,
- exactTypes=exactTypes,
- matchTags=matchTags,
- matchConstraints=matchConstraints)
+ self._verifyComponent(idx, value)
self._verifySubtypeSpec(value, idx)
if self._componentValues[idx] is None:
self._componentValuesSet = self._componentValuesSet + 1
@@ -932,28 +911,22 @@ class Set(SequenceAndSetBase):
return c
def setComponentByType(self, tagSet, value=None, innerFlag=0,
- verifyConstraints=True,
- exactTypes=False,
- matchTags=True,
- matchConstraints=True):
+ verifyConstraints=True):
idx = self._componentType.getPositionByType(tagSet)
t = self._componentType.getTypeByPosition(idx)
if innerFlag: # set inner component by inner tagSet
if t.getTagSet():
return self.setComponentByPosition(
- idx, value, verifyConstraints,
- exactTypes, matchTags, matchConstraints
+ idx, value, verifyConstraints
)
else:
t = self.setComponentByPosition(idx).getComponentByPosition(idx)
return t.setComponentByType(
- tagSet, value, innerFlag,
- verifyConstraints, exactTypes, matchTags, matchConstraints
+ tagSet, value, innerFlag, verifyConstraints
)
else: # set outer component by inner tagSet
return self.setComponentByPosition(
- idx, value,
- verifyConstraints, exactTypes, matchTags, matchConstraints
+ idx, value, verifyConstraints
)
def getComponentTagMap(self):
@@ -1026,11 +999,7 @@ class Choice(Set):
else:
myClone.setComponentByType(tagSet, c.clone())
- def setComponentByPosition(self, idx, value=None,
- verifyConstraints=True,
- exactTypes=False,
- matchTags=True,
- matchConstraints=True):
+ def setComponentByPosition(self, idx, value=None, verifyConstraints=True):
l = len(self._componentValues)
if idx >= l:
self._componentValues = self._componentValues + (idx-l+1)*[None]
@@ -1048,10 +1017,7 @@ class Choice(Set):
)
if verifyConstraints:
if self._componentTypeLen:
- self._verifyComponent(idx, value,
- exactTypes=exactTypes,
- matchTags=matchTags,
- matchConstraints=matchConstraints)
+ self._verifyComponent(idx, value)
self._verifySubtypeSpec(value, idx)
self._componentValues[idx] = value
self._currentIdx = idx
diff --git a/test/type/test_univ.py b/test/type/test_univ.py
index 417c18d..606d47b 100644
--- a/test/type/test_univ.py
+++ b/test/type/test_univ.py
@@ -299,35 +299,18 @@ class SequenceOf(unittest.TestCase):
try:
s.setComponentByPosition(0, o)
except:
- assert 0, 'inner supertype tag not allowed with exactTypes=False'
- try:
- s.setComponentByPosition(0, o, exactTypes=True)
- except:
- try:
- s.setComponentByPosition(0, o, exactTypes=True,
- matchTags=False)
- except:
- assert 0, 'inner supertype tag denied with exactTypes=True & matchTags=False'
+ pass
else:
- assert 0, 'inner supertype tag allowed with exactTypes=True'
+ assert 0, 'inner supertype tag allowed'
def testComponentConstraintsMatching(self):
s = self.s1.clone()
o = univ.OctetString().subtype(subtypeSpec=constraint.ConstraintsUnion(constraint.SingleValueConstraint(str2octs('cba'))))
try:
s.setComponentByPosition(0, o.clone('cba'))
except:
- assert 0, 'inner supertype constraint not allowed with exactTypes=False'
- try:
- s.setComponentByPosition(0, o.clone('cba'), exactTypes=True)
- except:
- try:
- s.setComponentByPosition(0, o.clone('cba'),
- exactTypes=True,
- matchConstraints=False)
- except:
- assert 0, 'inner supertype constraint denied with exactTypes=True & matchConstriaints=False'
+ pass
else:
- assert 0, 'inner supertype constraint allowed with exactTypes=True'
+ assert 0, 'inner supertype constraint allowed'
def testSizeSpec(self):
s = self.s1.clone(sizeSpec=constraint.ConstraintsUnion(
constraint.ValueSizeConstraint(1,1)
@@ -423,34 +406,18 @@ class Sequence(unittest.TestCase):
try:
s.setComponentByName('name', o)
except:
- assert 0, 'inner supertype tag not allowed with exactTypes=False'
- try:
- s.setComponentByName('name', o, exactTypes=True)
- except:
- try:
- s.setComponentByName('name', o, exactTypes=True,
- matchTags=False)
- except:
- assert 0, 'inner supertype tag denied with exactTypes=True & matchTags=False'
+ pass
else:
- assert 0, 'inner supertype tag allowed with exactTypes=True'
+ assert 0, 'inner supertype tag allowed'
def testComponentConstraintsMatching(self):
s = self.s1.clone()
o = univ.OctetString().subtype(subtypeSpec=constraint.ConstraintsUnion(constraint.SingleValueConstraint(str2octs('cba'))))
try:
s.setComponentByName('name', o.clone('cba'))
except:
- assert 0, 'inner supertype constraint not allowed with exactTypes=False'
- try:
- s.setComponentByName('name', o.clone('cba'), exactTypes=True)
- except:
- try:
- s.setComponentByName('name', o.clone('cba'),
- exactTypes=True, matchConstraints=False)
- except:
- assert 0, 'inner supertype constraint denied with exactTypes=True & matchConstriaints=False'
+ pass
else:
- assert 0, 'inner supertype constraint allowed with exactTypes=True'
+ assert 0, 'inner supertype constraint allowed'
class SetOf(unittest.TestCase):
def setUp(self):