Skip to content

fix: escape Windows paths in STARTUP_CODE string formatting #637

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 22, 2025

Conversation

ijkwxyz
Copy link
Contributor

@ijkwxyz ijkwxyz commented Apr 21, 2025

When running on Windows systems, the STARTUP_CODE template's file assignment could fail due to unescaped backslashes in the Windows path. This occurs when formatting the path string into STARTUP_CODE, as Windows paths (e.g., "C:\Users...") contain backslashes that need proper escaping in Python strings.

The fix uses Path.as_posix() to convert Windows backslashes to forward slashes, which Python handles correctly across all operating systems. This ensures the path string is properly escaped when inserted into the STARTUP_CODE template.

Technical details:

  • Uses existing pathlib.Path object's as_posix() method
  • Converts "C:\path\to\file" to "C:/path/to/file"
  • Maintains cross-platform compatibility
  • Fixes string formatting issues without changing functionality

When running on Windows systems, the STARTUP_CODE template's __file__ assignment could fail due to unescaped backslashes in the Windows path. This occurs when formatting the path string into STARTUP_CODE, as Windows paths (e.g., "C:\Users\...") contain backslashes that need proper escaping in Python strings.

The fix uses Path.as_posix() to convert Windows backslashes to forward slashes, which Python handles correctly across all operating systems. This ensures the path string is properly escaped when inserted into the STARTUP_CODE template.

Technical details:
- Uses existing pathlib.Path object's as_posix() method
- Converts "C:\path\to\file" to "C:/path/to/file"
- Maintains cross-platform compatibility
- Fixes string formatting issues without changing functionality
@hx2A hx2A merged commit 2eedeb4 into py5coding:main Apr 22, 2025
@hx2A
Copy link
Collaborator

hx2A commented Apr 22, 2025

Thanks @ijkwxyz !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants