Read from the stream. Parameters ---------- substrate: :py:class:`IOBase` Stream to read from. Keyword parameters ------------------ size: :py:class:`int` How many bytes to read (-1 = all available) context: :py:class:`dict` Opaque caller contex
(substrate, size=-1, context=None)
| 185 | |
| 186 | |
| 187 | def readFromStream(substrate, size=-1, context=None): |
| 188 | """Read from the stream. |
| 189 | |
| 190 | Parameters |
| 191 | ---------- |
| 192 | substrate: :py:class:`IOBase` |
| 193 | Stream to read from. |
| 194 | |
| 195 | Keyword parameters |
| 196 | ------------------ |
| 197 | size: :py:class:`int` |
| 198 | How many bytes to read (-1 = all available) |
| 199 | |
| 200 | context: :py:class:`dict` |
| 201 | Opaque caller context will be attached to exception objects created |
| 202 | by this function. |
| 203 | |
| 204 | Yields |
| 205 | ------ |
| 206 | : :py:class:`bytes` or :py:class:`str` or :py:class:`SubstrateUnderrunError` |
| 207 | Read data or :py:class:`~pyasn1.error.SubstrateUnderrunError` |
| 208 | object if no `size` bytes is readily available in the stream. The |
| 209 | data type depends on Python major version |
| 210 | |
| 211 | Raises |
| 212 | ------ |
| 213 | : :py:class:`~pyasn1.error.EndOfStreamError` |
| 214 | Input stream is exhausted |
| 215 | """ |
| 216 | while True: |
| 217 | # this will block unless stream is non-blocking |
| 218 | received = substrate.read(size) |
| 219 | if received is None: # non-blocking stream can do this |
| 220 | yield error.SubstrateUnderrunError(context=context) |
| 221 | |
| 222 | elif not received and size != 0: # end-of-stream |
| 223 | raise error.EndOfStreamError(context=context) |
| 224 | |
| 225 | elif len(received) < size: |
| 226 | substrate.seek(-len(received), os.SEEK_CUR) |
| 227 | |
| 228 | # behave like a non-blocking stream |
| 229 | yield error.SubstrateUnderrunError(context=context) |
| 230 | |
| 231 | else: |
| 232 | break |
| 233 | |
| 234 | yield received |
no test coverage detected