Whether the handle is opened in binary mode
(handle: FilePath | BaseBuffer, mode: str)
| 1226 | |
| 1227 | |
| 1228 | def _is_binary_mode(handle: FilePath | BaseBuffer, mode: str) -> bool: |
| 1229 | """Whether the handle is opened in binary mode""" |
| 1230 | # specified by user |
| 1231 | if "t" in mode or "b" in mode: |
| 1232 | return "b" in mode |
| 1233 | |
| 1234 | # exceptions |
| 1235 | text_classes = ( |
| 1236 | # classes that expect string but have 'b' in mode |
| 1237 | codecs.StreamWriter, |
| 1238 | codecs.StreamReader, |
| 1239 | codecs.StreamReaderWriter, |
| 1240 | ) |
| 1241 | if issubclass(type(handle), text_classes): |
| 1242 | return False |
| 1243 | |
| 1244 | return isinstance(handle, _get_binary_io_classes()) or "b" in getattr( |
| 1245 | handle, "mode", mode |
| 1246 | ) |
| 1247 | |
| 1248 | |
| 1249 | @functools.lru_cache |
no test coverage detected