diff --git a/tosfs/core.py b/tosfs/core.py index 69e9058..edd6006 100644 --- a/tosfs/core.py +++ b/tosfs/core.py @@ -524,16 +524,21 @@ def info( bucket_type = self._get_bucket_type(bucket) if bucket_type == TOS_BUCKET_TYPE_FNS: - if info := self._object_info(bucket, key, version_id): - return info + result = self._object_info(bucket, key, version_id) - return self._get_dir_info(bucket, key, path, fullpath) + if not result: + result = self._get_dir_info(bucket, key, fullpath) else: # Priority is given to judging dir, followed by file. - if info := self._get_dir_info(bucket, key, path, fullpath): - return info + result = self._get_dir_info(bucket, key, fullpath) - return self._object_info(bucket, key, version_id) + if not result: + result = self._object_info(bucket, key, version_id) + + if not result: + raise FileNotFoundError(f"Can not get information for path: {path}") + + return result def exists(self, path: str, **kwargs: Any) -> bool: """Check if a path exists in the TOS. @@ -1263,6 +1268,9 @@ def cp_file( bucket, key, vers = self._split_path(path1) info = self.info(path1, bucket, key, version_id=vers) + if not info: + raise FileNotFoundError(f"Can not get information for path: {path1}") + if info["type"] == "directory": logger.warning("Do not support copy directory %s.", path1) return @@ -1690,6 +1698,7 @@ def _find_file_dir( out = [self.info(path)] except FileNotFoundError: out = [] + dirs = { self._parent(o["name"]): { "Key": self._parent(o["name"]).rstrip("/"), @@ -1782,7 +1791,7 @@ def _bucket_info(self, bucket: str) -> dict: def _object_info( self, bucket: str, key: str, version_id: Optional[str] = None - ) -> dict: + ) -> Optional[dict]: """Get the information of an object. Parameters @@ -1846,9 +1855,9 @@ def _object_info( except Exception as e: raise TosfsError(f"Tosfs failed with unknown error: {e}") from e - return {} + return None - def _get_dir_info(self, bucket: str, key: str, path: str, fullpath: str) -> dict: + def _get_dir_info(self, bucket: str, key: str, fullpath: str) -> Optional[dict]: try: # We check to see if the path is a directory by attempting to list its # contexts. If anything is found, it is indeed a directory @@ -1871,7 +1880,7 @@ def _get_dir_info(self, bucket: str, key: str, path: str, fullpath: str) -> dict "type": "directory", } - return {} + return None except (TosClientError, TosServerError) as e: raise e except Exception as e: diff --git a/tosfs/tests/test_fsspec_api.py b/tosfs/tests/test_fsspec_api.py index f5f5232..833d478 100644 --- a/tosfs/tests/test_fsspec_api.py +++ b/tosfs/tests/test_fsspec_api.py @@ -112,7 +112,8 @@ def test_info(fsspecfs: Any, bucket: str, temporary_workspace: str): # Some FS might not support 'size' for directories, so it's not strictly checked # Test non-existent path - fsspecfs.info(f"{bucket}/{temporary_workspace}/non_existent") + with pytest.raises(FileNotFoundError): + fsspecfs.info(f"{bucket}/{temporary_workspace}/non_existent") # Test protocol stripping protocol_included_path = fsspecfs._strip_protocol(file_path)