Skip to content

Commit 083359f

Browse files
authored
PYTHON-1714 Add c extension use to client metadata (#1874)
1 parent 3ef565f commit 083359f

File tree

6 files changed

+44
-19
lines changed

6 files changed

+44
-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: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@
9999
from pymongo.asynchronous.settings import TOPOLOGY_TYPE
100100
from pymongo.asynchronous.topology import _ErrorContext
101101
from pymongo.client_options import ClientOptions
102-
from pymongo.common import _UUID_REPRESENTATIONS, CONNECT_TIMEOUT
102+
from pymongo.common import _UUID_REPRESENTATIONS, CONNECT_TIMEOUT, has_c
103103
from pymongo.compression_support import _have_snappy, _have_zstd
104104
from pymongo.driver_info import DriverInfo
105105
from pymongo.errors import (
@@ -347,7 +347,10 @@ async def test_read_preference(self):
347347

348348
async def test_metadata(self):
349349
metadata = copy.deepcopy(_METADATA)
350-
metadata["driver"]["name"] = "PyMongo|async"
350+
if has_c():
351+
metadata["driver"]["name"] = "PyMongo|c|async"
352+
else:
353+
metadata["driver"]["name"] = "PyMongo|async"
351354
metadata["application"] = {"name": "foobar"}
352355
client = self.simple_client("mongodb://foo:27017/?appname=foobar&connect=false")
353356
options = client.options
@@ -370,7 +373,10 @@ async def test_metadata(self):
370373
with self.assertRaises(TypeError):
371374
self.simple_client(driver=("Foo", "1", "a"))
372375
# Test appending to driver info.
373-
metadata["driver"]["name"] = "PyMongo|async|FooDriver"
376+
if has_c():
377+
metadata["driver"]["name"] = "PyMongo|c|async|FooDriver"
378+
else:
379+
metadata["driver"]["name"] = "PyMongo|async|FooDriver"
374380
metadata["driver"]["version"] = "{}|1.2.3".format(_METADATA["driver"]["version"])
375381
client = self.simple_client(
376382
"foo",
@@ -1931,7 +1937,10 @@ def test_sigstop_sigcont(self):
19311937
async def _test_handshake(self, env_vars, expected_env):
19321938
with patch.dict("os.environ", env_vars):
19331939
metadata = copy.deepcopy(_METADATA)
1934-
metadata["driver"]["name"] = "PyMongo|async"
1940+
if has_c():
1941+
metadata["driver"]["name"] = "PyMongo|c|async"
1942+
else:
1943+
metadata["driver"]["name"] = "PyMongo|async"
19351944
if expected_env is not None:
19361945
metadata["env"] = expected_env
19371946

test/test_client.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787
from bson.tz_util import utc
8888
from pymongo import event_loggers, message, monitoring
8989
from pymongo.client_options import ClientOptions
90-
from pymongo.common import _UUID_REPRESENTATIONS, CONNECT_TIMEOUT
90+
from pymongo.common import _UUID_REPRESENTATIONS, CONNECT_TIMEOUT, has_c
9191
from pymongo.compression_support import _have_snappy, _have_zstd
9292
from pymongo.driver_info import DriverInfo
9393
from pymongo.errors import (
@@ -339,7 +339,10 @@ def test_read_preference(self):
339339

340340
def test_metadata(self):
341341
metadata = copy.deepcopy(_METADATA)
342-
metadata["driver"]["name"] = "PyMongo"
342+
if has_c():
343+
metadata["driver"]["name"] = "PyMongo|c"
344+
else:
345+
metadata["driver"]["name"] = "PyMongo"
343346
metadata["application"] = {"name": "foobar"}
344347
client = self.simple_client("mongodb://foo:27017/?appname=foobar&connect=false")
345348
options = client.options
@@ -362,7 +365,10 @@ def test_metadata(self):
362365
with self.assertRaises(TypeError):
363366
self.simple_client(driver=("Foo", "1", "a"))
364367
# Test appending to driver info.
365-
metadata["driver"]["name"] = "PyMongo|FooDriver"
368+
if has_c():
369+
metadata["driver"]["name"] = "PyMongo|c|FooDriver"
370+
else:
371+
metadata["driver"]["name"] = "PyMongo|FooDriver"
366372
metadata["driver"]["version"] = "{}|1.2.3".format(_METADATA["driver"]["version"])
367373
client = self.simple_client(
368374
"foo",
@@ -1889,7 +1895,10 @@ def test_sigstop_sigcont(self):
18891895
def _test_handshake(self, env_vars, expected_env):
18901896
with patch.dict("os.environ", env_vars):
18911897
metadata = copy.deepcopy(_METADATA)
1892-
metadata["driver"]["name"] = "PyMongo"
1898+
if has_c():
1899+
metadata["driver"]["name"] = "PyMongo|c"
1900+
else:
1901+
metadata["driver"]["name"] = "PyMongo"
18931902
if expected_env is not None:
18941903
metadata["env"] = expected_env
18951904

tools/synchro.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
"default_async": "default",
102102
"aclose": "close",
103103
"PyMongo|async": "PyMongo",
104+
"PyMongo|c|async": "PyMongo|c",
104105
"AsyncTestGridFile": "TestGridFile",
105106
"AsyncTestGridFileNoConnect": "TestGridFileNoConnect",
106107
}

0 commit comments

Comments
 (0)