Skip to content

PYTHON-3046 Document support for backslashreplace and surrogateescape #836

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion bson/codec_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,8 @@ class CodecOptions(_options_base):
- `unicode_decode_error_handler`: The error handler to apply when
a Unicode-related error occurs during BSON decoding that would
otherwise raise :exc:`UnicodeDecodeError`. Valid options include
'strict', 'replace', and 'ignore'. Defaults to 'strict'.
'strict', 'replace', 'backslashreplace', 'surrogateescape', and
'ignore'. Defaults to 'strict'.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I decided not to link to https://docs.python.org/3/library/codecs.html#error-handlers here because not all of Python's builtin handlers work with bson decoding. For example xmlcharrefreplace, namereplace, and surrogatepass do not work.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. Makes sense.

- `tzinfo`: A :class:`~datetime.tzinfo` subclass that specifies the
timezone to/from which :class:`~datetime.datetime` objects should be
encoded/decoded.
Expand Down
3 changes: 2 additions & 1 deletion pymongo/mongo_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,8 @@ def __init__(
- `unicode_decode_error_handler`: The error handler to apply when
a Unicode-related error occurs during BSON decoding that would
otherwise raise :exc:`UnicodeDecodeError`. Valid options include
'strict', 'replace', and 'ignore'. Defaults to 'strict'.
'strict', 'replace', 'backslashreplace', 'surrogateescape', and
'ignore'. Defaults to 'strict'.
- `srvServiceName`: (string) The SRV service name to use for
"mongodb+srv://" URIs. Defaults to "mongodb". Use it like so::

Expand Down
71 changes: 23 additions & 48 deletions test/test_bson.py
Original file line number Diff line number Diff line change
Expand Up @@ -994,57 +994,32 @@ def test_decode_all_defaults(self):
def test_unicode_decode_error_handler(self):
enc = encode({"keystr": "foobar"})

# Test handling of bad key value.
# Test handling of bad key value, bad string value, and both.
invalid_key = enc[:7] + b'\xe9' + enc[8:]
replaced_key = b'ke\xe9str'.decode('utf-8', 'replace')
ignored_key = b'ke\xe9str'.decode('utf-8', 'ignore')

dec = decode(invalid_key,
CodecOptions(unicode_decode_error_handler="replace"))
self.assertEqual(dec, {replaced_key: "foobar"})

dec = decode(invalid_key,
CodecOptions(unicode_decode_error_handler="ignore"))
self.assertEqual(dec, {ignored_key: "foobar"})

self.assertRaises(InvalidBSON, decode, invalid_key, CodecOptions(
unicode_decode_error_handler="strict"))
self.assertRaises(InvalidBSON, decode, invalid_key, CodecOptions())
self.assertRaises(InvalidBSON, decode, invalid_key)

# Test handing of bad string value.
invalid_val = BSON(enc[:18] + b'\xe9' + enc[19:])
replaced_val = b'fo\xe9bar'.decode('utf-8', 'replace')
ignored_val = b'fo\xe9bar'.decode('utf-8', 'ignore')

dec = decode(invalid_val,
CodecOptions(unicode_decode_error_handler="replace"))
self.assertEqual(dec, {"keystr": replaced_val})

dec = decode(invalid_val,
CodecOptions(unicode_decode_error_handler="ignore"))
self.assertEqual(dec, {"keystr": ignored_val})

self.assertRaises(InvalidBSON, decode, invalid_val, CodecOptions(
unicode_decode_error_handler="strict"))
self.assertRaises(InvalidBSON, decode, invalid_val, CodecOptions())
self.assertRaises(InvalidBSON, decode, invalid_val)

# Test handing bad key + bad value.
invalid_val = enc[:18] + b'\xe9' + enc[19:]
invalid_both = enc[:7] + b'\xe9' + enc[8:18] + b'\xe9' + enc[19:]

dec = decode(invalid_both,
CodecOptions(unicode_decode_error_handler="replace"))
self.assertEqual(dec, {replaced_key: replaced_val})

dec = decode(invalid_both,
CodecOptions(unicode_decode_error_handler="ignore"))
self.assertEqual(dec, {ignored_key: ignored_val})

self.assertRaises(InvalidBSON, decode, invalid_both, CodecOptions(
unicode_decode_error_handler="strict"))
self.assertRaises(InvalidBSON, decode, invalid_both, CodecOptions())
self.assertRaises(InvalidBSON, decode, invalid_both)
# Ensure that strict mode raises an error.
for invalid in [invalid_key, invalid_val, invalid_both]:
self.assertRaises(InvalidBSON, decode, invalid, CodecOptions(
unicode_decode_error_handler="strict"))
self.assertRaises(InvalidBSON, decode, invalid, CodecOptions())
self.assertRaises(InvalidBSON, decode, invalid)

# Test all other error handlers.
for handler in ['replace', 'backslashreplace', 'surrogateescape',
'ignore']:
expected_key = b'ke\xe9str'.decode('utf-8', handler)
expected_val = b'fo\xe9bar'.decode('utf-8', handler)
doc = decode(invalid_key,
CodecOptions(unicode_decode_error_handler=handler))
self.assertEqual(doc, {expected_key: "foobar"})
doc = decode(invalid_val,
CodecOptions(unicode_decode_error_handler=handler))
self.assertEqual(doc, {"keystr": expected_val})
doc = decode(invalid_both,
CodecOptions(unicode_decode_error_handler=handler))
self.assertEqual(doc, {expected_key: expected_val})

# Test handling bad error mode.
dec = decode(enc,
Expand Down