Skip to content

Commit 9c23278

Browse files
authored
Fix wrong type of Variable shape (pyccel#2095)
Fix the type of the shape of a Variable following a call to `set_changeable_shape`. Fixes pyccel#2094
1 parent ef002f8 commit 9c23278

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ All notable changes to this project will be documented in this file.
8787
- #2078 : Fix translation of classes containing comments.
8888
- #2041 : Include all type extension methods by default.
8989
- #2082 : Allow the use of a list comprehension to initialise an array.
90+
- #2094 : Fix slicing of array allocated in an if block.
9091

9192
### Changed
9293

pyccel/ast/variable.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ def set_changeable_shape(self):
245245
Indicate that the exact shape is unknown, e.g. if the allocate is done in
246246
an If block.
247247
"""
248-
self._shape = [PyccelArrayShapeElement(self, LiteralInteger(i)) for i in range(self.rank)]
248+
self._shape = tuple(PyccelArrayShapeElement(self, LiteralInteger(i)) for i in range(self.rank))
249249

250250
def set_init_shape(self, shape):
251251
"""

tests/epyccel/test_arrays_multiple_assignments.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,26 @@ def f(c : 'float'):
167167
c = np.random.random()
168168
assert f(c) == g(c)
169169

170+
#==============================================================================
171+
def test_creation_in_if_heap_shape(language):
172+
173+
def f(c : 'float'):
174+
import numpy as np
175+
if c > 0.5:
176+
x = np.ones(2, dtype=int)
177+
else:
178+
x = np.ones(7, dtype=int)
179+
180+
y = x[1:-1]
181+
return y.sum()
182+
183+
g = epyccel(f, language=language)
184+
185+
# Check result of pyccelized function
186+
import numpy as np
187+
c = np.random.random()
188+
assert f(c) == g(c)
189+
170190
#==============================================================================
171191
def test_Reassign_to_Target():
172192

0 commit comments

Comments
 (0)