Skip to content

Commit 4511baa

Browse files
committed
fix: account for iterdir() behavior change in Python 3.13
In Python 3.13, `iterdir()` raises exceptions immediately instead of lazily. This change forces early evaluation to ensure consistent behavior across Python versions. Ref: python/cpython#107320
1 parent cb1dc38 commit 4511baa

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

questionpy_sdk/webserver/state.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,14 @@ def __init__(self, package_state_dir: Path) -> None:
7575
async def read_question_states(self) -> dict[str, str]:
7676
def _read_question_states() -> dict[str, str]:
7777
try:
78-
dir_iter = self._package_state_dir.iterdir()
78+
dir_entries = list(self._package_state_dir.iterdir())
7979
except FileNotFoundError:
8080
# Package dir might not have been created yet
8181
return {}
8282
else:
8383
return {
8484
path.name: (path / self.StateFilename.QUESTION_STATE).read_text()
85-
for path in dir_iter
85+
for path in dir_entries
8686
if path.is_dir() and re.match(ID_RE, path.name)
8787
}
8888

@@ -105,9 +105,16 @@ def _delete_question() -> None:
105105
(question_path / self.StateFilename.QUESTION_STATE).unlink()
106106
except FileNotFoundError as err:
107107
raise webserver_errors.MissingQuestionStateError from err
108-
for path in question_path.iterdir():
109-
if path.is_dir() and re.match(ID_RE, path.name):
110-
self._delete_attempt_data_sync(question_id, path.name)
108+
109+
try:
110+
dir_entries = list(question_path.iterdir())
111+
except FileNotFoundError:
112+
return
113+
else:
114+
for path in dir_entries:
115+
if path.is_dir() and re.match(ID_RE, path.name):
116+
self._delete_attempt_data_sync(question_id, path.name)
117+
111118
with contextlib.suppress(OSError):
112119
# Ignore in case of non-empty directory
113120
question_path.rmdir()
@@ -121,12 +128,12 @@ def _read_attempts_sync(self, question_id: str) -> dict[str, Attempt]:
121128
attempts: dict[str, Attempt] = {}
122129

123130
try:
124-
dir_iter = self._question_path(question_id).iterdir()
131+
dir_entries = list(self._question_path(question_id).iterdir())
125132
except FileNotFoundError:
126133
# Question dir might not have been created yet
127134
return attempts
128135

129-
for path in dir_iter:
136+
for path in dir_entries:
130137
attempt_id = path.name
131138
if path.is_dir() and re.match(ID_RE, attempt_id):
132139
state = (path / self.StateFilename.ATTEMPT_STATE).read_text()

0 commit comments

Comments
 (0)