Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PYTHON-1714 Add c extension use to client metadata #1874

Merged
merged 1 commit into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 1 addition & 11 deletions pymongo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.

Expand Down
10 changes: 10 additions & 0 deletions pymongo/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions pymongo/pool_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
MAX_POOL_SIZE,
MIN_POOL_SIZE,
WAIT_QUEUE_TIMEOUT,
has_c,
)

if TYPE_CHECKING:
Expand Down Expand Up @@ -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"],
Expand Down
17 changes: 13 additions & 4 deletions test/asynchronous/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
from pymongo.asynchronous.settings import TOPOLOGY_TYPE
from pymongo.asynchronous.topology import _ErrorContext
from pymongo.client_options import ClientOptions
from pymongo.common import _UUID_REPRESENTATIONS, CONNECT_TIMEOUT
from pymongo.common import _UUID_REPRESENTATIONS, CONNECT_TIMEOUT, has_c
from pymongo.compression_support import _have_snappy, _have_zstd
from pymongo.driver_info import DriverInfo
from pymongo.errors import (
Expand Down Expand Up @@ -343,7 +343,10 @@ async def test_read_preference(self):

async def test_metadata(self):
metadata = copy.deepcopy(_METADATA)
metadata["driver"]["name"] = "PyMongo|async"
if has_c():
metadata["driver"]["name"] = "PyMongo|c|async"
else:
metadata["driver"]["name"] = "PyMongo|async"
metadata["application"] = {"name": "foobar"}
client = self.simple_client("mongodb://foo:27017/?appname=foobar&connect=false")
options = client.options
Expand All @@ -366,7 +369,10 @@ 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"
if has_c():
metadata["driver"]["name"] = "PyMongo|c|async|FooDriver"
else:
metadata["driver"]["name"] = "PyMongo|async|FooDriver"
metadata["driver"]["version"] = "{}|1.2.3".format(_METADATA["driver"]["version"])
client = self.simple_client(
"foo",
Expand Down Expand Up @@ -1927,7 +1933,10 @@ 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"
if has_c():
metadata["driver"]["name"] = "PyMongo|c|async"
else:
metadata["driver"]["name"] = "PyMongo|async"
if expected_env is not None:
metadata["env"] = expected_env

Expand Down
17 changes: 13 additions & 4 deletions test/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
from bson.tz_util import utc
from pymongo import event_loggers, message, monitoring
from pymongo.client_options import ClientOptions
from pymongo.common import _UUID_REPRESENTATIONS, CONNECT_TIMEOUT
from pymongo.common import _UUID_REPRESENTATIONS, CONNECT_TIMEOUT, has_c
from pymongo.compression_support import _have_snappy, _have_zstd
from pymongo.driver_info import DriverInfo
from pymongo.errors import (
Expand Down Expand Up @@ -335,7 +335,10 @@ def test_read_preference(self):

def test_metadata(self):
metadata = copy.deepcopy(_METADATA)
metadata["driver"]["name"] = "PyMongo"
if has_c():
metadata["driver"]["name"] = "PyMongo|c"
else:
metadata["driver"]["name"] = "PyMongo"
metadata["application"] = {"name": "foobar"}
client = self.simple_client("mongodb://foo:27017/?appname=foobar&connect=false")
options = client.options
Expand All @@ -358,7 +361,10 @@ 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"
if has_c():
metadata["driver"]["name"] = "PyMongo|c|FooDriver"
else:
metadata["driver"]["name"] = "PyMongo|FooDriver"
metadata["driver"]["version"] = "{}|1.2.3".format(_METADATA["driver"]["version"])
client = self.simple_client(
"foo",
Expand Down Expand Up @@ -1885,7 +1891,10 @@ 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"
if has_c():
metadata["driver"]["name"] = "PyMongo|c"
else:
metadata["driver"]["name"] = "PyMongo"
if expected_env is not None:
metadata["env"] = expected_env

Expand Down
1 change: 1 addition & 0 deletions tools/synchro.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
"default_async": "default",
"aclose": "close",
"PyMongo|async": "PyMongo",
"PyMongo|c|async": "PyMongo|c",
"AsyncTestGridFile": "TestGridFile",
"AsyncTestGridFileNoConnect": "TestGridFileNoConnect",
}
Expand Down
Loading