Skip to content
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

Fix Windows path encoding for special characters (#3761) #3768

Merged
merged 5 commits into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions marimo/_server/file_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,14 @@ def _rename_file(self, new_filename: str) -> None:
assert self.filename is not None
self._create_parent_directories(new_filename)
try:
os.rename(self.filename, new_filename)
# Use pathlib.Path for cross-platform path handling
src_path = pathlib.Path(self.filename)
dst_path = pathlib.Path(new_filename)
src_path.rename(dst_path)
except Exception as err:
raise HTTPException(
status_code=HTTPStatus.SERVER_ERROR,
detail="Failed to rename from {0} to {1}".format(
self.filename, new_filename
),
detail=f"Failed to rename from {self.filename} to {new_filename}",
) from err

def _save_file(
Expand Down
20 changes: 20 additions & 0 deletions tests/_server/test_file_manager.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import os
import shutil
import sys
import tempfile
from typing import Generator
Expand Down Expand Up @@ -429,3 +430,22 @@ def cell1():
assert changed_cell_ids == {"MJUe"}
# Clean up
os.remove(temp_file.name)


def test_rename_with_special_chars(app_file_manager: AppFileManager) -> None:
"""Test that renaming files with special characters works."""
# Create a temporary file
temp_dir = tempfile.mkdtemp()
try:
initial_path = os.path.join(temp_dir, "test.py")
with open(initial_path, "w") as f:
f.write("import marimo")
app_file_manager.filename = initial_path

# Try to rename to path with special characters
new_path = os.path.join(temp_dir, "test & space.py")
app_file_manager.rename(new_path)
assert app_file_manager.filename == new_path
assert os.path.exists(new_path)
finally:
shutil.rmtree(temp_dir)
Loading