Skip to content

Commit f77d57f

Browse files
authored
Fix usage of used_names in pyccel_to_sympy (pyccel#1958)
`used_names` in `Scope` used to be a `set` however it was changed to a `dict` to map the Python unique names to their low-level equivalent. In `pyccel_to_sympy` there was 1 line which still used `used_names` as though it were a set. This causes errors if the code manages to hit this code block (see pyccel#1924 ). This MR corrects the docstrings which still described `used_names` as a `set` and fixes the broken line. Fixes pyccel#1924
1 parent 5e514f2 commit f77d57f

File tree

7 files changed

+24
-6
lines changed

7 files changed

+24
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ All notable changes to this project will be documented in this file.
5555
- #1930 : Preserve ordering of import targets.
5656
- #1951 : Fix return type for class whose argument cannot be wrapped.
5757
- #1892 : Fix implementation of list function when an iterable is passed as parameter.
58+
- #1924 : Fix internal error arising in Duplicate or list comprehensions.
5859

5960
### Changed
6061

pyccel/ast/sympy_helper.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
from .operators import PyccelDiv, PyccelMinus, PyccelAssociativeParenthesis
2424
from .variable import Variable
2525

26-
__all__ = ('sympy_to_pyccel',
27-
'pyccel_to_sympy')
26+
__all__ = ('pyccel_to_sympy',
27+
'sympy_to_pyccel')
2828

2929
#==============================================================================
3030
def sympy_to_pyccel(expr, symbol_map):

pyccel/parser/scope.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ class Scope(object):
4646
parent_scope : Scope, default: None
4747
The enclosing scope.
4848
49-
used_symbols : set, default: None
50-
A set of all the names which we know will appear in the scope and which
51-
we therefore want to avoid when creating new names.
49+
used_symbols : dict, default: None
50+
A dictionary mapping all the names which we know will appear in the scope and which
51+
we therefore want to avoid when creating new names to their collisionless name.
5252
5353
original_symbols : dict, default: None
5454
A dictionary which maps names used in the code to the original name used

pyccel/parser/semantic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -922,7 +922,7 @@ def _create_Duplicate(self, val, length):
922922
length = length.python_value
923923
else:
924924
symbol_map = {}
925-
used_symbols = {}
925+
used_symbols = set()
926926
sympy_length = pyccel_to_sympy(length, symbol_map, used_symbols)
927927
if isinstance(sympy_length, sp_Integer):
928928
length = int(sympy_length)

tests/epyccel/modules/functionals.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,7 @@ def functional_for_2d_range_const():
5656
def functional_for_3d_range():
5757
a = [i*j for i in range(1,3) for j in range(i,4) for k in range(i,j)]
5858
return len(a), a[0], a[1], a[2], a[3]
59+
60+
def unknown_length_functional(x : 'int[:]'):
61+
a = [i*3 for i in range(len(x)*2)]
62+
return len(a), a[0], a[-1]

tests/epyccel/modules/tuples.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
'tuples_mul_homogeneous2',
4141
'tuples_mul_homogeneous3',
4242
'tuples_mul_homogeneous4',
43+
'tuples_mul_homogeneous5',
4344
'tuples_mul_inhomogeneous',
4445
'tuples_mul_inhomogeneous2',
4546
'tuples_mul_homogeneous_2d',
@@ -304,6 +305,14 @@ def tuples_mul_homogeneous4():
304305
i = 1
305306
return b[0], b[i], b[2], b[3], b[4], b[5]
306307

308+
def tuples_mul_homogeneous5():
309+
import numpy as np
310+
s = 2
311+
a = np.ones(5)
312+
b = (1,2,3)*(len(a)*s)
313+
i = 1
314+
return b[0], b[i], b[2], b[3], b[4], b[5]
315+
307316
def tuples_mul_inhomogeneous():
308317
a = (1,False)
309318
b = a*3

tests/epyccel/test_functionals.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,7 @@ def test_functional_for_2d_array_range_const(language):
6969

7070
def test_functional_for_3d_range(language):
7171
compare_epyccel(functionals.functional_for_3d_range, language)
72+
73+
def test_unknown_length_functional(language):
74+
y = randint(100, size = 20)
75+
compare_epyccel(functionals.unknown_length_functional, language, y)

0 commit comments

Comments
 (0)