(package: Package, resource: Resource)
Return a file-like object opened for binary reading of the resource.
Return a file-like object opened for binary reading of the resource.
Return a file - like object opened for binary reading of the resource .
def open_binary(package: Package, resource: Resource) -> BinaryIO:
    """Return a file-like object opened for binary reading of the resource."""
    resource = _common.normalize_path(resource)
    package = _common.get_package(package)
    reader = _common.get_resource_reader(package)
    if reader is not None:
        return reader.open_resource(resource)
    spec = cast(ModuleSpec, package.__spec__)
    # Using pathlib doesn't work well here due to the lack of 'strict'
    # argument for pathlib.Path.resolve() prior to Python 3.6.
    if spec.submodule_search_locations is not None:
        paths = spec.submodule_search_locations
    elif spec.origin is not None:
        paths = [os.path.dirname(os.path.abspath(spec.origin))]

    for package_path in paths:
        full_path = os.path.join(package_path, resource)
        try:
            return open(full_path, mode='rb')
        except OSError:
            # Just assume the loader is a resource loader; all the relevant
            # importlib.machinery loaders are and an AttributeError for
            # get_data() will make it clear what is needed from the loader.
            loader = cast(ResourceLoader, spec.loader)
            data = None
            if hasattr(spec.loader, 'get_data'):
                with suppress(OSError):
                    data = loader.get_data(full_path)
            if data is not None:
                return BytesIO(data)

    raise FileNotFoundError(f'{resource!r} resource not found in {spec.name!r}')
def open_binary ( package : Package , resource : Resource ) -> BinaryIO : resource = _common . normalize_path ( resource ) package = _common . get_package ( package ) reader = _common . get_resource_reader ( package ) if reader is not None : return reader . open_resource ( resource ) spec = cast ( ModuleSpec , package . __spec__ ) # Using pathlib doesn't work well here due to the lack of 'strict' # argument for pathlib.Path.resolve() prior to Python 3.6. if spec . submodule_search_locations is not None : paths = spec . submodule_search_locations elif spec . origin is not None : paths = [ os . path . dirname ( os . path . abspath ( spec . origin ) ) ] for package_path in paths : full_path = os . path . join ( package_path , resource ) try : return open ( full_path , mode = 'rb' ) except OSError : # Just assume the loader is a resource loader; all the relevant # importlib.machinery loaders are and an AttributeError for # get_data() will make it clear what is needed from the loader. loader = cast ( ResourceLoader , spec . loader ) data = None if hasattr ( spec . loader , 'get_data' ) : with suppress ( OSError ) : data = loader . get_data ( full_path ) if data is not None : return BytesIO ( data ) raise FileNotFoundError ( f'{resource!r} resource not found in {spec.name!r}' )