Skip to content

Commit

Permalink
Use a temporary directory in place of file name
Browse files Browse the repository at this point in the history
Different OS auto delete/sharing semantics are just wonky.
  • Loading branch information
danieldk committed Jan 19, 2023
1 parent 8c27fcf commit 2f9d8f7
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions spacy/tests/training/test_corpus.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@
@pytest.mark.parametrize("min_length, max_length", [(0, 0), (0, 5), (5, 0), (5, 5)])
def test_plain_text_reader(min_length, max_length):
nlp = English()
with _string_to_tmp_file(PLAIN_TEXT_DOC) as f:
with _string_to_tmp_file(PLAIN_TEXT_DOC) as file_path:
corpus = PlainTextCorpus(
Path(f.name), min_length=min_length, max_length=max_length
file_path, min_length=min_length, max_length=max_length
)

check = [
Expand All @@ -55,11 +55,12 @@ def test_plain_text_reader(min_length, max_length):


@contextmanager
def _string_to_tmp_file(s: str) -> Generator[IO, None, None]:
with tempfile.NamedTemporaryFile(suffix=".txt") as f:
f.write(s.encode("utf-8"))
f.seek(0)
yield f
def _string_to_tmp_file(s: str) -> Generator[Path, None, None]:
with tempfile.TemporaryDirectory() as d:
file_path = Path(d) / "string.txt"
with open(file_path, "wb") as f:
f.write(s.encode("utf-8"))
yield file_path


def _examples_to_tokens(
Expand Down

0 comments on commit 2f9d8f7

Please sign in to comment.