From db187bd6bd0b191db68857f46bbbe093f5170720 Mon Sep 17 00:00:00 2001 From: tkrabel Date: Wed, 1 Nov 2023 08:48:07 +0100 Subject: [PATCH] bugfix: every autoimport gets its own connection --- rope/contrib/autoimport/sqlite.py | 10 +++++----- ropetest/conftest.py | 7 +++++++ ropetest/contrib/autoimport/autoimporttest.py | 9 +++++++++ 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/rope/contrib/autoimport/sqlite.py b/rope/contrib/autoimport/sqlite.py index 14561610e..5d8554a90 100644 --- a/rope/contrib/autoimport/sqlite.py +++ b/rope/contrib/autoimport/sqlite.py @@ -68,7 +68,6 @@ def filter_package(package: Package) -> bool: _deprecated_default: bool = object() # type: ignore -thread_local = local() class AutoImport: @@ -127,6 +126,7 @@ def __init__( "`AutoImport(memory=True)` explicitly.", DeprecationWarning, ) + self.thread_local = local() self.connection = self.create_database_connection( project=project, memory=memory, @@ -169,16 +169,16 @@ def connection(self): This makes sure AutoImport can be shared across threads. """ - if not hasattr(thread_local, "connection"): - thread_local.connection = self.create_database_connection( + if not hasattr(self.thread_local, "connection"): + self.thread_local.connection = self.create_database_connection( project=self.project, memory=self.memory, ) - return thread_local.connection + return self.thread_local.connection @connection.setter def connection(self, value: sqlite3.Connection): - thread_local.connection = value + self.thread_local.connection = value def _setup_db(self): models.Metadata.create_table(self.connection) diff --git a/ropetest/conftest.py b/ropetest/conftest.py index d2efc68c6..32a35aaa6 100644 --- a/ropetest/conftest.py +++ b/ropetest/conftest.py @@ -18,6 +18,13 @@ def project_path(project): yield pathlib.Path(project.address) +@pytest.fixture +def project2(): + project = testutils.sample_project("sample_project2") + yield project + testutils.remove_project(project) + + """ Standard project structure for pytest fixtures /mod1.py -- mod1 diff --git a/ropetest/contrib/autoimport/autoimporttest.py b/ropetest/contrib/autoimport/autoimporttest.py index 65c1235ca..d65d8b2bf 100644 --- a/ropetest/contrib/autoimport/autoimporttest.py +++ b/ropetest/contrib/autoimport/autoimporttest.py @@ -108,6 +108,15 @@ def foo(): assert [("from pkg1 import foo", "foo")] == results +def test_connection(project: Project, project2: Project): + ai1 = AutoImport(project) + ai2 = AutoImport(project) + ai3 = AutoImport(project2) + + assert ai1.connection is not ai2.connection + assert ai1.connection is not ai3.connection + + @contextmanager def assert_database_is_reset(conn): conn.execute("ALTER TABLE names ADD COLUMN deprecated_column")