Skip to content

Commit af18e5f

Browse files
committed
Merge branch 'master' of github.com:mongodb/mongo-python-driver
2 parents 8082ca0 + 9e64ed1 commit af18e5f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+277
-223
lines changed

.evergreen/generated_configs/variants.yml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -319,14 +319,6 @@ buildvariants:
319319
tags: []
320320

321321
# Green framework tests
322-
- name: green-eventlet-rhel8
323-
tasks:
324-
- name: .test-standard .python-3.9 .sync
325-
display_name: Green Eventlet RHEL8
326-
run_on:
327-
- rhel87-small
328-
expansions:
329-
GREEN_FRAMEWORK: eventlet
330322
- name: green-gevent-rhel8
331323
tasks:
332324
- name: .test-standard .sync

.evergreen/scripts/generate_config.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -300,12 +300,8 @@ def create_stable_api_variants():
300300
def create_green_framework_variants():
301301
variants = []
302302
host = DEFAULT_HOST
303-
for framework in ["eventlet", "gevent"]:
303+
for framework in ["gevent"]:
304304
tasks = [".test-standard .sync"]
305-
if framework == "eventlet":
306-
# Eventlet has issues with dnspython > 2.0 and newer versions of CPython
307-
# https://jira.mongodb.org/browse/PYTHON-5284
308-
tasks = [".test-standard .python-3.9 .sync"]
309305
expansions = dict(GREEN_FRAMEWORK=framework)
310306
display_name = get_variant_name(f"Green {framework.capitalize()}", host)
311307
variant = create_variant(tasks, display_name, host=host, expansions=expansions)

.evergreen/scripts/run_tests.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,7 @@ def handle_perf(start_time: datetime):
6767

6868

6969
def handle_green_framework() -> None:
70-
if GREEN_FRAMEWORK == "eventlet":
71-
import eventlet
72-
73-
# https://github.com/eventlet/eventlet/issues/401
74-
eventlet.sleep()
75-
eventlet.monkey_patch()
76-
elif GREEN_FRAMEWORK == "gevent":
70+
if GREEN_FRAMEWORK == "gevent":
7771
from gevent import monkey
7872

7973
monkey.patch_all()

.evergreen/scripts/setup-dev-env.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ if [ -f $HOME/.visualStudioEnv.sh ]; then
4747
SSH_TTY=1 source $HOME/.visualStudioEnv.sh
4848
set -u
4949
fi
50-
uv sync --frozen
50+
uv sync
5151

5252
echo "Setting up python environment... done."
5353

5454
# Ensure there is a pre-commit hook if there is a git checkout.
5555
if [ -d .git ] && [ ! -f .git/hooks/pre-commit ]; then
56-
uv run --frozen pre-commit install
56+
uv run pre-commit install
5757
fi
5858

5959
popd > /dev/null

.evergreen/scripts/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def get_test_options(
104104
parser.add_argument(
105105
"--green-framework",
106106
nargs=1,
107-
choices=["eventlet", "gevent"],
107+
choices=["gevent"],
108108
help="Optional green framework to test against.",
109109
)
110110
parser.add_argument(

.pre-commit-config.yaml

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,6 @@ repos:
105105
# - test/test_client.py:188: te ==> the, be, we, to
106106
args: ["-L", "fle,fo,infinit,isnt,nin,te,aks"]
107107

108-
- repo: https://github.com/astral-sh/uv-pre-commit
109-
# uv version.
110-
rev: 0.8.17
111-
hooks:
112-
- id: uv-lock
113-
114108
- repo: local
115109
hooks:
116110
- id: executable-shell
@@ -128,3 +122,14 @@ repos:
128122
language: python
129123
require_serial: true
130124
additional_dependencies: ["shrub.py>=3.10.0", "pyyaml>=6.0.2"]
125+
126+
- id: uv-lock
127+
name: uv-lock
128+
entry: uv lock
129+
language: python
130+
require_serial: true
131+
files: ^(uv\.lock|pyproject\.toml|requirements.txt|requirements/.*\.txt)$
132+
pass_filenames: false
133+
fail_fast: true
134+
additional_dependencies:
135+
- "uv>=0.8.4"

bson/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1009,7 +1009,7 @@ def _dict_to_bson(
10091009
try:
10101010
elements.append(_element_to_bson(key, value, check_keys, opts))
10111011
except InvalidDocument as err:
1012-
raise InvalidDocument(f"Invalid document {doc} | {err}") from err
1012+
raise InvalidDocument(f"Invalid document: {err}", doc) from err
10131013
except AttributeError:
10141014
raise TypeError(f"encoder expected a mapping type but got: {doc!r}") from None
10151015

bson/_cbsonmodule.c

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1645,11 +1645,11 @@ static int write_raw_doc(buffer_t buffer, PyObject* raw, PyObject* _raw_str) {
16451645
}
16461646

16471647

1648-
/* Update Invalid Document error message to include doc.
1648+
/* Update Invalid Document error to include doc as a property.
16491649
*/
16501650
void handle_invalid_doc_error(PyObject* dict) {
16511651
PyObject *etype = NULL, *evalue = NULL, *etrace = NULL;
1652-
PyObject *msg = NULL, *dict_str = NULL, *new_msg = NULL;
1652+
PyObject *msg = NULL, *new_msg = NULL, *new_evalue = NULL;
16531653
PyErr_Fetch(&etype, &evalue, &etrace);
16541654
PyObject *InvalidDocument = _error("InvalidDocument");
16551655
if (InvalidDocument == NULL) {
@@ -1659,26 +1659,22 @@ void handle_invalid_doc_error(PyObject* dict) {
16591659
if (evalue && PyErr_GivenExceptionMatches(etype, InvalidDocument)) {
16601660
PyObject *msg = PyObject_Str(evalue);
16611661
if (msg) {
1662-
// Prepend doc to the existing message
1663-
PyObject *dict_str = PyObject_Str(dict);
1664-
if (dict_str == NULL) {
1665-
goto cleanup;
1666-
}
1667-
const char * dict_str_utf8 = PyUnicode_AsUTF8(dict_str);
1668-
if (dict_str_utf8 == NULL) {
1669-
goto cleanup;
1670-
}
16711662
const char * msg_utf8 = PyUnicode_AsUTF8(msg);
16721663
if (msg_utf8 == NULL) {
16731664
goto cleanup;
16741665
}
1675-
PyObject *new_msg = PyUnicode_FromFormat("Invalid document %s | %s", dict_str_utf8, msg_utf8);
1666+
PyObject *new_msg = PyUnicode_FromFormat("Invalid document: %s", msg_utf8);
1667+
if (new_msg == NULL) {
1668+
goto cleanup;
1669+
}
1670+
// Add doc to the error instance as a property.
1671+
PyObject *new_evalue = PyObject_CallFunctionObjArgs(InvalidDocument, new_msg, dict, NULL);
16761672
Py_DECREF(evalue);
16771673
Py_DECREF(etype);
16781674
etype = InvalidDocument;
16791675
InvalidDocument = NULL;
1680-
if (new_msg) {
1681-
evalue = new_msg;
1676+
if (new_evalue) {
1677+
evalue = new_evalue;
16821678
} else {
16831679
evalue = msg;
16841680
}
@@ -1689,7 +1685,7 @@ void handle_invalid_doc_error(PyObject* dict) {
16891685
PyErr_Restore(etype, evalue, etrace);
16901686
Py_XDECREF(msg);
16911687
Py_XDECREF(InvalidDocument);
1692-
Py_XDECREF(dict_str);
1688+
Py_XDECREF(new_evalue);
16931689
Py_XDECREF(new_msg);
16941690
}
16951691

bson/errors.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
"""Exceptions raised by the BSON package."""
1616
from __future__ import annotations
1717

18+
from typing import Any, Optional
19+
1820

1921
class BSONError(Exception):
2022
"""Base class for all BSON exceptions."""
@@ -31,6 +33,17 @@ class InvalidStringData(BSONError):
3133
class InvalidDocument(BSONError):
3234
"""Raised when trying to create a BSON object from an invalid document."""
3335

36+
def __init__(self, message: str, document: Optional[Any] = None) -> None:
37+
super().__init__(message)
38+
self._document = document
39+
40+
@property
41+
def document(self) -> Any:
42+
"""The invalid document that caused the error.
43+
44+
..versionadded:: 4.16"""
45+
return self._document
46+
3447

3548
class InvalidId(BSONError):
3649
"""Raised when trying to create an ObjectId from invalid data."""

doc/changelog.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
Changelog
22
=========
33

4+
Changes in Version 4.16.0 (XXXX/XX/XX)
5+
--------------------------------------
6+
7+
PyMongo 4.16 brings a number of changes including:
8+
9+
- Removed invalid documents from :class:`bson.errors.InvalidDocument` error messages as
10+
doing so may leak sensitive user data.
11+
Instead, invalid documents are stored in :attr:`bson.errors.InvalidDocument.document`.
12+
- Removed support for Eventlet.
13+
Eventlet is actively being sunset by its maintainers and has compatibility issues with PyMongo's dnspython dependency.
14+
415
Changes in Version 4.15.1 (2025/09/16)
516
--------------------------------------
617

0 commit comments

Comments
 (0)