From: Sergey Matveev Date: Fri, 4 Jun 2021 15:40:37 +0000 (+0300) Subject: Add proper __slots__ for mixin classes X-Git-Tag: 9.0~1 X-Git-Url: http://www.git.cypherpunks.ru/?p=pyderasn.git;a=commitdiff_plain;h=6e7c396eadd0193a18870650b6c23abbacea1591 Add proper __slots__ for mixin classes --- diff --git a/pyderasn.py b/pyderasn.py index 4a27bbe..4a36927 100755 --- a/pyderasn.py +++ b/pyderasn.py @@ -4784,6 +4784,8 @@ class UTF8String(CommonString): class AllowableCharsMixin: + __slots__ = () + @property def allowable_chars(self): return frozenset(chr(c) for c in self._allowable_chars) @@ -4795,6 +4797,9 @@ class AllowableCharsMixin: return value +NUMERIC_ALLOWABLE_CHARS = frozenset(digits.encode("ascii") + b" ") + + class NumericString(AllowableCharsMixin, CommonString): """Numeric string @@ -4804,11 +4809,14 @@ class NumericString(AllowableCharsMixin, CommonString): >>> NumericString().allowable_chars frozenset(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ' ']) """ - __slots__ = () + __slots__ = ("_allowable_chars",) tag_default = tag_encode(18) encoding = "ascii" asn1_type_name = "NumericString" - _allowable_chars = frozenset(digits.encode("ascii") + b" ") + + def __init__(self, *args, **kwargs): + self._allowable_chars = NUMERIC_ALLOWABLE_CHARS + super().__init__(*args, **kwargs) PrintableStringState = namedtuple( @@ -4818,6 +4826,11 @@ PrintableStringState = namedtuple( ) +PRINTABLE_ALLOWABLE_CHARS = frozenset( + (ascii_letters + digits + " '()+,-./:=?").encode("ascii") +) + + class PrintableString(AllowableCharsMixin, CommonString): """Printable string @@ -4830,13 +4843,10 @@ class PrintableString(AllowableCharsMixin, CommonString): >>> obj.allow_asterisk, obj.allow_ampersand (True, False) """ - __slots__ = () + __slots__ = ("_allowable_chars",) tag_default = tag_encode(19) encoding = "ascii" asn1_type_name = "PrintableString" - _allowable_chars = frozenset( - (ascii_letters + digits + " '()+,-./:=?").encode("ascii") - ) _asterisk = frozenset("*".encode("ascii")) _ampersand = frozenset("&".encode("ascii")) @@ -4857,11 +4867,13 @@ class PrintableString(AllowableCharsMixin, CommonString): :param allow_asterisk: allow asterisk character :param allow_ampersand: allow ampersand character """ + allowable_chars = PRINTABLE_ALLOWABLE_CHARS if allow_asterisk: - self._allowable_chars |= self._asterisk + allowable_chars |= self._asterisk if allow_ampersand: - self._allowable_chars |= self._ampersand - super(PrintableString, self).__init__( + allowable_chars |= self._ampersand + self._allowable_chars = allowable_chars + super().__init__( value, bounds, impl, expl, default, optional, _decoded, ctx, ) @@ -4930,6 +4942,11 @@ class VideotexString(CommonString): asn1_type_name = "VideotexString" +IA5_ALLOWABLE_CHARS = frozenset(b"".join( + chr(c).encode("ascii") for c in range(128) +)) + + class IA5String(AllowableCharsMixin, CommonString): """IA5 string @@ -4944,13 +4961,14 @@ class IA5String(AllowableCharsMixin, CommonString): >>> IA5String().allowable_chars frozenset(["NUL", ... "DEL"]) """ - __slots__ = () + __slots__ = ("_allowable_chars",) tag_default = tag_encode(22) encoding = "ascii" asn1_type_name = "IA5" - _allowable_chars = frozenset(b"".join( - chr(c).encode("ascii") for c in range(128) - )) + + def __init__(self, *args, **kwargs): + self._allowable_chars = IA5_ALLOWABLE_CHARS + super().__init__(*args, **kwargs) LEN_YYMMDDHHMMSSZ = len("YYMMDDHHMMSSZ") @@ -4961,6 +4979,11 @@ LEN_YYYYMMDDHHMMSSZ = len("YYYYMMDDHHMMSSZ") LEN_LEN_YYYYMMDDHHMMSSZ = len_encode(LEN_YYYYMMDDHHMMSSZ) +VISIBLE_ALLOWABLE_CHARS = frozenset(b"".join( + chr(c).encode("ascii") for c in range(ord(" "), ord("~") + 1) +)) + + class VisibleString(AllowableCharsMixin, CommonString): """Visible string @@ -4970,13 +4993,14 @@ class VisibleString(AllowableCharsMixin, CommonString): >>> VisibleString().allowable_chars frozenset([" ", ... "~"]) """ - __slots__ = () + __slots__ = ("_allowable_chars",) tag_default = tag_encode(26) encoding = "ascii" asn1_type_name = "VisibleString" - _allowable_chars = frozenset(b"".join( - chr(c).encode("ascii") for c in range(ord(" "), ord("~") + 1) - )) + + def __init__(self, *args, **kwargs): + self._allowable_chars = VISIBLE_ALLOWABLE_CHARS + super().__init__(*args, **kwargs) class ISO646String(VisibleString): @@ -6084,6 +6108,8 @@ SequenceState = namedtuple( class SequenceEncode1stMixing: + __slots__ = () + def _encode1st(self, state): state.append(0) idx = len(state) - 1 diff --git a/tests/test_pyderasn.py b/tests/test_pyderasn.py index 4edf060..f48548e 100644 --- a/tests/test_pyderasn.py +++ b/tests/test_pyderasn.py @@ -122,6 +122,7 @@ from pyderasn import UTCTime from pyderasn import UTF8String from pyderasn import VideotexString from pyderasn import VisibleString +import pyderasn max_examples = environ.get("MAX_EXAMPLES") @@ -7369,10 +7370,10 @@ class TestGoMarshalVectors(TestCase): seq["erste"] = PrintableString("test") self.assertSequenceEqual(seq.encode(), hexdec("3006130474657374")) # Asterisk is actually not allowable - PrintableString._allowable_chars |= set(b"*") + pyderasn.PRINTABLE_ALLOWABLE_CHARS |= set(b"*") seq["erste"] = PrintableString("test*") self.assertSequenceEqual(seq.encode(), hexdec("30071305746573742a")) - PrintableString._allowable_chars -= set(b"*") + pyderasn.PRINTABLE_ALLOWABLE_CHARS -= set(b"*") class Seq(Sequence): schema = (