]> Cypherpunks.ru repositories - pyderasn.git/blobdiff - pyderasn.py
environ import is used only in __main__
[pyderasn.git] / pyderasn.py
index fcaa5a6cdcce2e4057305b5cc37f0c9ffa164913..6da6e92706e2186339505e65c7743bc64f14ce89 100755 (executable)
@@ -75,9 +75,17 @@ tags simultaneously.
 There are :py:func:`pyderasn.tag_ctxp` and :py:func:`pyderasn.tag_ctxc`
 functions, allowing you to easily create ``CONTEXT``
 ``PRIMITIVE``/``CONSTRUCTED`` tags, by specifying only the required tag
-number. Pay attention that explicit tags always have *constructed* tag
-(``tag_ctxc``), but implicit tags for primitive types are primitive
-(``tag_ctxp``).
+number.
+
+.. note::
+
+   EXPLICIT tags always have **constructed** tag. PyDERASN does not
+   explicitly check correctness of schema input here.
+
+.. note::
+
+   Implicit tags have **primitive** (``tag_ctxp``) encoding for
+   primitive values.
 
 ::
 
@@ -663,7 +671,6 @@ from copy import copy
 from datetime import datetime
 from datetime import timedelta
 from math import ceil
-from os import environ
 from string import ascii_letters
 from string import digits
 from sys import version_info
@@ -5419,19 +5426,17 @@ class Sequence(Obj):
             return spec.default
         return None
 
-    def _encoded_values(self):
-        raws = []
+    def _values_for_encoding(self):
         for name, spec in iteritems(self.specs):
             value = self._value.get(name)
             if value is None:
                 if spec.optional:
                     continue
                 raise ObjNotReady(name)
-            raws.append(value.encode())
-        return raws
+            yield value
 
     def _encode(self):
-        v = b"".join(self._encoded_values())
+        v = b"".join(v.encode() for v in self._values_for_encoding())
         return b"".join((self.tag, len_encode(len(v)), v))
 
     def _decode(self, tlv, offset, decode_path, ctx, tag_only):
@@ -5666,8 +5671,8 @@ class Set(Sequence):
     .. _allow_unordered_set_ctx:
 
     DER prohibits unordered values encoding and will raise an error
-    during decode. If If :ref:`bered <bered_ctx>` context option is set,
-    then no error will occure. Also you can disable strict values
+    during decode. If :ref:`bered <bered_ctx>` context option is set,
+    then no error will occur. Also you can disable strict values
     ordering check by setting ``"allow_unordered_set": True``
     :ref:`context <ctx>` option.
     """
@@ -5676,7 +5681,7 @@ class Set(Sequence):
     asn1_type_name = "SET"
 
     def _encode(self):
-        raws = self._encoded_values()
+        raws = [v.encode() for v in self._values_for_encoding()]
         raws.sort()
         v = b"".join(raws)
         return b"".join((self.tag, len_encode(len(v)), v))
@@ -6043,11 +6048,11 @@ class SequenceOf(Obj):
     def __getitem__(self, key):
         return self._value[key]
 
-    def _encoded_values(self):
-        return [v.encode() for v in self._value]
+    def _values_for_encoding(self):
+        return iter(self._value)
 
     def _encode(self):
-        v = b"".join(self._encoded_values())
+        v = b"".join(v.encode() for v in self._values_for_encoding())
         return b"".join((self.tag, len_encode(len(v)), v))
 
     def _decode(self, tlv, offset, decode_path, ctx, tag_only, ordering_check=False):
@@ -6210,7 +6215,7 @@ class SetOf(SequenceOf):
     asn1_type_name = "SET OF"
 
     def _encode(self):
-        raws = self._encoded_values()
+        raws = [v.encode() for v in self._values_for_encoding()]
         raws.sort()
         v = b"".join(raws)
         return b"".join((self.tag, len_encode(len(v)), v))
@@ -6368,6 +6373,7 @@ def main():  # pragma: no cover
     if args.defines_by_path is not None:
         ctx["defines_by_path"] = obj_by_path(args.defines_by_path)
     obj, tail = schema().decode(der, ctx=ctx)
+    from os import environ
     print(pprinter(
         obj,
         oid_maps=oid_maps,