Skip to content

Commit 59a2b65

Browse files
committed
utf-8 encoding
1 parent a1d5381 commit 59a2b65

File tree

3 files changed

+16
-13
lines changed

3 files changed

+16
-13
lines changed

codeflash/code_utils/checkpoint.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@
1111
from rich.prompt import Confirm
1212

1313
from codeflash.cli_cmds.console import console
14+
from codeflash.code_utils.compat import codeflash_temp_dir
1415

1516
if TYPE_CHECKING:
1617
import argparse
1718

1819

1920
class CodeflashRunCheckpoint:
20-
def __init__(self, module_root: Path, checkpoint_dir: Path = Path("/tmp")) -> None: # noqa: S108
21+
def __init__(self, module_root: Path, checkpoint_dir: Path | None = None) -> None:
22+
if checkpoint_dir is None:
23+
checkpoint_dir = codeflash_temp_dir
2124
self.module_root = module_root
2225
self.checkpoint_dir = Path(checkpoint_dir)
2326
# Create a unique checkpoint file name
@@ -37,7 +40,7 @@ def _initialize_checkpoint_file(self) -> None:
3740
"last_updated": time.time(),
3841
}
3942

40-
with self.checkpoint_path.open("w") as f:
43+
with self.checkpoint_path.open("w", encoding="utf-8") as f:
4144
f.write(json.dumps(metadata) + "\n")
4245

4346
def add_function_to_checkpoint(
@@ -66,7 +69,7 @@ def add_function_to_checkpoint(
6669
**additional_info,
6770
}
6871

69-
with self.checkpoint_path.open("a") as f:
72+
with self.checkpoint_path.open("a", encoding="utf-8") as f:
7073
f.write(json.dumps(function_data) + "\n")
7174

7275
# Update the metadata last_updated timestamp
@@ -75,7 +78,7 @@ def add_function_to_checkpoint(
7578
def _update_metadata_timestamp(self) -> None:
7679
"""Update the last_updated timestamp in the metadata."""
7780
# Read the first line (metadata)
78-
with self.checkpoint_path.open() as f:
81+
with self.checkpoint_path.open(encoding="utf-8") as f:
7982
metadata = json.loads(f.readline())
8083
rest_content = f.read()
8184

@@ -84,7 +87,7 @@ def _update_metadata_timestamp(self) -> None:
8487

8588
# Write all lines to a temporary file
8689

87-
with self.checkpoint_path.open("w") as f:
90+
with self.checkpoint_path.open("w", encoding="utf-8") as f:
8891
f.write(json.dumps(metadata) + "\n")
8992
f.write(rest_content)
9093

@@ -94,7 +97,7 @@ def cleanup(self) -> None:
9497
self.checkpoint_path.unlink(missing_ok=True)
9598

9699
for file in self.checkpoint_dir.glob("codeflash_checkpoint_*.jsonl"):
97-
with file.open() as f:
100+
with file.open(encoding="utf-8") as f:
98101
# Skip the first line (metadata)
99102
first_line = next(f)
100103
metadata = json.loads(first_line)
@@ -116,7 +119,7 @@ def get_all_historical_functions(module_root: Path, checkpoint_dir: Path) -> dic
116119
to_delete = []
117120

118121
for file in checkpoint_dir.glob("codeflash_checkpoint_*.jsonl"):
119-
with file.open() as f:
122+
with file.open(encoding="utf-8") as f:
120123
# Skip the first line (metadata)
121124
first_line = next(f)
122125
metadata = json.loads(first_line)
@@ -139,8 +142,8 @@ def get_all_historical_functions(module_root: Path, checkpoint_dir: Path) -> dic
139142

140143
def ask_should_use_checkpoint_get_functions(args: argparse.Namespace) -> Optional[dict[str, dict[str, str]]]:
141144
previous_checkpoint_functions = None
142-
if args.all and (sys.platform == "linux" or sys.platform == "darwin") and Path("/tmp").is_dir(): # noqa: S108 #TODO: use the temp dir from codeutils-compat.py
143-
previous_checkpoint_functions = get_all_historical_functions(args.module_root, Path("/tmp")) # noqa: S108
145+
if args.all and codeflash_temp_dir.is_dir():
146+
previous_checkpoint_functions = get_all_historical_functions(args.module_root, codeflash_temp_dir)
144147
if previous_checkpoint_functions and Confirm.ask(
145148
"Previous Checkpoint detected from an incomplete optimization run, shall I continue the optimization from that point?",
146149
default=True,

codeflash/code_utils/env_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def get_cached_gh_event_data() -> dict[str, Any]:
120120
event_path = os.getenv("GITHUB_EVENT_PATH")
121121
if not event_path:
122122
return {}
123-
with Path(event_path).open() as f:
123+
with Path(event_path).open(encoding='utf-8') as f:
124124
return json.load(f) # type: ignore # noqa
125125

126126

codeflash/verification/coverage_utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@ def load_from_sqlite_database(
4646

4747
reporter = JsonReporter(cov)
4848
temp_json_file = database_path.with_suffix(".report.json")
49-
with temp_json_file.open("w") as f:
49+
with temp_json_file.open("w", encoding="utf-8") as f:
5050
try:
5151
reporter.report(morfs=[source_code_path.as_posix()], outfile=f)
5252
except NoDataError:
5353
sentry_sdk.capture_message(f"No coverage data found for {function_name} in {source_code_path}")
5454
return CoverageUtils.create_empty(source_code_path, function_name, code_context)
55-
with temp_json_file.open() as f:
55+
with temp_json_file.open(encoding="utf-8") as f:
5656
original_coverage_data = json.load(f)
5757

5858
coverage_data, status = CoverageUtils._parse_coverage_file(temp_json_file, source_code_path)
@@ -92,7 +92,7 @@ def load_from_sqlite_database(
9292
def _parse_coverage_file(
9393
coverage_file_path: Path, source_code_path: Path
9494
) -> tuple[dict[str, dict[str, Any]], CoverageStatus]:
95-
with coverage_file_path.open() as f:
95+
with coverage_file_path.open(encoding="utf-8") as f:
9696
coverage_data = json.load(f)
9797

9898
candidates = generate_candidates(source_code_path)

0 commit comments

Comments
 (0)