From 7fca7aed1ba44a1e3497600bc25741924111a6e3 Mon Sep 17 00:00:00 2001 From: "Jeffrey A. Clark" Date: Mon, 23 Sep 2024 15:45:34 -0400 Subject: [PATCH] Add c extension use to client metadata - Move has_c to common - Check if has_c in pool_options - Update tests to check for "PyMongo|c" --- pymongo/__init__.py | 12 +----------- pymongo/common.py | 10 ++++++++++ pymongo/pool_options.py | 6 ++++++ test/asynchronous/test_client.py | 8 ++++---- test/test_client.py | 8 ++++---- 5 files changed, 25 insertions(+), 19 deletions(-) diff --git a/pymongo/__init__.py b/pymongo/__init__.py index 8116788bc3..6416f939e8 100644 --- a/pymongo/__init__.py +++ b/pymongo/__init__.py @@ -88,7 +88,7 @@ from pymongo import _csot from pymongo._version import __version__, get_version_string, version_tuple -from pymongo.common import MAX_SUPPORTED_WIRE_VERSION, MIN_SUPPORTED_WIRE_VERSION +from pymongo.common import MAX_SUPPORTED_WIRE_VERSION, MIN_SUPPORTED_WIRE_VERSION, has_c from pymongo.cursor import CursorType from pymongo.operations import ( DeleteMany, @@ -116,16 +116,6 @@ """Current version of PyMongo.""" -def has_c() -> bool: - """Is the C extension installed?""" - try: - from pymongo import _cmessage # type: ignore[attr-defined] # noqa: F401 - - return True - except ImportError: - return False - - def timeout(seconds: Optional[float]) -> ContextManager[None]: """**(Provisional)** Apply the given timeout for a block of operations. diff --git a/pymongo/common.py b/pymongo/common.py index a073eba577..360389c25d 100644 --- a/pymongo/common.py +++ b/pymongo/common.py @@ -1060,3 +1060,13 @@ def update(self, other: Mapping[str, Any]) -> None: # type: ignore[override] def cased_key(self, key: str) -> Any: return self.__casedkeys[key.lower()] + + +def has_c() -> bool: + """Is the C extension installed?""" + try: + from pymongo import _cmessage # type: ignore[attr-defined] # noqa: F401 + + return True + except ImportError: + return False diff --git a/pymongo/pool_options.py b/pymongo/pool_options.py index 6ec97d7d1b..61486c91c6 100644 --- a/pymongo/pool_options.py +++ b/pymongo/pool_options.py @@ -33,6 +33,7 @@ MAX_POOL_SIZE, MIN_POOL_SIZE, WAIT_QUEUE_TIMEOUT, + has_c, ) if TYPE_CHECKING: @@ -363,6 +364,11 @@ def __init__( # }, # 'platform': 'CPython 3.8.0|MyPlatform' # } + if has_c(): + self.__metadata["driver"]["name"] = "{}|{}".format( + self.__metadata["driver"]["name"], + "c", + ) if not is_sync: self.__metadata["driver"]["name"] = "{}|{}".format( self.__metadata["driver"]["name"], diff --git a/test/asynchronous/test_client.py b/test/asynchronous/test_client.py index f610f32779..ea6809e30d 100644 --- a/test/asynchronous/test_client.py +++ b/test/asynchronous/test_client.py @@ -343,7 +343,7 @@ async def test_read_preference(self): async def test_metadata(self): metadata = copy.deepcopy(_METADATA) - metadata["driver"]["name"] = "PyMongo|async" + metadata["driver"]["name"] = "PyMongo|c|async" metadata["application"] = {"name": "foobar"} client = self.simple_client("mongodb://foo:27017/?appname=foobar&connect=false") options = client.options @@ -366,7 +366,7 @@ async def test_metadata(self): with self.assertRaises(TypeError): self.simple_client(driver=("Foo", "1", "a")) # Test appending to driver info. - metadata["driver"]["name"] = "PyMongo|async|FooDriver" + metadata["driver"]["name"] = "PyMongo|c|async|FooDriver" metadata["driver"]["version"] = "{}|1.2.3".format(_METADATA["driver"]["version"]) client = self.simple_client( "foo", @@ -410,7 +410,7 @@ async def test_metadata(self): @mock.patch.dict("os.environ", {ENV_VAR_K8S: "1"}) def test_container_metadata(self): metadata = copy.deepcopy(_METADATA) - metadata["driver"]["name"] = "PyMongo|async" + metadata["driver"]["name"] = "PyMongo|c|async" metadata["env"] = {} metadata["env"]["container"] = {"orchestrator": "kubernetes"} client = self.simple_client("mongodb://foo:27017/?appname=foobar&connect=false") @@ -1927,7 +1927,7 @@ def test_sigstop_sigcont(self): async def _test_handshake(self, env_vars, expected_env): with patch.dict("os.environ", env_vars): metadata = copy.deepcopy(_METADATA) - metadata["driver"]["name"] = "PyMongo|async" + metadata["driver"]["name"] = "PyMongo|c|async" if expected_env is not None: metadata["env"] = expected_env diff --git a/test/test_client.py b/test/test_client.py index bc45325f0b..08acef0080 100644 --- a/test/test_client.py +++ b/test/test_client.py @@ -335,7 +335,7 @@ def test_read_preference(self): def test_metadata(self): metadata = copy.deepcopy(_METADATA) - metadata["driver"]["name"] = "PyMongo" + metadata["driver"]["name"] = "PyMongo|c" metadata["application"] = {"name": "foobar"} client = self.simple_client("mongodb://foo:27017/?appname=foobar&connect=false") options = client.options @@ -358,7 +358,7 @@ def test_metadata(self): with self.assertRaises(TypeError): self.simple_client(driver=("Foo", "1", "a")) # Test appending to driver info. - metadata["driver"]["name"] = "PyMongo|FooDriver" + metadata["driver"]["name"] = "PyMongo|c|FooDriver" metadata["driver"]["version"] = "{}|1.2.3".format(_METADATA["driver"]["version"]) client = self.simple_client( "foo", @@ -402,7 +402,7 @@ def test_metadata(self): @mock.patch.dict("os.environ", {ENV_VAR_K8S: "1"}) def test_container_metadata(self): metadata = copy.deepcopy(_METADATA) - metadata["driver"]["name"] = "PyMongo" + metadata["driver"]["name"] = "PyMongo|c" metadata["env"] = {} metadata["env"]["container"] = {"orchestrator": "kubernetes"} client = self.simple_client("mongodb://foo:27017/?appname=foobar&connect=false") @@ -1885,7 +1885,7 @@ def test_sigstop_sigcont(self): def _test_handshake(self, env_vars, expected_env): with patch.dict("os.environ", env_vars): metadata = copy.deepcopy(_METADATA) - metadata["driver"]["name"] = "PyMongo" + metadata["driver"]["name"] = "PyMongo|c" if expected_env is not None: metadata["env"] = expected_env