Peek into stream. Parameters ---------- substrate: :py:class:`IOBase` Stream to read from. size: :py:class:`int` How many bytes to peek (-1 = all available) Returns ------- : :py:class:`bytes` or :py:class:`str` The return type depends on Python
(substrate, size=-1)
| 149 | |
| 150 | |
| 151 | def peekIntoStream(substrate, size=-1): |
| 152 | """Peek into stream. |
| 153 | |
| 154 | Parameters |
| 155 | ---------- |
| 156 | substrate: :py:class:`IOBase` |
| 157 | Stream to read from. |
| 158 | |
| 159 | size: :py:class:`int` |
| 160 | How many bytes to peek (-1 = all available) |
| 161 | |
| 162 | Returns |
| 163 | ------- |
| 164 | : :py:class:`bytes` or :py:class:`str` |
| 165 | The return type depends on Python major version |
| 166 | """ |
| 167 | if hasattr(substrate, "peek"): |
| 168 | received = substrate.peek(size) |
| 169 | if received is None: |
| 170 | yield |
| 171 | |
| 172 | while len(received) < size: |
| 173 | yield |
| 174 | |
| 175 | yield received |
| 176 | |
| 177 | else: |
| 178 | current_position = substrate.tell() |
| 179 | try: |
| 180 | for chunk in readFromStream(substrate, size): |
| 181 | yield chunk |
| 182 | |
| 183 | finally: |
| 184 | substrate.seek(current_position) |
| 185 | |
| 186 | |
| 187 | def readFromStream(substrate, size=-1, context=None): |
no test coverage detected