diff --git a/nomad/files.py b/nomad/files.py index 3b4ce16d353e597f46553c73d6cf474f05cfbe53..30a3c334f992151b01501ce1747106f7709dc4ae 100644 --- a/nomad/files.py +++ b/nomad/files.py @@ -1778,13 +1778,15 @@ class PublicUploadFiles(UploadFiles): mode = kwargs.get('mode') if len(args) == 0 else args[0] if 'mode' in kwargs: del kwargs['mode'] + + encoding = kwargs.pop('encoding', None) mode = mode if mode else 'rb' try: zf = self._open_raw_zip_file() f = zf.open(file_path, 'r', **kwargs) if 't' in mode: - return io.TextIOWrapper(f) + return io.TextIOWrapper(f, encoding=encoding) else: return f except FileNotFoundError: diff --git a/tests/test_files.py b/tests/test_files.py index 4b53542556b7acd583aa26abbe4b5f5ea692e86b..68204fb516e02030002624493f87f66d469fa130 100644 --- a/tests/test_files.py +++ b/tests/test_files.py @@ -246,9 +246,18 @@ class UploadFilesContract(UploadFilesFixtures): _, entries, upload_files = test_upload for entry in entries: for file_path in entry.files: - mode = 'rb' if file_path.endswith('.h5') else 'r' + mode = 'rb' if file_path.endswith('.h5') else 'rt' with upload_files.raw_file(file_path, mode) as f: assert len(f.read()) > 0 + if 't' in mode: + with upload_files.raw_file(file_path, mode, encoding='utf-8') as f: + content = f.read() + assert isinstance( + content, str + ), 'Content should be a string in text mode' + assert ( + len(content) > 0 + ), f'File {file_path} with utf-8 encoding should not be empty' def test_rawfile_size(self, test_upload: UploadWithFiles): _, entries, upload_files = test_upload