Skip to content
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

Merged
merged 5 commits into from
Dec 3, 2024

Conversation

navjots18
Copy link
Contributor

@navjots18 navjots18 commented Sep 12, 2024

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

@navjots18 navjots18 changed the title Updated Invalid Document error message to have doc Updated Invalid Document error message to include doc Sep 13, 2024
@blink1073
Copy link
Member

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.

@navjots18
Copy link
Contributor Author

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

@blink1073 blink1073 requested review from blink1073 and removed request for Jibola September 13, 2024 16:29
PyErr_Fetch(&etype, &evalue, &etrace);
PyObject *InvalidDocument = _error("InvalidDocument");

if (PyErr_GivenExceptionMatches(etype, InvalidDocument)) {
Copy link
Member

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
Copy link
Member

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?

Copy link
Contributor Author

@navjots18 navjots18 Sep 16, 2024

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");
Copy link
Member

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.

Copy link
Contributor Author

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?

Copy link
Member

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.

Copy link
Contributor Author

@navjots18 navjots18 Nov 7, 2024

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?

@blink1073 blink1073 changed the title Updated Invalid Document error message to include doc PYTHON-1982 Updated Invalid Document error message to include doc Nov 12, 2024
@blink1073 blink1073 changed the title PYTHON-1982 Updated Invalid Document error message to include doc PYTHON-1982 Update Invalid Document error message to include doc Nov 12, 2024
Copy link
Member

@blink1073 blink1073 left a comment

Choose a reason for hiding this comment

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

LGTM, thank you!

@blink1073 blink1073 merged commit fdcbe2e into mongodb:master Dec 3, 2024
44 of 47 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants