From e014c5628b0cdf384a34e8f5b386455f84bcc833 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Wed, 8 Oct 2025 17:23:39 -0700 Subject: [PATCH] Replace unsafe tempfile calls with safe ones --- test/fixtures.py | 5 +++-- test/test_conversation_metadata.py | 10 ++++++---- test/test_message_text_index_population.py | 4 +++- test/test_property_index_population.py | 4 +++- test/test_related_terms_fast.py | 4 +++- test/test_related_terms_index_population.py | 4 +++- test/test_storage_providers_unified.py | 10 ++++++---- 7 files changed, 27 insertions(+), 14 deletions(-) diff --git a/test/fixtures.py b/test/fixtures.py index 5cab741..bfac089 100644 --- a/test/fixtures.py +++ b/test/fixtures.py @@ -68,8 +68,9 @@ def temp_dir() -> Iterator[str]: @pytest.fixture def temp_db_path() -> Iterator[str]: """Create a temporary SQLite database file for testing.""" - fd, path = tempfile.mkstemp(suffix=".sqlite") - os.close(fd) + temp_file = tempfile.NamedTemporaryFile(suffix=".sqlite", delete=False) + path = temp_file.name + temp_file.close() yield path if os.path.exists(path): os.remove(path) diff --git a/test/test_conversation_metadata.py b/test/test_conversation_metadata.py index 4388079..c8282d6 100644 --- a/test/test_conversation_metadata.py +++ b/test/test_conversation_metadata.py @@ -250,10 +250,12 @@ async def test_multiple_conversations_different_dbs( related_terms_settings = RelatedTermIndexSettings(embedding_settings) # Create temporary database files - fd1, db_path1 = tempfile.mkstemp(suffix=".sqlite") - fd2, db_path2 = tempfile.mkstemp(suffix=".sqlite") - os.close(fd1) - os.close(fd2) + temp_file1 = tempfile.NamedTemporaryFile(suffix=".sqlite", delete=False) + db_path1 = temp_file1.name + temp_file1.close() + temp_file2 = tempfile.NamedTemporaryFile(suffix=".sqlite", delete=False) + db_path2 = temp_file2.name + temp_file2.close() try: # Create first provider with conversation "conv1" diff --git a/test/test_message_text_index_population.py b/test/test_message_text_index_population.py index 4b82bcd..e42dbbb 100644 --- a/test/test_message_text_index_population.py +++ b/test/test_message_text_index_population.py @@ -23,7 +23,9 @@ async def test_message_text_index_population_from_database(): """Test that message text index is correctly populated when reopening a database.""" load_dotenv() - temp_db_path = tempfile.mktemp(suffix=".sqlite") + temp_db_file = tempfile.NamedTemporaryFile(suffix=".sqlite", delete=False) + temp_db_path = temp_db_file.name + temp_db_file.close() try: # Use the test model that's already configured in the system diff --git a/test/test_property_index_population.py b/test/test_property_index_population.py index 751ba9f..a3d72f7 100644 --- a/test/test_property_index_population.py +++ b/test/test_property_index_population.py @@ -38,7 +38,9 @@ async def get_embeddings(self, keys: list[str]) -> np.ndarray: async def test_property_index_population_from_database(really_needs_auth): """Test that property index is correctly populated when reopening a database.""" load_dotenv() - temp_db_path = tempfile.mktemp(suffix=".sqlite") + temp_db_file = tempfile.NamedTemporaryFile(suffix=".sqlite", delete=False) + temp_db_path = temp_db_file.name + temp_db_file.close() try: embedding_model = MockEmbeddingModel() diff --git a/test/test_related_terms_fast.py b/test/test_related_terms_fast.py index 5d6e8d6..7d8ad18 100644 --- a/test/test_related_terms_fast.py +++ b/test/test_related_terms_fast.py @@ -19,7 +19,9 @@ @pytest.mark.asyncio async def test_related_terms_index_minimal(): """Fast test with minimal data to verify related terms functionality.""" - temp_db_path = tempfile.mktemp(suffix=".sqlite") + temp_db_file = tempfile.NamedTemporaryFile(suffix=".sqlite", delete=False) + temp_db_path = temp_db_file.name + temp_db_file.close() try: # Create minimal test data with test embedding model (no API keys needed) diff --git a/test/test_related_terms_index_population.py b/test/test_related_terms_index_population.py index 6b362f7..8d4ca7e 100644 --- a/test/test_related_terms_index_population.py +++ b/test/test_related_terms_index_population.py @@ -27,7 +27,9 @@ async def test_related_terms_index_population_from_database(really_needs_auth): """Test that related terms index is correctly populated when reopening a database.""" load_dotenv() - temp_db_path = tempfile.mktemp(suffix=".sqlite") + temp_db_file = tempfile.NamedTemporaryFile(suffix=".sqlite", delete=False) + temp_db_path = temp_db_file.name + temp_db_file.close() try: # Use the test model that's already configured in the system diff --git a/test/test_storage_providers_unified.py b/test/test_storage_providers_unified.py index 94070ff..cf02476 100644 --- a/test/test_storage_providers_unified.py +++ b/test/test_storage_providers_unified.py @@ -590,10 +590,12 @@ async def test_storage_provider_independence( import tempfile import os - fd1, temp_path1 = tempfile.mkstemp(suffix=".sqlite") - os.close(fd1) - fd2, temp_path2 = tempfile.mkstemp(suffix=".sqlite") - os.close(fd2) + temp_file1 = tempfile.NamedTemporaryFile(suffix=".sqlite", delete=False) + temp_path1 = temp_file1.name + temp_file1.close() + temp_file2 = tempfile.NamedTemporaryFile(suffix=".sqlite", delete=False) + temp_path2 = temp_file2.name + temp_file2.close() sqlite_provider1 = None sqlite_provider2 = None