BUG: Fix a reference count leak in npy_find_descr_for_scalar. #27672
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.
Backport of #27666.
The reference count for common->singleton is incremented twice, when it should only be incremented once.
This leak was found when running Google's tests with NumPy 2.1.2, and appears to be a new leak as of NumPy 2.1, probably introduced in 1cb4044.
In particular, this test:
https://github.com/protocolbuffers/protobuf/blob/6cb71402940c6645e49959dfc915f16f4d2e6c20/python/google/protobuf/internal/numpy/numpy_test.py#L67 runs in Py_DEBUG mode and verifies that the total reference count before and after various test cases is unchanged. The same test case has found other NumPy reference count leaks in the past and it may be sensible to add something similar to NumPy's own test suite.