Skip to content

fix: handle possible PyModule_AddObject failure#21468

Merged
opencv-pushbot merged 1 commit intoopencv:3.4from
VadimLevin:dev/vlevin/handle-pymodule-add-object-result
Jan 19, 2022
Merged

fix: handle possible PyModule_AddObject failure#21468
opencv-pushbot merged 1 commit intoopencv:3.4from
VadimLevin:dev/vlevin/handle-pymodule-add-object-result

Conversation

@VadimLevin
Copy link
Copy Markdown
Contributor

Comment from Python documentation:

Unlike other functions that steal references, PyModule_AddObject() only decrements the reference count of value on success.
This means that its return value must be checked, and calling code must Py_DECREF() value manually on error.

Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

  • I agree to contribute to the project under Apache 2 License.
  • To the best of my knowledge, the proposed patch is not based on a code under GPL or other license that is incompatible with OpenCV
  • The PR is proposed to proper branch
  • There is reference to original bug report and related work
  • There is accuracy test, performance test and test data in opencv_extra repository, if applicable
    Patch to opencv_extra has the same branch name.
  • The feature is well documented and sample code can be built with the project CMake

PyModule_AddObject(m, #WNAME, (PyObject *)pyopencv_##NAME##_TypePtr); \
if (PyModule_AddObject(m, #WNAME, (PyObject *)pyopencv_##NAME##_TypePtr) < 0) \
{ \
printf("Failed to register a new type: " #WNAME ", base (" #BASE ")\n"); \
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should we expect some of these messages on CI? (are there some failures?)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

No failures on CI is expected.
If this check is failed than there is definitely something wrong with whole bindings code generation.
Reference implementation of PyModule_AddObject from CPython repository: link
All error checking happens in PyModule_AddObjectRef

Comment from Python documentation:
Unlike other functions that steal references, `PyModule_AddObject()` only
decrements the reference count of value on success.
This means that its return value must be checked, and calling code must
`Py_DECREF()` value manually on error.
@VadimLevin VadimLevin force-pushed the dev/vlevin/handle-pymodule-add-object-result branch from e0e0e75 to 76e34d6 Compare January 18, 2022 08:39
Copy link
Copy Markdown
Member

@alalek alalek left a comment

Choose a reason for hiding this comment

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

LGTM 👍

@opencv-pushbot opencv-pushbot merged commit 25f2527 into opencv:3.4 Jan 19, 2022
@alalek alalek mentioned this pull request Jan 19, 2022
@alalek alalek mentioned this pull request Feb 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants