44import contextlib
55import logging
66import os
7+ from pathlib import Path
78
89from pyrit .models import Message , MessagePiece , Score
910from pyrit .output .conversation .base import ConversationPrinterBase
@@ -224,11 +225,13 @@ def _format_image_content(self, *, image_path: str) -> list[str]:
224225 @staticmethod
225226 def _format_link_path (path : str ) -> str :
226227 """Return a markdown-friendly link (POSIX separators, relative if possible)."""
228+ path_obj = Path (path )
227229 try :
228- relative_path = os . path . relpath ( path )
230+ relative_path = str ( path_obj . relative_to ( Path . cwd ()) )
229231 except ValueError :
230- # Different mount/drive than cwd (Windows). Fall back to the absolute path.
231- relative_path = os .path .abspath (path )
232+ # Path is not under cwd (different drive on Windows, or simply outside cwd).
233+ # Fall back to the absolute path.
234+ relative_path = str (path_obj .resolve ())
232235 return relative_path .replace ("\\ " , "/" )
233236
234237 def _maybe_blur_image_on_disk (self , * , image_path : str ) -> str | None :
@@ -251,30 +254,30 @@ def _maybe_blur_image_on_disk(self, *, image_path: str) -> str | None:
251254 str | None: The path to the blurred image, or ``None`` on failure.
252255 """
253256 try :
254- blurred_path = self ._blurred_destination (image_path = image_path )
255- if os . path . exists (blurred_path ):
257+ blurred_path = Path ( self ._blurred_destination (image_path = image_path ) )
258+ if blurred_path . exists ():
256259 logger .debug (f"Reusing cached blurred image at { blurred_path } " )
257- return blurred_path
260+ return str ( blurred_path )
258261
259- os . makedirs ( os . path . dirname ( blurred_path ) or "." , exist_ok = True )
262+ blurred_path . parent . mkdir ( parents = True , exist_ok = True )
260263
261264 from pyrit .output ._image_utils import blur_image_bytes
262265
263266 with open (image_path , "rb" ) as f :
264267 original_bytes = f .read ()
265268 blurred_bytes = blur_image_bytes (image_bytes = original_bytes , radius = self ._blur_radius )
266269
267- temp_path = f"{ blurred_path } .tmp.{ os .getpid ()} "
270+ temp_path = blurred_path . parent / f"{ blurred_path . name } .tmp.{ os .getpid ()} "
268271 try :
269272 with open (temp_path , "wb" ) as f :
270273 f .write (blurred_bytes )
271274 os .replace (temp_path , blurred_path )
272275 except Exception :
273- if os . path . exists (temp_path ):
276+ if temp_path . exists ():
274277 with contextlib .suppress (OSError ):
275- os . remove ( temp_path )
278+ temp_path . unlink ( )
276279 raise
277- return blurred_path
280+ return str ( blurred_path )
278281 except Exception as exc :
279282 logger .warning (f"Failed to write blurred image for { image_path } ; falling back to a text link. Error: { exc } " )
280283 return None
@@ -289,9 +292,9 @@ def _blurred_destination(self, *, image_path: str) -> str:
289292 Returns:
290293 str: Path to the blurred file (sibling by default, or under ``blurred_dir``).
291294 """
292- directory = self . _blurred_dir if self . _blurred_dir is not None else os . path . dirname (image_path )
293- stem = os . path . splitext ( os . path . basename ( image_path ))[ 0 ]
294- return os . path . join (directory , f"{ stem } _blurred.png" )
295+ image_path_obj = Path (image_path )
296+ directory = Path ( self . _blurred_dir ) if self . _blurred_dir is not None else image_path_obj . parent
297+ return str (directory / f"{ image_path_obj . stem } _blurred.png" )
295298
296299 def _format_audio_content (self , * , audio_path : str ) -> list [str ]:
297300 """
0 commit comments