diff --git a/swarm/environment/tools/reader/readers.py b/swarm/environment/tools/reader/readers.py index 92cbbb6..891522d 100644 --- a/swarm/environment/tools/reader/readers.py +++ b/swarm/environment/tools/reader/readers.py @@ -268,27 +268,8 @@ def parse(self, file_path: Path) -> str: class PythonReader(Reader): def parse(self, file_path: Path) -> str: - #swarmlog("SYS", f"Executing and reading Python file from {file_path}.", Cost.instance().value) - logger.info(f"Executing and reading Python file from {file_path}.") - execution_result = "" - error = "" - file_content = "" - try: - completed_process = subprocess.run(["python", file_path], capture_output=True, text=True, check=True) - execution_result = "Output:\n" + completed_process.stdout - except subprocess.CalledProcessError as e: - error = "Error:\n" + e.stderr - except Exception as e: - #swarmlog("ERROR", f"Error executing Python file: {e}", Cost.instance().value) - logger.info(f"Error executing Python file: {e}") - - try: - with open(file_path, "r") as file: - file_content = "\nFile Content:\n" + file.read() - except Exception as e: - #swarmlog("ERROR", f"Error reading Python file: {e}", Cost.instance().value) - logger.info(f"Error reading Python file: {e}") - return file_content, execution_result, error + logger.info(f"Reading Python file from {file_path}.") + return Path(file_path).read_text(errors="replace") class IMGReader(Reader): @@ -364,7 +345,7 @@ def set_reader(self, suffix) -> None: #swarmlog("SYS", f"Setting Reader to {type(self.reader).__name__}", Cost.instance().value) logger.info(f"Setting Reader to {type(self.reader).__name__}") - def read_file(self, file_path: Path, task="describe the file") -> str: + def read_file(self, file_path: str, task="describe the file") -> str: suffix = '.' + file_path.split(".")[-1] self.set_reader(suffix) if isinstance(self.reader, IMGReader) or isinstance(self.reader, VideoReader): @@ -394,18 +375,11 @@ def read(self, task, file): file_content = self.file_reader.read_file(file, task) suffix = file.split(".")[-1] - if suffix in ['py', 'java', 'cpp', 'c', 'js', 'css', 'html', 'htm', 'xml']: - files_content += f'\nThe {suffix} file contains:\n---\n{file_content[0]}' - if file_content[1] != '': - files_content += f'\nExecution result:\n{file_content[1]}' - if file_content[2] != '': - files_content += f'\nExecution error message:\n{file_content[2]}' - files_content += '\n---' - - elif suffix in ['txt', 'jsonl', 'csv', 'json', 'jsonld', 'jsonl', 'yaml', 'yml', - 'xlsx', 'xls', 'jpg', 'png', 'jpeg', 'gif', 'bmp', 'mp3', 'wav', - 'ogg', 'mp4', 'avi', 'mkv', 'mov', 'pdf', 'doc', 'docx', 'ppt', - 'pptx', 'md', 'markdown', 'tex', 'zip', 'tar', 'gz', '7z', 'rar']: + if suffix in ['py', 'java', 'cpp', 'c', 'js', 'css', 'html', 'htm', 'xml', + 'txt', 'jsonl', 'csv', 'json', 'jsonld', 'yaml', 'yml', + 'xlsx', 'xls', 'jpg', 'png', 'jpeg', 'gif', 'bmp', 'mp3', 'wav', + 'ogg', 'mp4', 'avi', 'mkv', 'mov', 'pdf', 'doc', 'docx', 'ppt', + 'pptx', 'md', 'markdown', 'tex', 'zip', 'tar', 'gz', '7z', 'rar']: files_content += f'\nThe {suffix} file contains:\n---\n{file_content}\n---' return files_content diff --git a/test/test_python_reader.py b/test/test_python_reader.py new file mode 100644 index 0000000..881db4e --- /dev/null +++ b/test/test_python_reader.py @@ -0,0 +1,23 @@ +from swarm.environment.tools.reader.readers import FileReader, GeneralReader + + +def test_python_reader_returns_source_without_executing(tmp_path): + """Reading a .py attachment must return its source text, not run it.""" + marker = tmp_path / "marker" + attachment = tmp_path / "attachment.py" + attachment.write_text( + f"open({str(marker)!r}, 'w').write('ran')\nx = 1 + 1\n" + ) + + # PythonReader.parse returns the source as plain text. + source = FileReader().read_file(str(attachment)) + assert isinstance(source, str) + assert "x = 1 + 1" in source + # The attachment must not have been executed. + assert not marker.exists() + + # GeneralReader.read must treat .py like any other text file (no 3-tuple indexing). + rendered = GeneralReader().read("describe the file", str(attachment)) + assert isinstance(rendered, str) + assert "x = 1 + 1" in rendered + assert not marker.exists()