From: Sergey Matveev Date: Thu, 6 Feb 2020 10:59:02 +0000 (+0300) Subject: PrintableString'es .allow_asterisk and .allow_ampersand properties X-Git-Tag: 6.0~4 X-Git-Url: http://www.git.cypherpunks.ru/?p=pyderasn.git;a=commitdiff_plain;h=317317fc2ecfd5fa2305f31701c97be5fbe91d4c PrintableString'es .allow_asterisk and .allow_ampersand properties --- diff --git a/doc/news.rst b/doc/news.rst index fe8137e..60247ba 100644 --- a/doc/news.rst +++ b/doc/news.rst @@ -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: diff --git a/pyderasn.py b/pyderasn.py index abb5be5..8ae9d6e 100755 --- a/pyderasn.py +++ b/pyderasn.py @@ -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, ) diff --git a/tests/test_pyderasn.py b/tests/test_pyderasn.py index a39d125..010b17f 100644 --- a/tests/test_pyderasn.py +++ b/tests/test_pyderasn.py @@ -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(