-
Notifications
You must be signed in to change notification settings - Fork 1.1k
PYTHON-5289 Validate ignored bits are 0 on write for bson.BinaryVector #2397
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
Open
caseyclements
wants to merge
4
commits into
mongodb:master
Choose a base branch
from
caseyclements:PYTHON-5280-EncodingRequiresZeros
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
0e8bdf6
BSONVector - updated treatment of ignored bits to match spec.
caseyclements 34c0ecf
Manually removed test case: PACKED_BIT with inconsistent padding. Thi…
caseyclements 8199493
Updated changelog
caseyclements 093a6dc
Fix typing by adding BinaryVector.__len__
caseyclements File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -739,7 +739,7 @@ def test_vector(self): | |
"""Tests of subtype 9""" | ||
# We start with valid cases, across the 3 dtypes implemented. | ||
# Work with a simple vector that can be interpreted as int8, float32, or ubyte | ||
list_vector = [127, 7] | ||
list_vector = [127, 8] | ||
# As INT8, vector has length 2 | ||
binary_vector = Binary.from_vector(list_vector, BinaryVectorDtype.INT8) | ||
vector = binary_vector.as_vector() | ||
|
@@ -764,18 +764,18 @@ def test_vector(self): | |
uncompressed = "" | ||
for val in list_vector: | ||
uncompressed += format(val, "08b") | ||
assert uncompressed[:-padding] == "0111111100000" | ||
assert uncompressed[:-padding] == "0111111100001" | ||
|
||
# It is worthwhile explicitly showing the values encoded to BSON | ||
padded_doc = {"padded_vec": padded_vec} | ||
assert ( | ||
encode(padded_doc) | ||
== b"\x1a\x00\x00\x00\x05padded_vec\x00\x04\x00\x00\x00\t\x10\x03\x7f\x07\x00" | ||
== b"\x1a\x00\x00\x00\x05padded_vec\x00\x04\x00\x00\x00\t\x10\x03\x7f\x08\x00" | ||
) | ||
# and dumped to json | ||
assert ( | ||
json_util.dumps(padded_doc) | ||
== '{"padded_vec": {"$binary": {"base64": "EAN/Bw==", "subType": "09"}}}' | ||
== '{"padded_vec": {"$binary": {"base64": "EAN/CA==", "subType": "09"}}}' | ||
) | ||
|
||
# FLOAT32 is also implemented | ||
|
@@ -791,8 +791,15 @@ def test_vector(self): | |
else: | ||
self.fail("Failed to raise an exception.") | ||
|
||
# Test form of Binary.from_vector(BinaryVector) | ||
# Test one must pass zeros for all ignored bits | ||
try: | ||
Binary.from_vector([255], BinaryVectorDtype.PACKED_BIT, padding=7) | ||
except Exception as exc: | ||
self.assertIsInstance(exc, ValueError) | ||
else: | ||
self.fail("Failed to raise an exception.") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This assertion should use assertRaises. |
||
|
||
# Test form of Binary.from_vector(BinaryVector) | ||
assert padded_vec == Binary.from_vector( | ||
BinaryVector(list_vector, BinaryVectorDtype.PACKED_BIT, padding) | ||
) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So in the next major version, this warning will become an error to match the behavior in
from_vector
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. We make the significant change (exception not warning) in the next major change and document in changelog and api. The ticket for that is PYTHON-5280 and has 5.0 as its fix version.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add "pymongo version 5.0" rather than "the next major version". This helps should them the version (5.0) and that the warning is coming from pymongo.