问题描述
我已成功使用 pyasn1 对我的大部分数据进行编码,但是在将 Setof 构造添加到序列时遇到了问题。这是一个表示结构的模式:
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 对象添加一个 TroubleSome 类型的元素。 这是我尝试执行此操作的代码:
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
我不明白如何将 TroubleSome 集合放入序列中,并使标签匹配。
这是回溯的一部分:
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,256>>,encoding iso-8859-1>,sizeSpec=<ConstraintsIntersection object>,payload [<OctetString value object,encoding iso-8859-1,payload [12]>,<OctetString value object,payload [WILL]>,payload [3]>]> vs <NamedTypes object,types <OptionalNamedType object,type troubleSome=<TroubleSome schema object,tags 128:32:16>,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
我在 univ.py 的第 2246 行插入了一个 print() 语句,导致回溯中的行号比 F33 上未修改的 univ.py 增加了一个。
解决方法
好的,我的优秀同事找到了解决方案:
top_one['nextType']['fourthOne']['troubleSome'].setComponentByPosition(0,'12')
top_one['nextType']['fourthOne']['troubleSome'].setComponentByPosition(1,'WILL')
top_one['nextType']['fourthOne']['troubleSome'].setComponentByPosition(2,'3')