Skip to content

Commit

Permalink
gh-12: Allow passing of tagMap and typeMap args (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
tiran committed Aug 23, 2022
1 parent 4ca2d86 commit 4176c0a
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 14 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Expand Up @@ -5,6 +5,9 @@ Revision 0.5.0.rc2, released ??-??-2022
- Re-add ``tagMap`` and ``typeMap`` module level attributes to all
encoder and decoder modules. They are aliases for ``TAG_MAP`` and
``TYPE_MAP``, [issue #9](https://github.com/pyasn1/pyasn1/issues/9).
- Restore API for passing for ``tagMap`` and ``typeMap`` arguments
to ``Encoder`` and ``Decoder`` classes by name and position,
[issue #12](https://github.com/pyasn1/pyasn1/issues/12).

Revision 0.5.0.rc1, released 08-08-2022
---------------------------------------
Expand Down
7 changes: 4 additions & 3 deletions pyasn1/codec/ber/decoder.py
Expand Up @@ -13,6 +13,7 @@
from pyasn1.codec.streaming import isEndOfStream
from pyasn1.codec.streaming import peekIntoStream
from pyasn1.codec.streaming import readFromStream
from pyasn1.compat import _MISSING
from pyasn1.compat.integer import from_bytes
from pyasn1.compat.octets import oct2int, octs2ints, ints2octs, null
from pyasn1.error import PyAsn1Error
Expand Down Expand Up @@ -1487,9 +1488,9 @@ class SingleItemDecoder(object):
TAG_MAP = TAG_MAP
TYPE_MAP = TYPE_MAP

def __init__(self, **options):
self._tagMap = options.get('tagMap', self.TAG_MAP)
self._typeMap = options.get('typeMap', self.TYPE_MAP)
def __init__(self, tagMap=_MISSING, typeMap=_MISSING, **ignored):
self._tagMap = tagMap if tagMap is not _MISSING else self.TAG_MAP
self._typeMap = typeMap if typeMap is not _MISSING else self.TYPE_MAP

# Tag & TagSet objects caches
self._tagCache = {}
Expand Down
13 changes: 8 additions & 5 deletions pyasn1/codec/ber/encoder.py
Expand Up @@ -9,6 +9,7 @@
from pyasn1 import debug
from pyasn1 import error
from pyasn1.codec.ber import eoo
from pyasn1.compat import _MISSING
from pyasn1.compat.integer import to_bytes
from pyasn1.compat.octets import (int2oct, oct2int, ints2octs, null,
str2octs, isOctetsType)
Expand Down Expand Up @@ -785,9 +786,9 @@ class SingleItemEncoder(object):
TAG_MAP = TAG_MAP
TYPE_MAP = TYPE_MAP

def __init__(self, **options):
self._tagMap = options.get('tagMap', self.TAG_MAP)
self._typeMap = options.get('typeMap', self.TYPE_MAP)
def __init__(self, tagMap=_MISSING, typeMap=_MISSING, **ignored):
self._tagMap = tagMap if tagMap is not _MISSING else self.TAG_MAP
self._typeMap = typeMap if typeMap is not _MISSING else self.TYPE_MAP

def __call__(self, value, asn1Spec=None, **options):
try:
Expand Down Expand Up @@ -852,8 +853,10 @@ def __call__(self, value, asn1Spec=None, **options):
class Encoder(object):
SINGLE_ITEM_ENCODER = SingleItemEncoder

def __init__(self, **options):
self._singleItemEncoder = self.SINGLE_ITEM_ENCODER(**options)
def __init__(self, tagMap=_MISSING, typeMap=_MISSING, **options):
self._singleItemEncoder = self.SINGLE_ITEM_ENCODER(
tagMap=tagMap, typeMap=typeMap, **options
)

def __call__(self, pyObject, asn1Spec=None, **options):
return self._singleItemEncoder(
Expand Down
7 changes: 4 additions & 3 deletions pyasn1/codec/native/decoder.py
Expand Up @@ -6,6 +6,7 @@
#
from pyasn1 import debug
from pyasn1 import error
from pyasn1.compat import _MISSING
from pyasn1.type import base
from pyasn1.type import char
from pyasn1.type import tag
Expand Down Expand Up @@ -139,9 +140,9 @@ class SingleItemDecoder(object):
TAG_MAP = TAG_MAP
TYPE_MAP = TYPE_MAP

def __init__(self, **options):
self._tagMap = options.get('tagMap', self.TAG_MAP)
self._typeMap = options.get('typeMap', self.TYPE_MAP)
def __init__(self, tagMap=_MISSING, typeMap=_MISSING, **ignored):
self._tagMap = tagMap if tagMap is not _MISSING else self.TAG_MAP
self._typeMap = typeMap if typeMap is not _MISSING else self.TYPE_MAP

def __call__(self, pyObject, asn1Spec, **options):

Expand Down
7 changes: 4 additions & 3 deletions pyasn1/codec/native/encoder.py
Expand Up @@ -8,6 +8,7 @@

from pyasn1 import debug
from pyasn1 import error
from pyasn1.compat import _MISSING
from pyasn1.type import base
from pyasn1.type import char
from pyasn1.type import tag
Expand Down Expand Up @@ -180,9 +181,9 @@ class SingleItemEncoder(object):
TAG_MAP = TAG_MAP
TYPE_MAP = TYPE_MAP

def __init__(self, **options):
self._tagMap = options.get('tagMap', self.TAG_MAP)
self._typeMap = options.get('typeMap', self.TYPE_MAP)
def __init__(self, tagMap=_MISSING, typeMap=_MISSING, **ignored):
self._tagMap = tagMap if tagMap is not _MISSING else self.TAG_MAP
self._typeMap = typeMap if typeMap is not _MISSING else self.TYPE_MAP

def __call__(self, value, **options):
if not isinstance(value, base.Asn1Item):
Expand Down
3 changes: 3 additions & 0 deletions pyasn1/compat/__init__.py
@@ -1 +1,4 @@
# This file is necessary to make this directory a package.

# sentinal for missing argument
_MISSING = object()
20 changes: 20 additions & 0 deletions tests/codec/der/test_encoder.py
Expand Up @@ -639,6 +639,26 @@ def testInitializedDefaultOctetStringIsNotEncoded(self):
assert encoder.encode(self.s) == ints2octs((48, 0))


class ClassConstructorTestCase(BaseTestCase):
def testKeywords(self):
tagmap = {"tagmap": True}
typemap = {"typemap": True}

sie = encoder.Encoder()._singleItemEncoder
self.assertIs(sie._tagMap, encoder.TAG_MAP)
self.assertIs(sie._typeMap, encoder.TYPE_MAP)

sie = encoder.Encoder(
tagMap=tagmap, typeMap=typemap
)._singleItemEncoder
self.assertIs(sie._tagMap, tagmap)
self.assertIs(sie._typeMap, typemap)

sie = encoder.Encoder(tagmap, typemap)._singleItemEncoder
self.assertIs(sie._tagMap, tagmap)
self.assertIs(sie._typeMap, typemap)


suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])

if __name__ == '__main__':
Expand Down

0 comments on commit 4176c0a

Please sign in to comment.