MCPcopy Create free account
hub / github.com/PierreGode/Ragnar / asSeekableStream

Function asSeekableStream

pager_lib/pyasn1/codec/streaming.py:81–115  ·  view source on GitHub ↗

Convert object to seekable byte-stream. Parameters ---------- substrate: :py:class:`bytes` or :py:class:`io.IOBase` or :py:class:`univ.OctetString` Returns ------- : :py:class:`io.IOBase` Raises ------ : :py:class:`~pyasn1.error.PyAsn1Error` If the supp

(substrate)

Source from the content-addressed store, hash-verified

79
80
81def asSeekableStream(substrate):
82 """Convert object to seekable byte-stream.
83
84 Parameters
85 ----------
86 substrate: :py:class:`bytes` or :py:class:`io.IOBase` or :py:class:`univ.OctetString`
87
88 Returns
89 -------
90 : :py:class:`io.IOBase`
91
92 Raises
93 ------
94 : :py:class:`~pyasn1.error.PyAsn1Error`
95 If the supplied substrate cannot be converted to a seekable stream.
96 """
97 if isinstance(substrate, io.BytesIO):
98 return substrate
99
100 elif isinstance(substrate, bytes):
101 return io.BytesIO(substrate)
102
103 elif isinstance(substrate, univ.OctetString):
104 return io.BytesIO(substrate.asOctets())
105
106 try:
107 if substrate.seekable(): # Will fail for most invalid types
108 return substrate
109 else:
110 return CachingStreamWrapper(substrate)
111
112 except AttributeError:
113 raise error.UnsupportedSubstrateError(
114 "Cannot convert " + substrate.__class__.__name__ +
115 " to a seekable bit stream.")
116
117
118def isEndOfStream(substrate):

Callers 4

valueDecoderMethod · 0.90
indefLenValueDecoderMethod · 0.90
__init__Method · 0.90
__call__Method · 0.90

Calls 3

seekableMethod · 0.80
asOctetsMethod · 0.45

Tested by

no test coverage detected