Skip to content

Commit 376e81b

Browse files
committed
POC: rename network_layer.py to network_layer_shared.py
Rename pymongo/network_layer.py to pymongo/network_layer_shared.py to make explicit that this file is intentionally shared between async and sync code, and is not processed by synchro. Update all 9 import sites. Add clarifying module docstrings to periodic_executor.py and lock.py explaining why they also live in the top-level package alongside both async and sync implementations.
1 parent ab44a21 commit 376e81b

10 files changed

Lines changed: 42 additions & 12 deletions

File tree

pymongo/asynchronous/encryption.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
ServerSelectionTimeoutError,
8282
)
8383
from pymongo.helpers_shared import _get_timeout_details
84-
from pymongo.network_layer import async_socket_sendall
84+
from pymongo.network_layer_shared import async_socket_sendall
8585
from pymongo.operations import UpdateOne
8686
from pymongo.pool_options import PoolOptions
8787
from pymongo.pool_shared import (
@@ -211,7 +211,7 @@ async def kms_request(self, kms_context: MongoCryptKmsContext) -> None:
211211
if _IS_SYNC:
212212
data = conn.recv(kms_context.bytes_needed)
213213
else:
214-
from pymongo.network_layer import ( # type: ignore[attr-defined]
214+
from pymongo.network_layer_shared import ( # type: ignore[attr-defined]
215215
async_receive_data_socket,
216216
)
217217

pymongo/asynchronous/network.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
from pymongo.logger import _COMMAND_LOGGER, _CommandStatusMessage, _debug_log
3939
from pymongo.message import _OpMsg
4040
from pymongo.monitoring import _is_speculative_authenticate
41-
from pymongo.network_layer import (
41+
from pymongo.network_layer_shared import (
4242
async_receive_message,
4343
async_sendall,
4444
)

pymongo/asynchronous/pool.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,11 @@
7878
ConnectionCheckOutFailedReason,
7979
ConnectionClosedReason,
8080
)
81-
from pymongo.network_layer import AsyncNetworkingInterface, async_receive_message, async_sendall
81+
from pymongo.network_layer_shared import (
82+
AsyncNetworkingInterface,
83+
async_receive_message,
84+
async_sendall,
85+
)
8286
from pymongo.pool_options import PoolOptions
8387
from pymongo.pool_shared import (
8488
SSLErrors,

pymongo/lock.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
"""Internal helpers for lock and condition coordination primitives."""
15+
"""Internal helpers for lock and condition coordination primitives, shared by async and sync.
16+
17+
This module is intentionally kept in the top-level pymongo/ package because it
18+
provides both threading.Lock (for sync code) and the asyncio Lock/Condition
19+
backports (for async code). They co-exist here to give callers a single import
20+
point. This file is not processed by synchro."""
1621

1722
from __future__ import annotations
1823

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,14 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
"""Internal network layer helper methods."""
15+
"""Internal network layer helper methods, shared by both async and sync code.
16+
17+
This module is intentionally kept in the top-level pymongo/ package (rather than
18+
pymongo/asynchronous/ or pymongo/synchronous/) because it contains both the async
19+
networking classes (AsyncNetworkingInterface, PyMongoProtocol) and the sync
20+
NetworkingInterface. The two implementations share enough internal logic that
21+
splitting them would require duplication or a complex import dependency chain.
22+
This file is not processed by synchro."""
1623
from __future__ import annotations
1724

1825
import asyncio

pymongo/periodic_executor.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@
1212
# implied. See the License for the specific language governing
1313
# permissions and limitations under the License.
1414

15-
"""Run a target function on a background thread."""
15+
"""Periodic executor for background tasks, shared by both async and sync code.
16+
17+
This module is intentionally kept in the top-level pymongo/ package because it
18+
defines both AsyncPeriodicExecutor (asyncio task-based) and PeriodicExecutor
19+
(threading-based). The two classes are fundamentally different at runtime but
20+
share an interface, and synchro cannot auto-generate one from the other.
21+
This file is not processed by synchro."""
1622

1723
from __future__ import annotations
1824

pymongo/pool_shared.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@
3737
_CertificateError,
3838
)
3939
from pymongo.helpers_shared import _get_timeout_details, format_timeout_details
40-
from pymongo.network_layer import AsyncNetworkingInterface, NetworkingInterface, PyMongoProtocol
40+
from pymongo.network_layer_shared import (
41+
AsyncNetworkingInterface,
42+
NetworkingInterface,
43+
PyMongoProtocol,
44+
)
4145
from pymongo.pool_options import PoolOptions
4246
from pymongo.ssl_support import PYSSLError, SSLError, _has_sni
4347

pymongo/synchronous/encryption.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
ServerSelectionTimeoutError,
7777
)
7878
from pymongo.helpers_shared import _get_timeout_details
79-
from pymongo.network_layer import sendall
79+
from pymongo.network_layer_shared import sendall
8080
from pymongo.operations import UpdateOne
8181
from pymongo.pool_options import PoolOptions
8282
from pymongo.pool_shared import (
@@ -210,7 +210,7 @@ def kms_request(self, kms_context: MongoCryptKmsContext) -> None:
210210
if _IS_SYNC:
211211
data = conn.recv(kms_context.bytes_needed)
212212
else:
213-
from pymongo.network_layer import ( # type: ignore[attr-defined]
213+
from pymongo.network_layer_shared import ( # type: ignore[attr-defined]
214214
receive_data_socket,
215215
)
216216

pymongo/synchronous/network.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
from pymongo.logger import _COMMAND_LOGGER, _CommandStatusMessage, _debug_log
3939
from pymongo.message import _OpMsg
4040
from pymongo.monitoring import _is_speculative_authenticate
41-
from pymongo.network_layer import (
41+
from pymongo.network_layer_shared import (
4242
receive_message,
4343
sendall,
4444
)

pymongo/synchronous/pool.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,11 @@
7575
ConnectionCheckOutFailedReason,
7676
ConnectionClosedReason,
7777
)
78-
from pymongo.network_layer import NetworkingInterface, receive_message, sendall
78+
from pymongo.network_layer_shared import (
79+
NetworkingInterface,
80+
receive_message,
81+
sendall,
82+
)
7983
from pymongo.pool_options import PoolOptions
8084
from pymongo.pool_shared import (
8185
SSLErrors,

0 commit comments

Comments
 (0)