Skip to content

Commit 0b9de8e

Browse files
committed
PYTHON-1714 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"
1 parent e03f8f2 commit 0b9de8e

File tree

5 files changed

+25
-19
lines changed

5 files changed

+25
-19
lines changed

pymongo/__init__.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888

8989
from pymongo import _csot
9090
from pymongo._version import __version__, get_version_string, version_tuple
91-
from pymongo.common import MAX_SUPPORTED_WIRE_VERSION, MIN_SUPPORTED_WIRE_VERSION
91+
from pymongo.common import MAX_SUPPORTED_WIRE_VERSION, MIN_SUPPORTED_WIRE_VERSION, has_c
9292
from pymongo.cursor import CursorType
9393
from pymongo.operations import (
9494
DeleteMany,
@@ -116,16 +116,6 @@
116116
"""Current version of PyMongo."""
117117

118118

119-
def has_c() -> bool:
120-
"""Is the C extension installed?"""
121-
try:
122-
from pymongo import _cmessage # type: ignore[attr-defined] # noqa: F401
123-
124-
return True
125-
except ImportError:
126-
return False
127-
128-
129119
def timeout(seconds: Optional[float]) -> ContextManager[None]:
130120
"""**(Provisional)** Apply the given timeout for a block of operations.
131121

pymongo/common.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,3 +1060,13 @@ def update(self, other: Mapping[str, Any]) -> None: # type: ignore[override]
10601060

10611061
def cased_key(self, key: str) -> Any:
10621062
return self.__casedkeys[key.lower()]
1063+
1064+
1065+
def has_c() -> bool:
1066+
"""Is the C extension installed?"""
1067+
try:
1068+
from pymongo import _cmessage # type: ignore[attr-defined] # noqa: F401
1069+
1070+
return True
1071+
except ImportError:
1072+
return False

pymongo/pool_options.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
MAX_POOL_SIZE,
3434
MIN_POOL_SIZE,
3535
WAIT_QUEUE_TIMEOUT,
36+
has_c,
3637
)
3738

3839
if TYPE_CHECKING:
@@ -363,6 +364,11 @@ def __init__(
363364
# },
364365
# 'platform': 'CPython 3.8.0|MyPlatform'
365366
# }
367+
if has_c():
368+
self.__metadata["driver"]["name"] = "{}|{}".format(
369+
self.__metadata["driver"]["name"],
370+
"c",
371+
)
366372
if not is_sync:
367373
self.__metadata["driver"]["name"] = "{}|{}".format(
368374
self.__metadata["driver"]["name"],

test/asynchronous/test_client.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ async def test_read_preference(self):
343343

344344
async def test_metadata(self):
345345
metadata = copy.deepcopy(_METADATA)
346-
metadata["driver"]["name"] = "PyMongo|async"
346+
metadata["driver"]["name"] = "PyMongo|c|async"
347347
metadata["application"] = {"name": "foobar"}
348348
client = self.simple_client("mongodb://foo:27017/?appname=foobar&connect=false")
349349
options = client.options
@@ -366,7 +366,7 @@ async def test_metadata(self):
366366
with self.assertRaises(TypeError):
367367
self.simple_client(driver=("Foo", "1", "a"))
368368
# Test appending to driver info.
369-
metadata["driver"]["name"] = "PyMongo|async|FooDriver"
369+
metadata["driver"]["name"] = "PyMongo|c|async|FooDriver"
370370
metadata["driver"]["version"] = "{}|1.2.3".format(_METADATA["driver"]["version"])
371371
client = self.simple_client(
372372
"foo",
@@ -410,7 +410,7 @@ async def test_metadata(self):
410410
@mock.patch.dict("os.environ", {ENV_VAR_K8S: "1"})
411411
def test_container_metadata(self):
412412
metadata = copy.deepcopy(_METADATA)
413-
metadata["driver"]["name"] = "PyMongo|async"
413+
metadata["driver"]["name"] = "PyMongo|c|async"
414414
metadata["env"] = {}
415415
metadata["env"]["container"] = {"orchestrator": "kubernetes"}
416416
client = self.simple_client("mongodb://foo:27017/?appname=foobar&connect=false")
@@ -1927,7 +1927,7 @@ def test_sigstop_sigcont(self):
19271927
async def _test_handshake(self, env_vars, expected_env):
19281928
with patch.dict("os.environ", env_vars):
19291929
metadata = copy.deepcopy(_METADATA)
1930-
metadata["driver"]["name"] = "PyMongo|async"
1930+
metadata["driver"]["name"] = "PyMongo|c|async"
19311931
if expected_env is not None:
19321932
metadata["env"] = expected_env
19331933

test/test_client.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ def test_read_preference(self):
335335

336336
def test_metadata(self):
337337
metadata = copy.deepcopy(_METADATA)
338-
metadata["driver"]["name"] = "PyMongo"
338+
metadata["driver"]["name"] = "PyMongo|c"
339339
metadata["application"] = {"name": "foobar"}
340340
client = self.simple_client("mongodb://foo:27017/?appname=foobar&connect=false")
341341
options = client.options
@@ -358,7 +358,7 @@ def test_metadata(self):
358358
with self.assertRaises(TypeError):
359359
self.simple_client(driver=("Foo", "1", "a"))
360360
# Test appending to driver info.
361-
metadata["driver"]["name"] = "PyMongo|FooDriver"
361+
metadata["driver"]["name"] = "PyMongo|c|FooDriver"
362362
metadata["driver"]["version"] = "{}|1.2.3".format(_METADATA["driver"]["version"])
363363
client = self.simple_client(
364364
"foo",
@@ -402,7 +402,7 @@ def test_metadata(self):
402402
@mock.patch.dict("os.environ", {ENV_VAR_K8S: "1"})
403403
def test_container_metadata(self):
404404
metadata = copy.deepcopy(_METADATA)
405-
metadata["driver"]["name"] = "PyMongo"
405+
metadata["driver"]["name"] = "PyMongo|c"
406406
metadata["env"] = {}
407407
metadata["env"]["container"] = {"orchestrator": "kubernetes"}
408408
client = self.simple_client("mongodb://foo:27017/?appname=foobar&connect=false")
@@ -1885,7 +1885,7 @@ def test_sigstop_sigcont(self):
18851885
def _test_handshake(self, env_vars, expected_env):
18861886
with patch.dict("os.environ", env_vars):
18871887
metadata = copy.deepcopy(_METADATA)
1888-
metadata["driver"]["name"] = "PyMongo"
1888+
metadata["driver"]["name"] = "PyMongo|c"
18891889
if expected_env is not None:
18901890
metadata["env"] = expected_env
18911891

0 commit comments

Comments
 (0)