Skip to content

Comments

Context manager#295

Merged
andrivet merged 12 commits intoandrivet:masterfrom
fsantovito:context-manager
Feb 6, 2026
Merged

Context manager#295
andrivet merged 12 commits intoandrivet:masterfrom
fsantovito:context-manager

Conversation

@fsantovito
Copy link

This PR introduces a new context-manager based API for both Encoder and Decoder, aimed at improving readability and usability while remaining fully backward compatible with the existing API.

The new API allows encoding and decoding ASN.1 structures using Python with blocks (e.g. with encoder.sequence():), without breaking or modifying the current behavior.

Added context manager support to Encoder and Decoder
Introduced helper methods (Encoder.sequence() and Encoder.set()) to replace explicit enter() / leave() calls
Existing code using the old API continues to work unchanged
New tests demonstrate equivalence between the legacy API and the context-manager–based API

All existing and new tests are passing
I was only able to test this change on Python 3.13, as this is the only version available in my environment

The goal is to make complex ASN.1 structures easier to read, write, and maintain, while preserving full compatibility with current users of the library.

with asn1.Encoder() as encoder:
    with encoder.sequence():
        encoder.write('test1', asn1.Numbers.PrintableString)
        encoder.write('test2', asn1.Numbers.PrintableString)

        with encoder.sequence():
            encoder.write(1, asn1.Numbers.Integer)
            encoder.write(0.125, asn1.Numbers.Real)
            encoder.write(b'\x01\x02\x03', asn1.Numbers.OctetString)

Feedback is very welcome

@andrivet
Copy link
Owner

andrivet commented Feb 2, 2026

What a nice PR! Thanks. I will need some time to review it and test on old versions of Python to be sure that everything is OK and compatible.

@andrivet
Copy link
Owner

andrivet commented Feb 2, 2026

I look briefly at the tox build and the code, as it is, is not compatible with Python 2.7. In particular, there is this error:

ImportError: cannot import name _GeneratorContextManager

This (internal) class does not exist for Python 2.7.

There are other issues (formatting, ...) but they are easier to fix.

@fsantovito
Copy link
Author

I fixed the bug in Python 2.7 and fixed some minor issues reported in tox. There's a problem with Python 3.9, and I temporarily disabled it, but I can't easily fix it. Can you try?

@andrivet
Copy link
Owner

andrivet commented Feb 6, 2026

I will look at Python 3.9. A note: since it is a new feature and not a bug fix, the new version will be "3.2.0", not "3.1.1" (semver). I can take care of it.

@andrivet andrivet merged commit fd4c417 into andrivet:master Feb 6, 2026
1 check passed
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

Successfully merging this pull request may close these issues.

2 participants