-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
PYTHON-1982 Update Invalid Document error message to include doc #1854
base: master
Are you sure you want to change the base?
Conversation
Thanks @navjots18! It looks like there is a segmentation fault in the C implementation. Are you able to reproduce that failure locally? I can take a closer look on Monday, but https://dnmtechs.com/handling-segmentation-fault-errors-in-python-3-programming/ is a good resource. |
Hi @blink1073 , it was happening because i was assigning some variables to eType and eValue and then using Py_DECREF on the variables. Changed this which fixed the segmentation error on my local |
bson/_cbsonmodule.c
Outdated
PyErr_Fetch(&etype, &evalue, &etrace); | ||
PyObject *InvalidDocument = _error("InvalidDocument"); | ||
|
||
if (PyErr_GivenExceptionMatches(etype, InvalidDocument)) { |
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.
Can you please reduce the nesting by using if (InvalidDocument && PyErr_GivenExceptionMatches(etype, InvalidDocument))
?
try: | ||
elements.append(_element_to_bson(key, value, check_keys, opts)) | ||
except InvalidDocument as err: | ||
raise InvalidDocument(f"Invalid document {doc} | {err}") from err |
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.
I'm not sure this is the pattern we want, in the C version of this code we recursively call write_dict() for subdocuments which means if we end up erroring in a nested field the error will be something like:
Invalid document {"a": {"b": {"c": ...}}} | Invalid document {"b": {"c": ...}} | Invalid document {"c": ...} | ...
Is that the intended behavior?
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.
@ShaneHarvey You're right, we can check the top_level param to prevent this from happening
if (PyErr_Occurred()) { | ||
PyObject *etype, *evalue, *etrace; | ||
PyErr_Fetch(&etype, &evalue, &etrace); | ||
PyObject *InvalidDocument = _error("InvalidDocument"); |
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.
There's a lot of missing error/NULL checks in this code. I'm not comfortable merging this for 4.9 so it will need to wait for the next release.
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.
@blink1073 @ShaneHarvey I took context from code written in the same file, would appreciate if can you point out some existing code from where i can see what all error/Nulls check to put here?
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.
Hi @navjots18, apologies for the delay. I think all we're missing in this block is initializing the etype
, evalue
, and etrace
to NULL as we do here. Otherwise I agree this matches the rest of the existing code block.
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.
Hey @blink1073 fixed these issues, can you review again?
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.
LGTM, thank you!
Updated bson and cbson code to add the doc in the Invalid Document error message and added Unit test cases.
Note: Make sure to run to
python _setup.py build_ext --inplace
to install C file changes