summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIlya Etingof <etingof@gmail.com>2019-10-01 10:31:21 +0200
committerIlya Etingof <etingof@gmail.com>2019-10-01 10:31:21 +0200
commit2aa38f0348e74f1151de8bd6c230d58503e85a9a (patch)
treef007f413ffbe6e993250d771a8c3ec2007d6e158
parentcbd65b03b1c68eb42df11261d655d69cb7170ef3 (diff)
downloadpyasn1-git-2aa38f0348e74f1151de8bd6c230d58503e85a9a.tar.gz
Add minor performance optimising changes
-rw-r--r--pyasn1/codec/ber/decoder.py6
-rw-r--r--pyasn1/codec/streaming.py7
2 files changed, 9 insertions, 4 deletions
diff --git a/pyasn1/codec/ber/decoder.py b/pyasn1/codec/ber/decoder.py
index db7301c..10b80eb 100644
--- a/pyasn1/codec/ber/decoder.py
+++ b/pyasn1/codec/ber/decoder.py
@@ -1443,6 +1443,9 @@ for typeDecoder in TAG_MAP.values():
stStop) = [x for x in range(10)]
+EOO_SENTINEL = ints2octs((0, 0))
+
+
class SingleItemDecoder(object):
defaultErrorState = stErrorCondition
#defaultErrorState = stDumpRawValue
@@ -1459,7 +1462,6 @@ class SingleItemDecoder(object):
# Tag & TagSet objects caches
self.__tagCache = {}
self.__tagSetCache = {}
- self.__eooSentinel = ints2octs((0, 0))
def __call__(self, substrate, asn1Spec=None,
tagSet=None, length=None, state=stDecodeTag,
@@ -1480,7 +1482,7 @@ class SingleItemDecoder(object):
if isinstance(eoo_candidate, SubstrateUnderrunError):
yield eoo_candidate
- if eoo_candidate == self.__eooSentinel:
+ if eoo_candidate == EOO_SENTINEL:
if LOG:
LOG('end-of-octets sentinel found')
yield eoo.endOfOctets
diff --git a/pyasn1/codec/streaming.py b/pyasn1/codec/streaming.py
index 65c318c..6d0146b 100644
--- a/pyasn1/codec/streaming.py
+++ b/pyasn1/codec/streaming.py
@@ -98,7 +98,10 @@ def asSeekableStream(substrate):
: :py:class:`~pyasn1.error.PyAsn1Error`
If the supplied substrate cannot be converted to a seekable stream.
"""
- if isinstance(substrate, bytes):
+ if isinstance(substrate, io.BytesIO):
+ return substrate
+
+ elif isinstance(substrate, bytes):
return io.BytesIO(substrate)
elif isinstance(substrate, univ.OctetString):
@@ -225,7 +228,7 @@ def readFromStream(substrate, size=-1, context=None):
if received is None: # non-blocking stream can do this
yield error.SubstrateUnderrunError(context=context)
- elif size != 0 and not received: # end-of-stream
+ elif not received and size != 0: # end-of-stream
raise error.EndOfStreamError(context=context)
elif len(received) < size: