1

我已成功使用 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 对此进行编译,而我的问题是将 TroubleSome 类型的元素添加到 TopType 对象。这是我试图这样做的代码:

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, 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 行将 print() 语句插入到 univ.py 中,导致回溯中的行号比 F33 上未修改的 univ.py 增加了 1。

4

1 回答 1

0

好的,我的优秀同事找到了解决方案:

top_one['nextType']['fourthOne']['troubleSome'].setComponentByPosition(0, '12')
top_one['nextType']['fourthOne']['troubleSome'].setComponentByPosition(1, 'WILL')
top_one['nextType']['fourthOne']['troubleSome'].setComponentByPosition(2, '3')
于 2021-03-13T07:48:56.683 回答