]> Cypherpunks.ru repositories - pyderasn.git/commitdiff
PrintableString'es .allow_asterisk and .allow_ampersand properties
authorSergey Matveev <stargrave@stargrave.org>
Thu, 6 Feb 2020 10:59:02 +0000 (13:59 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Thu, 6 Feb 2020 13:36:51 +0000 (16:36 +0300)
doc/news.rst
pyderasn.py
tests/test_pyderasn.py

index fe8137e877f2939c156b7024ea5d6d93d6e2cf2c..60247ba70ee523a52622dc3150766a95213b9975 100644 (file)
@@ -10,6 +10,8 @@ News
 * Copies made previously with ``.copy()`` lacked ``.defined`` field,
   now they are not
 * All objects are friendly to ``pickle`` libraries
+* ``PrintableString`` has ``allow_asterisk`` and ``allow_ampersand``
+  property
 
 .. _release5.6:
 
index abb5be5fd83f8482d9268295c833a1f5ee3230bb..8ae9d6e27b8ced4f8de733da94cae4e09b31cfbc 100755 (executable)
@@ -3991,6 +3991,10 @@ class PrintableString(AllowableCharsMixin, CommonString):
 
     >>> PrintableString().allowable_chars
     frozenset([' ', "'", ..., 'z'])
+    >>> obj = PrintableString("foo*bar", allow_asterisk=True)
+    PrintableString PrintableString foo*bar
+    >>> obj.allow_asterisk, obj.allow_ampersand
+    (True, False)
     """
     __slots__ = ()
     tag_default = tag_encode(19)
@@ -4026,6 +4030,18 @@ class PrintableString(AllowableCharsMixin, CommonString):
             value, bounds, impl, expl, default, optional, _decoded,
         )
 
+    @property
+    def allow_asterisk(self):
+        """Is asterisk character allowed?
+        """
+        return self._asterisk <= self._allowable_chars
+
+    @property
+    def allow_ampersand(self):
+        """Is ampersand character allowed?
+        """
+        return self._ampersand <= self._allowable_chars
+
     def _value_sanitize(self, value):
         value = super(PrintableString, self)._value_sanitize(value)
         if not frozenset(value) <= self._allowable_chars:
@@ -4061,8 +4077,8 @@ class PrintableString(AllowableCharsMixin, CommonString):
             expl=self._expl if expl is None else expl,
             default=self.default if default is None else default,
             optional=self.optional if optional is None else optional,
-            allow_asterisk=self._asterisk <= self._allowable_chars,
-            allow_ampersand=self._ampersand <= self._allowable_chars,
+            allow_asterisk=self.allow_asterisk,
+            allow_ampersand=self.allow_ampersand,
         )
 
 
index a39d125ee036d8c12259732254a11543aec88c15..010b17f7244dad285235ecfd7dbf88482efa0c5a 100644 (file)
@@ -3540,14 +3540,22 @@ class TestPrintableString(
                 ("&", {"allow_ampersand": True}),
                 ("&*", {"allow_asterisk": True, "allow_ampersand": True}),
         ):
-            s = "hello invalid " + c
+            s = "hello invalid"
+            obj = self.base_klass(s)
+            for prop in kwargs.keys():
+                self.assertFalse(getattr(obj, prop))
+            s += c
             with assertRaisesRegex(self, DecodeError, "non-printable"):
                 self.base_klass(s)
             self.base_klass(s, **kwargs)
             klass = self.base_klass(**kwargs)
             obj = klass(s)
+            for prop in kwargs.keys():
+                self.assertTrue(getattr(obj, prop))
             obj = copy(obj)
             obj(s)
+            for prop in kwargs.keys():
+                self.assertTrue(getattr(obj, prop))
 
 
 class TestTeletexString(