Skip to content

Commit 47db389

Browse files
committed
Merge branch 'master' of github.com:mongodb/mongo-python-driver
2 parents 35698e0 + 1b89da4 commit 47db389

File tree

6 files changed

+50
-2
lines changed

6 files changed

+50
-2
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ repos:
2424
entry: bash ./tools/synchro.sh
2525
language: python
2626
require_serial: true
27+
fail_fast: true
2728
additional_dependencies:
2829
- ruff==0.1.3
2930
- unasync

CONTRIBUTING.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,18 @@ you are attempting to validate new spec tests in PyMongo.
246246

247247
Follow the [Python Driver Release Process Wiki](https://wiki.corp.mongodb.com/display/DRIVERS/Python+Driver+Release+Process).
248248

249+
## Asyncio considerations
250+
251+
PyMongo adds asyncio capability by modifying the source files in `*/asynchronous` to `*/synchronous` using
252+
[unasync](https://github.com/python-trio/unasync/) and some custom transforms.
253+
254+
Where possible, edit the code in `*/asynchronous/*.py` and not the synchronous files.
255+
You can run `pre-commit run --all-files synchro` before running tests if you are testing synchronous code.
256+
257+
To prevent the `synchro` hook from accidentally overwriting code, it first checks to see whether a sync version
258+
of a file is changing and not its async counterpart, and will fail.
259+
In the unlikely scenario that you want to override this behavior, first export `OVERRIDE_SYNCHRO_CHECK=1`.
260+
249261
## Converting a test to async
250262
The `tools/convert_test_to_async.py` script takes in an existing synchronous test file and outputs a
251263
partially-converted asynchronous version of the same name to the `test/asynchronous` directory.

test/asynchronous/test_encryption.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@
4242
from pymongo.asynchronous.helpers import anext
4343
from pymongo.daemon import _spawn_daemon
4444

45+
try:
46+
from pymongo.pyopenssl_context import IS_PYOPENSSL
47+
except ImportError:
48+
IS_PYOPENSSL = False
49+
4550
sys.path[0:0] = [""]
4651

4752
from test import (
@@ -2921,6 +2926,10 @@ async def _test(self, provider, master_key):
29212926
await self.client_encryption.create_data_key(provider, master_key=master_key)
29222927

29232928
async def test_kms_retry(self):
2929+
if IS_PYOPENSSL:
2930+
self.skipTest(
2931+
"PyOpenSSL does not support a required method for this test, Connection.makefile"
2932+
)
29242933
await self._test("aws", {"region": "foo", "key": "bar", "endpoint": "127.0.0.1:9003"})
29252934
await self._test("azure", {"keyVaultEndpoint": "127.0.0.1:9003", "keyName": "foo"})
29262935
await self._test(

test/test_encryption.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@
4242
from pymongo.synchronous.collection import Collection
4343
from pymongo.synchronous.helpers import next
4444

45+
try:
46+
from pymongo.pyopenssl_context import IS_PYOPENSSL
47+
except ImportError:
48+
IS_PYOPENSSL = False
49+
4550
sys.path[0:0] = [""]
4651

4752
from test import (
@@ -2903,6 +2908,10 @@ def _test(self, provider, master_key):
29032908
self.client_encryption.create_data_key(provider, master_key=master_key)
29042909

29052910
def test_kms_retry(self):
2911+
if IS_PYOPENSSL:
2912+
self.skipTest(
2913+
"PyOpenSSL does not support a required method for this test, Connection.makefile"
2914+
)
29062915
self._test("aws", {"region": "foo", "key": "bar", "endpoint": "127.0.0.1:9003"})
29072916
self._test("azure", {"keyVaultEndpoint": "127.0.0.1:9003", "keyName": "foo"})
29082917
self._test(

tools/synchro.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919

2020
from __future__ import annotations
2121

22+
import os
2223
import re
24+
import sys
2325
from os import listdir
2426
from pathlib import Path
2527

@@ -356,6 +358,19 @@ def unasync_directory(files: list[str], src: str, dest: str, replacements: dict[
356358

357359

358360
def main() -> None:
361+
modified_files = [f"./{f}" for f in sys.argv[1:]]
362+
errored = False
363+
for fname in async_files + gridfs_files:
364+
# If the async file was modified, we don't need to check if the sync file was also modified.
365+
if str(fname) in modified_files:
366+
continue
367+
sync_name = str(fname).replace("asynchronous", "synchronous")
368+
if sync_name in modified_files and "OVERRIDE_SYNCHRO_CHECK" not in os.environ:
369+
print(f"Refusing to overwrite {sync_name}")
370+
errored = True
371+
if errored:
372+
raise ValueError("Aborting synchro due to errors")
373+
359374
unasync_directory(async_files, _pymongo_base, _pymongo_dest_base, replacements)
360375
unasync_directory(gridfs_files, _gridfs_base, _gridfs_dest_base, replacements)
361376
unasync_directory(test_files, _test_base, _test_dest_base, replacements)

tools/synchro.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
#!/bin/bash -eu
1+
#!/bin/bash
22

3-
python ./tools/synchro.py
3+
set -eu
4+
5+
python ./tools/synchro.py "$@"
46
python -m ruff check pymongo/synchronous/ gridfs/synchronous/ test/ --fix --silent
57
python -m ruff format pymongo/synchronous/ gridfs/synchronous/ test/ --silent

0 commit comments

Comments
 (0)