Pyasn1 nested sequence - incompatible tags

570 Views Asked by At

I need help with my code. I have two structs, which are using third.

class Bar(univ.Sequence):
    componentType = namedtype.NamedTypes(
    namedtype.NamedType('first',univ.Integer().subtype(implicitTag=tag.Tag(tag.tagClassContext,tag.tagFormatSimple,0))),
    namedtype.NamedType('second',univ.Integer().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 1)))
    )

class Foo(univ.Sequence):
    componentType = namedtype.NamedTypes(
    namedtype.OptionalNamedType('test', Bar().subtype(implicitTag=tag.Tag(tag.tagClassContext,tag.tagFormatSimple,3))),
    )

class Foo1(univ.Sequence):
    componentType = namedtype.NamedTypes(
    namedtype.OptionalNamedType('test', Bar().subtype(implicitTag=tag.Tag(tag.tagClassContext,tag.tagFormatSimple,2))),
    )

So, in the class Foo, Bar has a tag [3] and in the class Foo1 e.g. [2]. If only I had one class I would do

tagSet = tagBaseSet = tag.initTagSet(tag.Tag(tag.tagClassContext,tag.tagFormatSimple,3))

in the bar class (and it's working). But how do I cope with that kind of problem? Any help would be appreciated.

Edit: Here's asn1 representation:

Bar     ::= SEQUENCE {
first           [0] INTEGER,
second          [1] INTEGER
}

Foo     ::= SEQUENCE {
  first             [0] INTEGER,
  second            [1] INTEGER OPTIONAL,
  third             [2] INTEGER OPTIONAL,
  bar               [3] Bar
}

Foo1    ::= SEQUENCE {
  first             [0] INTEGER,
  second            [1] INTEGER OPTIONAL,
  bar               [2] Bar
}

EDIT2: Code: run and then delete comments (I get incompatible tags. Without tagSet in Bar, in both cases, too):

from pyasn1.type import univ,namedtype,tag

class Bar(univ.Sequence):
    componentType = namedtype.NamedTypes(
    namedtype.NamedType('first',univ.Integer().subtype(implicitTag=tag.Tag(tag.tagClassContext,tag.tagFormatSimple,0))),
    namedtype.NamedType('second',univ.Integer().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 1)))
    )
    tagSet = tagBaseSet = tag.initTagSet(tag.Tag(tag.tagClassContext,tag.tagFormatConstructed,3))

class Foo(univ.Sequence):
    componentType = namedtype.NamedTypes(
    namedtype.OptionalNamedType('test', Bar().subtype(implicitTag=tag.Tag(tag.tagClassContext,tag.tagFormatSimple,3))),
    )

class Foo1(univ.Sequence):
    componentType = namedtype.NamedTypes(
    namedtype.OptionalNamedType('test', Bar().subtype(implicitTag=tag.Tag(tag.tagClassContext,tag.tagFormatSimple,2))),
    )

bar = Bar()
bar.setComponentByName('first',1)
bar.setComponentByName('second',2)

fo = Foo()
fo.setComponentByName('test',bar)

# fo1 = Foo1()
# fo1.setComponentByName('test',bar)

print fo.prettyPrint()
# print fo1.prettyPrint()
0

There are 0 best solutions below