Skip to content

Commit 8cb4ea5

Browse files
authored
Merge branch 'master' into PYTHON-5572
2 parents 999a794 + 29c4c2c commit 8cb4ea5

File tree

9 files changed

+60
-32
lines changed

9 files changed

+60
-32
lines changed

.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

.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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
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+
413
Changes in Version 4.15.1 (2025/09/16)
514
--------------------------------------
615

justfile

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# See https://just.systems/man/en/ for instructions
22
set shell := ["bash", "-c"]
3-
# Do not modify the lock file when running justfile commands.
4-
export UV_FROZEN := "1"
53

64
# Commonly used command segments.
75
typing_run := "uv run --group typing --extra aws --extra encryption --extra ocsp --extra snappy --extra test --extra zstd"
@@ -16,7 +14,7 @@ default:
1614

1715
[private]
1816
resync:
19-
@uv sync --quiet --frozen
17+
@uv sync --quiet
2018

2119
install:
2220
bash .evergreen/scripts/setup-dev-env.sh

test/test_bson.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,7 +1163,7 @@ def __repr__(self):
11631163
):
11641164
encode({"t": Wrapper(1)})
11651165

1166-
def test_doc_in_invalid_document_error_message(self):
1166+
def test_doc_in_invalid_document_error_as_property(self):
11671167
class Wrapper:
11681168
def __init__(self, val):
11691169
self.val = val
@@ -1173,10 +1173,11 @@ def __repr__(self):
11731173

11741174
self.assertEqual("1", repr(Wrapper(1)))
11751175
doc = {"t": Wrapper(1)}
1176-
with self.assertRaisesRegex(InvalidDocument, f"Invalid document {doc}"):
1176+
with self.assertRaisesRegex(InvalidDocument, "Invalid document:") as cm:
11771177
encode(doc)
1178+
self.assertEqual(cm.exception.document, doc)
11781179

1179-
def test_doc_in_invalid_document_error_message_mapping(self):
1180+
def test_doc_in_invalid_document_error_as_property_mapping(self):
11801181
class MyMapping(abc.Mapping):
11811182
def keys(self):
11821183
return ["t"]
@@ -1192,6 +1193,11 @@ def __len__(self):
11921193
def __iter__(self):
11931194
return iter(["t"])
11941195

1196+
def __eq__(self, other):
1197+
if isinstance(other, MyMapping):
1198+
return True
1199+
return False
1200+
11951201
class Wrapper:
11961202
def __init__(self, val):
11971203
self.val = val
@@ -1201,8 +1207,9 @@ def __repr__(self):
12011207

12021208
self.assertEqual("1", repr(Wrapper(1)))
12031209
doc = MyMapping()
1204-
with self.assertRaisesRegex(InvalidDocument, f"Invalid document {doc}"):
1210+
with self.assertRaisesRegex(InvalidDocument, "Invalid document:") as cm:
12051211
encode(doc)
1212+
self.assertEqual(cm.exception.document, doc)
12061213

12071214

12081215
class TestCodecOptions(unittest.TestCase):

uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)