Skip to content

PYTHON-5676 Consolidate Command Execution Logic#2789

Draft
aclark4life wants to merge 3 commits intomongodb:masterfrom
aclark4life:PYTHON-5676
Draft

PYTHON-5676 Consolidate Command Execution Logic#2789
aclark4life wants to merge 3 commits intomongodb:masterfrom
aclark4life:PYTHON-5676

Conversation

@aclark4life
Copy link
Copy Markdown
Contributor

PYTHON-5676

Changes in this PR

Test Plan

Checklist

Checklist for Author

  • Did you update the changelog (if necessary)?
  • Is there test coverage?
  • Is any followup work tracked in a JIRA ticket? If so, add link(s).

Checklist for Reviewer

  • Does the title of the PR reference a JIRA Ticket?
  • Do you fully understand the implementation? (Would you be comfortable explaining how this code works to someone else?)
  • Is all relevant documentation (README or docstring) updated?

…ion()

Since MIN_SUPPORTED_WIRE_VERSION=8 (MongoDB 4.2+), use_command() always
returns True and OP_MSG is always used. Remove the unreachable OP_QUERY
branches from Server.run_operation(), along with the dead else-branches
in Cursor and CommandCursor that referenced _OpReply.

- Hard-code use_cmd=True / apply_timeout=True in run_operation()
- Always use user_fields=_CURSOR_DOC_FIELDS and legacy_response=False
- Remove isinstance(reply, _OpMsg) exhaust check (reply is always _OpMsg)
- Hard-code from_command=True in Response/PinnedResponse construction
- Remove dead else-branches with assert isinstance(response.data, _OpReply)
  in Cursor._send_message and CommandCursor._send_message
- Add scope document PYTHON-5676-scope.md outlining all four phases
Eliminate the ~13-line _COMMAND_LOGGER/_debug_log boilerplate that was
copy-pasted identically across five command execution paths. Add three
helpers to pymongo/command_helpers.py and call them from all five sites.

- New pymongo/command_helpers.py: _log_command_started, _log_command_succeeded,
  _log_command_failed — pure functions, no I/O, no async/sync split needed
- network.py, server.py, bulk.py, client_bulk.py: replace inline _debug_log
  blocks with helper calls; remove _COMMAND_LOGGER/_CommandStatusMessage
  imports where no longer needed
Extract _network_command_core() from network.command() to handle the
common send/receive/APM/decrypt pipeline. Both the standard command
path and the cursor (find/getMore) path now use the same core function.

- New _network_command_core() in network.py: takes a pre-built message
  and handles APM, send_message, receive_message, unpack, process,
  check, decrypt — returns (docs, reply, duration)
- command() is now a thin wrapper: encode → _network_command_core → return docs[0]
- run_operation() is now a thin wrapper: build message → _network_command_core
  → wrap in Response/PinnedResponse
- _CURSOR_DOC_FIELDS moved from server.py to network.py (only used in core)
- server.py drops _decode_all_selective, _check_command_response,
  _convert_exception, NotPrimaryError, OperationFailure imports
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant