I have successfully used pyasn1 to encode most of my data, but am having trouble adding a SetOf construction to a sequence. Here is a schema that represents the structure:
TroubleAddingSetOf
DEFINITIONS IMPLICIT TAGS ::=
BEGIN
TopType ::= CHOICE
{
nextType NextType,
topSequence TopSequence
}
TopSequence ::= SEQUENCE OF NextType
NextType ::= CHOICE
{
fourthOne [4] OtherType,
...
}
OtherType ::= SEQUENCE
{
troubleSome [16] TroubleSome OPTIONAL
}
TroubleSome ::= SET SIZE (1..40) OF OCTET STRING (SIZE (1..256))
END -- end of TroubleAddingSetOf
asn1ate compiles this, and my problem is adding an element of type TroubleSome to the TopType object. Here is my code attempting to do that:
from pyasn1.type import univ, char, namedtype, namedval, tag, constraint, useful
class TroubleSome(univ.SetOf):
pass
TroubleSome.componentType = univ.OctetString().subtype(subtypeSpec=constraint.ValueSizeConstraint(1, 256))
TroubleSome.subtypeSpec=constraint.ValueSizeConstraint(1, 40)
class OtherType(univ.Sequence):
pass
OtherType.componentType = namedtype.NamedTypes(
namedtype.OptionalNamedType('troubleSome', TroubleSome().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 16)))
)
class NextType(univ.Choice):
pass
NextType.componentType = namedtype.NamedTypes(
namedtype.NamedType('fourthOne', OtherType().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 4)))
)
class TopSequence(univ.SequenceOf):
pass
TopSequence.componentType = NextType()
class TopType(univ.Choice):
pass
TopType.componentType = namedtype.NamedTypes(
namedtype.NamedType('nextType', NextType()),
namedtype.NamedType('topSequence', TopSequence())
)
trouble_some = TroubleSome()
trouble_some.setComponentByPosition(0, '12')
trouble_some.setComponentByPosition(1, 'WILL')
trouble_some.setComponentByPosition(2, '3')
top_one = TopType()
# This fails with a PyAsn1Error exception, partly reproduced below:
top_one['nextType']['fourthOne']['troubleSome'] = trouble_some
I do not understand how to put the TroubleSome set into the sequence, and make the tags match.
Here is part of the Traceback:
Traceback (most recent call last):
File "/usr/lib/python3.9/site-packages/pyasn1/type/univ.py", line 2249, in __setitem__
self.setComponentByName(idx, value)
File "/usr/lib/python3.9/site-packages/pyasn1/type/univ.py", line 2415, in setComponentByName
return self.setComponentByPosition(
File "/usr/lib/python3.9/site-packages/pyasn1/type/univ.py", line 2604, in setComponentByPosition
raise error.PyAsn1Error('Component value is tag-incompatible: %r vs %r' % (value, componentType))
pyasn1.error.PyAsn1Error: Component value is tag-incompatible: <TroubleSome value object, tagSet=<TagSet object, tags 0:32:17>, subtypeSpec=<ValueSizeConstraint object, consts 1, 40>, componentType=<OctetString schema object, tagSet <TagSet object, tags 0:0:4>, subtypeSpec <ConstraintsIntersection object, consts <ValueSizeConstraint object, consts 1, 256>>, encoding iso-8859-1>, sizeSpec=<ConstraintsIntersection object>, payload [<OctetString value object, tagSet <TagSet object, tags 0:0:4>, subtypeSpec <ConstraintsIntersection object, consts <ValueSizeConstraint object, consts 1, 256>>, encoding iso-8859-1, payload [12]>, <OctetString value object, tagSet <TagSet object, tags 0:0:4>, subtypeSpec <ConstraintsIntersection object, consts <ValueSizeConstraint object, consts 1, 256>>, encoding iso-8859-1, payload [WILL]>, <OctetString value object, tagSet <TagSet object, tags 0:0:4>, subtypeSpec <ConstraintsIntersection object, consts <ValueSizeConstraint object, consts 1, 256>>, encoding iso-8859-1, payload [3]>]> vs <NamedTypes object, types <OptionalNamedType object, type troubleSome=<TroubleSome schema object, tagSet=<TagSet object, tags 128:32:16>, subtypeSpec=<ValueSizeConstraint object, consts 1, 40>, componentType=<OctetString schema object, tagSet <TagSet object, tags 0:0:4>, subtypeSpec <ConstraintsIntersection object, consts <ValueSizeConstraint object, consts 1, 256>>, encoding iso-8859-1>, sizeSpec=<ConstraintsIntersection object>>>>
This is on Fedora 33:
$ rpm -q python3 python3-pyasn1
python3-3.9.2-1.fc33.x86_64
python3-pyasn1-0.4.8-3.fc33.noarch
I inserted a print() statement into univ.py at line 2246, causing the line numbers in the traceback to be increased by one over an unmodified univ.py on F33.