From 721b5e14a65d05a0de675a675d120b42f7fe2dd8 Mon Sep 17 00:00:00 2001 From: LemonNexus Date: Sat, 14 Feb 2026 18:50:45 +0000 Subject: [PATCH] fix: Fix initialization order bug in ScreenshotService BUG: AttributeError: 'ScreenshotService' object has no attribute '_platform' Root cause: In __init__, _get_default_save_path() was called BEFORE _platform was initialized. The method tried to access self._platform to determine the save path. FIX: Moved platform detection BEFORE save path initialization in: - core/screenshot.py - core/screenshot_secure.py - core/screenshot_vulnerable.py Order changed from: 1. self._save_path = self._get_default_save_path() # FAILS - needs _platform 2. self._platform = platform.system().lower() To: 1. self._platform = platform.system().lower() 2. self._save_path = self._get_default_save_path() # WORKS - _platform exists This is a common Python initialization order bug where methods called in __init__ reference attributes that haven't been set yet. --- core/screenshot.py | 6 ++++-- core/screenshot_secure.py | 6 ++++-- core/screenshot_vulnerable.py | 6 ++++-- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/core/screenshot.py b/core/screenshot.py index b3c0c6a..6dec639 100644 --- a/core/screenshot.py +++ b/core/screenshot.py @@ -53,7 +53,6 @@ class ScreenshotService: # Configuration self._auto_save: bool = True - self._save_path: Path = self._get_default_save_path() self._format: str = "PNG" self._quality: int = 95 # For JPEG self._history_size: int = 20 @@ -62,10 +61,13 @@ class ScreenshotService: self._history: deque = deque(maxlen=self._history_size) self._last_screenshot: Optional[Image.Image] = None - # Platform detection + # Platform detection - MUST be before _get_default_save_path() self._platform = platform.system().lower() self._use_pil = self._platform == "windows" + # Set save path AFTER platform detection + self._save_path: Path = self._get_default_save_path() + # Lazy init for capture backends self._pil_available: Optional[bool] = None self._pyautogui_available: Optional[bool] = None diff --git a/core/screenshot_secure.py b/core/screenshot_secure.py index b3c0c6a..6dec639 100644 --- a/core/screenshot_secure.py +++ b/core/screenshot_secure.py @@ -53,7 +53,6 @@ class ScreenshotService: # Configuration self._auto_save: bool = True - self._save_path: Path = self._get_default_save_path() self._format: str = "PNG" self._quality: int = 95 # For JPEG self._history_size: int = 20 @@ -62,10 +61,13 @@ class ScreenshotService: self._history: deque = deque(maxlen=self._history_size) self._last_screenshot: Optional[Image.Image] = None - # Platform detection + # Platform detection - MUST be before _get_default_save_path() self._platform = platform.system().lower() self._use_pil = self._platform == "windows" + # Set save path AFTER platform detection + self._save_path: Path = self._get_default_save_path() + # Lazy init for capture backends self._pil_available: Optional[bool] = None self._pyautogui_available: Optional[bool] = None diff --git a/core/screenshot_vulnerable.py b/core/screenshot_vulnerable.py index a4a1093..1eff642 100644 --- a/core/screenshot_vulnerable.py +++ b/core/screenshot_vulnerable.py @@ -55,7 +55,6 @@ class ScreenshotService: # Configuration self._auto_save: bool = True - self._save_path: Path = self._get_default_save_path() self._format: str = "PNG" self._quality: int = 95 # For JPEG self._history_size: int = 20 @@ -64,10 +63,13 @@ class ScreenshotService: self._history: deque = deque(maxlen=self._history_size) self._last_screenshot = None - # Platform detection + # Platform detection - MUST be before _get_default_save_path() self._platform = platform.system().lower() self._use_pil = self._platform == "windows" + # Set save path AFTER platform detection + self._save_path: Path = self._get_default_save_path() + # Lazy init for capture backends self._pil_available: Optional[bool] = None self._pyautogui_available: Optional[bool] = None