8000 encode() modifies object, selects value for DefaultedNamedType field · Issue #53 · pyasn1/pyasn1 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

encode() modifies object, selects value for DefaultedNamedType field #53

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
jefferyto opened this issue Nov 28, 2023 · 0 comments
Open

Comments

@jefferyto
Copy link

I'm updating the pyasn1 package for OpenWrt to 0.5.1, and testing with the example from the readme:

Python 3.11.6 (main, Nov 23 2023, 21:55:55) [GCC 12.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from pyasn1.type import namedtype
>>> from pyasn1.type import tag
>>> from pyasn1.type import univ
>>> from pyasn1.codec.der.encoder import encode
>>> from pyasn1.codec.der.decoder import decode
>>>
>>> class Record(univ.Sequence):
...     componentType = namedtype.NamedTypes(
...         namedtype.NamedType('id', univ.Integer()),
...         namedtype.OptionalNamedType(
...             'room', univ.Integer().subtype(
...                 implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 0)
...             )
...         ),
...         namedtype.DefaultedNamedType(
...             'house', univ.Integer(0).subtype(
...                 implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 1)
...             )
...         )
...     )
...
>>> r1 = Record()
>>> r1['id'] = 123
>>> r1['room'] = 321
>>>
>>> r2 = Record()
>>> r2['id'] = 123
>>> r2['room'] = 321
>>>
>>> r1 == r2
True
>>> encode(r1)
b'0\x07\x02\x01{\x80\x02\x01A'
>>> r1 == r2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.11/site-packages/pyasn1/type/base.py", line 549, in __eq__
  File "/usr/lib/python3.11/site-packages/pyasn1/type/base.py", line 549, in __eq__
  File "/usr/lib/python3.11/site-packages/pyasn1/type/base.py", line 213, in plug
pyasn1.error.PyAsn1Error: Attempted "__eq__" operation on ASN.1 schema object
>>> r1
<Record value object, tagSet=<TagSet object, tags 0:32:16>, subtypeSpec=<ConstraintsIntersection object>, componentType=<NamedTypes object, types <NamedType object, type id=<Integer schema object, tagSet <TagSet object, tags 0:0:2>>>, <OptionalNamedType object, type room=<Integer schema object, tagSet <TagSet object, tags 128:0:0>>>, <DefaultedNamedType object, type house=<Integer value object, tagSet <TagSet object, tags 128:0:1>, payload [0]>>>, sizeSpec=<ConstraintsIntersection object>, payload [<Integer value object, tagSet <TagSet object, tags 0:0:2>, payload [123]>, <Integer value object, tagSet <TagSet object, tags 128:0:0>, payload [321]>, <Integer value object, tagSet <TagSet object, tags 128:0:1>, payload [0]>]>
>>> r2
<Record value object, tagSet=<TagSet object, tags 0:32:16>, subtypeSpec=<ConstraintsIntersection object>, componentType=<NamedTypes object, types <NamedType object, type id=<Integer schema object, tagSet <TagSet object, tags 0:0:2>>>, <OptionalNamedType object, type room=<Integer schema object, tagSet <TagSet object, tags 128:0:0>>>, <DefaultedNamedType object, type house=<Integer value object, tagSet <TagSet object, tags 128:0:1>, payload [0]>>>, sizeSpec=<ConstraintsIntersection object>, payload [<Integer value object, tagSet <TagSet object, tags 0:0:2>, payload [123]>, <Integer value object, tagSet <TagSet object, tags 128:0:0>, payload [321]>, <NoValue object>]>
>>> str(r1)
'Record:\n id=123\n room=321\n house=0\n'
>>> str(r2)
'Record:\n id=123\n room=321\n'
>>>

I've also tried the native encoder (pyasn1.codec.native.encoder.encode) and I get the same result.

I'm not sure if this is an issue or expected behaviour, but I wouldn't expect serializing a ASN.1 object would modify the object itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant
0