我已经成功地使用pyasn1对我的大部分数据进行了编码,但是在向序列添加一组结构时遇到了问题。以下是表示结构的模式:
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编译这个,我的问题是向TopType对象添加一个类型为Troublemy的元素。 下面是我尝试这样做的代码:
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
我不明白如何将麻烦的集合放入序列中,并使标记匹配。
以下是回溯的一部分:
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>>>>
这是在Fedora 33上:
$ rpm -q python3 python3-pyasn1
python3-3.9.2-1.fc33.x86_64
python3-pyasn1-0.4.8-3.fc33.noarch
我在第2246行的univ.py中插入了一个print()语句,导致回溯中的行号比F33上未修改的univ.py增加1
好的,我的优秀同事找到了一个解决方案:
相关问题 更多 >
编程相关推荐