numba icon indicating copy to clipboard operation
numba copied to clipboard

Assignment of jitclass-owned array fails when list comprehension is used in the calling function

Open sebbelese opened this issue 3 years ago • 4 comments

With numba 0.55.1.

Consider the following code:

import numpy as np
from numba import njit
from numba.experimental import jitclass
from numba import float64    # import the types

spec = [
    ('var', float64[:]),          
]
@jitclass(spec)
class varClass():
    def __init__(self, a):
        self.var = np.array([a])


@njit
def printVar():
    
    vinst = varClass(1.)
    print(vinst.var)
    vinst.var[0] = 0.0
    print(vinst.var)
    
    # Uncommenting this line breaks the var update
    # positionsPartGroups = [i for i in range(10)]   

printVar()

If I run the script, the output is:

[1.]
[0.]

which is correct.

If I uncomment the last line of the printVar function, which should have no effect, the output is

[1.]
[1.]

which is not correct.

Note that building the list with a loop works:

positionPartGroups= []
    for i in range(10):
        positionPartGroups.append(i)

sebbelese avatar Feb 03 '22 15:02 sebbelese

I can confirm that uncommenting the last line changes the printed output before it

sklam avatar Feb 03 '22 15:02 sklam

I believe the problem is coming from InlineClosureCallPass

debug output contains this:


InlineClosureCallPass: start inline arraycall
fix_array_assign: found SetItem: $24load_attr.9[$const26.10] = $const20.7
find_array_def: $24load_attr.9getattr(value=vinst, attr=var)

No reason for the closure in [i for i in range(10)] affects the setitem.

sklam avatar Feb 03 '22 16:02 sklam

It still fails with numba 0.57.0

sebbelese avatar May 15 '23 14:05 sebbelese

Still affecting numba-0.60.0

sebbelese avatar Sep 09 '24 08:09 sebbelese