Description
Describe the issue:
Hi,
At PyVista, we test against the numpy
nightly wheels. Our tests recently failed and a patch (pyvista/pyvista#7538) was needed to fix the tests. However, I think this patch should not be needed, and is in fact a regression with numpy
(i.e. a bug has been introduced).
I locally checked out, installed, and tested against recent commits to main and found that the regression is caused by #29006
As mentioned in pyvista/pyvista#7538, our tests usually pass with
assert np.array_equal([42], [42])
But with #29006, we are getting variable results like:
assert np.array_equal([1], [42])
assert np.array_equal([44636345150720], [42])
I don't really understand the changes in #29006 or how they might cause this, but from our end the error is generated for a special case where our code creates a temporary object internally to store the array, and it seems the reference to the array is being lost somehow or pre-maturely freed, causing the array values to be corrupted.
The code below fails with commit 0506cf6, but succeeds with c2d5e00 (i.e. the commit immediately preceding). I tried creating a minimal example using only numpy
, but the references to the array are complex and difficult to reproduce since PyVista wraps a lower-level C++ package (VTK) which also holds its own references to the array data.
Reproduce the code example:
import pyvista as pv
import numpy as np
# Define an array-like input
key = 'data'
value = [42]
# Store the array with a container
# internally, the array is cast as an `ndarray` instance
multi = pv.MultiBlock()
multi.field_data[key] = value
# Nest the container into another
name = 'nested'
root = pv.MultiBlock({name:multi})
# Move the array from the nested container to the root.
# Internally, this method will use a temp object to store the array
separator = '::'
root.move_nested_field_data_to_root(field_data_mode='prepend', separator=separator)
# The root container should now contain the values from the nested
# array that was moved to the root
new_key = name + separator + key
assert np.array_equal(root.field_data[new_key], value) # ERROR
Error message:
N/A
Python and NumPy Versions:
2.3.0.dev0+git20250519.0506cf6
3.12.2 (v3.12.2:6abddd9f6a, Feb 6 2024, 17:02:06) [Clang 13.0.0 (clang-1300.0.29.30)]
Runtime Environment:
No response
Context for the issue:
This bug seems to cause arrays to be pre-maturely gc'd in some cases, which should probably be avoided. And the root cause has been identified, meaning that the fix can be easily implemented with a simple revert.