Skip to content

Commit 6c5251c

Browse files
committed
Some formatting
1 parent 17232e2 commit 6c5251c

File tree

4 files changed

+82
-37
lines changed

4 files changed

+82
-37
lines changed

.readthedocs.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
version: 2
22

33
python:
4-
version: "3.8"
54
install:
65
- requirements: requirements-dev.txt

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2007-2021 the Python-ASN1 authors.
1+
Copyright (c) 2007-2025 the Python-ASN1 authors.
22

33
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
44

src/asn1.py

+80-34
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# made available under the MIT license. Consult the file "LICENSE" that is
55
# distributed together with this file for the exact licensing terms.
66
#
7-
# Python-ASN1 is copyright (c) 2007-2016 by the Python-ASN1 authors. See the
7+
# Python-ASN1 is copyright (c) 2007-2025 by the Python-ASN1 authors. See the
88
# file "AUTHORS" for a complete overview.
99

1010
"""
@@ -24,7 +24,6 @@
2424
from builtins import str
2525
from contextlib import contextmanager
2626
from enum import IntEnum
27-
from numbers import Number
2827

2928
__version__ = "2.7.1"
3029

@@ -60,31 +59,40 @@ class Classes(IntEnum):
6059

6160

6261
Tag = collections.namedtuple('Tag', 'nr typ cls')
63-
"""A named tuple to represent ASN.1 tags as returned by `Decoder.peek()` and
64-
`Decoder.read()`."""
62+
"""
63+
A named tuple to represent ASN.1 tags as returned by `Decoder.peek()` and
64+
`Decoder.read()`.
65+
"""
6566

6667

6768
class Error(Exception):
68-
"""ASN.11 encoding or decoding error."""
69+
"""
70+
ASN.11 encoding or decoding error.
71+
"""
6972

7073

7174
class Encoder(object):
72-
"""ASN.1 encoder. Uses DER encoding.
75+
"""
76+
ASN.1 encoder. Uses DER encoding.
7377
"""
7478

7579
def __init__(self): # type: () -> None
76-
"""Constructor."""
80+
"""
81+
Constructor.
82+
"""
7783
self.m_stack = None
7884

7985
def start(self): # type: () -> None
80-
"""This method instructs the encoder to start encoding a new ASN.1
86+
"""
87+
This method instructs the encoder to start encoding a new ASN.1
8188
output. This method may be called at any time to reset the encoder,
8289
and resets the current output (if any).
8390
"""
8491
self.m_stack = [[]]
8592

8693
def enter(self, nr, cls=None): # type: (int, int) -> None
87-
"""This method starts the construction of a constructed type.
94+
"""
95+
This method starts the construction of a constructed type.
8896
8997
Args:
9098
nr (int): The desired ASN.1 type. Use ``Numbers`` enumeration.
@@ -107,7 +115,8 @@ def enter(self, nr, cls=None): # type: (int, int) -> None
107115
self.m_stack.append([])
108116

109117
def leave(self): # type: () -> None
110-
"""This method completes the construction of a constructed type and
118+
"""
119+
This method completes the construction of a constructed type and
111120
writes the encoded representation to the output buffer.
112121
"""
113122
if self.m_stack is None:
@@ -121,7 +130,8 @@ def leave(self): # type: () -> None
121130

122131
@contextmanager
123132
def construct(self, nr, cls=None): # type: (int, int) -> None
124-
"""This method - context manager calls enter and leave methods,
133+
"""
134+
This method - context manager calls enter and leave methods,
125135
for better code mapping.
126136
127137
Usage:
@@ -160,7 +170,8 @@ def construct(self, nr, cls=None): # type: (int, int) -> None
160170
self.leave()
161171

162172
def write(self, value, nr=None, typ=None, cls=None): # type: (object, int, int, int) -> None
163-
"""This method encodes one ASN.1 tag and writes it to the output buffer.
173+
"""
174+
This method encodes one ASN.1 tag and writes it to the output buffer.
164175
165176
Note:
166177
Normally, ``value`` will be the only parameter to this method.
@@ -224,7 +235,8 @@ def write(self, value, nr=None, typ=None, cls=None): # type: (object, int, int,
224235
self._emit(value)
225236

226237
def output(self): # type: () -> bytes
227-
"""This method returns the encoded ASN.1 data as plain Python ``bytes``.
238+
"""
239+
This method returns the encoded ASN.1 data as plain Python ``bytes``.
228240
This method can be called multiple times, also during encoding.
229241
In the latter case the data that has been encoded so far is
230242
returned.
@@ -403,15 +415,18 @@ def _encode_object_identifier(self, oid): # type: (str) -> bytes
403415

404416

405417
class Decoder(object):
406-
"""ASN.1 decoder. Understands BER (and DER which is a subset)."""
418+
"""
419+
ASN.1 decoder. Understands BER (and DER which is a subset).
420+
"""
407421

408422
def __init__(self): # type: () -> None
409423
"""Constructor."""
410424
self.m_stack = None
411425
self.m_tag = None
412426

413427
def start(self, data): # type: (bytes) -> None
414-
"""This method instructs the decoder to start decoding the ASN.1 input
428+
"""
429+
This method instructs the decoder to start decoding the ASN.1 input
415430
``data``, which must be a passed in as plain Python bytes.
416431
This method may be called at any time to start a new decoding job.
417432
If this method is called while currently decoding another input, that
@@ -436,7 +451,8 @@ def start(self, data): # type: (bytes) -> None
436451
self.m_tag = None
437452

438453
def peek(self): # type: () -> Tag
439-
"""This method returns the current ASN.1 tag (i.e. the tag that a
454+
"""
455+
This method returns the current ASN.1 tag (i.e. the tag that a
440456
subsequent `Decoder.read()` call would return) without updating the
441457
decoding offset. In case no more data is available from the input,
442458
this method returns ``None`` to signal end-of-file.
@@ -466,7 +482,8 @@ def peek(self): # type: () -> Tag
466482
return self.m_tag
467483

468484
def read(self, tagnr=None): # type: (Number) -> (Tag, any)
469-
"""This method decodes one ASN.1 tag from the input and returns it as a
485+
"""
486+
This method decodes one ASN.1 tag from the input and returns it as a
470487
``(tag, value)`` tuple. ``tag`` is a 3-tuple ``(nr, typ, cls)``,
471488
while ``value`` is a Python object representing the ASN.1 value.
472489
The offset in the input is increased so that the next `Decoder.read()`
@@ -492,15 +509,17 @@ def read(self, tagnr=None): # type: (Number) -> (Tag, any)
492509
return tag, value
493510

494511
def eof(self): # type: () -> bool
495-
"""Return True if we are at the end of input.
512+
"""
513+
Return True if we are at the end of input.
496514
497515
Returns:
498516
bool: True if all input has been decoded, and False otherwise.
499517
"""
500518
return self._end_of_input()
501519

502520
def enter(self): # type: () -> None
503-
"""This method enters the constructed type that is at the current
521+
"""
522+
This method enters the constructed type that is at the current
504523
decoding offset.
505524
506525
Note:
@@ -521,7 +540,8 @@ def enter(self): # type: () -> None
521540
self.m_tag = None
522541

523542
def leave(self): # type: () -> None
524-
"""This method leaves the last constructed type that was
543+
"""
544+
This method leaves the last constructed type that was
525545
`Decoder.enter()`-ed.
526546
527547
Note:
@@ -539,7 +559,9 @@ def leave(self): # type: () -> None
539559
self.m_tag = None
540560

541561
def _read_tag(self): # type: () -> Tag
542-
"""Read a tag from the input."""
562+
"""
563+
Read a tag from the input.
564+
"""
543565
byte = self._read_byte()
544566
cls = byte & 0xc0
545567
typ = byte & 0x20
@@ -554,7 +576,9 @@ def _read_tag(self): # type: () -> Tag
554576
return Tag(nr=nr, typ=typ, cls=cls)
555577

556578
def _read_length(self): # type: () -> int
557-
"""Read a length from the input."""
579+
"""
580+
Read a length from the input.
581+
"""
558582
byte = self._read_byte()
559583
if byte & 0x80:
560584
count = byte & 0x7f
@@ -573,7 +597,9 @@ def _read_length(self): # type: () -> int
573597
return length
574598

575599
def _read_value(self, cls, nr, length): # type: (int, int, int) -> any
576-
"""Read a value from the input."""
600+
"""
601+
Read a value from the input.
602+
"""
577603
bytes_data = self._read_bytes(length)
578604
if cls != Classes.Universal:
579605
value = bytes_data
@@ -598,7 +624,9 @@ def _read_value(self, cls, nr, length): # type: (int, int, int) -> any
598624
return value
599625

600626
def _read_byte(self): # type: () -> int
601-
"""Return the next input byte, or raise an error on end-of-input."""
627+
"""
628+
Return the next input byte, or raise an error on end-of-input.
629+
"""
602630
index, input_data = self.m_stack[-1]
603631
try:
604632
byte = input_data[index]
@@ -608,8 +636,10 @@ def _read_byte(self): # type: () -> int
608636
return byte
609637

610638
def _read_bytes(self, count): # type: (int) -> bytes
611-
"""Return the next ``count`` bytes of input. Raise error on
612-
end-of-input."""
639+
"""
640+
Return the next ``count`` bytes of input. Raise error on
641+
end-of-input.
642+
"""
613643
index, input_data = self.m_stack[-1]
614644
bytes_data = input_data[index:index + count]
615645
if len(bytes_data) != count:
@@ -618,14 +648,18 @@ def _read_bytes(self, count): # type: (int) -> bytes
618648
return bytes_data
619649

620650
def _end_of_input(self): # type: () -> bool
621-
"""Return True if we are at the end of input."""
651+
"""
652+
Return True if we are at the end of input.
653+
"""
622654
index, input_data = self.m_stack[-1]
623655
assert not index > len(input_data)
624656
return index == len(input_data)
625657

626658
@staticmethod
627659
def _decode_boolean(bytes_data): # type: (bytes) -> bool
628-
"""Decode a boolean value."""
660+
"""
661+
Decode a boolean value.
662+
"""
629663
if len(bytes_data) != 1:
630664
raise Error('ASN1 syntax error')
631665
if bytes_data[0] == 0:
@@ -634,7 +668,9 @@ def _decode_boolean(bytes_data): # type: (bytes) -> bool
634668

635669
@staticmethod
636670
def _decode_integer(bytes_data): # type: (bytes) -> int
637-
"""Decode an integer value."""
671+
"""
672+
Decode an integer value.
673+
"""
638674
values = [int(b) for b in bytes_data]
639675
# check if the integer is normalized
640676
if len(values) > 1 and (values[0] == 0xff and values[1] & 0x80 or values[0] == 0x00 and not (values[1] & 0x80)):
@@ -663,19 +699,25 @@ def _decode_integer(bytes_data): # type: (bytes) -> int
663699

664700
@staticmethod
665701
def _decode_octet_string(bytes_data): # type: (bytes) -> bytes
666-
"""Decode an octet string."""
702+
"""
703+
Decode an octet string.
704+
"""
667705
return bytes_data
668706

669707
@staticmethod
670708
def _decode_null(bytes_data): # type: (bytes) -> any
671-
"""Decode a Null value."""
709+
"""
710+
Decode a Null value.
711+
"""
672712
if len(bytes_data) != 0:
673713
raise Error('ASN1 syntax error')
674714
return None
675715

676716
@staticmethod
677717
def _decode_object_identifier(bytes_data): # type: (bytes) -> str
678-
"""Decode an object identifier."""
718+
"""
719+
Decode an object identifier.
720+
"""
679721
result = []
680722
value = 0
681723
for i in range(len(bytes_data)):
@@ -697,12 +739,16 @@ def _decode_object_identifier(bytes_data): # type: (bytes) -> str
697739

698740
@staticmethod
699741
def _decode_printable_string(bytes_data): # type: (bytes) -> str
700-
"""Decode a printable string."""
742+
"""
743+
Decode a printable string.
744+
"""
701745
return bytes_data.decode('utf-8')
702746

703747
@staticmethod
704748
def _decode_bitstring(bytes_data): # type: (bytes) -> str
705-
"""Decode a bitstring."""
749+
"""
750+
Decode a bitstring.
751+
"""
706752
if len(bytes_data) == 0:
707753
raise Error('ASN1 syntax error')
708754

tests/test_asn1.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# made available under the MIT license. Consult the file "LICENSE" that
55
# is distributed together with this file for the exact licensing terms.
66
#
7-
# Python-ASN1 is copyright (c) 2007-2016 by the Python-ASN1 authors. See the
7+
# Python-ASN1 is copyright (c) 2007-2025 by the Python-ASN1 authors. See the
88
# file "AUTHORS" for a complete overview.
99

1010
from __future__ import absolute_import

0 commit comments

Comments
 (0)