Skip to content

Commit fd4c417

Browse files
authored
Merge pull request #295 from fsantovito/context-manager
I solved the issue with Python 3.9. However, I do not like the way old versions of Python are current installed because DeadSnakes removes old versions from time to time and it breaks frequently the builds. I will review this (and also add Python 3.14). So I merge this PR as it is currently, and will do the modifications in later commits.
2 parents 372b1d9 + ed3977c commit fd4c417

11 files changed

Lines changed: 565 additions & 38 deletions

File tree

AUTHORS.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ The following people have contributed to Python-ASN1. Collectively they own the
66

77
* Geert Jansen <geert@boskant.nl>
88
* Sebastien Andrivet <sebastien@andrivet.com>
9+
* Filippo Santovito <filippo.santovito@gmail.com>
910

CHANGELOG.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
Changelog
22
=========
33

4+
3.1.1 (2026-31-01)
5+
------------------
6+
7+
* Support for using Encoder via context manager
8+
* Support for encoding sequences via context manager
9+
* Support for encoding sets via context manager
10+
* Support for using Decoder via context manager
11+
412
3.1.0 (2025-05-16)
513
------------------
614

Dockerfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM ubuntu:20.04
1+
FROM ubuntu:22.04
22
LABEL authors="Sebastien Andrivet"
33

44
ARG DEBIAN_FRONTEND=noninteractive
@@ -12,10 +12,10 @@ RUN apt-get update && \
1212
python2.7 \
1313
python3.4 \
1414
python3.5 \
15-
python3.6 python3.6-distutils \
15+
# python3.6 python3.6-distutils \
1616
python3.7 python3.7-distutils \
1717
python3.8 python3.8-distutils \
18-
python3.9 \
18+
# python3.9 \
1919
python3.10 \
2020
python3.11 \
2121
python3.12 \

docs/credits.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ The following projects have provided inspiration for Python-ASN1:
77
* `Samba`_ In particular **libads** for the stack based approach for
88
building constructed types.
99

10-
.. _pyasn1: https://pyasn1.sourceforge.net/
10+
.. _pyasn1: https://web.archive.org/web/20191231214902/http://snmplabs.com/pyasn1/index.html
1111
.. _Samba: https://github.com/samba-team/samba/tree/master/source3/libads

docs/usage.rst

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,16 @@ If you want to encode data and retrieve its DER-encoded representation, use code
9393
encoder.write('1.2.3', asn1.Numbers.ObjectIdentifier)
9494
encoded_bytes = encoder.output()
9595
96+
or using the new context manager api:
97+
98+
.. code-block:: python
99+
100+
import asn1
101+
102+
with asn1.Encoder() as encoder:
103+
encoder.write('1.2.3', asn1.Numbers.ObjectIdentifier)
104+
encoded_bytes = encoder.output()
105+
96106
It is also possible to encode data directly to a file or any stream:
97107

98108
.. code-block:: python
@@ -104,6 +114,16 @@ It is also possible to encode data directly to a file or any stream:
104114
encoder.start(f)
105115
encoder.write('1.2.3', asn1.Numbers.ObjectIdentifier)
106116
117+
or using the new context manager api:
118+
119+
.. code-block:: python
120+
121+
import asn1
122+
123+
with open('output.der', 'wb') as f:
124+
with asn1.Encoder(stream=f) as encoder:
125+
encoder.write('1.2.3', asn1.Numbers.ObjectIdentifier)
126+
107127
You can encode complex data structures such as sequences and sets:
108128

109129
.. code-block:: python
@@ -139,6 +159,22 @@ If you want to precisely specify the ASN.1 type, you have to use the `Encoder.en
139159
encoder.leave()
140160
encoder.leave()
141161
162+
or using the new context manager api:
163+
164+
.. code-block:: python
165+
166+
import asn1
167+
168+
with open('output.der', 'wb') as f:
169+
with asn1.Encoder(stream=f) as encoder:
170+
with encoder.sequence():
171+
encoder.write('test1', asn1.Numbers.PrintableString)
172+
encoder.write('test2', asn1.Numbers.PrintableString)
173+
with encoder.sequence():
174+
encoder.write(1, asn1.Numbers.Integer)
175+
encoder.write(0.125, asn1.Numbers.Real)
176+
encoder.write(b'\x01\x02\x03', asn1.Numbers.OctetString)
177+
142178
This also allows to encode data progressively, without having to keep everything in memory.
143179

144180
DER and CER
@@ -196,6 +232,15 @@ If you want to decode ASN.1 from BER (DER, CER, ...) encoded bytes, use code suc
196232
decoder.start(encoded_bytes)
197233
tag, value = decoder.read()
198234
235+
or using the new context manager api:
236+
237+
.. code-block:: python
238+
239+
import asn1
240+
241+
with asn1.Decoder(stream=encoded_bytes) as decoder:
242+
tag, value = decoder.read()
243+
199244
It is also possible to decode data directly from a file or any stream:
200245

201246
.. code-block:: python

examples/examples-cm-decoder.py

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# This file is part of Python-ASN1. Python-ASN1 is free software that is
4+
# made available under the MIT license. Consult the file "LICENSE" that
5+
# is distributed together with this file for the exact licensing terms.
6+
#
7+
# Python-ASN1 is copyright (c) 2007-2016 by the Python-ASN1 authors. See
8+
# the file "AUTHORS" for a complete overview.
9+
10+
from __future__ import absolute_import, division, print_function, unicode_literals
11+
12+
from builtins import open
13+
import asn1
14+
import pprint
15+
16+
17+
def example1():
18+
"""Decoding from a file."""
19+
print("Example 1")
20+
with open("example3.der", "rb") as f:
21+
with asn1.Decoder(stream=f) as decoder:
22+
tag, value = decoder.read()
23+
print(tag)
24+
print(value)
25+
print()
26+
27+
28+
def example2():
29+
"""Decoding of a bit string with unused bits."""
30+
print("Example 2")
31+
encoded = b"\x23\x0c\x03\x02\x00\x0b\x03\x02\x00\x0b\x03\x02\x04\x0f"
32+
with asn1.Decoder(stream=encoded) as decoder:
33+
tag, (val, unused) = decoder.read(asn1.ReadFlags.WithUnused)
34+
print("Tag:", tag)
35+
print("Value:", val)
36+
print("Unused bits:", unused)
37+
print()
38+
39+
40+
def example3():
41+
"""Decoding of sequences."""
42+
print("Example 3")
43+
encoded = b"\x30\x80\x13\x05\x74\x65\x73\x74\x31\x13\x05\x74\x65\x73\x74\x32\x30\x80\x02\x01\x01\x09\x03\x80\xfd\x01\x04\x03\x01\x02\x03\x00\x00\x00\x00"
44+
with asn1.Decoder(stream=encoded) as decoder:
45+
tag, value = decoder.read()
46+
print(tag)
47+
pprint.pprint(value)
48+
print()
49+
50+
51+
def example4():
52+
"""Decoding of a complex data."""
53+
print("Example 4")
54+
encoded = (
55+
b"\x30\x82\x04\x0e\x30\x82\x03\x77\xa0\x03\x02\x01\x02\x02\x02\x15"
56+
b"\x30\x30\x0d\x06\x09\x2a\x86\x48\x86\xf7\x0d\x01\x01\x05\x05\x00"
57+
b"\x30\x81\xbb\x31\x0b\x30\x09\x06\x03\x55\x04\x06\x13\x02\x2d\x2d"
58+
b"\x31\x12\x30\x10\x06\x03\x55\x04\x08\x13\x09\x53\x6f\x6d\x65\x53"
59+
b"\x74\x61\x74\x65\x31\x11\x30\x0f\x06\x03\x55\x04\x07\x13\x08\x53"
60+
b"\x6f\x6d\x65\x43\x69\x74\x79\x31\x19\x30\x17\x06\x03\x55\x04\x0a"
61+
b"\x13\x10\x53\x6f\x6d\x65\x4f\x72\x67\x61\x6e\x69\x7a\x61\x74\x69"
62+
b"\x6f\x6e\x31\x1f\x30\x1d\x06\x03\x55\x04\x0b\x13\x16\x53\x6f\x6d"
63+
b"\x65\x4f\x72\x67\x61\x6e\x69\x7a\x61\x74\x69\x6f\x6e\x61\x6c\x55"
64+
b"\x6e\x69\x74\x31\x1e\x30\x1c\x06\x03\x55\x04\x03\x13\x15\x6c\x6f"
65+
b"\x63\x61\x6c\x68\x6f\x73\x74\x2e\x6c\x6f\x63\x61\x6c\x64\x6f\x6d"
66+
b"\x61\x69\x6e\x31\x29\x30\x27\x06\x09\x2a\x86\x48\x86\xf7\x0d\x01"
67+
b"\x09\x01\x16\x1a\x72\x6f\x6f\x74\x40\x6c\x6f\x63\x61\x6c\x68\x6f"
68+
b"\x73\x74\x2e\x6c\x6f\x63\x61\x6c\x64\x6f\x6d\x61\x69\x6e\x30\x1e"
69+
b"\x17\x0d\x30\x38\x30\x32\x30\x35\x30\x39\x32\x33\x33\x31\x5a\x17"
70+
b"\x0d\x30\x39\x30\x32\x30\x34\x30\x39\x32\x33\x33\x31\x5a\x30\x81"
71+
b"\xbb\x31\x0b\x30\x09\x06\x03\x55\x04\x06\x13\x02\x2d\x2d\x31\x12"
72+
b"\x30\x10\x06\x03\x55\x04\x08\x13\x09\x53\x6f\x6d\x65\x53\x74\x61"
73+
b"\x74\x65\x31\x11\x30\x0f\x06\x03\x55\x04\x07\x13\x08\x53\x6f\x6d"
74+
b"\x65\x43\x69\x74\x79\x31\x19\x30\x17\x06\x03\x55\x04\x0a\x13\x10"
75+
b"\x53\x6f\x6d\x65\x4f\x72\x67\x61\x6e\x69\x7a\x61\x74\x69\x6f\x6e"
76+
b"\x31\x1f\x30\x1d\x06\x03\x55\x04\x0b\x13\x16\x53\x6f\x6d\x65\x4f"
77+
b"\x72\x67\x61\x6e\x69\x7a\x61\x74\x69\x6f\x6e\x61\x6c\x55\x6e\x69"
78+
b"\x74\x31\x1e\x30\x1c\x06\x03\x55\x04\x03\x13\x15\x6c\x6f\x63\x61"
79+
b"\x6c\x68\x6f\x73\x74\x2e\x6c\x6f\x63\x61\x6c\x64\x6f\x6d\x61\x69"
80+
b"\x6e\x31\x29\x30\x27\x06\x09\x2a\x86\x48\x86\xf7\x0d\x01\x09\x01"
81+
b"\x16\x1a\x72\x6f\x6f\x74\x40\x6c\x6f\x63\x61\x6c\x68\x6f\x73\x74"
82+
b"\x2e\x6c\x6f\x63\x61\x6c\x64\x6f\x6d\x61\x69\x6e\x30\x81\x9f\x30"
83+
b"\x0d\x06\x09\x2a\x86\x48\x86\xf7\x0d\x01\x01\x01\x05\x00\x03\x81"
84+
b"\x8d\x00\x30\x81\x89\x02\x81\x81\x00\xd5\x18\xcd\x40\x91\x90\x27"
85+
b"\x5a\x77\x37\x22\xca\xba\x05\xdf\x13\x31\xe8\x74\x43\x4f\x7e\x08"
86+
b"\xa3\xa5\x76\xcd\x7b\xdd\x37\xd0\x7f\x12\x9e\x81\x73\x87\x55\x66"
87+
b"\x0d\xda\x68\xee\x38\xeb\x34\xe2\xf4\xeb\x95\xd5\xe0\xde\xef\x08"
88+
b"\x57\xf9\x03\x14\x69\xa8\x6f\x7c\xa4\xfa\x64\x51\x39\x36\xd5\x09"
89+
b"\x37\x61\x83\x13\x8c\x41\x25\xba\x60\x91\x20\x86\x5b\x60\xb5\xe2"
90+
b"\x83\x65\x66\xad\x06\xb3\x45\x71\x83\x67\xd2\xe5\x5f\x40\x42\x4b"
91+
b"\x37\xf8\x87\xd0\x09\x49\xb8\xad\x34\x76\xa3\x1b\xbf\xc1\x0f\xb7"
92+
b"\xfb\x43\xbe\x62\x33\x02\x02\x10\x61\x02\x03\x01\x00\x01\xa3\x82"
93+
b"\x01\x1d\x30\x82\x01\x19\x30\x1d\x06\x03\x55\x1d\x0e\x04\x16\x04"
94+
b"\x14\x0a\x4b\xfa\x87\x54\x17\x7e\x30\xb4\x21\x71\x56\x51\x0f\xd2"
95+
b"\x91\xc3\x30\x02\x36\x30\x81\xe9\x06\x03\x55\x1d\x23\x04\x81\xe1"
96+
b"\x30\x81\xde\x80\x14\x0a\x4b\xfa\x87\x54\x17\x7e\x30\xb4\x21\x71"
97+
b"\x56\x51\x0f\xd2\x91\xc3\x30\x02\x36\xa1\x81\xc1\xa4\x81\xbe\x30"
98+
b"\x81\xbb\x31\x0b\x30\x09\x06\x03\x55\x04\x06\x13\x02\x2d\x2d\x31"
99+
b"\x12\x30\x10\x06\x03\x55\x04\x08\x13\x09\x53\x6f\x6d\x65\x53\x74"
100+
b"\x61\x74\x65\x31\x11\x30\x0f\x06\x03\x55\x04\x07\x13\x08\x53\x6f"
101+
b"\x6d\x65\x43\x69\x74\x79\x31\x19\x30\x17\x06\x03\x55\x04\x0a\x13"
102+
b"\x10\x53\x6f\x6d\x65\x4f\x72\x67\x61\x6e\x69\x7a\x61\x74\x69\x6f"
103+
b"\x6e\x31\x1f\x30\x1d\x06\x03\x55\x04\x0b\x13\x16\x53\x6f\x6d\x65"
104+
b"\x4f\x72\x67\x61\x6e\x69\x7a\x61\x74\x69\x6f\x6e\x61\x6c\x55\x6e"
105+
b"\x69\x74\x31\x1e\x30\x1c\x06\x03\x55\x04\x03\x13\x15\x6c\x6f\x63"
106+
b"\x61\x6c\x68\x6f\x73\x74\x2e\x6c\x6f\x63\x61\x6c\x64\x6f\x6d\x61"
107+
b"\x69\x6e\x31\x29\x30\x27\x06\x09\x2a\x86\x48\x86\xf7\x0d\x01\x09"
108+
b"\x01\x16\x1a\x72\x6f\x6f\x74\x40\x6c\x6f\x63\x61\x6c\x68\x6f\x73"
109+
b"\x74\x2e\x6c\x6f\x63\x61\x6c\x64\x6f\x6d\x61\x69\x6e\x82\x02\x15"
110+
b"\x30\x30\x0c\x06\x03\x55\x1d\x13\x04\x05\x30\x03\x01\x01\xff\x30"
111+
b"\x0d\x06\x09\x2a\x86\x48\x86\xf7\x0d\x01\x01\x05\x05\x00\x03\x81"
112+
b"\x81\x00\x4e\x12\x46\x58\xa3\x57\xc5\x9a\xab\xfa\x32\xf5\xde\x87"
113+
b"\xfb\x77\xa8\x79\x38\x1d\x4f\xd3\x7c\x3a\x16\x60\x82\x7d\x92\xa1"
114+
b"\x58\xd2\x53\x7b\x11\x90\xec\x6d\xb0\xb0\x58\xee\x33\xb4\x7b\x1d"
115+
b"\xb8\x95\xd8\x98\xc3\x10\x81\x83\x08\x46\xe8\x9a\xb9\x6c\xbf\x8f"
116+
b"\x9e\x73\xf7\x61\x89\xc4\x6a\x1b\xc1\x98\xc6\xab\xfc\x91\xb6\x59"
117+
b"\xb8\xa5\x05\x91\x2a\xbb\xc4\x30\x16\x53\xbf\x1a\xfe\x2f\x01\x25"
118+
b"\xae\xef\xc7\xb9\xfa\xa5\x53\xf8\xd9\xf5\x8f\xae\x91\xea\x57\x28"
119+
b"\xfa\xdf\x34\x03\x29\xe8\x97\xee\x2e\x9e\x8a\x62\x45\xc7\xfc\x58"
120+
b"\xb4\x5a"
121+
)
122+
123+
with asn1.Decoder(stream=encoded) as decoder:
124+
tag, value = decoder.read()
125+
print(tag)
126+
pprint.pprint(value)
127+
print()
128+
129+
130+
example1()
131+
example2()
132+
example3()
133+
example4()

examples/examples-cm-encoder.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# This file is part of Python-ASN1. Python-ASN1 is free software that is
4+
# made available under the MIT license. Consult the file "LICENSE" that
5+
# is distributed together with this file for the exact licensing terms.
6+
#
7+
# Python-ASN1 is copyright (c) 2007-2016 by the Python-ASN1 authors. See
8+
# the file "AUTHORS" for a complete overview.
9+
10+
from __future__ import absolute_import, division, print_function, unicode_literals
11+
12+
from builtins import open, str
13+
import asn1
14+
import binascii
15+
import pprint
16+
17+
18+
def example1():
19+
"""Encoding an object identifier."""
20+
print("Example 1")
21+
with asn1.Encoder() as encoder:
22+
encoder.write("1.2.3", asn1.Numbers.ObjectIdentifier)
23+
print(str(binascii.hexlify(encoder.output(), " ", 1).upper(), encoding="ascii"))
24+
print()
25+
26+
27+
def example2():
28+
"""Encoding an object identifier directly to a file."""
29+
print("Example 2")
30+
with open("example2.der", "wb") as f:
31+
with asn1.Encoder(
32+
stream=f, encoding=asn1.Encoding.DER
33+
) as encoder: # CER is the default when using a stream
34+
encoder.write("1.2.3", asn1.Numbers.ObjectIdentifier)
35+
36+
37+
def example3():
38+
"""Encoding of complex data."""
39+
print("Example 3")
40+
with open("example3.der", "wb") as f:
41+
with asn1.Encoder(
42+
stream=f, encoding=asn1.Encoding.DER
43+
) as encoder: # CER is the default when using a stream
44+
encoder.write(["test1", "test2", [1, 0.125, b"\x01\x02\x03"]])
45+
print()
46+
47+
48+
def example4():
49+
"""Encoding of sequences."""
50+
print("Example 4")
51+
with asn1.Encoder() as encoder:
52+
with encoder.sequence():
53+
encoder.write("test1", asn1.Numbers.PrintableString)
54+
encoder.write("test2", asn1.Numbers.PrintableString)
55+
with encoder.sequence():
56+
encoder.write(1, asn1.Numbers.Integer)
57+
encoder.write(0.125, asn1.Numbers.Real)
58+
encoder.write(b"\x01\x02\x03", asn1.Numbers.OctetString)
59+
print(str(binascii.hexlify(encoder.output(), " ", 1).upper(), encoding="ascii"))
60+
print()
61+
62+
63+
def example5():
64+
"""Using CER encoding with a stream (file)."""
65+
print("Example 5")
66+
with open("example6.cer", "wb") as f:
67+
with asn1.Encoder(stream=f) as encoder:
68+
encoder.write("1.2.3", asn1.Numbers.ObjectIdentifier)
69+
70+
71+
def example6():
72+
"""Using DER encoding with a stream (file)."""
73+
print("Example 6")
74+
with open("example7.der", "wb") as f:
75+
with asn1.Encoder(stream=f, encoding=asn1.Encoding.DER) as encoder:
76+
encoder.write("1.2.3", asn1.Numbers.ObjectIdentifier)
77+
78+
79+
example1()
80+
example2()
81+
example3()
82+
example4()
83+
example5()
84+
example6()

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def read(*names, **kwargs):
2929

3030
setup(
3131
name='asn1',
32-
version='3.1.0',
32+
version='3.1.1',
3333
license='BSD',
3434
description='Python-ASN1 is a simple ASN.1 encoder and decoder for Python 2.7+ and 3.5+.',
3535
long_description='%s\n%s' % (

0 commit comments

Comments
 (0)